본문 바로가기

Kubernetes

[Kubernetes] Pod - init container & infra container

반응형

init container를 적용한 Pod

 - 앱 컨테이너 실행 전에 미리 동작시킬 컨테이너

 - 본 contrainer가 실행되기 전에 사전 작업이 필요할 경우 사용

 - 초기화 컨테이너가 모두 실행된 후에 앱 컨테이너

 

cat init-container-exam.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]

 

myservice, mydb 가 실행되면 main 컨테이너가 실행, 실행되지 않으면 컨테이너는 실행되지 않는다.

 

yaml 실행 후 pod 상태 확인

kubectl create -f init-container-exam.yaml

kubectl get pods  -o wide
NAME        READY   STATUS     RESTARTS   AGE   IP       NODE                NOMINATED NODE   READINESS GATES
myapp-pod   0/1     Init:0/2   0          6s    <none>   node1.example.com   <none>           <none>

 

myservice, mydb 가 실행되지 않아서 STATUS의 Init:0/2 확인할 수 있다.

 

myservice, mydb를 순차적으로 실행하면 STATUS가 Init:1/2 → Running, Ready 0/1 →1/1 로 변경됨을 확인할 수 있다.

cat init-container-exam-svc.yaml 

apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376

cat init-container-exam-svc-db.yaml 

apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377
    
kubectl create -f init-container-exam-svc.yaml
kubectl create -f init-container-exam-svc-db.yaml

kubectl get pods  -o wide
NAME        READY   STATUS     RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
myapp-pod   0/1     Init:1/2   0          3m52s   10.36.0.1   node1.example.com   <none>           <none>

kubectl get pods  -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
myapp-pod   1/1     Running   0          33m   10.36.0.1   node1.example.com   <none>           <none>

 

infra container(pause)

 - Pod의 환경을 만들어주는 컨테이너

 

nginx 웹서버 컨테이너 생성 후 webserver가 동작하는 노드 확인

kubectl run webserver --image=nginx:1.14 --port=80

kubectl get pods  -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
myapp-pod   1/1     Running   0          33m   10.36.0.1   node1.example.com   <none>           <none>
webserver   1/1     Running   0          23m   10.44.0.1   node2.example.com   <none>           <none>

 

webserver가 동작하는 node2 서버에 현재 동작 중인 컨테이너 정보를 출력

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

docker ps 명령어를 통해서 컨테이너 정보를 출력시 아무 정보도 나오지 않는다.

 

현재 master, node 의 Kubernetes 버전은 v1.25로  컨테이너 런타임이 docker → containerd 로 변경되었기 때문이다.

kubectl get node -o wide
NAME                 STATUS   ROLES           AGE     VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
master.example.com   Ready    control-plane   5d23h   v1.25.4   10.100.0.104   <none>        Ubuntu 20.04.3 LTS   5.15.0-53-generic   containerd://1.4.9
node1.example.com    Ready    <none>          5d23h   v1.25.4   10.100.0.101   <none>        Ubuntu 20.04.3 LTS   5.11.0-36-generic   containerd://1.4.9
node2.example.com    Ready    <none>          5d23h   v1.25.4   10.100.0.102   <none>        Ubuntu 20.04.3 LTS   5.15.0-53-generic   containerd://1.4.9

 

docker ps 명령어 대신 crictl ps 명령어를 통해 pause 컨테이너를 확인하면 crictl에는 pause 컨테이너를 확인 할 수 없었다.

crictl ps
WARN[0000] runtime connect using default endpoints: [unix:///var/run/dockershim.sock unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead. 
ERRO[0000] unable to determine runtime API version: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /var/run/dockershim.sock: connect: no such file or directory" 
WARN[0000] image connect using default endpoints: [unix:///var/run/dockershim.sock unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead. 
ERRO[0000] unable to determine image API version: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /var/run/dockershim.sock: connect: no such file or directory" 
CONTAINER           IMAGE               CREATED             STATE               NAME                ATTEMPT             POD ID              POD
2beba1386d4e1       295c7be079025       58 minutes ago      Running             webserver           0                   13dcd9f9b51ad       webserver
c2c7f282b6abb       690c3345cc9c3       About an hour ago   Running             weave-npc           3                   dbe8283676047       weave-net-r8v7n
d08bfd697d080       62fea85d60522       About an hour ago   Running             weave               3                   dbe8283676047       weave-net-r8v7n
b5d4a0d3b0ce3       2c2bc18642790       About an hour ago   Running             kube-proxy          3                   d7f3d3d0bc000       kube-proxy-x5gsk

 

crictl images 명령어를 통해서만 pause를 확인할 수 있습니다.

crictl images
WARN[0000] image connect using default endpoints: [unix:///var/run/dockershim.sock unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead. 
ERRO[0000] unable to determine image API version: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /var/run/dockershim.sock: connect: no such file or directory" 
IMAGE                             TAG                 IMAGE ID            SIZE
docker.io/library/centos          7                   eeb6ee3f44bd0       76.1MB
docker.io/library/nginx           1.14                295c7be079025       44.7MB
docker.io/library/nginx           latest              88736fe827391       56.8MB
docker.io/weaveworks/weave-kube   latest              62fea85d60522       30.9MB
docker.io/weaveworks/weave-npc    latest              690c3345cc9c3       12.8MB
k8s.gcr.io/pause                  3.2                 80d28bedfe5de       300kB
registry.k8s.io/kube-proxy        v1.25.4             2c2bc18642790       20.3MB

 

WARN[0000] image connect using default endpoints:

[unix:///var/run/dockershim.sock unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead. 
ERRO[0000] unable to determine image API version: rpc error:

code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /var/run/dockershim.sock: connect: no such file or directory" 

 

crictl 명령어 사용시 위와 같은 에러 문구가 나올 경우,

/etc/crictl.yaml 파일에 아래와 같은 runtime-endpoint, image-endpoint 에 추가하면 crictl 명령어 실행시 에러가 나타나지

않는다. 

runtime-endpoint: unix:///var/run/containerd/containerd.sock
image-endpoint: unix:///var/run/containerd/containerd.sock
timeout: 0
debug: false
pull-image-on-create: false
disable-pull-on-run: false

 

crictl images
IMAGE                             TAG                 IMAGE ID            SIZE
docker.io/library/busybox         1.28                8c811b4aec35f       728kB
docker.io/library/nginx           1.14                295c7be079025       44.7MB
docker.io/library/redis           latest              3358aea34e8c8       42.4MB
docker.io/smlinux/unhealthy       latest              2b208508abf77       264MB
docker.io/weaveworks/weave-kube   latest              62fea85d60522       30.9MB
docker.io/weaveworks/weave-npc    latest              690c3345cc9c3       12.8MB
k8s.gcr.io/pause                  3.2                 80d28bedfe5de       300kB
registry.k8s.io/kube-proxy        v1.25.4             2c2bc18642790       20.3MB

 

 

설정에 대한 자세한 내용은 아래 링크 참고하면 된다.

https://kubernetes.io/docs/tasks/debug/debug-cluster/crictl/

 

Debugging Kubernetes nodes with crictl

FEATURE STATE: Kubernetes v1.11 [stable] crictl is a command-line interface for CRI-compatible container runtimes. You can use it to inspect and debug container runtimes and applications on a Kubernetes node. crictl and its source are hosted in the cri-too

kubernetes.io

 

 

[참고]

- https://www.youtube.com/watch?v=ChArV14J6Ek&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=13 

반응형