2023.02.01
cat > nginx-rs.yaml
>
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-nginx
spec:
replicas: 3
selector:
matchLabels:
app: webui
template:
metadata:
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
kubectl get rs
kubectl get replicasets.apps
kubectl describe rs rs-nginx
kubectl describe rs rs-nginx
pod를 지워보면
kubectl delete pod [pod이름]
먼저 pod를 creation하고 나중에 지운다.
관계를 끊어주고
kubectl delete rs rs-nginx --cascade=false
다시 pod를 지워보면 pod가 살아나지 않는 것을 볼 수 있음.
cat > rs-lab.yaml
>
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-mainui
spec:
replicas: 2
selector:
matchLabels:
app: main
template:
metadata:
name: apache
labels:
app: main
rel: stable
spec:
containers:
- name: httpd-container
image: httpd:2.2
kubectl apply -f rs-lab.yaml
kubectl scale rs rs-mainui --replicas=1
cat > nginx-deployment.yaml
>
apiVersion: apps/v1
kind: Deployment
metadata:
name: rs-nginx
spec:
replicas: 3
selector:
matchLabels:
app: webui
template:
metadata:
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
kubectl apply -f nginx-deployment.yaml
kubectl scale deployment rs-nginx --replicas=2
cat > nginx-deployment-exam.yaml
>
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deploy
spec:
selector:
matchLabels:
app: webui
replicas: 3
template:
metadata:
labels:
app: webui
spec:
containers:
- image: nginx:1.14
name: web
ports:
- containerPort: 80
kubectl apply -f nginx-deployment-exam.yaml
kubectl set image deploy app-deploy web=nginx:1.15 --record
를 실행하면 1.15버전으로 새로 생성된 pod들이 등장한다.
(하나씩 순차적으로 생성하고 기존 pod를 하나씩 삭제함)
❗ rs는 그대로 있음!
- 상태 확인
kubectl rollout status deployment app-deploy
- 배포를 잠시 미룸
kubectl rollout pause deployment app-deploy
- 미뤄둔 배포를 진행
kubectl rollout resume deployment app-deploy
- 명령 확인
kubectl rollout history deployment app-deploy
- 방금 명령 취소
kubectl rollout undo deployment app-deploy
cat > nginx-deployment-exam2.yaml
>
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-nginx
annotations:
kubernetes.io/change-cause: version 1.14
spec:
progressDeadlineSeconds: 600
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
replicas: 3
selector:
matchLabels:
app: webui
template:
metadata:
labels:
app: webui
spec:
containers:
- name: web
image: nginx:1.14
ports:
- containerPort: 80
kubectl apply -f nginx-deployment-exam2.yaml
vi 에디터에서
kubernetes.io/change-cause: version 1.15
image: nginx:1.15
로 버전을 바꾸고
kubectl apply -f nginx-deployment-exam2.yaml
하면 버전이 바뀌는 것을 확인할 수 있다
undo도 가능하다.
kubectl rollout undo deployment deploy-nginx
(--to-revision=3)
도 붙여서 버전에 따른 undo가 가능
cat > deb-lab.yaml
>
apiVersion: apps/v1
kind: Deployment
metadata:
name: dep-mainui
annotations:
kubernetes.io/change-cause: version 2.2
spec:
replicas: 2
selector:
matchLabels:
app: main
template:
metadata:
labels:
app: main
rel: stable
spec:
containers:
- name: apache
image: httpd:2.2
ports:
- containerPort: 80
cat > nginx-daemonset.yaml
>
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-nginx
spec:
selector:
matchLabels:
app: webui
template:
metadata:
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
kubectl apply -f nginx-daemonset.yaml
kubectl delete pod [pod 이름]
하면 pod가 다시 생성됨
node 별로 pod가 하나씩 생성
kubectl edit daemonset daemonset-nginx
에서 container의 이미지 버전을 변경해주면 pod가 삭제되고 새로 만들어짐
kubectl rollout undo daemonset daemonset-nginx
을 하면 이미지 버전이 변경되기 전으로 돌아감
cat > nginx-stateful.yaml
>
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: sf-nginx
spec:
replicas: 3
serviceName: sf-service
# podManagementPolicy: OrderedReady
podManagementPolicy: Parallel
selector:
matchLabels:
app: webui
template:
metadata:
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
replicaset과 다른 특이점은 serviceName이 들어가는 부분이다.
kubectl apply -f nginx-stateful.yaml
kubectl get statefulsets.apps
kubectl delete pod sf-nginx-1
하면 해당 pod가 삭제됐다가 똑같이 다시 살아남
scale-out, scale-in 가능
kubectl scale statefulset sf-nginx --replicas=5
rollback도 가능
kubectl edit statefulsets.apps sf-nginx
kubectl rollout undo statefulset sf-nginx
cat > nginx-job.yaml
>
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5 # 총 몇개를 돌지
# parallelism: 2 # 동시에 몇개까지 돌지 (마지막엔 나머지만// 여기서는 1)
activeDeadlineSeconds: 5
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 25; echo 'Bye'"
restartPolicy: Never # 성공할 때까지 돌아라
# restartPolicy: OnFailure # 실패해도 종료해라
# backoffLimit: 3 # 3번까지 돌아라
kubectl apply -f nginx-job.yaml
한 번 실행되고 나서 Terminating 된다.
kubectl get jobs.batch centos-job
으로 보면 job의 COMPLETIONS 내용을 볼 수 있음
: 일정한 주기로 반복함
cat > cronjob-exam.yaml
>
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-exam
spec:
schedule: "* * * * *"
startingDeadlineSeconds: 500
# concurrencyPolicy: Allow
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- echo Hello; sleep 10; echo Bye
restartPolicy: Never
job
과 비교해 schedule
과 jobTemplate
이 추가 되었음.
kubectl apply -f cronjob-exam.yaml
kubectl get cronjobs.batch -o yaml
로 확인