kubenetes入门学习-六-Pod控制器应用进阶

原理听着好累,加油!

Pod控制器应用进阶
   资源格式清单格式:
      一级字段:apiVersion(group/version),kind(资源类别),metadata(name,namespace,labels,annotations,.....),spec,status(只读)
      
  kubectl explain pods.spec.containers  
  kubectl explain pods.spec.containers.ports
  Pod资源:
      spec.container <[]object>
      - name
        image         镜像来源
        imagePullPolicy
           Always, Never, IfNotPresent
        ports <[]Object>
        修改镜像中的默认应用:
        command, args 
        https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/    
        标签:
           key=value
              key:字母、数字 _ - .     
              value: 可以为空,只能字母或者数字开头或者结尾,中间可使用              

一个资源对象上可以有多个标签,一个标签也可以添加到多个资源对象上

k8s两种标签选择器:
        等值关系  = == !=
        集合关系  
           KEY in (VALUE1,VALUE2,....)
           KEY notin (VALUE1,VALUE2,....)
           KEY 
           !key

许多资源支持内嵌字段定义其使用的标签选择器
     matchLabels:直接给定键值
     matchExpression:基于给定的表达式来定义使用的标签选择器,{key: "KEY", operator: "OPERTOR", values: [VAL1,VAL2,...]}
        操作符:
           In,NotIn: values字段的值必须为非空列表;
           Exists,NotExists:values字段的值必须为空列表;
nodeSelector  
     节点标签选择器
      nodeSelector:
      disktype: ssd   指定标签选择主机
      
nodeName

annotations:
    与labels不同的地方在于,它不能用于挑选资源对象,仅用于为对象提供元数据。

Pod生命周期
     状态:Pending,Running,Failed,Succeeded,Unkuown,....
     
     创建Pod:
     初始化--|----主容器启动------------------|
             |poststart---------------pre stop|
                  存活性探测
                  就绪性探测
    Pod生命周期中的重要行为::
        初始化容器
        容器探测:
            liveness  存活性探测
            readiness 就绪性探测
    restartPolicy:
        Always, OnFailure,Never. Default to Always.
    探针类型有三种
        ExecAction、TCPSockketAction、HTTPGetAction   

    就绪性探测和service ip关系
      客户端访问直接访问service ip,service ip通过标签选择器选择后端pod执行调度。
      
    
===================================
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: nginx:1.14-alpine
    imagePullPolicy: IfNotPresent   
  - name: apache
    image: httpd:2.4
    ports:
    - name: http
      containerPort:80
    - name: https
      containerPort:443
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 3600"
  nodeSelector:
    disktype: ssd 
===============================
[root@master manifests]# kubectl explain pods.spec.containers
KIND:     Pod
VERSION:  v1

RESOURCE: containers <[]Object>

DESCRIPTION:
     List of containers belonging to the pod. Containers cannot currently be
     added or removed. There must be at least one container in a Pod. Cannot be
     updated.

     A single application container that you want to run within a pod.

FIELDS:
   args    <[]string>
     Arguments to the entrypoint. The docker image's CMD is used if this is not
     provided. Variable references $(VAR_NAME) are expanded using the
     container's environment. If a variable cannot be resolved, the reference in
     the input string will be unchanged. The $(VAR_NAME) syntax can be escaped
     with a double $$, ie: $$(VAR_NAME). Escaped references will never be
     expanded, regardless of whether the variable exists or not. Cannot be
     updated. More info:
     https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell

   command    <[]string>
     Entrypoint array. Not executed within a shell. The docker image's
     ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)
     are expanded using the container's environment. If a variable cannot be
     resolved, the reference in the input string will be unchanged. The
     $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).
     Escaped references will never be expanded, regardless of whether the
     variable exists or not. Cannot be updated. More info:
     https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell

   env    <[]Object>
     List of environment variables to set in the container. Cannot be updated.

   envFrom    <[]Object>
     List of sources to populate environment variables in the container. The
     keys defined within a source must be a C_IDENTIFIER. All invalid keys will
     be reported as an event when the container is starting. When a key exists
     in multiple sources, the value associated with the last source will take
     precedence. Values defined by an Env with a duplicate key will take
     precedence. Cannot be updated.

   image    
     Docker image name. More info:
     https://kubernetes.io/docs/concepts/containers/images This field is
     optional to allow higher level config management to default or override
     container images in workload controllers like Deployments and StatefulSets.

   imagePullPolicy    
     Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always
     if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
     More info:
     https://kubernetes.io/docs/concepts/containers/images#updating-images

   lifecycle    
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.

   livenessProbe    
     Periodic probe of container liveness. Container will be restarted if the
     probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   name     -required-
     Name of the container specified as a DNS_LABEL. Each container in a pod
     must have a unique name (DNS_LABEL). Cannot be updated.

   ports    <[]Object>
     List of ports to expose from the container. Exposing a port here gives the
     system additional information about the network connections a container
     uses, but is primarily informational. Not specifying a port here DOES NOT
     prevent that port from being exposed. Any port which is listening on the
     default "0.0.0.0" address inside a container will be accessible from the
     network. Cannot be updated.

   readinessProbe    
     Periodic probe of container service readiness. Container will be removed
     from service endpoints if the probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   resources    
     Compute Resources required by this container. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/

   securityContext    
     Security options the pod should run with. More info:
     https://kubernetes.io/docs/concepts/policy/security-context/ More info:
     https://kubernetes.io/docs/tasks/configure-pod-container/security-context/

   stdin    
     Whether this container should allocate a buffer for stdin in the container
     runtime. If this is not set, reads from stdin in the container will always
     result in EOF. Default is false.

   stdinOnce    
     Whether the container runtime should close the stdin channel after it has
     been opened by a single attach. When stdin is true the stdin stream will
     remain open across multiple attach sessions. If stdinOnce is set to true,
     stdin is opened on container start, is empty until the first client
     attaches to stdin, and then remains open and accepts data until the client
     disconnects, at which time stdin is closed and remains closed until the
     container is restarted. If this flag is false, a container processes that
     reads from stdin will never receive an EOF. Default is false

   terminationMessagePath    
     Optional: Path at which the file to which the container's termination
     message will be written is mounted into the container's filesystem. Message
     written is intended to be brief final status, such as an assertion failure
     message. Will be truncated by the node if greater than 4096 bytes. The
     total message length across all containers will be limited to 12kb.
     Defaults to /dev/termination-log. Cannot be updated.

   terminationMessagePolicy    
     Indicate how the termination message should be populated. File will use the
     contents of terminationMessagePath to populate the container status message
     on both success and failure. FallbackToLogsOnError will use the last chunk
     of container log output if the termination message file is empty and the
     container exited with an error. The log output is limited to 2048 bytes or
     80 lines, whichever is smaller. Defaults to File. Cannot be updated.

   tty    
     Whether this container should allocate a TTY for itself, also requires
     'stdin' to be true. Default is false.

   volumeDevices    <[]Object>
     volumeDevices is the list of block devices to be used by the container.
     This is a beta feature.

   volumeMounts    <[]Object>
     Pod volumes to mount into the container's filesystem. Cannot be updated.

   workingDir    
     Container's working directory. If not specified, the container runtime's
     default will be used, which might be configured in the container image.
     Cannot be updated.

[root@master manifests]# kubectl explain pods.spec.containers.ports
KIND:     Pod
VERSION:  v1

RESOURCE: ports <[]Object>

DESCRIPTION:
     List of ports to expose from the container. Exposing a port here gives the
     system additional information about the network connections a container
     uses, but is primarily informational. Not specifying a port here DOES NOT
     prevent that port from being exposed. Any port which is listening on the
     default "0.0.0.0" address inside a container will be accessible from the
     network. Cannot be updated.

     ContainerPort represents a network port in a single container.

FIELDS:
   containerPort     -required-
     Number of port to expose on the pod's IP address. This must be a valid port
     number, 0 < x < 65536.

   hostIP    
     What host IP to bind the external port to.

   hostPort    
     Number of port to expose on the host. If specified, this must be a valid
     port number, 0 < x < 65536. If HostNetwork is specified, this must match
     ContainerPort. Most containers do not need this.

   name    
     If specified, this must be an IANA_SVC_NAME and unique within the pod. Each
     named port in a pod must have a unique name. Name for the port that can be
     referred to by services.

   protocol    
     Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".
     
标签
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx-7849c4bbcd-dscjr          1/1     Running   0          2d20h
nginx-7849c4bbcd-vdd45          1/1     Running   0          2d20h
nginx-7849c4bbcd-wrvks          1/1     Running   0          2d20h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h
pod-demo                        2/2     Running   21         21h
[root@master manifests]# kubectl get pods --show-labels
NAME                            READY   STATUS    RESTARTS   AGE     LABELS
nginx-7849c4bbcd-dscjr          1/1     Running   0          2d20h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-vdd45          1/1     Running   0          2d20h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-wrvks          1/1     Running   0          2d20h   pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h   pod-template-hash=84cbfc56b6,run=nginx-deploy
pod-demo                        2/2     Running   21         21h     app=myapp,tier=frontend
[root@master manifests]# kubectl get pods -L app        -L 获取标签为app的标签值
NAME                            READY   STATUS    RESTARTS   AGE     APP
nginx-7849c4bbcd-dscjr          1/1     Running   0          2d20h   
nginx-7849c4bbcd-vdd45          1/1     Running   0          2d20h   
nginx-7849c4bbcd-wrvks          1/1     Running   0          2d20h   
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h   
pod-demo                        2/2     Running   21         21h     myapp
[root@master manifests]# kubectl get pods -l app   只显标签值为app的pod
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   21         21h
[root@master manifests]# kubectl get pods -L app,run
NAME                            READY   STATUS    RESTARTS   AGE     APP     RUN
nginx-7849c4bbcd-dscjr          1/1     Running   0          2d20h           nginx
nginx-7849c4bbcd-vdd45          1/1     Running   0          2d20h           nginx
nginx-7849c4bbcd-wrvks          1/1     Running   0          2d20h           nginx
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h           nginx-deploy
pod-demo                        2/2     Running   21         21h     myapp   
[root@master manifests]# kubectl label pods pod-demo release=canary   打标签
pod/pod-demo labeled
[root@master manifests]# kubectl get pods -l app --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   21         21h   app=myapp,release=canary,tier=frontend
[root@master manifests]# kubectl label pods pod-demo release=stable
error: 'release' already has a value (canary), and --overwrite is false
[root@master manifests]# kubectl label pods pod-demo release=stable --overwrite  覆盖
pod/pod-demo labeled
[root@master manifests]# kubectl get pods -l app --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   21         21h   app=myapp,release=stable,tier=[root@master manifests]# kubectl get pods -l release
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   21         21h
[root@master manifests]# kubectl get pods -l release,app
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   21         21h

k8s两种标签选择器:
        等值关系  = ==
        集合关系  
           KEY in (VALUE1,VALUE2,....)
           KEY notin (VALUE1,VALUE2,....)
           KEY 
           !key
[root@master manifests]# kubectl label pods nginx-deploy-84cbfc56b6-mjcw5 release=canary
pod/nginx-deploy-84cbfc56b6-mjcw5 labeled
[root@master manifests]# kubectl get pods -l release,app
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   22         22h
[root@master manifests]# kubectl get pods -l release=canary
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h
[root@master manifests]# kubectl get pods -l release=stable,app=myapp
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   22         22h
[root@master manifests]# kubectl get pods -l release!=canary
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7849c4bbcd-dscjr   1/1     Running   0          2d21h
nginx-7849c4bbcd-vdd45   1/1     Running   0          2d21h
nginx-7849c4bbcd-wrvks   1/1     Running   0          2d21h
pod-demo                 2/2     Running   22         22h

[root@master manifests]# kubectl get pods -l "release in (canary,bete,aplha)"
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h
[root@master manifests]# kubectl get pods -l "release notin (canary,bete,aplha)"
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7849c4bbcd-dscjr   1/1     Running   0          2d21h
nginx-7849c4bbcd-vdd45   1/1     Running   0          2d21h
nginx-7849c4bbcd-wrvks   1/1     Running   0          2d21h
pod-demo                 2/2     Running   22         22

[root@master manifests]# kubectl get nodes
NAME     STATUS   ROLES    AGE    VERSION
master   Ready    master   4d2h   v1.13.3
node01   Ready       4d2h   v1.13.3
node02   Ready       4d2h   v1.13.3
[root@master manifests]# kubectl get nodes --show-labels
NAME     STATUS   ROLES    AGE    VERSION   LABELS
master   Ready    master   4d2h   v1.13.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01   Ready       4d2h   v1.13.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node01
node02   Ready       4d2h   v1.13.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02
[root@master manifests]# kubectl label nodes node01 disktype=ssd
node/node01 labeled
[root@master manifests]# kubectl get nodes --show-labels
NAME     STATUS   ROLES    AGE    VERSION   LABELS
master   Ready    master   4d2h   v1.13.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01   Ready       4d2h   v1.13.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node01
node02   Ready       4d2h   v1.13.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02

指定带ssd标签的主机
[root@master manifests]# cat pod-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: nginx:1.14-alpine
  - name: apache
    image: httpd:2.4
    ports:
    - name: http
      containerPort: 80
    - name: https
      containerPort: 443
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 3600"
  nodeSelector:
    disktype: ssd
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx-7849c4bbcd-dscjr          1/1     Running   0          2d21h
nginx-7849c4bbcd-vdd45          1/1     Running   0          2d21h
nginx-7849c4bbcd-wrvks          1/1     Running   0          2d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h
pod-demo                        2/2     Running   0          9s
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx-7849c4bbcd-dscjr          1/1     Running   0          2d21h
nginx-7849c4bbcd-vdd45          1/1     Running   0          2d21h
nginx-7849c4bbcd-wrvks          1/1     Running   0          2d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h
pod-demo                        2/2     Running   0          11s
[root@master manifests]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
nginx-7849c4bbcd-dscjr          1/1     Running   0          2d21h   10.244.1.13   node01            
nginx-7849c4bbcd-vdd45          1/1     Running   0          2d21h   10.244.2.15   node02            
nginx-7849c4bbcd-wrvks          1/1     Running   0          2d21h   10.244.1.14   node01            
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          2d22h   10.244.2.9    node02            
pod-demo                        2/2     Running   0          16s     10.244.1.17   node01            
[root@master manifests]# kubectl describe pod pod-demo

探针测试
探针类型有三种
        ExecAction、TCPSockketAction、HTTPGetAction 
        
ExecAction实例
[root@master manifests]# cat liveness-exec.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: busybox:latest
    command: ["/bin/sh","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
liveness-exec-pod               0/1     CrashLoopBackOff   3          72s   这里已经重启3次了
nginx-7849c4bbcd-dscjr          1/1     Running            0          3d21h
nginx-7849c4bbcd-vdd45          1/1     Running            0          3d21h
nginx-7849c4bbcd-wrvks          1/1     Running            0          3d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running            0          3d22h
^C[root@master manifests]# kubectl describe liveness-exec-pod
error: the server doesn't have a resource type "liveness-exec-pod"
[root@master manifests]# kubectl describe pod liveness-exec-pod
Name:               liveness-exec-pod
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node01/10.249.6.101
Start Time:         Tue, 05 Mar 2019 08:48:00 -0500
Labels:            
Annotations:        
Status:             Running
IP:                 10.244.1.18
Containers:
  liveness-exec-container:
    Container ID:  docker://abe787745904f4c45324ea80b3d587dc11ed9e382ebdead4e9f31e8041dcc4b7
    Image:         busybox:latest
    Image ID:      docker-pullable://busybox@sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
    Port:          
    Host Port:    
    Command:
      /bin/sh
      touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 3600
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    2
      Started:      Tue, 05 Mar 2019 08:49:42 -0500
      Finished:     Tue, 05 Mar 2019 08:49:42 -0500
    Ready:          False
    Restart Count:  4      这个库
    Liveness:       exec [test -e /tmp/healthy] delay=1s timeout=1s period=3s #success=1 #failure=3  这里
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-6q28w (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-6q28w:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-6q28w
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  117s                default-scheduler  Successfully assigned default/liveness-exec-pod to node01
  Normal   Pulling    69s (x4 over 116s)  kubelet, node01    pulling image "busybox:latest"
  Normal   Pulled     68s (x4 over 115s)  kubelet, node01    Successfully pulled image "busybox:latest"
  Normal   Created    68s (x4 over 115s)  kubelet, node01    Created container
  Normal   Started    67s (x4 over 114s)  kubelet, node01    Started container
  Warning  BackOff    66s (x9 over 111s)  kubelet, node01    Back-off restarting failed container
[root@master manifests]# kubectl get pods -w
NAME                            READY   STATUS             RESTARTS   AGE
liveness-exec-pod               0/1     CrashLoopBackOff   4          2m46s  重启四次了
nginx-7849c4bbcd-dscjr          1/1     Running            0          3d21h
nginx-7849c4bbcd-vdd45          1/1     Running            0          3d21h
nginx-7849c4bbcd-wrvks          1/1     Running            0          3d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running            0          3d22h
[root@master manifests]# kubectl delete -f liveness-exec.yaml    删除掉他
pod "liveness-exec-pod" deleted

HTTPGetAction 实例     
[root@master manifests]# cat liveness-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-container
    image: nginx
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
[root@master manifests]# kubectl create -f liveness-httpget.yaml 
pod/liveness-httpget-pod created
[root@master manifests]# kubectl get pod
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   0          8s
nginx-7849c4bbcd-dscjr          1/1     Running   0          3d21h
nginx-7849c4bbcd-vdd45          1/1     Running   0          3d21h
nginx-7849c4bbcd-wrvks          1/1     Running   0          3d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          3d22h
[root@master manifests]# kubectl describe pod liveness-httpget-pod
Name:               liveness-httpget-pod
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node02/10.249.6.102
Start Time:         Tue, 05 Mar 2019 08:58:45 -0500
Labels:            
Annotations:        
Status:             Running
IP:                 10.244.2.17
Containers:
  liveness-httpget-container:
    Container ID:   docker://3ca6b2cd842dc03f89c600b18942271a69bb34b541411e4186ee8242c892e821
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:18c0755594af107923baa2e65fcef35aea4ab0cea7862d19c27aa127bacb458e
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 05 Mar 2019 08:58:51 -0500
    Ready:          True
    Restart Count:  0
    Liveness:       http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-6q28w (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-6q28w:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-6q28w
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  88s   default-scheduler  Successfully assigned default/liveness-httpget-pod to node02
  Normal  Pulling    87s   kubelet, node02    pulling image "nginx"
  Normal  Pulled     82s   kubelet, node02    Successfully pulled image "nginx"
  Normal  Created    82s   kubelet, node02    Created container
  Normal  Started    82s   kubelet, node02    Started container
手动进入容器
[root@master manifests]# kubectl exec -it liveness-httpget-pod -- /bin/sh
#
# ls
50x.html  index.html
# rm index.html
# ls 
50x.html
这里立马退出了容器命令行
[root@master manifests]# kubectl describe pod liveness-httpget-pod
Name:               liveness-httpget-pod
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node02/10.249.6.102
Start Time:         Tue, 05 Mar 2019 08:58:45 -0500
Labels:            
Annotations:        
Status:             Running
IP:                 10.244.2.17
Containers:
  liveness-httpget-container:
    Container ID:   docker://09feb70eeb2fe1870fb63145770321afc54470353bfeebba5151c0acadc915cf
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:18c0755594af107923baa2e65fcef35aea4ab0cea7862d19c27aa127bacb458e
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 05 Mar 2019 09:03:02 -0500
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 05 Mar 2019 08:58:51 -0500
      Finished:     Tue, 05 Mar 2019 09:03:00 -0500
    Ready:          True
    Restart Count:  1  这里重启了一次
    Liveness:       http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3 这里失败了3次
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-6q28w (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-6q28w:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-6q28w
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  5m2s                 default-scheduler  Successfully assigned default/liveness-httpget-pod to node02
  Normal   Pulling    47s (x2 over 5m1s)   kubelet, node02    pulling image "nginx"
  Warning  Unhealthy  47s (x3 over 53s)    kubelet, node02    Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    47s                  kubelet, node02    Killing container with id docker://liveness-httpget-container:Container failed liveness probe.. Container will be killed and recreated.
  Normal   Pulled     45s (x2 over 4m56s)  kubelet, node02    Successfully pulled image "nginx"
  Normal   Created    45s (x2 over 4m56s)  kubelet, node02    Created container
  Normal   Started    45s (x2 over 4m56s)  kubelet, node02    Started container
[root@master manifests]# kubectl get pod
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          6m31s  过一会这里重新部署上了
nginx-7849c4bbcd-dscjr          1/1     Running   0          3d21h
nginx-7849c4bbcd-vdd45          1/1     Running   0          3d21h
nginx-7849c4bbcd-wrvks          1/1     Running   0          3d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          3d22h

就绪性探测和service ip关系
   客户端访问直接访问service ip,service ip通过标签选择器选择后端pod执行调度。
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-container
    image: nginx
    ports:
    - name: http
      containerPort: 80
    readinessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
[root@master manifests]# kubectl create -f readiness-httpget.yaml 
pod/readiness-httpget-pod created
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          17m
nginx-7849c4bbcd-dscjr          1/1     Running   0          3d21h
nginx-7849c4bbcd-vdd45          1/1     Running   0          3d21h
nginx-7849c4bbcd-wrvks          1/1     Running   0          3d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          3d22h
readiness-httpget-pod           1/1     Running   0          7s
同样进去删掉
[root@master manifests]# kubectl exec -it readiness-httpget-pod -- /bin/sh
# cd html
# ls
50x.html  index.html
# rm index.html
[root@master ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          20m
nginx-7849c4bbcd-dscjr          1/1     Running   0          3d21h
nginx-7849c4bbcd-vdd45          1/1     Running   0          3d21h
nginx-7849c4bbcd-wrvks          1/1     Running   0          3d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          3d22h
readiness-httpget-pod           0/1     Running   0          2m55s   不就绪了 ,不健康了

创建一个文件
[root@master manifests]# kubectl exec -it readiness-httpget-pod -- /bin/sh
 # echo 1 >index.html
[root@master ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          22m
nginx-7849c4bbcd-dscjr          1/1     Running   0          3d21h
nginx-7849c4bbcd-vdd45          1/1     Running   0          3d21h
nginx-7849c4bbcd-wrvks          1/1     Running   0          3d21h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          3d22h
readiness-httpget-pod           1/1     Running   0          5m2s   恢复了

生命周期存活检测  
apiVersion: v1
kind: Pod
metadata:
  name: poststart-pod
  namespace: default
spec:
  containers:
  - name: busybox-httpd
    image: busybox:latest
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","mkdir -p /data/web/html; echo home_page >> /data/web/html/index.html"]
    command: ["/bin/httpd"]
    args: ["-f","-h /data/web/html"]

群名称:k8s学习群   群   号:153144292

你可能感兴趣的:(devops,kubenetes,kubernetes)