kubernetesライフサイクルとpodコントローラ
18881 ワード
1,livenessProbeプローブの3つのプローブ方式:exec、httpGet、tcpSocket helpコマンド:
execプローブテスト:
ステータスの表示:
httpGetプローブテスト:
ステータスの表示:
コンテナに入る:
2,readinessProbeプローブ
ステータスの表示:
コンテナ削除に入る
ステータスの表示:
3,lifecycle postStartテスト作成:
コンテナの表示:
4,podコントローラRelicationController,Replica Set,Deployment,DaemonSet,Job,Cronjob,StatefulSet
ReplicaSetコントローラ:helpコマンド:
ファイルの作成
ステータスの表示:
Labelの繰り返しテストを行います.
重複するlabelが発生すると、podコントローラはすぐに別のlabelを減らし、labelがユーザーのニーズに厳格に合致することを保証します.
label数を変更するには:
注意:rsはコントローラ名、myappはpod名です.editでバージョンを変更すると、バージョンはすぐに変更されません.podを削除する必要があります.rsがpodを自動的に作成したときのバージョンは、ターゲットを変更するバージョンです.
Deploymentコントローラ:helpコマンド:
ファイルの作成:
表示:
監視を開始:
ファイルの更新、rsの表示
patchで数量を変更します.
patchによる更新ポリシーの変更:
テスト:
監視:
更新を続行:
監視:
DaemonSet制御:helpコマンド:
ファイルの作成:
表示:
接続svc:
コンテナに入る:
スクロール更新:
監視:
kubectl explain pods.spec.containers.livenessProbe
execプローブテスト:
[root@master manifests]# cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-containers
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"]
initialDelaySeconds: 1
periodSeconds: 3
ステータスの表示:
[root@master manifests]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 17h
liveness-exec-pod 0/1 ContainerCreating 0 12s
myapp-746644f8d6-2msxj 1/1 Running 1 17h
myapp-746644f8d6-ch5n2 1/1 Running 1 17h
nginx-deploy-7689897d8d-rvp28 1/1 Running 1 18h
pod-demo 2/2 Running 2 15h
liveness-exec-pod 0/1 ErrImagePull 0 34s
liveness-exec-pod 0/1 ImagePullBackOff 0 46s
liveness-exec-pod 0/1 ErrImagePull 0 90s
liveness-exec-pod 0/1 ImagePullBackOff 0 105s
^C[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 17h
liveness-exec-pod 0/1 ImagePullBackOff 0 2m10s
myapp-746644f8d6-2msxj 1/1 Running 1 17h
myapp-746644f8d6-ch5n2 1/1 Running 1 17h
nginx-deploy-7689897d8d-rvp28 1/1 Running 1 18h
pod-demo 2/2 Running 2 15h
httpGetプローブテスト:
[root@master manifests]# cat liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-exec-containers
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
[root@master manifests]#
ステータスの表示:
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 18h
liveness-exec-pod 0/1 ImagePullBackOff 0 15m
liveness-httpget-pod 1/1 Running 0 3m
myapp-746644f8d6-2msxj 1/1 Running 1 17h
myapp-746644f8d6-ch5n2 1/1 Running 1 17h
nginx-deploy-7689897d8d-rvp28 1/1 Running 1 18h
pod-demo 2/2 Running 2 15h
コンテナに入る:
[root@master manifests]# kubectl exec -it liveness-httpget-pod -- /bin/sh
2,readinessProbeプローブ
[root@master manifests]# cat readiness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-containers
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
ステータスの表示:
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 18h
liveness-httpget-pod 1/1 Running 1 30m
myapp-746644f8d6-2msxj 1/1 Running 1 18h
myapp-746644f8d6-ch5n2 1/1 Running 1 18h
nginx-deploy-7689897d8d-rvp28 1/1 Running 1 18h
pod-demo 2/2 Running 2 16h
readiness-httpget-pod 1/1 Running 0 74s
コンテナ削除に入る
[root@master manifests]# kubectl exec -it readiness-httpget-pod -- /bin/sh
/ # rm /usr/share/nginx/html/index.html
ステータスの表示:
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 18h
liveness-httpget-pod 1/1 Running 1 30m
myapp-746644f8d6-2msxj 1/1 Running 1 18h
myapp-746644f8d6-ch5n2 1/1 Running 1 18h
nginx-deploy-7689897d8d-rvp28 1/1 Running 1 18h
pod-demo 2/2 Running 2 16h
readiness-httpget-pod 0/1 Running 0 78s
3,lifecycle postStartテスト作成:
[root@master manifests]# cat postatrt-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: poststart-pod
namespace: default
spec:
containers:
- name: busybox-httpd
image: busybox:latest
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ['mkdir','-p','/data/web/html']
command: ['/bin/sh','-c','sleep 3600']
#args: ["-f","-h /data/web/html"]
[root@master manifests]#
コンテナの表示:
[root@master manifests]# kubectl exec -it poststart-pod -- /bin/sh
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # ls /data/
web
/ # ls /data/web/
html
4,podコントローラRelicationController,Replica Set,Deployment,DaemonSet,Job,Cronjob,StatefulSet
ReplicaSetコントローラ:helpコマンド:
[root@master manifests]# kubectl explain rs
ファイルの作成
[root@master manifests]# cat rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp
namespace:
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
name: myapp-pod
labels:
app: myapp
release: canary
environment: qa
spec:
containers:
- name: myapp-container
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
ステータスの表示:
[root@master manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Completed 0 19h run=client
liveness-httpget-pod 1/1 Running 1 104m
myapp-r6n2h 1/1 Running 0 74s app=myapp,environment=qa,release=canary
myapp-xpxds 1/1 Running 0 74s app=myapp,environment=qa,release=canary
pod-demo 2/2 Running 4 17h app=myapp,tier=frontend
poststart-pod 1/1 Running 0 50m
readiness-httpget-pod 1/1 Running 0 75m
Labelの繰り返しテストを行います.
[root@master manifests]# kubectl label pods pod-demo release=canary
pod/pod-demo labeled
[root@master manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Completed 0 19h run=client
liveness-httpget-pod 1/1 Running 1 107m
myapp-r6n2h 1/1 Running 0 4m44s app=myapp,environment=qa,release=canary
pod-demo 2/2 Running 4 17h app=myapp,release=canary,tier=frontend
poststart-pod 1/1 Running 0 53m
readiness-httpget-pod 1/1 Running 0 78m
重複するlabelが発生すると、podコントローラはすぐに別のlabelを減らし、labelがユーザーのニーズに厳格に合致することを保証します.
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 19h
liveness-httpget-pod 1/1 Running 1 109m
myapp-r6n2h 1/1 Running 0 6m8s
myapp-sxj8c 1/1 Running 0 46s
poststart-pod 1/1 Running 0 54m
readiness-httpget-pod 1/1 Running 0 80m
[root@master manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Completed 0 19h run=client
liveness-httpget-pod 1/1 Running 1 109m
myapp-r6n2h 1/1 Running 0 6m17s app=myapp,environment=qa,release=canary
myapp-sxj8c 1/1 Running 0 55s app=myapp,environment=qa,release=canary
poststart-pod 1/1 Running 0 55m
readiness-httpget-pod 1/1 Running 0 80m
[root@master manifests]#
label数を変更するには:
[root@master manifests]# kubectl edit rs myapp
注意:rsはコントローラ名、myappはpod名です.editでバージョンを変更すると、バージョンはすぐに変更されません.podを削除する必要があります.rsがpodを自動的に作成したときのバージョンは、ターゲットを変更するバージョンです.
Deploymentコントローラ:helpコマンド:
[root@master ~]# kubectl explain deploy
ファイルの作成:
[root@master manifests]# cat deploy-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
labels:
app: myapp
release: canary
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
表示:
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 23h
liveness-httpget-pod 1/1 Running 1 5h32m
myapp-deploy-f4db5d79c-bvfth 1/1 Running 0 3m53s
myapp-deploy-f4db5d79c-fsbzq 1/1 Running 0 3m53s
myapp-deploy-f4db5d79c-qrc97 1/1 Running 0 2m
poststart-pod 1/1 Running 4 4h38m
readiness-httpget-pod 1/1 Running 0 5h3m
監視を開始:
[root@master manifests]# kubectl get pods -l app=myapp -w
NAME READY STATUS RESTARTS AGE
myapp-deploy-f4db5d79c-bvfth 1/1 Running 0 4m38s
myapp-deploy-f4db5d79c-fsbzq 1/1 Running 0 4m38s
myapp-deploy-f4db5d79c-qrc97 1/1 Running 0 2m45s
ファイルの更新、rsの表示
[root@master manifests]# kubectl get rs
NAME DESIRED CURRENT READY AGE
myapp-deploy-55b78d8548 3 3 3 3m12s
myapp-deploy-f4db5d79c 0 0 0 9m55s
patchで数量を変更します.
[root@master manifests]# kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
deployment.extensions/myapp-deploy patched
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 23h
liveness-httpget-pod 1/1 Running 1 5h43m
myapp-deploy-55b78d8548-6pp77 1/1 Running 0 7m39s
myapp-deploy-55b78d8548-d9rx6 1/1 Running 0 7s
myapp-deploy-55b78d8548-dh5bn 1/1 Running 0 7m33s
myapp-deploy-55b78d8548-nrk4w 1/1 Running 0 7s
myapp-deploy-55b78d8548-wk2jr 1/1 Running 0 7m36s
poststart-pod 1/1 Running 4 4h49m
readiness-httpget-pod 1/1 Running 0 5h14m
patchによる更新ポリシーの変更:
[root@master manifests]# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
deployment.extensions/myapp-deploy patched
[root@master manifests]# kubectl describe deploy myapp-deploy
Name: myapp-deploy
Namespace: default
CreationTimestamp: Tue, 24 Sep 2019 14:32:58 +0800
Labels: app=myapp
release=canary
Annotations: deployment.kubernetes.io/revision: 2
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"myapp-deploy","namespace":"default"},"spec":{"replicas":3...
Selector: app=myapp,release=canary
Replicas: 5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 0 max unavailable, 1 max surge
テスト:
[root@master manifests]# kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3 && kubectl rollout pause deployment myapp-deploy
監視:
[root@master manifests]# kubectl get pod -l app=myapp -w
更新を続行:
[root@master manifests]# kubectl rollout resume deployment myapp-deployment
監視:
[root@master manifests]# kubectl rollout status deployment myapp-deploy
DaemonSet制御:helpコマンド:
[root@master manifests]# kubectl explain ds
ファイルの作成:
[root@master manifests]# cat ds-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: redis
role: logstor
template:
metadata:
labels:
app: redis
role: logstor
spec:
containers:
- name: redis
image: redis:4.0-alpine
ports:
- name: redis
containerPort: 6379
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat-ds
namespace: default
spec:
selector:
matchLabels:
app: filebeat
release: stable
template:
metadata:
labels:
app: filebeat
release: stable
spec:
containers:
- name: filebeat
image: ikubernetes/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local
- name: REDIS_LOG_LEVEL
value: info
[root@master manifests]#
表示:
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 24h
filebeat-ds-fvhdd 1/1 Running 0 43s
filebeat-ds-mmspz 1/1 Running 0 43s
filebeat-ds-snjnh 1/1 Running 0 43s
liveness-httpget-pod 1/1 Running 1 6h27m
myapp-ds-frsst 1/1 Running 0 11m
myapp-ds-pm2pr 1/1 Running 0 11m
myapp-ds-rrzbv 1/1 Running 0 11m
poststart-pod 1/1 Running 5 5h33m
readiness-httpget-pod 1/1 Running 0 5h59m
redis-5c998b644f-5l7cw 1/1 Running 0 43s
接続svc:
[root@master manifests]# kubectl expose deployment redis --port=6379
[root@master manifests]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 6d4h
myapp NodePort 10.106.65.163 80:30167/TCP 24h
nginx ClusterIP 10.101.119.241 80/TCP 24h
redis ClusterIP 10.103.32.112 6379/TCP 3s
コンテナに入る:
redis-5c998b644f-5l7cw 1/1 Running 0 3m32s
[root@master manifests]# kubectl exec -it redis-5c998b644f-5l7cw -- /bin/sh
/data # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp 0 0 :::6379 :::* LISTEN
/data # ls
/data # nslooup redis.default.svc.cluster.local
/bin/sh: nslooup: not found
/data # nslookup redis.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve
Name: redis.default.svc.cluster.local
Address 1: 10.103.32.112 redis.default.svc.cluster.local
/data # redis-cli -h redis.default.svc.cluster.local
redis.default.svc.cluster.local:6379> keys *
(empty list or set)
redis.default.svc.cluster.local:6379> exit
/data #
スクロール更新:
[root@master manifests]# kubectl set image daemonset filebeat-ds filebeat=ikubernetes/filebeat:5.6.6-alpine
daemonset.extensions/filebeat-ds image updated
[root@master manifests]#
監視:
[root@master manifests]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 25h
filebeat-ds-fvhdd 1/1 Running 0 71m
filebeat-ds-mmspz 1/1 Running 0 71m
filebeat-ds-nh4t2 0/1 ContainerCreating 0 1s
liveness-httpget-pod 1/1 Running 1 7h38m
myapp-ds-frsst 1/1 Running 0 81m
myapp-ds-pm2pr 1/1 Running 0 81m
myapp-ds-rrzbv 1/1 Running 0 81m
poststart-pod 1/1 Running 6 6h44m
readiness-httpget-pod 1/1 Running 0 7h9m
redis-5c998b644f-5l7cw 1/1 Running 0 71m