Zero To One
쿠버네티스 자습서 본문
순서
1. 파드 생성
2. 파드 적용
3. 서비스
4. 디플로이먼트
5. 롤링 업테이트
1. 파드 생성
1-1. 디렉토리를 생성해준다
mkdir kub
1-2. simple-pod.ymal를 생성하고 코드를 넣어준다.
nano simple-pod.ymal
코드 (쿠버네티스 예제)
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
https://kubernetes.io/docs/concepts/workloads/pods/
2. 파드 적용
minikube start
kubectl apply -f simple-pod.yaml
> minikube start
😄 Ubuntu 20.04 의 minikube v1.25.2
✨ 기존 프로필에 기반하여 docker 드라이버를 사용하는 중
🧯 The requested memory allocation of 15743MiB does not leave room for system overhead (total system memory: 15743MiB). You may face stability issues.
💡 권장: Start minikube with less memory allocated: 'minikube start --memory=3900mb'
👍 minikube 클러스터의 minikube 컨트롤 플레인 노드를 시작하는 중
🚜 베이스 이미지를 다운받는 중 ...
🔄 Restarting existing docker container for "minikube" ...
❗ This container is having trouble accessing https://k8s.gcr.io
💡 To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
🐳 쿠버네티스 v1.23.4-rc.0 을 Docker 20.10.12 런타임으로 설치하는 중
▪ kubelet.housekeeping-interval=5m
🔎 Kubernetes 구성 요소를 확인...
▪ Using image kubernetesui/dashboard:v2.3.1
▪ Using image kubernetesui/metrics-scraper:v1.0.7
▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟 애드온 활성화 : storage-provisioner, default-storageclass, dashboard
🏄 끝났습니다! kubectl이 "minikube" 클러스터와 "default" 네임스페이스를 기본적으로 사용하도록 구성되었습니다.
> kubectl apply -f simple-pod.yaml
pod/nginx created
pod/nginx created 라는 결과가 출력되었다면, 즉시 명령어 kubectl get pods 를 통해 상태를 확인한다
> kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-7bc9d7884c-vpkff 1/1 Running 1 (11h ago) 19h
nginx 1/1 Running 0 105s
상세한 정보를 보려면 다음을 입력
> kubectl describe pod/nginx
Name: nginx
Namespace: default
Priority: 0
Node: minikube/192.168.94.2
Start Time: Fri, 22 Apr 2022 12:04:40 +0900
Labels: <none>
Annotations: <none>
Status: Running
IP: 172.17.0.6
IPs:
IP: 172.17.0.6
Containers:
nginx:
Container ID: docker://822951d3451159e697bd561ba37e43337e5d80ead8e624ff2282a64590d4c862
Image: nginx:1.14.2
Image ID: docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 22 Apr 2022 12:04:41 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6lh82 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-6lh82:
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 4m4s default-scheduler Successfully assigned default/nginx to minikube
Normal Pulled 4m4s kubelet Container image "nginx:1.14.2" already present on machine
Normal Created 4m4s kubelet Created container nginx
Normal Started 4m4s kubelet Started container nginx
3. 서비스
3-1. 서비스를 위한 ymal파일 작성
//simple-service.yaml
apiVersion: v1
kind: Service # 종류 : 서비스
metadata:
name: nginx
namespace: default
spec:
selector: # 어떤 파드와 연결해야하는지 지정, selector가 nginx를 찾아서 연결해줌
app: nginx # 배포하려는 파드를 지정합니다. 당연히 파드가 이미 실행중이어야 합니다.
type: LoadBalancer
ports:
- name: nginx
protocol: TCP
port: 80
targetPort: 80
3-2. 실행
> kubectl apply -f simple-service.yaml
service/nginx created
3-3. 확인
> kubectl get pod,service
NAME READY STATUS RESTARTS AGE
pod/hello-minikube-7bc9d7884c-vpkff 1/1 Running 1 (12h ago) 19h
pod/nginx 1/1 Running 0 17m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello-minikube NodePort 10.104.212.184 <none> 7080:32133/TCP 19h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19h
service/nginx LoadBalancer 10.98.206.229 <pending> 80:30137/TCP 47s
3-4. 터널 뚫기 (새 터미널에서 실행)
minikube tunnel
기존 터미널에서 kubectl get pod,service 를 보면, service/nginx에 EXTERNAL-IP가 뜬 것을 확인할 수 있다
> kubectl get pod,service
NAME READY STATUS RESTARTS AGE
pod/hello-minikube-7bc9d7884c-vpkff 1/1 Running 2 (10m ago) 19h
pod/nginx 1/1 Running 1 (10m ago) 31m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello-minikube NodePort 10.104.212.184 <none> 7080:32133/TCP 19h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19h
service/nginx LoadBalancer 10.98.206.229 10.98.206.229 80:30137/TCP 14m
4. 디플로이먼트
4-1. 기존의 pod와 service 삭제
> kubectl delete pod/nginx
pod "nginx" deleted
> kubectl delete service/nginx
service "nginx" deleted
> kubectl get all
NAME READY STATUS RESTARTS AGE
pod/hello-minikube-7bc9d7884c-vpkff 1/1 Running 2 (15m ago) 19h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello-minikube NodePort 10.104.212.184 <none> 7080:32133/TCP 19h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-minikube 1/1 1 1 19h
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-minikube-7bc9d7884c 1 1 1 19h
4-2. 디플로이먼트를 통해 파드를 생성
#nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment # 종류 : deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # pod 3개 생성
selector: # seletor로 nginx를 만든다
matchLabels: # matchLables를 이용해 pod를 찾는다
app: nginx
template:
# 여기서부터 파드 템플릿이다
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
# 여기까지 파드 템플릿이다
4-3. 실행
> kubectl apply -f nginx-deplyment.yaml
deployment.apps/nginx-deployment created
4-4. 확인
생성된것
- 디플로이먼트 1개 (nginx-deplyment)
- 파드 3개
- 레플리카셋 1개
> kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-9456bbbf9-dlmsc 0/1 ContainerCreating 0 3s
pod/nginx-deployment-9456bbbf9-n6whj 0/1 ContainerCreating 0 3s
pod/nginx-deployment-9456bbbf9-qwrnv 0/1 ContainerCreating 0 3s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 0/3 3 0 3s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-9456bbbf9 3 3 0 3s
4-5. 삭제
> kubectl delete deployment.apps/nginx-deployment
deployment.apps "nginx-deployment" deleted
5. 롤링 업데이트
5-1. 이전 nginx-deployment는 실행시킨채로 다음과 같이 변경
#nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment # 종류 : deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
strategy:
type: RollingUpdate #롤링업테이트 전략 사용
selector:
matchLabels:
app: nginx
template:
# 여기서부터 파드 템플릿이다
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21.6 #버전 변경
ports:
- containerPort: 80
# 여기까지 파드 템플릿이다
5-2. 중간과정
> kubectl apply -f nginx-deplyment.yaml
deployment.apps/nginx-deployment configured
> kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-9456bbbf9-8jqmh 1/1 Running 0 7m11s
pod/nginx-deployment-9456bbbf9-rgx7b 1/1 Running 0 7m11s
pod/nginx-deployment-9456bbbf9-w42h6 1/1 Running 0 7m11s
pod/nginx-deployment-cb69f686c-fw5mx 0/1 ContainerCreating 0 2s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3/3 1 3 7m11s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-9456bbbf9 3 3 3 7m11s
replicaset.apps/nginx-deployment-cb69f686c 1 1 0 2s
> kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-9456bbbf9-8jqmh 1/1 Terminating 0 7m23s
pod/nginx-deployment-9456bbbf9-rgx7b 1/1 Running 0 7m23s
pod/nginx-deployment-9456bbbf9-w42h6 1/1 Running 0 7m23s
pod/nginx-deployment-cb69f686c-csjjk 0/1 ContainerCreating 0 0s
pod/nginx-deployment-cb69f686c-fw5mx 1/1 Running 0 14s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3/3 2 3 7m23s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-9456bbbf9 2 2 2 7m23s
replicaset.apps/nginx-deployment-cb69f686c 2 2 1 14s
> kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-9456bbbf9-rgx7b 1/1 Running 0 7m24s
pod/nginx-deployment-9456bbbf9-w42h6 1/1 Terminating 0 7m24s
pod/nginx-deployment-cb69f686c-csjjk 1/1 Running 0 1s
pod/nginx-deployment-cb69f686c-fw5mx 1/1 Running 0 15s
pod/nginx-deployment-cb69f686c-xxwlz 0/1 ContainerCreating 0 0s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3/3 3 3 7m24s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-9456bbbf9 1 1 1 7m24s
replicaset.apps/nginx-deployment-cb69f686c 3 3 2 15s
> kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-9456bbbf9-rgx7b 1/1 Terminating 0 7m26s
pod/nginx-deployment-cb69f686c-csjjk 1/1 Running 0 3s
pod/nginx-deployment-cb69f686c-fw5mx 1/1 Running 0 17s
pod/nginx-deployment-cb69f686c-xxwlz 1/1 Running 0 2s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3/3 3 3 7m26s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-9456bbbf9 0 0 0 7m26s
replicaset.apps/nginx-deployment-cb69f686c 3 3 3 17s
- 디플로이먼트가 레플리카셋을 하나 더 만든다
- 레플리카셋에 의해서 새 버전의 컨테이너가 만들어지고 교체된다
- 구버전의 레플리카셋이 3->2->1->0 으로 줄어들고, 새버전의 레플리카셋이 1->2->3으로 생성되는 것을 볼 수 있다