IT STUDY LOG
Sprint - 새 버전이 망가졌어요 본문
#학습 목표
- 파드 명세를 작성할 수 있다.
- 디플로이먼트 명세를 작성할 수 있다.
- 서비스를 이용해 파드를 노출할 수 있다.
- kubectl apply 명령을 이용해 리소스를 생성할 수 있다.
- 디플로이먼트 명세를 수정(또는 재작성)하여 새로운 버전을 배포할 수 있다.
- kubectl rollout 명령을 이용해 롤링 배포 현황을 확인할 수 있다.
- 새로운 버전에 문제가 발생했을 때 롤백할 수 있다.
#해결 과제
💡 cozserver-deployment-v1.yaml
💡 cozserver-deployment-v2.yaml
💡 cozserver-deployment-v3.yaml
💡 cozserver-service.yaml
💡 rollback-log.txt (롤백 터미널 로그)
#과제 항목별 진행 상황
✏️ STEP 0 : 도커로 컨테이너 구동 복습
$ docker run -p 80:8080 sebcontents/cozserver:1.0
Unable to find image 'sebcontents/cozserver:1.0' locally
1.0: Pulling from sebcontents/cozserver
59bf1c3509f3: Pull complete
683dd8c3cc08: Pull complete
ae5b2724f19b: Pull complete
39190df3f477: Pull complete
ace5267edc85: Pull complete
054a12501f62: Pull complete
5012be8966ae: Pull complete
00df233fc0a3: Pull complete
Digest: sha256:b22208c1daa48966da36df4bcd365167b7585b8708d2a669c016cb2b9049eb11
Status: Downloaded newer image for sebcontents/cozserver:1.0
Example app listening at http://localhost:8080
[v1] running in pod ::ffff:172.17.0.1
✏️ STEP 1 : 파드
0. minikube 컨테이너 실행
$ minikube start
😄 Darwin 10.15.7 의 minikube v1.30.1
✨ 기존 프로필에 기반하여 docker 드라이버를 사용하는 중
👍 minikube 클러스터의 minikube 컨트롤 플레인 노드를 시작하는 중
🚜 베이스 이미지를 다운받는 중 ...
🔄 Restarting existing docker container for "minikube" ...
🐳 쿠버네티스 v1.26.3 을 Docker 23.0.2 런타임으로 설치하는 중
🔗 Configuring bridge CNI (Container Networking Interface) ...
🔎 Kubernetes 구성 요소를 확인...
▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟 애드온 활성화 : storage-provisioner
🏄 끝났습니다! kubectl이 "minikube" 클러스터와 "default" 네임스페이스를 기본적으로 사용하도록 구성되었습니다.
1. sebcontents/cozserver:1.0 이미지를 바탕으로 cozserver-pod.yaml 에 파드 명세 작성
# cozserver-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: cozserver
spec:
containers:
- name: cozserver
image: sebcontents/cozserver:1.0
ports:
- containerPort: 8080
2. kubectl 명령어로 파드 실행
$ kubectl apply -f cozserver-pod.yaml
pod/cozserver created
$ kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default cozserver 0/1 ContainerCreating 0 8s
kube-system coredns-787d4945fb-xl4b5 1/1 Running 1 (115s ago) 14h
kube-system etcd-minikube 1/1 Running 1 (115s ago) 14h
kube-system kube-apiserver-minikube 1/1 Running 1 (114s ago) 14h
kube-system kube-controller-manager-minikube 1/1 Running 1 (115s ago) 14h
kube-system kube-proxy-7cx2g 1/1 Running 1 (115s ago) 14h
kube-system kube-scheduler-minikube 1/1 Running 1 (115s ago) 14h
kube-system storage-provisioner 1/1 Running 3 (49s ago) 14h
$ kubectl describe pod cozserver
Name: cozserver
Namespace: default
Priority: 0
Service Account: default
Node: minikube/
Start Time: Fri, 19 May 2023 10:26:55 +0900
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.244.0.9
IPs:
IP: 10.244.0.9
Containers:
cozserver:
Container ID: docker://99e3faabf244961a798945a09f738b17a5554c48e05b57c6cfafae85aba1e059
Image: sebcontents/cozserver:1.0
Image ID: docker-pullable://sebcontents/cozserver@sha256:b22208c1daa48966da36df4bcd365167b7585b8708d2a669c016cb2b9049eb11
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 19 May 2023 10:27:11 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-hn6kk (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-hn6kk:
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 61s default-scheduler Successfully assigned default/cozserver to minikube
Normal Pulling 60s kubelet Pulling image "sebcontents/cozserver:1.0"
Normal Pulled 46s kubelet Successfully pulled image "sebcontents/cozserver:1.0" in 14.497008832s (14.497067596s including waiting)
Normal Created 45s kubelet Created container cozserver
Normal Started 45s kubelet Started container cozserver
3. 생성한 pods 삭제
$ kubectl delete pods cozserver
pod "cozserver" deleted
$ kubectl get pods
No resources found in default namespace.
✏️ STEP 2: 디플로이먼트를 이용한 1.0 배포
1. sebcontents/cozserver:1.0 이미지를 바탕으로 cozserver-deployment-v1.yaml에 파드 디플로이먼트 명세 작성
# cozserver-deployment-v1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cozserver-deployment # metadata.name : 디플로이먼트명
labels:
app: cozserver
spec:
replicas: 1 # spec.replicas 필드: 디플로이먼트가 생성하는 레플리카 파드 수
selector:
matchLabels:
app: cozserver # spec.selector 필드: 디플로이먼트가 관리할 파드를 찾는 방법을 정의 (파드 템플릿에 정의된 레이블을 선택)
template:
metadata:
labels:
app: cozserver
spec:
containers:
- name: cozserver # 생성할 파드명
image: sebcontents/cozserver:1.0 # 생성할 파드 이미지
ports: # 파드 컨테이너에 노출할 포트
- containerPort: 8080
2. kubectl 명령어로 디플로이먼트 명세 실행
$ kubectl apply -f cozserver-deployment-v1.yaml --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/cozserver-deployment created
# NAME: 네임스페이스에 있는 디플로이먼트 이름의 목록
# READY: 사용자가 사용할 수 있는 애플리케이션의 레플리카의 수를 표시
# UP-TO-DATE: 의도한 상태를 얻기 위해 업데이트된 레플리카의 수를 표시
# AVAILABLE: 사용자가 사용할 수 있는 애플리케이션 레플리카의 수를 표시
# AGE: 애플리케이션의 실행된 시간을 표시
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
cozserver-deployment 1/1 1 1 11s
$ kubectl describe deployment cozserver-deployment
Name: cozserver-deployment
Namespace: default
CreationTimestamp: Fri, 19 May 2023 11:25:12 +0900
Labels: app=cozserver
Annotations: deployment.kubernetes.io/revision: 1
kubernetes.io/change-cause: kubectl apply --filename=cozserver-deployment-v1.yaml --record=true
Selector: app=cozserver
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=cozserver
Containers:
cozserver:
Image: sebcontents/cozserver:1.0
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: cozserver-deployment-58df6df99f (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 24s deployment-controller Scaled up replica set cozserver-deployment-58df6df99f to 1
3. kubectl 명령어로 레플리카셋 출력
# NAME: 네임스페이스에 있는 레플리카셋 이름의 목록
# DESIRED: 디플로이먼트의 생성 시 정의된 의도한 애플리케이션 레플리카의 수
# CURRENT: 현재 실행 중인 레플리카의 수를 표시
# READY: 사용자가 사용할 수 있는 애플리케이션의 레플리카의 수를 표시
# AGE: 애플리케이션의 실행된 시간을 표시
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
cozserver-deployment-58df6df99f 1 1 1 4m48s
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cozserver-deployment-58df6df99f-6jb86 1/1 Running 0 5m8s app=cozserver,pod-template-hash=58df6df99f
✏️ STEP 3: 서비스
1. cozserver-service.yaml에 서비스 명세 작성
# cozserver-service.yaml
apiVersion: v1
kind: Service
metadata:
name: cozserver-service
spec:
type: LoadBalancer
selector:
app: cozserver
ports:
- protocol: TCP
port: 80 # 노출할 포트
targetPort: 8080 # 파드 포트
2. kubectl 명령어로 서비스 명세 실행
$ kubectl apply -f cozserver-service.yaml
service/cozserver-service created
# minikube는 터널링을 해주어야 external-ip를 로컬호스트로 연결할 수 있음
$ minikube tunnel
# 서비스 조회
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cozserver-service LoadBalancer 10.96.120.43 127.0.0.1 80:31500/TCP 34s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15h
3. docker exec -it <컨테이너 ID> /bin/sh로 컨테이너 내부 확인
$ docker exec -it ddf08e8a703a /bin/sh
$ ls
Dockerfile node_modules package.json
app.js package-lock.json
$ cat app.js
const express = require('express')
const cors = require('cors')
const os = require('os')
const app = express()
app.use(cors())
const port = 8080
app.get('/', (req, res) => {
console.log('[v1] running in pod ' + req.socket.remoteAddress)
res.send(`
<h1>CozServer에 오신 것을 환영합니다!</h1>
<div>버전: v1.0</div>
<div>파드 이름: ${os.hostname()}</div>
<div>${req.socket.remoteAddress}에서 오셨군요!</div>
`)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
4. http://localhost로 접속해보기
✏️ STEP 4: 2.0 배포, 롤아웃
1. sebcontents/cozserver:2.0 이미지를 바탕으로 cozserver-deployment-v2.yaml에 파드 디플로이먼트 명세 작성
# cozserver-deployment-v2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cozserver-deployment
labels:
app: cozserver
spec:
replicas: 1
strategy:
type: RollingUpdate
selector:
matchLabels:
app: cozserver
template:
metadata:
labels:
app: cozserver
spec:
containers:
- name: cozserver
image: sebcontents/cozserver:2.0
ports:
- containerPort: 8080
2. kubectl get all 명령어를 통해 적용할 때, 배포되는 과정을 꼭 확인
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/cozserver-deployment-58df6df99f-g7fwh 1/1 Terminating 0 12m
pod/cozserver-deployment-64f6b57986-pw5lq 1/1 Running 0 37s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/cozserver-service LoadBalancer 10.96.120.43 <pending> 80:31500/TCP 11m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/cozserver-deployment 1/1 1 1 12m
NAME DESIRED CURRENT READY AGE
replicaset.apps/cozserver-deployment-58df6df99f 0 0 0 12m
replicaset.apps/cozserver-deployment-64f6b57986 1 1 1 38s
$ kubectl rollout history deployment/cozserver-deployment
deployment.apps/cozserver-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=cozserver-deployment-v1.yaml --record=true
2 kubectl apply --filename=cozserver-deployment-v2.yaml --record=true
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
cozserver-deployment-64f6b57986-pw5lq 1/1 Running 0 4m58s
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
cozserver-deployment-58df6df99f 0 0 0 17m
cozserver-deployment-64f6b57986 1 1 1 5m15s
$ kubectl describe deployments
Name: cozserver-deployment
Namespace: default
CreationTimestamp: Fri, 19 May 2023 11:25:12 +0900
Labels: app=cozserver
Annotations: deployment.kubernetes.io/revision: 2
kubernetes.io/change-cause: kubectl apply --filename=cozserver-deployment-v2.yaml --record=true
Selector: app=cozserver
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=cozserver
Containers:
cozserver:
Image: sebcontents/cozserver:2.0
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: cozserver-deployment-64f6b57986 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set cozserver-deployment-58df6df99f to 1
Normal ScalingReplicaSet 5m32s deployment-controller Scaled up replica set cozserver-deployment-64f6b57986 to 1
Normal ScalingReplicaSet 5m24s deployment-controller Scaled down replica set cozserver-deployment-58df6df99f to 0 from 1
레플리카셋의 변화 과정
-
DESIRED, CURRENT, READY의 의미
-
3. http://localhost로 접속해보기
✏️ STEP 5: 3.0 배포와 2.0으로의 롤백
1. sebcontents/cozserver:3.0 이미지를 바탕으로 cozserver-deployment-v3.yaml에 파드 디플로이먼트 명세 작성
# cozserver-deployment-v3.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cozserver-deployment
labels:
app: cozserver
spec:
replicas: 1
strategy: # 배포 전략 설정
type: RollingUpdate
selector:
matchLabels:
app: cozserver
template:
metadata:
labels:
app: cozserver
spec:
containers:
- name: cozserver
image: sebcontents/cozserver:3.0
ports:
- containerPort: 8080
2. http://localhost 로 접속해보기
3. cozserver-deployment-v3.yaml에 파드 디플로이먼트 명세 작성
# 롤아웃 상태 확인
$ kubectl rollout status deployment/cozserver-deployment
deployment "cozserver-deployment" successfully rolled out
# 롤아웃 내역 확인
$ kubectl rollout history deployment cozserver-deployment
deployment.apps/cozserver-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=cozserver-deployment-v1.yaml --record=true
2 kubectl apply --filename=cozserver-deployment-v2.yaml --record=true
3 kubectl apply --filename=cozserver-deployment-v3.yaml --record=true
4. kubectl rollout undo 명령어로 이전 버전 롤백하기
$ kubectl rollout undo deployment/cozserver-deployment
deployment.apps/cozserver-deployment rolled back
$ kubectl describe deployments
Name: cozserver-deployment
Namespace: default
CreationTimestamp: Fri, 19 May 2023 11:25:12 +0900
Labels: app=cozserver
Annotations: deployment.kubernetes.io/revision: 4 #revision 버전
kubernetes.io/change-cause: kubectl apply --filename=cozserver-deployment-v2.yaml --record=true
Selector: app=cozserver
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=cozserver
Containers:
cozserver:
Image: sebcontents/cozserver:2.0
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: cozserver-deployment-64f6b57986 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 37m deployment-controller Scaled up replica set cozserver-deployment-58df6df99f to 1
Normal ScalingReplicaSet 25m deployment-controller Scaled up replica set cozserver-deployment-64f6b57986 to 1
Normal ScalingReplicaSet 25m deployment-controller Scaled down replica set cozserver-deployment-58df6df99f to 0 from 1
Normal ScalingReplicaSet 5m deployment-controller Scaled up replica set cozserver-deployment-69bf859597 to 1
Normal ScalingReplicaSet 4m53s deployment-controller Scaled down replica set cozserver-deployment-64f6b57986 to 0 from 1
Normal ScalingReplicaSet 30s deployment-controller Scaled up replica set cozserver-deployment-64f6b57986 to 1 from 0
Normal ScalingReplicaSet 29s deployment-controller Scaled down replica set cozserver-deployment-69bf859597 to 0 from 1
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
cozserver-deployment 1/1 1 1 38m
5. 롤백이 잘 되었는지 확인
$ kubectl rollout history deployment cozserver-deployment
deployment.apps/cozserver-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=cozserver-deployment-v1.yaml --record=true
2 kubectl apply --filename=cozserver-deployment-v2.yaml --record=true
3 kubectl apply --filename=cozserver-deployment-v3.yaml --record=true
$ kubectl rollout undo deployment/cozserver-deployment
deployment.apps/cozserver-deployment rolled back
$ kubectl rollout history deployment cozserver-deployment
deployment.apps/cozserver-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=cozserver-deployment-v1.yaml --record=true
3 kubectl apply --filename=cozserver-deployment-v3.yaml --record=true
4 kubectl apply --filename=cozserver-deployment-v2.yaml --record=true
#References
https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
https://kubernetes.io/ko/docs/concepts/workloads/controllers/deployment/
https://kubernetes.io/ko/docs/concepts/services-networking/service/
https://kubernetes.io/ko/docs/concepts/services-networking/connect-applications-service/
https://honggg0801.tistory.com/45
https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/deployment-v1/
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#rollout
https://kubernetes.io/ko/docs/reference/kubectl/cheatsheet/
https://kubernetes.io/ko/docs/concepts/workloads/controllers/replicaset/
'devops bootcamp 4 > pair/team log' 카테고리의 다른 글
Sprint - 모니터링 시스템 구축 (0) | 2023.06.05 |
---|---|
Sprint - Auto Scaling + CloudWatch를 이용한 알림 (0) | 2023.06.02 |
Sprint - Terraform x AWS (0) | 2023.05.17 |
Sprint - 서버리스 애플리케이션 (0) | 2023.05.11 |
Sprint - API Gateway와 서버리스 애플리케이션 (1) | 2023.05.11 |