Kubernetes Pods
Pod 란?
Pod(파드)는 Application의 최소 실행 단위이며, Kubernetes에서 관리의 기본 단위로써 그룹화한 가상 호스트와 같은 것을 의미한다.
이 Pod는 가상 Network Interface(동일한 IP, 파일 시스템)를 공유하기 때문에 가상 호스트와 같은 역할을 한다.
하나 혹은 다수의 Application container, storage, network 등의 정보를 포함한다.
Pod의 특징
- Pod에는 각각 고유한 private IP 할당한다.
- Pod 안에 있는 container는 pod 의 IP를 local host 로 공유한다.
- Pod는 사실상 Pod를 가지고 있는 virtual machine이라고 할 수 있다.
Pod 제어
이러한 Pod를 만들고 정보를 확인하는 방법을 살펴 보겠다.
Pod 기동 - kubectl run
우선은 Pod의 기동에 대해 알아보겠다. 아래 명령을 실행하면 Pod가 기동된다.
kubectl run –-image [이미지명]:[태그명] (옵션) [Pod명]
아래 명령으로 WebServer인 nginx를 띄어보도록 하겠다.
kubectl run webserver --image=nginx:1.14 --port 80
실행 결과:
% kubectl run webserver --image=nginx:1.14 --port 80
pod/webserver created
kubectl run
에 대해서도 docker run
과 마찬가지로 다양한 옵션을 지정할 수 있다. 예를 들어, -e
옵션을 사용하여 환경 변수를 설정할 수 있다.
기타 옵션은 아래 페이지를 참조하여라.
Pod 목록 조회 - kubectl get
Pod 목록을 보려면 다음과 같은 명령을 실행한다.
kubectl get pods
실행 결과:
% kubectl get pods
NAME READY STATUS RESTARTS AGE
webserver 0/1 ContainerCreating 0 4s
생성하자 마자 바로 실행하면 위와 같이 상태가 생성중(ContainerCreating)이라는 메세지가 나온다.
% kubectl get pods
NAME READY STATUS RESTARTS AGE
webserver 1/1 Running 0 16s
시간이 좀 더 지나서 해보면, 실행 상태(Running)라는 메세지가 나온다.
Pod 목록을 상세하게 더 보려면 다음과 같은 명령을 실행한다.
kubectl get pods -o wide
실행 결과:
% kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webserver 0/1 Running 0 9s <none> minikube <none> <none>
Pod 상세 조회 - kubectl describe
Pod의 메타데이터를 보려면, 다음 명령을 실행한다.
kubectl describe pod [Pod명]
실행 결과:
% kubectl describe pod webserver
Name: webserver
Namespace: default
Priority: 0
Service Account: default
Node: minikube/192.168.49.2
Start Time: Fri, 09 Sep 2022 08:36:18 +0900
Labels: run=webserver
Annotations: <none>
Status: Running
IP: 172.17.0.3
IPs:
IP: 172.17.0.3
Containers:
webserver:
Container ID: docker://dddee2b98f8badd1a09208d99e02ba881f79091a9670e42862a7aa5f45ec4200
Image: nginx:1.14
Image ID: docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 09 Sep 2022 08:36:30 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-xmw8p (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-xmw8p:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 22s default-scheduler Successfully assigned default/webserver to minikube
Normal Pulling 22s kubelet Pulling image "nginx:1.14"
Normal Pulled 11s kubelet Successfully pulled image "nginx:1.14" in 11.180480547s
Normal Created 11s kubelet Created container webserver
Normal Started 10s kubelet Started container webserver
Pod log 확인 - kubectl logs
Pod의 로그를 보고 싶다면, 다음 명령을 실행한다.
kubectl logs [Pod명]
실행 결과:
% kubectl logs webserver
172.17.0.4 - - [08/Sep/2022:23:38:30 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.85.0-DEV" "-"
Pod 실행 결과 확인 - curl
curl
명령어로 접속이 되는지 확인 해보겠다.
curl [IP]
실행 결과:
$ curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
minikube를 사용중 이라면...
minikube에서 실제 장비에서는 연결을 시도 하면 연결이 되지 않는다.
% curl 172.17.0.3
curl: (7) Failed to connect to 172.17.0.3 port 80 after 4036 ms: No route to host
아래와 같이 클러스터 내부에 들어가 curl
을 실행한다.
% kubectl run --image curlimages/curl:latest -it --restart Never --rm curl sh
If you don't see a command prompt, try pressing enter.
/ $ curl 172.17.0.3
Pod에 들어가 보기 - kubectl exec
작성한 Pod 안에 들어가기 위해서는 다음의 커멘드를 실행한다.
kubectl exec -it [Pod명] [이미지명]
실행 결과:
Pod 안으로 들어간다.
% kubectl exec webserver -it -- /bin/bash
root@webserver:/#
nginx의 첫페이지 파일이 있는 경로 이동한다.
root@webserver:/# cd /usr/share/nginx/html/
내용을 확인 한다.
root@webserver:/usr/share/nginx/html# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
내용을 수정한다.
root@webserver:/usr/share/nginx/html# echo "Hello, devkuma" > index.html
수정된 내용이 반영되었는지, 서버에 접속해 본다.
$ curl 172.17.0.3
Hello, devkuma
Pod에서 포트 포워드 - kubectl port-forward
kubectl port-forward 8080:80
yaml 파일 만들어서 Pod를 기동
여기서는 Pod 기동을 yaml으로 생성해서, 그 yaml를 이용해서 Pod를 기동시켜 보도록 하겠다.
-
명령어 만들기
먼저--dry-run
플래그 사용해서 실행되지 않는 명령어를 만들어 보자.kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client
--dry-run
플래그를 사용하면, 실제로 실행되지는 않고, 클러스터로 보낼 객체를 미리 볼 수 있다.실행 결과:
% kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client pod/webserver created (dry run)
-
yaml 설정 내용 확인하기
kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client -o yaml
실행 결과:
% kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client -o yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: webserver name: webserver spec: containers: - image: nginx:1.14 name: webserver ports: - containerPort: 80 resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {}
-
yaml 파일 만들기
이 파일 내용을webserver-pod.yaml
파일명으로 yaml을 생성한다.% kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client -o yaml > webserver-pod.yaml
-
기존 생성된 Pod 삭제하기 기존에 동일한 이름의 webserver Pod는 삭제한다.
% kubectl delete pod webserver pod "webserver" deleted
-
yaml로 새로운 Pod 생성하기 yaml 파일로 새로 파드를 생성해 보자.
kubectl create -f webserver-pod.yaml
재대로 생성이 되었는지 확인 한다.
% kubectl get pods NAME READY STATUS RESTARTS AGE webserver 1/1 Running 0 12s
Pod를 삭제 - kubectl delete
생성한 Pod를 삭제하려면, 다음 명령을 실행한다.
kubectl delete pod [Pod 명]
실행 결과:
% kubectl delete pod webserver
pod "webserver" deleted
Pod 배포하기
kubectl create deployment mainui --image=httpd: --replicas=3
kubectl get deployment.apps
kubectl describe deployment.apps mainui
kubectl get pods
kubectl get pods -o wide
kubectl edit deployment.apps maini