이미지 출처: https://www.fujitsu.com/sk/Images/ds_brocade_vdx_6940_ap.pdf
이미지 출처: https://mohan08p.medium.com/simplified-kubernetes-architecture-3febe12480eb
출처: https://www.samsungsds.com/kr/insights/docker.html
컨테이너(런타임) --배포--> 1 컨테이너 -> 1 애플리케이션 -> 컨테이너 하나 당 namespace(작업 공간)가 한 개 만들어진다. 따라서 각각 CPU와 RAM을 할당할 수 있다.
Pod(k8s) --> 1개 이상의 컨테이너가 그룹으로 묶인다. 만약 두 개의 컨테이너가 포함된 Pod가 있다면 namespcae(작업 공간)는 1개이며 따라서 하나의 CPU, RAM, NIC(IP)를 두 컨테이너가 공유한다.
dlrjsdn0414@cloudshell:~ (rapa-0901-lgw)$ gcloud container clusters get-credentials mytestcluster --zone asia-northeast2-b --project rapa-0901-lgw
Fetching cluster endpoint and auth data.
kubeconfig entry generated for mytestcluster.
dlrjsdn0414@cloudshell:~ (rapa-0901-lgw)$ kubectl get node
NAME STATUS ROLES AGE VERSION
gke-mytestcluster-default-pool-bd53b8ad-39ks Ready <none> 44m v1.22.11-gke.400
gke-mytestcluster-default-pool-bd53b8ad-fk0z Ready <none> 44m v1.22.11-gke.400
gke-mytestcluster-default-pool-bd53b8ad-ml13 Ready <none> 44m v1.22.11-gke.400
dlrjsdn0414@cloudshell:~ (rapa-0901-lgw)$ kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
gke-mytestcluster-default-pool-bd53b8ad-39ks Ready <none> 44m v1.22.11-gke.400 10.174.0.3 34.97.148.98 Ubuntu 20.04.4 LTS 5.4.0-1076-gke docker://19.3.8
gke-mytestcluster-default-pool-bd53b8ad-fk0z Ready <none> 44m v1.22.11-gke.400 10.174.0.2 34.97.134.126 Ubuntu 20.04.4 LTS 5.4.0-1076-gke docker://19.3.8
gke-mytestcluster-default-pool-bd53b8ad-ml13 Ready <none> 44m v1.22.11-gke.400 10.174.0.4 34.97.70.91 Ubuntu 20.04.4 LTS 5.4.0-1076-gke docker://19.3.8
dlrjsdn0414@cloudshell:~ (rapa-0901-lgw)$ mkdir 0901 ; cd 0901
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ touch nginx-pod.yaml
dlrjsdn0414@cloudshell:~ (rapa-0901-lgw)$ gcloud container clusters get-credentials mytestcluster --zone asia-northeast2-b --project rapa-0901-lgw
Fetching cluster endpoint and auth data.
kubeconfig entry generated for mytestcluster.
- Deployment
- 롤링 업데이트와 같은 버전 관리를 한다.
- ReplicaSet
- 고정된 수의 pod를 유지한다.
- label 이름과 pod의 개수를 확인한다.
- Pod
- label 이름을 가지고 있다.
kubectl run nginx-pod \
--image=nginx
외부에서 해당 애플리케이션을 즉시 노출시킬 수 없다. 이를 위해서는 별도의 service object를 이용해야 하며 service의 type으로는 clusterIP, nodePort, LoadBalancer(일반적으로 퍼블릭 클라우드에서 활용할 수 있음, on-premise에서는 metallb를 이용하여 환경 구성 가능함)가 있다.
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl api-resources
# 이름 단축 명령어 공유 여부
NAME SHORTNAMES APIVERSION NAMESPACED KIND
...
persistentvolumeclaims pvc v1 true PersistentVolumeClaim
persistentvolumes pv v1 false PersistentVolume
...
k8s는 namespace에 속하는 오브젝트와, namespace를 공유하는 오브젝트가 있다.
persistentvolumeclaims -> 특정 namespace에 속한다.
persistentvolumes(영구 볼륨) -> 특정 namespace에 속하지 않고, namespace를 공유한다.
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl api-resources | grep Pod
pods po v1 true Pod
podtemplates v1 true PodTemplate
horizontalpodautoscalers hpa autoscaling/v1 true HorizontalPodAutoscaler
pods metrics.k8s.io/v1beta1 true PodMetrics
poddisruptionbudgets pdb policy/v1 true PodDisruptionBudget
podsecuritypolicies psp policy/v1beta1 false PodSecurityPolicy
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get node
NAME STATUS ROLES AGE VERSION
gke-mytestcluster-default-pool-bd53b8ad-39ks Ready <none> 144m v1.22.11-gke.400
gke-mytestcluster-default-pool-bd53b8ad-fk0z Ready <none> 144m v1.22.11-gke.400
gke-mytestcluster-default-pool-bd53b8ad-ml13 Ready <none> 144m v1.22.11-gke.400
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get ns
NAME STATUS AGE
default Active 146m
kube-node-lease Active 146m
kube-public Active 146m
kube-system Active 146m
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
No resources found in default namespace.
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod --namespace kube-system
NAME READY STATUS RESTARTS AGE
event-exporter-gke-5479fd58c8-vjj5d 2/2 Running 0 146m
fluentbit-gke-kvt88 2/2 Running 0 146m
fluentbit-gke-m7lps 2/2 Running 0 146m
fluentbit-gke-qnssh 2/2 Running 0 146m
gke-metrics-agent-4s9hw 1/1 Running 0 146m
gke-metrics-agent-bl4j5 1/1 Running 0 146m
gke-metrics-agent-kjspq 1/1 Running 0 146m
konnectivity-agent-55846698cd-cgd7c 1/1 Running 0 146m
konnectivity-agent-55846698cd-h5sww 1/1 Running 0 144m
konnectivity-agent-55846698cd-q64mr 1/1 Running 0 144m
konnectivity-agent-autoscaler-555f599d94-5sm9w 1/1 Running 0 146m
kube-dns-85df8994db-54h4c 4/4 Running 0 143m
kube-dns-85df8994db-thhmq 4/4 Running 0 146m
kube-dns-autoscaler-f4d55555-q5hcr 1/1 Running 0 146m
kube-proxy-gke-mytestcluster-default-pool-bd53b8ad-39ks 1/1 Running 0 145m
kube-proxy-gke-mytestcluster-default-pool-bd53b8ad-fk0z 1/1 Running 0 145m
kube-proxy-gke-mytestcluster-default-pool-bd53b8ad-ml13 1/1 Running 0 145m
l7-default-backend-69fb9fd9f9-p4hgs 1/1 Running 0 146m
metrics-server-v0.4.5-fb4c49dd6-qz9zj 2/2 Running 0 142m
pdcsi-node-ckvng 2/2 Running 0 146m
pdcsi-node-srq7x 2/2 Running 0 146m
pdcsi-node-sxvll 2/2 Running 0 146m
dns는 3개 있음 -> worker node가 사용
kube-proxy는 3개 있음 -> worker node가 사용
metrics-server는 1개 있음 -> master node가 사용
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ vi nginx-pod.yaml
apiVersion: v1
kind:Pod
metadata:
name: my-nginx-pod
spec:
containers:
- name: my-nginx-ctn
image: nginx:latest
ports:
- containerPort: 80
protocol: TCP
kind: Pod
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ptah0414
Password:
WARNING! Your password will be stored unencrypted in /home/dlrjsdn0414/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl apply -f nginx-pod.yaml
pod/my-nginx-pod created
kubectl apply -f nginx-pod.yaml
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-pod 0/1 ContainerCreating 0 12s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-pod 1/1 Running 0 98s 10.96.1.7 gke-mytestcluster-default-pool-bd53b8ad-39ks <none> <none>
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl describe pod my-nginx-pod
Name: my-nginx-pod
Namespace: default
Priority: 0
Node: gke-mytestcluster-default-pool-bd53b8ad-39ks/10.174.0.3
Start Time: Thu, 01 Sep 2022 05:15:31 +0000
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.96.1.7
IPs:
IP: 10.96.1.7
Containers:
my-nginx-ctn:
Container ID: docker://1fe10b5bc2b0aede3e9d4691e363e82aa4497f8bff13f4dd1d3202a017821d7d
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:b95a99feebf7797479e0c5eb5ec0bdfa5d9f504bc94da550c2f58e839ea6914f
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 01 Sep 2022 05:15:48 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qb5tt (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-qb5tt:
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 2m49s default-scheduler Successfully assigned default/my-nginx-pod to gke-mytestcluster-default-pool-bd53b8ad-39ks
Normal Pulling 2m47s kubelet Pulling image "nginx:latest"
Normal Pulled 2m33s kubelet Successfully pulled image "nginx:latest" in 13.991675178s
Normal Created 2m32s kubelet Created container my-nginx-ctn
Normal Started 2m32s kubelet Started container my-nginx-ctn
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl exec -it \
> my-nginx-pod -- bash
root@my-nginx-pod:/#
root@my-nginx-pod:/# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
-> nginx가 호스팅된다.
apiVersion: v1
kind: Pod
metadata:
name: my-nginx-pod
spec:
containers:
- name: my-nginx-ctn
image: nginx:latest
ports:
- containerPort: 80
protocol: TCP
- name: my-centos-ctn
image: centos
command: ["tail"] # ENTRYPOINT
args: ["-f", "/dev/null"] # CMD
kind: Pod
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete -f nginx-pod.yaml
pod "my-nginx-pod" deleted
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl apply -f nginx-pod.yaml
pod/my-nginx-pod created
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-pod 2/2 Running 0 66s
-> 컨테이너 2개 실행 중
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl exec -it my-nginx-pod \
> -c my-centos-ctn -- bash
[root@my-nginx-pod /]#
[root@my-nginx-pod /]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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@my-nginx-pod /]#
centos에서 localhost:80에 접속을 하면 같은 pod 내에 존재하는 nginx 컨테이너(80 포트)에 접속된다. 같은 작업 공간 내에서 NIC 자원을 공유한다.
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete -f nginx-pod.yaml
pod "my-nginx-pod" deleted
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ vi nginx-rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-nginx
spec:
replicas: 3
selector:
matchLabels:
app: my-nginx-pods-label
template:
metadata:
name: my-nginx-pod
labels:
app: my-nginx-pods-label
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl apply -f nginx-rs.yaml
replicaset.apps/replicaset-nginx created
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset-nginx 3 3 1 7s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-nginx-7qhwf 0/1 ContainerCreating 0 10s
replicaset-nginx-bz2fb 0/1 ContainerCreating 0 10s
replicaset-nginx-t2fkc 1/1 Running 0 10s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
replicaset-nginx-7qhwf 0/1 ContainerCreating 0 14s <none> gke-mytestcluster-default-pool-bd53b8ad-fk0z <none> <none>
replicaset-nginx-bz2fb 0/1 ContainerCreating 0 14s <none> gke-mytestcluster-default-pool-bd53b8ad-ml13 <none> <none>
replicaset-nginx-t2fkc 1/1 Running 0 14s 10.96.1.9 gke-mytestcluster-default-pool-bd53b8ad-39ks <none> <none>
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-nginx-7qhwf 1/1 Running 0 54s app=my-nginx-pods-label
replicaset-nginx-bz2fb 1/1 Running 0 54s app=my-nginx-pods-label
replicaset-nginx-t2fkc 1/1 Running 0 54s app=my-nginx-pods-label
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-nginx-7qhwf 1/1 Running 0 2m5s
replicaset-nginx-bz2fb 1/1 Running 0 2m5s
replicaset-nginx-t2fkc 1/1 Running 0 2m5s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete pod replicaset-nginx-7qhwf
pod "replicaset-nginx-7qhwf" deleted
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-nginx-bz2fb 1/1 Running 0 2m48s
replicaset-nginx-mt74s 1/1 Running 0 21s
replicaset-nginx-t2fkc 1/1 Running 0 2m48s
-> 컨테이너가 재생성되었음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl edit pod replicaset-nginx-mt74s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-nginx-4q9k6 1/1 Running 0 5s
replicaset-nginx-bz2fb 1/1 Running 0 5m19s
replicaset-nginx-mt74s 1/1 Running 0 2m52s
replicaset-nginx-t2fkc 1/1 Running 0 5m19s
-> label이 제거되었으므로, 컨테이너가 새로 생성되었음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-nginx-4q9k6 1/1 Running 0 5s
replicaset-nginx-bz2fb 1/1 Running 0 5m19s
replicaset-nginx-mt74s 1/1 Running 0 2m52s
replicaset-nginx-t2fkc 1/1 Running 0 5m19s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete -f nginx-rs.yaml
replicaset.apps "replicaset-nginx" deleted
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-nginx-mt74s 1/1 Running 0 3m25s
-> label이 제거된 컨테이너는 지워지지 않았음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete pod replicaset-nginx-mt74s
pod "replicaset-nginx-mt74s" deleted
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ vi nginx-rs.yaml
spec:
replicas: 5
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl apply -f nginx-rs.yaml
replicaset.apps/replicaset-nginx created
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-nginx-596kc 1/1 Running 0 66s
replicaset-nginx-8lsnq 1/1 Running 0 66s
replicaset-nginx-lsqsb 1/1 Running 0 66s
replicaset-nginx-n9tpw 1/1 Running 0 66s
replicaset-nginx-ttqdr 1/1 Running 0 66s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete -f nginx-rs.yaml
replicaset.apps "replicaset-nginx" deleted
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ cp nginx-rs.yaml nginx-deploy.yaml
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ vi nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-nginx
spec:
replicas: 3
selector:
matchLabels:
app: my-nginx-pods-label
template:
metadata:
name: my-nginx-pod
labels:
app: my-nginx-pods-label
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl apply -f nginx-deploy.yaml
deployment.apps/deploy-nginx created
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get deploy,rs,pod
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deploy-nginx 3/3 3 3 76s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deploy-nginx-7f797dbc66 3 3 3 76s
NAME READY STATUS RESTARTS AGE
pod/deploy-nginx-7f797dbc66-8pcnr 1/1 Running 0 76s
pod/deploy-nginx-7f797dbc66-b9947 1/1 Running 0 76s
pod/deploy-nginx-7f797dbc66-nbp65 1/1 Running 0 76s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get rs --show-labels
NAME DESIRED CURRENT READY AGE LABELS
deploy-nginx-7f797dbc66 3 3 3 2m37s app=my-nginx-pods-label,pod-template-hash=7f797dbc66
-> my-nginx-pods-label라고 label을 지정했었음
-> label 뒷 부분의 pod-template-hash=7f797dbc66은 replicaset의 id임
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl rollout history deploy deploy-nginx
deployment.apps/deploy-nginx
REVISION CHANGE-CAUSE
1 <none>
-> 최초 배포인 1번 상태임
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl set image \
> deploy deploy-nginx \ # deploy의 이름: deploy-nginx
> nginx=httpd:latest # [컨테이너 이름]=[이미지 이름]
deployment.apps/deploy-nginx image updated
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
deploy-nginx-586b998bb6-c6s6f 1/1 Running 0 70s
deploy-nginx-586b998bb6-fdm9x 1/1 Running 0 64s
deploy-nginx-586b998bb6-sw9vr 1/1 Running 0 78s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl exec -it \
> deploy-nginx-586b998bb6-c6s6f -- bash
root@deploy-nginx-586b998bb6-c6s6f:/usr/local/apache2#
root@deploy-nginx-586b998bb6-c6s6f:/usr/local/apache2# cat htdocs/index.html
<html><body><h1>It works!</h1></body></html>
-> 컨테이너가 apache2 이미지로 변경되었음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl rollout history deploy deploy-nginx
deployment.apps/deploy-nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
-> 2번으로 변경되었음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl rollout undo deploy deploy-nginx \
> --to-revision=1
deployment.apps/deploy-nginx rolled back
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get rs --show-labels
NAME DESIRED CURRENT READY AGE LABELS
deploy-nginx-586b998bb6 0 0 0 6m59s app=my-nginx-pods-label,pod-template-hash=586b998bb6
deploy-nginx-7f797dbc66 3 3 3 13m app=my-nginx-pods-label,pod-template-hash=7f797dbc66
-> 최초 배포(1)로 rollback 되었음
-> deploy를 이용하면 하위에 있는 replicaset이 생성된다. 우리는 두 replicaset의 정보를 확인해서 둘 다 동일한 라벨이 있다는 것을 확인할 수 있다. 만약 rs에서 개수를 6개로 늘린다면(최초 배포는 3개) 신규 이미지를 활용한 pod만 6개로 3개가 추가 된다.
deployment는 이미지의 버전 관리가 가능하여 기존 버전에서 생성된 pod들과 신규 이미지에서 생성된 pod를 구분하기 위하여 추가적인 label을 자동 생성한 뒤, 이를 pod에 붙여준다. 이를 통해 기존 pod와 신규 pod를 구분할 수 있다.
deployment는 버전 관리를 위해 라벨을 자동으로 추가 부착한다.
NAME READY STATUS RESTARTS AGE
pod/deploy-nginx-7f797dbc66-66nbr 1/1 Running 0 11m
pod/deploy-nginx-7f797dbc66-69d85 1/1 Running 0 11m
pod/deploy-nginx-7f797dbc66-x7z9j 1/1 Running 0 11m
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl exec -it \
> pod/deploy-nginx-7f797dbc66-66nbr -- bash
root@deploy-nginx-7f797dbc66-66nbr:/# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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@deploy-nginx-7f797dbc66-66nbr:/#
-> nginx로 rollback 되었음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete -f nginx-deploy.yaml
deployment.apps "deploy-nginx" deleted
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get deploy,pod
No resources found in default namespace.
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ touch service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx-lb
spec:
type: LoadBalancer
ports:
- port: 80 # LB의 포트
targetPort: 80 # pod의 포트
protocol: TCP
selector:
app: my-nginx-pods-label
kind: Service
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl apply -f service.yaml
service/my-nginx-lb created
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.100.0.1 <none> 443/TCP 5h44m
my-nginx-lb LoadBalancer 10.100.5.213 <pending> 80:30479/TCP 48s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.100.0.1 <none> 443/TCP 5h44m
my-nginx-lb LoadBalancer 10.100.5.213 34.97.133.36 80:30479/TCP 59s
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl apply -f nginx-deploy.yaml
deployment.apps/deploy-nginx created
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl set image deploy deploy-nginx nginx=httpd:latest
deployment.apps/deploy-nginx image updated
-> httpd로 이미지가 변경되었음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl rollout undo \
> deploy deploy-nginx \
> --to-revision=1
deployment.apps/deploy-nginx rolled back
-> nginx로 rollback 되었음
dlrjsdn0414@cloudshell:~/0901 (rapa-0901-lgw)$ kubectl delete deploy,pod,svc --all
deployment.apps "deploy-nginx" deleted
pod "deploy-nginx-7f797dbc66-2ggwr" deleted
pod "deploy-nginx-7f797dbc66-w876n" deleted
pod "deploy-nginx-7f797dbc66-xpbjm" deleted
service "kubernetes" deleted
service "my-nginx-lb" deleted
object의 서비스: pod를 외부로 노출시키고 외부에서 연결이 가능하도록 하기 위한 오브젝트로써 다음과 같은 3가지를 이용한다.
1. cluster ip: 노드(가상머신) 밖으로 노출은 안 된다.
2. node port: 서비스의 특정 포트와 pod를 연결하여 외부로부터 연결 가능
3. load balancer: 로드밸런서를 이용하여 pod와 연결(L4)
L7 LoadBalancer와 같은 기능은 ingress 오브젝트에서 제공된다.