Cloud Shell 시스템에는 홈 디렉토리를 위한 5GB의 스토리지가 제공됩니다.
<1> 쿠버네티스 노드와 Pod와 관계 알아 보기
<2> 컨테이너 2개 가진 Pod생성하기
<3> 컨테이너 접속해 설정하기
<1> 쿠버네티스 노드와 Pod와 관계 알아 보기
1
노드 상태 확인
노드 서버 안에 Pod가 생성된다.
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
10.0.10.141 Ready node 34h v1.25.4
10.0.10.80 Ready node 34h v1.25.4
2
kubectl run으로 yaml 파일 만들기
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl run myweb --image nginx --dry-run=client -o yaml > myweb.yaml
topasvga@cloudshell:~ (ap-seoul-1)$ cat myweb.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: myweb
name: myweb
spec:
containers:
- image: nginx
name: myweb
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
3
파일만 만들었지 pod로 만든건 아니다.
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get pods
No resources found in default namespace.
4
myweb.yaml 파일로 만들기
topasvga@cloudshell:~ (ap-seoul-1)$ cat << EOF > myweb.yaml
> apiVersion: v1
> kind: Pod
> metadata:
> name: myweb
> spec:
> containers:
> - image: nginx:latest
> name: myweb-container
> ports:
> - containerPort: 80
> protocol: TCP
> EOF
5
만들어진 myweb.yaml 파일로 배포하기
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl apply -f myweb.yaml && kubectl get pod -w
pod/myweb created
6
pod 1개 만들어 졌다. 컨테이너도 1개
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb 1/1 Running 0 33s
7
파일로 다시 만들어도, pod는 이미 만들어져서 추가로 만들어 지진 않는다.
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl apply -f myweb.yaml
pod/myweb unchanged
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myweb 1/1 Running 0 85s 10.244.0.12 10.0.10.80 <none> <none>
8
Pod삭제 하기
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl delete pod myweb
pod "myweb" deleted
<2> 컨테이너 2개 가진 Pod생성하기
1
topasvga@cloudshell:~ (ap-seoul-1)$ 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
pod/myweb2 created
2
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb2 2/2 Running 0 66s
3
자세히 보기
pod에 컨테이너 2개가 생성 되었다.
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl describe pod myweb2
Name: myweb2
Namespace: default
Priority: 0
Node: 10.0.10.80/10.0.10.80
Start Time: Wed, 15 Feb 2023 21:23:59 +0000
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.244.0.13
IPs:
IP: 10.244.0.13
Containers:
myweb2-nginx:
:
:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m33s default-scheduler Successfully assigned default/myweb2 to 10.0.10.80
Normal Pulling 3m33s kubelet Pulling image "nginx"
Normal Pulled 3m30s kubelet Successfully pulled image "nginx" in 3.324741465s
Normal Created 3m30s kubelet Created container myweb2-nginx
Normal Started 3m30s kubelet Started container myweb2-nginx
Normal Pulling 3m30s kubelet Pulling image "nicolaka/netshoot"
Normal Pulled 3m12s kubelet Successfully pulled image "nicolaka/netshoot" in 17.662017124s
Normal Created 3m12s kubelet Created container myweb2-netshoot
Normal Started 3m12s kubelet Started container myweb2-netshoot
4
접속 로그로 확인하기
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl logs -f myweb2 -c myweb2-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/
2023/02/15 21:24:03 [notice] 1#1: nginx/1.23.3
2023/02/15 21:24:03 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/02/15 21:24:03 [notice] 1#1: OS: Linux 5.4.17-2136.314.6.2.el8uek.x86_64
2023/02/15 21:24:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/02/15 21:24:03 [notice] 1#1: start worker processes
2023/02/15 21:24:03 [notice] 1#1: start worker process 28
2023/02/15 21:24:03 [notice] 1#1: start worker process 29
127.0.0.1 - - [15/Feb/2023:21:24:26 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.87.0" "-"
127.0.0.1 - - [15/Feb/2023:21:24:31 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.87.0" "-"
<3> 컨테이너 접속해 설정하기
1
-c 옵션으로 컨테이너에 접속해보자.
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl exec -it myweb2 -c myweb2-netshoot -- zsh
2
myweb2 컨테이너에 접속이 된다.
myweb2# curl -s localhost |grep nginx
<title>Welcome to nginx!</title>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
<a href="http://nginx.org/">nginx.org</a>.<br/>
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
myweb2# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.244.0.13 myweb2
3
ip확인
10.244.0.13
myweb2# 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
3: eth0@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 8950 qdisc noqueue state UP group default
link/ether 0e:77:89:1c:d9:d2 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.244.0.13/25 brd 10.244.0.127 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::c77:89ff:fe1c:d9d2/64 scope link
valid_lft forever preferred_lft forever
4
컨테이너에서 데몬 떠있는것 확인
80포트 떠있음을 확인함.
myweb2# 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 [::]:*
myweb2#
나가기
myweb2# exit
5
다른 nginx 컨테이너로 들어가 업데이트 하기
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl exec -it myweb2 -c myweb2-nginx -- /bin/bash
root@myweb2:/# apt update
Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:4 http://deb.debian.org/debian bullseye/main amd64 Packages [8183 kB]
Get:5 http://deb.debian.org/debian-security bullseye-security/main amd64 Packages [226 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [14.6 kB]
Fetched 8632 kB in 1s (7443 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
1 package can be upgraded. Run 'apt list --upgradable' to see it.
6
컨테이너에서 툴 설치하기
root@myweb2:/# apt install -y procps net-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
:
:
Setting up procps (2:3.3.17-5) ...
Processing triggers for libc-bin (2.31-13+deb11u5) ...
7
컨테이너에서 웹 서비스 확인하기
root@myweb2:/# curl -s localhost |grep nginx
<title>Welcome to nginx!</title>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
<a href="http://nginx.org/">nginx.org</a>.<br/>
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
8
컨테이너 IP 확인하기
컨테이너 2개 모두 IP가 10.244.0.13 로 동일 하다.
모두 Pod안에 있기 때문에 IP는 동일한게 정상이다.
root@myweb2:/# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.244.0.13 myweb2
9
root@myweb2:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 8950
inet 10.244.0.13 netmask 255.255.255.128 broadcast 10.244.0.127
inet6 fe80::c77:89ff:fe1c:d9d2 prefixlen 64 scopeid 0x20<link>
ether 0e:77:89:1c:d9:d2 txqueuelen 0 (Ethernet)
RX packets 602 bytes 9967394 (9.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 434 bytes 31591 (30.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@myweb2:/# exit
exit
command terminated with exit code 127
10
Pod 삭제하기
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl delete pod myweb2
pod "myweb2" deleted
pod 삭제 확인하기
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get pods
No resources found in default namespace.
감사합니다.