kubernetes生命周期和pod控制器

1,livenessProbe探针
三种探测方式:exec、httpGet、tcpSocket
help命令:

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、ReplicaSet、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   <none>
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    <none>
readiness-httpget-pod   1/1     Running     0          75m    <none>

进行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    <none>
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     <none>
readiness-httpget-pod   1/1     Running     0          78m     <none>

当出现重复的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    <none>
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     <none>
readiness-httpget-pod   1/1     Running     0          80m     <none>
[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        <none>        443/TCP        6d4h
myapp        NodePort    10.106.65.163    <none>        80:30167/TCP   24h
nginx        ClusterIP   10.101.119.241   <none>        80/TCP         24h
redis        ClusterIP   10.103.32.112    <none>        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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值