# Pod 생성
## 다른 터미널에서 watch -d 'kubectl get pod' 실행 시 파드 정보 출력으로 이해를 돕는다
## 워커 노드에서는 watch -d 'docker ps --format "table {{.Image}}\t{{.Status}}\t{{.Names}}"' 실행 시 파드 정보 출력으로 이해를 돕는다
## 혹시 남아있는 Pod 삭제
kubectl delete pod --all
# YAML 파일 생성 : YAML 파일에 컨테이너가 사용할 포트(TCP 80)을 설정
## apply -f(file) 선언형 명령은 멱등성을 보장하여 여러번 실행하더라도 항상 YAML 정의서에 선언된 내용과 동일한 결과를 얻을 수 있습니다
# 엔터 입력 후 아래 apiVersion~protocol 복사 붙여넣기
cat > myweb.yaml
apiVersion: v1
kind: Pod
metadata:
name: myweb
spec:
containers:
- image: nginx:latest
name: myweb-container
ports:
- containerPort: 80
protocol: TCP
# CTRL+D 입력으로 파일을 생성(저장)한다
## 혹시 파일 생성이 어려울 경우 아래처럼 다운 받을 수 있습니다
## curl -s -O https://raw.githubusercontent.com/gasida/DKOS/main/3/myweb.yaml
# 생성된 파일 내용 확인
cat myweb.yaml
# YAML 파일로 Pod 배포
kubectl apply -f myweb.yaml && kubectl get pod -w
## Pod 정상 배포 확인 후 다시 실행해보고 Pod 배포 갯수 확인해보자(멱등성!)
kubectl apply -f myweb.yaml
kubectl apply -f myweb.yaml
kubectl get pod
# Pod 배치 노드, Pod 의 IP 정보를 확인
kubectl get pod -o wide
root@k8s-m:~# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myweb 1/1 Running 0 26s 172.16.228.68 k8s-w1 <none> <none>
# 배포된 노드에서 컨테이너 확인 : pause 컨테이너가 보인다!
docker ps --format "table {{.Image}}\t{{.Status}}\t{{.Names}}" |grep myweb
root@k8s-w1:~# docker ps --format "table {{.Image}}\t{{.Status}}\t{{.Names}}" |grep myweb
nginx Up About a minute k8s_myweb-container_myweb_default_7a4629ba-b1f8-4744-b7aa-a8db392b84ad_0
k8s.gcr.io/pause:3.4.1 Up About a minute k8s_POD_myweb_default_7a4629ba-b1f8-4744-b7aa-a8db392b84ad_0
# Pod 상세 정보 확인 : Pod 이름과 개별 Containe 이름은 다르다!, Port 80 사용 확인!
kubectl describe pod myweb
root@k8s-m:~# kubectl describe pod myweb
Name: myweb
Namespace: default
...
Status: Running
IP: 172.16.228.68
...
Containers:
myweb-container:
Container ID: docker://13997789d63088ca54a4e2051697b1d168047bfcc3001fa3ee9d56baac73103e
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
Port: 80/TCP
Host Port: 0/TCP
...
# Pod(Nginx) 에 curl 접속
ping 172.16.228.68
curl 172.16.228.68
# Pod 삭제
kubectl delete pod myweb
Created
July 14, 2021 12:16
-
-
Save ianychoi/746c80558e9634e1daa28c01bd764b5e to your computer and use it in GitHub Desktop.
쿠버네티스 실습 스크립트
apiVersion: v1
kind: Pod
metadata:
name: myweb2
spec:
containers:
- name: myweb2-nginx
image: nginx
ports:
- containerPort: 80
protocol: TCP
- name: myweb2-netshoot
image: nicolaka/netshoot
command: ["/bin/bash"]
args: ["-c", "while true; do sleep 5; curl localhost; done"] # 포드가 종료되지 않도록 유지합니다
# cat & here document 명령 조합으로 즉석(?) 리소스 생성 , containers 가 복수형을 주목!
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: myweb2
spec:
containers:
- name: myweb2-nginx
image: nginx
ports:
- containerPort: 80
protocol: TCP
- name: myweb2-netshoot
image: nicolaka/netshoot
command: ["/bin/bash"]
args: ["-c", "while true; do sleep 5; curl localhost; done"]
EOF
## 혹시 파일 생성이 어려울 경우 아래처럼 다운 받을 수 있습니다
## curl -s -O https://raw.githubusercontent.com/gasida/DKOS/main/3/myweb2.yaml
# 확인
# pod 정보 READY 에 2/2 를 확인 : pod 내 모든 컨테이너가 정상이여야지 status 가 Running 가 됨
k get pod -owide
root@k8s-m:~# k get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myweb2 2/2 Running 0 63s 172.16.46.6 k8s-w2 <none> <none>
# 배포된 워커노드에서 docker ps 로 확인
docker ps --format "table {{.Image}}\t{{.Status}}\t{{.Names}}" | grep myweb2
root@k8s-w2:~# docker ps --format "table {{.Image}}\t{{.Status}}\t{{.Names}}" | grep myweb2
nicolaka/netshoot Up About a minute k8s_myweb2-netshoot_myweb2_default_829cf80a-f55d-4ea6-b458-45e2b73620e7_0
nginx Up About a minute k8s_myweb2-nginx_myweb2_default_829cf80a-f55d-4ea6-b458-45e2b73620e7_0
k8s.gcr.io/pause:3.4.1 Up 2 minutes k8s_POD_myweb2_default_829cf80a-f55d-4ea6-b458-45e2b73620e7_0
# Pod 상세 정보에 컨테이너 2개 정보가 보인다
kubectl describe pod myweb2
root@k8s-m:~# kubectl describe pod myweb2
Name: myweb2
...
Containers:
myweb2-nginx:
Container ID: docker://2717dd093ee5c69a918c6c52461f47cf5f0c0330378730ce717d1fcabb0fc748
Image: nginx
...
myweb2-netshoot:
Container ID: docker://e3e3aef9ee53ef805336d4b6e0986f63e23c767b1648d18ff09948815c5f06a9
Image: nicolaka/netshoot
...
# 내부 컨테이너가 pause 컨테이너의 network namespace 를 공유
docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Names}}" | grep myweb2
root@k8s-w2:~# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Names}}" | grep myweb2
a7b5350b5b62 nicolaka/netshoot k8s_myweb2-netshoot_myweb2_default_baa75410-5e88-420d-a970-1c68c730a440_0
31aa478b06a0 nginx k8s_myweb2-nginx_myweb2_default_baa75410-5e88-420d-a970-1c68c730a440_0
4c9e92fd85d4 k8s.gcr.io/pause:3.4.1 k8s_POD_myweb2_default_baa75410-5e88-420d-a970-1c68c730a440_0
# 아래 처럼 위 pause 컨네이너를 nginx, netshoot 컨테이너가 공유하고 있음을 확인
root@k8s-w2:~# docker inspect a7b5350b5b62 |grep NetworkMode
"NetworkMode": "container:4c9e92fd85d417748d71e00a040be2497681464ca1ba719a5e5277ee0a123808",
root@k8s-w2:~# docker inspect 31aa478b06a0 |grep NetworkMode
"NetworkMode": "container:4c9e92fd85d417748d71e00a040be2497681464ca1ba719a5e5277ee0a123808",
# 터미널1 : nginx 컨테이너 웹 접속 로그 출력 : 접속자(myweb2-netshoot)의 IP 가 127.0.0.1!
kubectl logs -f myweb2 -c myweb2-nginx
root@k8s-m:~# kubectl logs -f myweb2 -c myweb2-nginx
127.0.0.1 - - [16/Jun/2021:06:22:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.77.0" "-"
127.0.0.1 - - [16/Jun/2021:06:23:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.77.0" "-"
...
# 터미널2
# myweb2-netshoot 컨테이너 내부에서 확인, -c 옵션으로 포드의 어떤 컨테이너에 대해 명령어를 수행할지 명시할 수 있음
kubectl exec -it myweb2 -c myweb2-netshoot -- zsh
root@k8s-m:~# kubectl exec -it myweb2 -c myweb2-netshoot -- /bin/bash
bash-5.1#
# 웹 접속 확인
curl -s localhost |grep nginx
bash-5.1# curl -s localhost |grep nginx
<title>Welcome to nginx!</title>
<h1>Welcome to nginx!</h1>
...
curl -s localhost/login.php
# 호스트 이름 확인
cat /etc/hosts
bash-5.1# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1 localhost
...
172.16.46.6 myweb2
# IP/MAC 확인
ip a
ip route show
bash-5.1# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
4: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1480 qdisc noqueue state UP group default
link/ether 7e:20:79:59:53:2c brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.16.46.6/32 brd 172.16.46.6 scope global eth0
valid_lft forever preferred_lft forever
## Calico를 디폴트로 하는 K8s의 경우 169.254.1.1로 라우팅
## - https://docs.projectcalico.org/reference/faq#why-does-my-container-have-a-route-to-16925411
## k3s 의 flannel 은 10.X.Y.1 로 출력됩니다!
bash-5.1# ip route show
default via 169.254.1.1 dev eth0
169.254.1.1 dev eth0 scope link
# TCP Listen 정보 확인 : TCP 80 리슨 상태이다!
ss -tln
bash-5.1# ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 511 [::]:80 [::]:*
# 프로세스 정보 확인
ps axf
ps -ef
bash-5.1# ps axf
PID USER TIME COMMAND
1 root 0:00 tail -f /dev/null
58 root 0:00 /bin/bash
76 root 0:00 ps axf
bash-5.1# ps -ef
PID USER TIME COMMAND
1 root 0:00 tail -f /dev/null
95 root 0:00 /bin/bash
104 root 0:00 ps -ef
# 빠져나오기
bash-5.1# exit
kubectl exec -it myweb2 -c myweb2-nginx -- /bin/bash
root@k8s-m:~# kubectl exec -it myweb2 -c myweb2-nginx -- /bin/bash
root@myweb2:/#
# 툴 설치
apt update
apt install -y procps net-tools
# 웹 접속 확인
curl -s localhost |grep nginx
# 호스트 이름 확인
cat /etc/hosts
# IP/MAC 확인
ifconfig
route -n
root@myweb2:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1480
inet 172.16.46.6 netmask 255.255.255.255 broadcast 172.16.46.6
ether 7e:20:79:59:53:2c txqueuelen 0 (Ethernet)
...
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
root@myweb2:/# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 169.254.1.1 0.0.0.0 UG 0 0 0 eth0
169.254.1.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
# TCP Listen 정보 확인 : TCP 80 리슨 상태이다!
netstat -tnlp
root@myweb2:/# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
tcp6 0 0 :::80 :::* LISTEN 1/nginx: master pro
# 프로세스 정보 확인
ps axf
ps -ef
root@myweb2:/# ps axf
PID TTY STAT TIME COMMAND
454 pts/0 Ss 0:00 /bin/bash
468 pts/0 R+ 0:00 \_ ps axf
1 ? Ss 0:00 nginx: master process nginx -g daemon off;
31 ? S 0:00 nginx: worker process
root@myweb2:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 04:22 ? 00:00:00 nginx: master process nginx -g daemon off;
nginx 31 1 0 04:22 ? 00:00:00 nginx: worker process
root 454 0 0 05:33 pts/0 00:00:00 /bin/bash
root 469 454 0 05:37 pts/0 00:00:00 ps -e
# 빠져나오기
bash-5.1# exit
# 모든 Pod 삭제
```bash
kubectl delete pod --all
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment