새소식

Cloud/Kubernetes

Kubernetes Volume - emptyDir로 sideCar 디자인패턴 구현하기 ( logstash -> elasticsearch)

  • -

2024.04.17 - [Cloud/Kubernetes] - Kubernetes Volume - emptyDir 사용해보기

 

Kubernetes Volume - emptyDir 사용해보기

emptyDir은 Pod 내의 Container 간의 볼륨 마운트를 위한 오브젝트이다. 주의할 점은, Pod와 Lifecycle이 같기 때문에 Pod가 삭제되면 같이 삭제되니만, emptyDir로 sideCar나 앰버서더 와 같은 다양한 디자인패

cstory-bo.tistory.com

위의 포스팅에서 설명한 emptyDir을 토대로 log를 수집하고 분석한 후 elasticsearch로 전달하는
사이드카 디자인 패턴을 구현할 수 있다.

 

vi tomcat.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      run: tomcat
  template:
    metadata:
      labels:
        run: tomcat
    spec:
      containers:
      - image: tomcat
        name: tomcat
        ports:
        - containerPort: 8080
        env:
        - name: UMASK
          value: "0022"
        volumeMounts:
        - name: tomcat-log
          mountPath: /usr/local/tomcat/logs
      - image: logstash:7.17.16
        name: logstash
        volumeMounts:
        - name: tomcat-log
          mountPath: /mnt
        args: ["-e input { file { path => \"/mnt/localhost_access_log.*\" } } output { stdout { codec => rubydebug } elasticsearch { hosts => [\"http://elasticsearch-svc.default.svc.cluster.local:9200\"] } }"]  
        
      volumes:
      - name: tomcat-log
        emptyDir: {}

Pod 안에 tomcat과 logstash 컨테이너를 띄우고

디렉토리를 마운트한다.

그리고 logstash에서 args 에 있는 명령어로 elasticsearch로 보낸다.

여기서 따로 elasticsearch 파드를 생성해줘야한다.

kubectl apply -f tomcat.yaml 로 적용하고

각 컨테이너에서 ls 로 로그가 생성되고 마운트 되었는지 확인한다.

kubectl exec -it tomcat-6b9bc87f45-hfjbn -c tomcat -- ls /usr/local/tomcat/logs
출력: catalina.2024-04-17.log  localhost_access_log.2024-04-17.txt

kubectl exec -it tomcat-6b9bc87f45-hfjbn -c logstash -- ls /mnt
출력: catalina.2024-04-17.log  localhost_access_log.2024-04-17.txt

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.