DevOps/CKA Test 준비

04.CKA Sidecar 초안

치킨맛코드 2023. 1. 10. 08:55

현실에서는 오토바이와 같은 탑승물의 동력을 이용해 같이 움직이는 운송수단을 sidecar라고 표현한다. 

k8s에서도 이와 유사한 기능을 활용해 만든 sidecar라는 개념이 있다.

 

pod의 로그를 수집하기 위한다거나 어딘가에 DB를 만들고 싶을때 등 pod의 기능을 추가하고 싶을때와 같이 기능을 확장하고 싶을때 주로 사용한다.

 

문제 예시

pod 생성 관리
특정 이름을 가진 namespace를 만들고, 그 네임스페이스 아래에 다음과 같은 pod를 생성하시오.
namespace : cka-exam
pod name : pod-01
image : busybox
환경변수(env) : CERT= "CKA-cert"
command: /bin/sh
args: -c "while true; do echo $(CERT); sleep 10; done

풀이과정
1. k8s위치 확인 ( 문제에서 요구하는 k8s와 지금 작업하는 k8s위치 확인)
==> kubeclt config use-context "문제에서 요구하는 k8s위치"
2. namespace 생성
==> kubectl create namespace "문제에서 요구하는 namespace"
==> kubectl get namespace "문제에서 요구하는 namespace"
3. yaml(etc..)생성하기
==> kubectl run "pod name" --image="image name" --diy-run -o yaml > yaml_name.yaml
4. vi로 yaml 값 수정하기
==> 문제에서 요구하는 내용을 추가하기.
      보통 pod name, namespace, image name, env, command, args를 추가
metadata ==> pod name. namespace 추가
spec ==> env, name, value, command, args추가


문제 예시
pod 로그 확인 및 결과 추출

pod의 특정 엡에 대한 log를 모니터링한 후 특정 메세지를 포함하는 log line을 추출 하십시오. 추출된 결과는 특정 위치에 기록하시오.
앱 이름 : custom-app
희망 메세지 : file not found
로그 저장 위치 : /opt/REPOPRT/2022/custom-app-log (미리 만들어져있는 디렉토리)
작업 클러스터 위치 : hk8s

풀이과정
1. kubectl config current-context
==> 현재 바라보고있는 cluster를 확인
2. kubectl config use-context hk8s
==> 문제에서 요구하는 cluster로 이동
3. kubectl get pods
==> pod실행 확인 ( 문제에서 요구하는 특정 앱"custom-app"이 있는지 확인)
4. kubectl logs custom-app | grep 'file not found'
==> custom-app이라는 pod에서 file not found라는 메세지만 출력한다.
5. kubectl logs custom-app | grep 'file not found' > /opt/REPOPRT/2022/custom-app-log
==> 해당 위치로 logs를 저장

static pod
kubelet에 의해 관리되는 API의 도움 없이도 사용가능한 pod이다.
/var/lib/kubelet/config.yaml에서 staticPodPath값을 통해 기본 경로를 확인할 수 있으며 해당 경로에 pod에 대한 yaml값을 저장해 둔다면, kubelet이 그 pod를 확인한 후 운영할 수 있게 된다. yaml 파일이 삭제된다면 자연스레 pod도 사라지게 된다.

문제 예시
hk8s-w1에 static pod를 만들어라.

풀이과정
1. ssh hk8s-w1
==> hk8s-w1로 접근한다. 
2. sudo -i
==> user 계정으로는 static pod를 만들 수 있는 권한이 없기에 반드시 sudo 명령어를 사용해야한다
3. cat /var/lib/kubelet/config.yaml
==> 경로를 확인한다.(cat/vi/vim등 결과값을 확인할 수 있으면 어느 명령어든 상관없다.)
4. vi /etc/kubernetes/manifests/pod.yaml
==> pod.yaml을 생성한다.
5. yaml값을 입력한다. yaml을 하나하나 입력하기 힘들다면 아무 pod를 생성한 후 그 yaml값을 사용해도 된다.
==> kubectl run webserver --image=nginx:1.14 --port=80 --dry-run=client -o yaml > webserver.yaml
80포트를 webserver라는 pod를 생성할 것이고, images는 nginx1.14를 사용할 것이다.
6. vi 혹은 vim, cat으로 yaml값을 확인한다.
==> vi webserver.yaml
7. 생성된 yaml을 편집한다
==> pod 수정 시 static의 pod가 수정된다.

문제 예시
특정 노드에 특정.yaml의 이름을 가지는 static pod를 생성하십시오
노드명 : hk8s-w1
yaml name : nginx-static-pod.yaml
pod name = nginx-static-pod
port: 80
image : nginx

풀이과정
1. ssh hk8s-w1
==> hk8s-w1로 접근한다. 
2. sudo -i
==> user 계정으로는 static pod를 만들 수 있는 권한이 없기에 반드시 sudo 명령어를 사용해야한다
3. cat /var/lib/kubelet/config.yaml
==> 문제에서 명시한 저장 경로가 없기에, 경로를 확인한다.(cat/vi/vim등 결과값을 확인할 수 있으면 어느 명령어든 상관없다.)
4. yaml값을 입력한다. yaml을 하나하나 입력하기 힘들다면 아무 pod를 생성한 후 그 yaml값을 사용해도 된다.
==> kubectl run nginx-static-pod --image=nginx --port=80 --dry-run=client -o yaml > nginx-static-pod.yaml


pod 운용
2개 이상의 pod를 활용하는 containers
컨테이너 상에 의존성이 있을 경우 주로 사용함
ex) webserver pod와 html을 만들고 관리하는 pod

문제예시
4개의 컨테이너를 동작시키는 pod를 생성하십시오
pod name :  eshop-frontend
pod images : nginx, redis, memcached, consul

* container가 무엇인지 몰라도, 생성을 한다는것 자체가 시험의 요지

1. kubectl run eshop-fontend --image=nginx --dry-run =client -o yaml > test.yaml
==> 일단 yaml을 만든다. cil에서는 imgae를 2개이상 만들 수 없기에(--image=nginx, redis --image=nginx, --image=redis 같은 방식은 안됨.) yaml을 생성 후 수정하도록 하자.
2. vi test.yaml
==> yaml값을 수정하도록 하자.
apiVersion: v1
kind: Pod
metadata:
    name: eshop-frontend
==> pod자체의 이름(eshop-frontend)을 이곳에 기입한다.
spec:
    containers:
    -  image: nginx
        name: nginx-container
==> container의 name은 본인이 보기 편한방식으로 작성하면 된다.
    -  image: redis
        name: redis-container
    -  image: memcached
        name: memcached-container
    -  image: consul
        name: consul-container

3.kubeclt apply -f test.yaml
==> 저장 후 해당 yaml파일을 실행시킨다.
       container images가 없으면 다운받은 후 실행시키기에 조금 딜레이가 있을 수 있다.

sidecar-container
* 항상 출제되는 문제
volume(저장소)가 추가된 container
main container가 동작하면서 생성되는 data를 volume에 저장하고, 이것을 sidecar container가 사용하는 방식. 기본 컨테이너 기능을 확장, 추가적인 작업을 하기 위해 붙인 컨테이너이다.
ex) 특정 pod가 생성하는 logs를 다른 pod에서 logs분석 프로그렘을 구동하는 행위 등

apiVersion: v1
kind: Pod
metadata:
    name: webserver
==> pod자체의 이름(webserver)을 이곳에 기입한다.

spec:
  volumes:
   -name: shared-logs
    emptyDir:{}
==>volumes값을 하나 생성할건데, 이름은 shared-logs다.

    containers:
    - name: main
      image: nginx
      volumeMounts:
      - name: shared-logs
        mountPath: /var/log/nginx
==> main이라는 nginx 컨테이너를 생성한다. 이 후 shard-logs라는 volume의 /var/log/nginx경로에 mount한다.
    - name: sidecar
      image: busybox
      command: ["sh","-c","while true; do cat log/access.log /log/erro.log; sleep 10; done"]
      volumeMounts:
      - name: shared-logs
        mountPath: /log
==> sidecar이라는 busybox컨테이너를 생성한다. 이 후 shard-logs라는 volume의 /var/log/nginx경로에 mount한다. 이 후 command 명령어를 실행(acces.log, error.log를 cat명령으로 배포)한다


문제예시
현재 운영중인 pod의 로그를 아키텍처에 통합하는 로그 스트리밍 사이드카 컨테이너를 운영하시오
Pod Name : eshop-cart-app
아키텍처 : kubernetes built-in logging
sidecar images : busybox
sidecar container name : price
price command : "/bin/sh", "-c", "taiil -n+1 -f /var/log/cart-app.log"
mount volume : /var/log
log file name : cart-app.log
* 참조 : 컨테이너 수정 금지

1. 동작중인 pod(eshop-cart-app)를 yaml로 생성
kubectl get pod eshop-cart-app
==> 먼저 동작중인 pod가 있는지 확인한다.
kubectl get pod eshop-cart-app -o yaml > cart-app.yaml
==> 이 후 -o yaml명령어를 사용해 해당 pod에 대한 yaml을 생성한다.
apiVersion: v1
kind: Pod
metadata:
    name: eshop-cart-app
spec:
    containers:
    - command:
      - /bin/sh
      - -c
      - 'i=1;while :; do  echo -e "$i: Price: $((RANDOM % 10000+1))" >> /var/log/cart-app.log;
         i=$((i+1)); sleep 2; done'
      image: busybox
      name: cart-app
      volumeMounts:
      - mountPart: /var/log
        name: varlog
    - name : price
      image: busybox
      args: [/bin/sh, -c, taiil -n+1 -f /var/log/cart-app.log]
      volumMounts:
      - name: cart-app.log
        mountPath: /var/log
    volumes:
    - emptyDir: {}
      name: varlog

2. side-car conatiner가 추가한다.
==>  kubectl apply -f cart-app.yaml

3. args가 잘 되는지 확인한다.
==> kubectl logs eshop-cart-app -c price