Study Memory Work
[K8S/CKA 자격증] Worloads & Scheduling - Pod 본문
- Pod
- Static Pod
- multi-container Pod
- sidecat-container Pod
Pod
어플리케이션 컨테이너를 실행할 때 Pod를 통해서 실행시킬 수 있다.
- Pod란 컨테이너를 표현하는 k8s API의 최소 단위
- Pod에는 하나 또는 여러 개의 컨테이너가 포함될 수 있음
- 동작방식 :
- master 컴포넌트에 Pod 실행 명령을 내리면
- Api 서버가 명령을 받아 인증
- Scheduler가 어느 node에서 실행시킬 지 결정 한 후
- 해당 node에 명령을 전달하여 Pod를 동작시킨다.
Pod 생성 방법
1. CLI 명령어로 Pod 생성하기
$ kubectl run {podName} --image=nginx:1.14 --port=80
# nginx:1.14버전의 컨테이너를 실행해라.
$ kubectl get pod
$ kubectl delete pod {podName}
- 명령어 실행
- API -> Scheduler에 의해 node 결정
- pod running
2. Yaml 파일을 이용하여 Pod 생성하기
예) dnginx 웹 서버 컨테이너를 pod로 동작시키기
- pod name: web
- image : nginx:1.14
- port : 80
$ kubectl run web --image=nginx:1.14 --port=80 --dry-run -o yaml > web.yaml
# --dry-run : 실제 실행하지 않고 실행 시 유효한지만 체크 해주는 옵션
$ vi web.yaml # 니즈에 맞게 수정
$ kubectl apply -f web.yaml # pod 생성
$ kubectl get pod -n devops
$ kubectl delete pod -n devops web # 삭제 방법1
$ kubectl delete pod -f web.yam # 삭제 방법2
Static Pod
API 서버 없이특정 worker노드에 있는 kubelet에 의해 직접 관리- 동작과정:
- /etc/kubernetes/manifests/ 디렉토리에 pod yaml 파일을 저장
- pod yaml 이 생성되면 kubelet이 Pod를 실행
- pod yaml 을 삭제하면 kublete이 Pod를 삭제
- static pod 디렉토리 구성
- kubelet config파일에 static Pod Path가 명시되어 있다.
$ cat /var/lib/kubelet/config.yaml
###
...
staticPodPath: /etc/kubernetes/manifests
...
###
- 디렉토리 수정시 kubelet 데몬 재실행
$ systemctl restart kubelet
- static pod 생성/실행 과정 :
# 1. worker node로 이동
$ ssh {nodeName}
# 2. root 권한으로 변경
$ sudo -i
# 3. kublete config 파일에서 static pod path 가져오기
$ cat /var/lib/kubelet/config.yaml
###
...
staticPodPath: /etc/kubernetes/manifests
###
# 4. static pod path로 이동
cd /etc/kubernetes/manifests
# 5. yaml 파일 만들기
$ kubectl run webserver --image=nginx:1.14 --dry-run:clirent -o yaml > webserver.yaml
$ kubectl get pod
# 위에서 만든 Pod가 자동 실행되고 있는 것을 확인할 수 있다.
# 6. pod 삭제
$ rm webserver.yaml
$ kubectl get pod
# 실행중인 Pod가 자동 삭제된 것을 볼 수 있다.
multi-container Pod
- 하나의 Pod에 여러 개의 컨테이너가 포함되어 함께 실행되는 것.
- 서로 컨테이너 간 의존성이 있는 것들은 이렇게 Pod내에 여러개의 컨테이너를 두고 사용한다.
Communicate Between Containers in the Same Pod Using a Shared Volume
This page shows how to use a Volume to communicate between two Containers running in the same Pod. See also how to allow processes to communicate by sharing process namespace between containers. Before you begin You need to have a Kubernetes cluster, and t
kubernetes.io
- Pod yaml 예제
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
restartPolicy: Never
volumes:
- name: shared-data
emptyDir: {}
containers: # 여러 컨테이너를 둘 수 있다.
- name: nginx-container
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: debian-container
image: debian
volumeMounts:
- name: shared-data
mountPath: /pod-data
command: ["/bin/sh"]
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
sidecar-container Pod
- 기본 컨테이너 기능을 확장하기 위해 사용 본래의 컨테이너는 기본 서비스에 충실하고, 추가 기능을 별도의 컨테이너를 이용해 적용
- multi-container과 같은 방식. 사용 의미만 조금 다를 뿐임!! main컨테이너의 추가작업을 다른 컨테이너(sidecar container)가 진행하는 느낌!
- 웹서버는 기본서비스에 충실하고, 추가로 생성되는 웹서버 로그는 별도의 사이드 카 컨테이너가 수집하여 외부 log aggregator로 전송하는 형태의 서비스

apiVersion: v1
kind: Pod
metadata:
name: webserver
spec:
volumes:
- name: shared-logs
emptyDir: {}
containers:
- name: main
image: nginx
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx # 볼륨을 해당 경로에 mount하고 있음
- name: sidecar
image: busybox
command: ["sh","-c","while true; do cat /log/access.log /log/error.log; sleep 10; done"]
volumeMounts:
- name: shared-logs
mountPath: /log # 볼륨을 해당 경로에 mount하면서 command라인에 log를 출력
[문제1] Pod 생성하기
작업 클러스터 : k8s
'cka-exam'이라는 namespace를 만들고, 'cka-exam' namespace에 아래와 같은 Pod를 생성하시오.
- pod Name: pod-01
- image: busybox
- 환경변수 : CERT = "CKA-cert"
- command: /bin/sh
- args: -c "while true; do echo $(CERT); sleep 10;done"
(풀이)
1. cluster 위치 확인
2. namespace 만들기 : cka-exam
3. Pod 생성
- yaml파일 작성 문서(환경변수)
컨테이너를 위한 커맨드와 인자 정의하기
본 페이지는 파드 안에서 컨테이너를 실행할 때 커맨드와 인자를 정의하는 방법에 대해 설명한다. 시작하기 전에 쿠버네티스 클러스터가 필요하고, kubectl 커맨드-라인 툴이 클러스터와 통신할
kubernetes.io
- command, args 작성 문서
컨테이너를 위한 커맨드와 인자 정의하기
본 페이지는 파드 안에서 컨테이너를 실행할 때 커맨드와 인자를 정의하는 방법에 대해 설명한다. 시작하기 전에 쿠버네티스 클러스터가 필요하고, kubectl 커맨드-라인 툴이 클러스터와 통신할
kubernetes.io
# 1. cluster 확인
$ kubectl config current-context
# 2. namespace 만들기 : cka-exam
$ kubectl create namespace cka-exam
# 3. pod 생성
$ kubectl run pod-01 --image=busybox --dry-run=client -o yaml > test.yaml
$ vi -f test.yaml
# 필요하지 않는 것은 삭제하고
# namespace, 환경변수, Command, Args 추가하기
####
...
metadata:
...
namespace: cka-exam
spec:
...
env:
- name: CERT
value: "CKA-cert"
command: ["/bin/sh"]
args: ["-c", "while true; do echo $(CERT); sleep 10;done"]
####
$ kubectl apply -f test.yaml
[문제2] pod의 로그 확인해서 결과 추출하기
작업 클러스터 : k8s
Pod "custom-app"의 log를 모니터링하고 'file not found'메세지를 포함하는 로그 라인인 추출하세요.
추출된 결과는 /opt/REPORT/2022/custom-app-log에 기록하세요.
(풀이)
1. file not found를 포함하는 log를 추출하여 /opt/REPORT/2022/custom-app-log에 저장
# 1. cluster 체크
$ kubectl config use-context
# 2. log 추출하여 저장
$ kubectl logs custom-app | grep "file not found" > /opt/REPORT/2022/custom-app-log
# 3. 확인
$ cat /opt/REPORT/2022/custom-app-log
[문제3] static pod 생성하기
hk8s-w1 노드에 nginx-static-pod.yaml 라는 이름의 Static Pod를 생성하세요.
- pod name: nginx-static-pod
- image: nginx
- port : 80
(풀이)
1. staticPod Path로 이동
2. yaml 생성
# 1. 클러스터 확인
$ kubectl config use-context
# 2. hk8s-w1 노드로 이동
$ ssh hk8s-w1
# 3. kubelet config 에서 static pod path로 추출
$ cat /var/lib/kublete/config.yaml
# 4. static pod path로 이동하여 yaml 파일 생성
$ cd {static pod path}
$ kubectl run nginx-static-pod --image=nginx:1.14 --port=80 --dry-run:client -o yaml > test.yaml
# 5. Pod 생성 확인
$ kubectl get pods
[문제4] multi container Pod 생성하기
작업 클러스터 : k8s
4개의 컨테이너를 동작시키는 eshop-frontend Pod를 생성하시오.
- pod image: nginx, redis, memcached, consul
(풀이)
1. pod yaml 파일 생성
2. yaml 파일 수정
3. pod 실행
- 복수개의 컨테이너를 생성하는 Pod yaml 문서
Communicate Between Containers in the Same Pod Using a Shared Volume
This page shows how to use a Volume to communicate between two Containers running in the same Pod. See also how to allow processes to communicate by sharing process namespace between containers. Before you begin You need to have a Kubernetes cluster, and t
kubernetes.io
# 1. 클러스터 확인
$ kubectl config current-context
# 2. Pod yaml 파일 생성 (run은 하나의 image만 넣을 수 있음)
$ kubectl run eshop-frontend --image=nginx --dry-run:client -o yaml > multiPod.yaml
# 3. Pod yaml 파일에 필요 containers 추가
vi multiPod.yaml
###
...
spec:
containers:
- name: nginx-container
image: nginx
- name: redis-container
image: redis
- name: memcached-container
image: memcached
- name: consul-container
image: consul
...
###
# 4. Pod 실행
kubectl apply -f multiPod.yaml
# 5. Pod 확인
kubectl get pods
[문제5] sidecar-container (필수출제 주제!)
작업 클러스터 : k8s
현재 운영중인 eshop-cart-app Pod의 로그를 Kubernetes built-in logging 아키텍처(예: kubectl logs)에 통합하는 로그 스트리밍 사이드카 컨테이너를 운영하시오.
- busybox 이미지를 사용하여 price라는 이름의 사이드카 컨테이너를 기존 eshop-cart-app에 추가합니다.
- 새 price 컨테이너는 다음과 같은 command를 실행해야 합니다.
- Command: /bin/sh, -c, “tail -n+1 -f /var/log/cart-app.log”
- /var/log에 마운트 된 볼륨을 사용하여 사이드카 컨테이너에서 로그 파일 cart-app.log를 사용해야 합니다.
- eshop-cart-app Pod와 cart-app 컨테이너를 수정하지 마시오.
# 1. running 중인 eshop-cart-app pod Pod의 yaml 파일 생성
$ kubectl get pod eshop-cart-app -o yaml > eshop-cart-app.yaml
# 2. yaml파일 수정
$ vi eshop-cart-app.yaml
###
...
volumes:
- name: varlog
emptyDir: {}
containers:
- name: nginx-container
image: nginx
volumeMounts:
- name: varlog
mountPath: /usr/share/nginx/html
- name: price
image: busybox
volumeMounts:
- name: varlog
mountPath: /var/log
command: [/bin/sh, -c, "tail -n+1 -f /var/log/cart-app.log"]
...
###
# 3. Pod 실행
$ kubectl delete pod eshop-cart-app
$ kubectl apply -f eshop-cart-app.yaml
'Infra > Kubernetes' 카테고리의 다른 글
[K8S/CKA 자격증] Worloads & Scheduling - node 관리 (0) | 2023.01.22 |
---|---|
[K8S/CKA 자격증] Worloads & Scheduling - Deployment (0) | 2023.01.22 |
[K8S/CKA 자격증] API 인증 / rbac 인증 (문제O) (0) | 2023.01.13 |
[K8S] The connection to the server localhost:8080 was refused - did you specify the right host or port? 해결방법 (0) | 2023.01.13 |
[K8S/CKA 자격증] Kubernetes Cluster upgrade (문제O) (0) | 2023.01.12 |