k8s

Kubernetes, OpenSearch를 이용한 로그 모니터링

089 2024. 1. 18. 16:41

Springboot프로젝트를 이용한 Log 모니터링 

 

 

helm차트를 이용해 opensearch를 저장소 연결

helm repo add opensearch http://opensearch-project.github.io/helm-charts/

 

다음으로 opensearch라는 namespace를 생성

k create namespace opensearch

 

연결한  helm저장소에 있는 opensearch를 설치

helm -n opensearch install opensearch opensearch/opensearch

 

opensearch는 대시보드를 지원한다. 대시보드도 설치 해주자

helm -n opensearch install dashboard opensearch/opensearch-dashboards

 

잘 설치 되었는지 확인!!!

 

 

아래 명령어로 port를 열어 접속

k -n opensearch port-forward dashboard-opensearch-dashboards-68fd8c7fb-t7rfw 5601

 

초기 아이디 비밀번호는 admin/admin

그 다음으로 deploy.yaml을 수정 해주자

    spec:
      terminationGracePeriodSeconds: 60
      volumes:
        - name: log-volume
          emptyDir: {}
        - name: config-volume
          configMap:
            name: fluentbit-config
      containers:
        - name: fluent-bit
          image: fluent/fluent-bit:2.2.0
          volumeMounts:
            - name: log-volume
              mountPath: /var/log/app
            - name: config-volume
              mountPath: /fluent-bit/etc/fluent-bit.conf
              subPath: fluent-bit.conf
        - name: chilisauce
          image: 0892668/chilisauce-be:0.0.7
          imagePullPolicy: Always
          volumeMounts:
            - name: log-volume
              mountPath: /var/log/app

 

 

다음은 application.yaml 파일에서 로깅 설정을 해줍니다.
로깅 설정이 잘못된 경우가 많고 일반적으로는 인덴트가 잘못된 경우가 많습니다
.
(logging이 최상위에 선언되어야 하며 spring 밑으로 들어가면 안 됩니다) 참고!!

logging:
  file:
    name: /var/log/app/chilisauce.log

 

 

다음은 fluentbit-configmap.yaml 파일을 생성해 줍니다.

apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentbit-config
  namespace: chilisauce
data:
  fluent-bit.conf: |
    [SERVICE]  
        Flush        1  
        Log_Level    info  
    
    [INPUT]  
        Name         tail  
        Path         /var/log/app/chilisauce.log  
    
    [OUTPUT]  
        Name         opensearch  
        Match        *  
        Host         opensearch-cluster-master.opensearch.svc.cluster.local  
        Port         9200  
        Logstash_Format On  
        Logstash_Prefix chilisauce-app-logs  
        Suppress_Type_Name On  
        tls          On  
        tls.verify   Off  
        http_User    admin  
        http_Passwd  admin

 

셋팅은 끝났다 log적재가 완료되면 index pattern에 위에 설정했던 값이 들어 온다.

Step 2 에 @Timestamp를 설정하고 마무리하면 된다.


OpenSearch Dashboards에 Discover에 들어가면 아래와같은 log모음집이 생긴다.