Skip to content

Instantly share code, notes, and snippets.

@ianychoi
Created July 14, 2021 13:43
Show Gist options
  • Select an option

  • Save ianychoi/e2ff59af1c878cf8b9975478ac6fe41f to your computer and use it in GitHub Desktop.

Select an option

Save ianychoi/e2ff59af1c878cf8b9975478ac6fe41f to your computer and use it in GitHub Desktop.
쿠버네티스 - 초기화 컨테이너
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"]
  • 생성 및 확인
#
curl -s -O https://raw.githubusercontent.com/gasida/DKOS/main/3/init.yaml
kubectl apply -f init.yaml && kubectl get pod -w

# 확인 : Init:0/2 가 보인다!, app container 는 생성되지도 않았다(0/1)
kubectl get pod -o wide
NAME        READY   STATUS     RESTARTS   AGE     IP             NODE     NOMINATED NODE   READINESS GATES
myapp-pod   0/1     Init:0/2   0          2m45s   172.16.46.18   k8s-w2   <none>           <none>

# 초기화 컨테이너 1번이 실행 중이지만, command 성공하지 못해서 초기화 컨테이너 2번은 대기중!
kubectl describe pod myapp-pod | grep 'Init Containers' -A 34
Init Containers:
  init-myservice:
    Container ID:  docker://0fb3a2c09215520a9d7a2ea8dc8e3affe3c3438ee08198fb5985f9f85cd3df95
    ...
    State:          Running
	  ...
  init-mydb:
    ...
    State:          Waiting
...

# command 가 왜 실패하는 지 확인! - netshoot 유용한 이미지의 bash 접근
kubectl run tmp-shell --rm -i --tty --image nicolaka/netshoot -- /bin/bash
[root@k8s-m ~ (kube:default)]# kubectl run tmp-shell --rm -i --tty --image nicolaka/netshoot -- /bin/bash
If you don't see a command prompt, try pressing enter.
bash-5.1# cat /var/run/secrets/kubernetes.io/serviceaccount/namespace ; echo
default

## 구글 도메인 조회
bash-5.1# nslookup www.google.com
Server:		10.96.0.10
Address:	10.96.0.10#53

Non-authoritative answer:
Name:	www.google.com
Address: 142.250.196.100
Name:	www.google.com
Address: 2404:6800:4004:819::2004

## cmd 에 설정된 주소로 조회
bash-5.1# nslookup myservice.default.svc.cluster.local
Server:		10.96.0.10
Address:	10.96.0.10#53
** server can't find myservice.default.svc.cluster.local: NXDOMAIN

bash-5.1# nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local
Server:		10.96.0.10
Address:	10.96.0.10#53
** server can't find myservice.default.svc.cluster.local: NXDOMAIN

## 빠져나오면 파드가 삭제됨
bash-5.1# exit
exit
Session ended, resume using 'kubectl attach tmp-shell -c tmp-shell -i -t' command when the pod is running
pod "tmp-shell" deleted
  • 서비스 생성 후 확인
# myservice 서비스 생성
cat << EOF | kubectl apply -f - && watch -d "kubectl describe pod myapp-pod | grep Events -A 12"
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
EOF

# 확인
kubectl get pod
NAME        READY   STATUS     RESTARTS   AGE
myapp-pod   0/1     Init:1/2   0          20m

kubectl describe pod myapp-pod

# mydb 서비스 생성
cat << EOF | kubectl apply -f - && watch -d "kubectl describe pod myapp-pod | grep Events -A 12"
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377
EOF

# 확인
kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
myapp-pod   1/1     Running   0          21m

# 다음 실습을 위해서 생성된 자원 삭제
kubectl delete -f init.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment