pod管理调度约束、与健康状态检查

pod的管理

[root@k8s-master ~]# vim pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-pod

  labels:

     app: nginx

spec:

  containers:

  - name: nginx

    image: nginx

创建pod

[root@k8s-master ~]# kubectl create -f pod.yaml

 

查看pod信息

[root@k8s-master ~]# kubectl get all

po/nginx-pod

                          1/1       Running   0          1m

查看pod的详细信息

[root@k8s-master ~]# kubectl describe po/nginx-pod

 

删除创建的pod

[root@k8s-master ~]# kubectl delete -f pod.yaml

 

POD的资源管理

[root@k8s-master ~]# vim pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-pod

  labels:

    app: nginx

spec:

  containers:

  - name: nginx

    image: nginx

    resources:

      requests:

        memory: "64Mi"

        cpu: "250m"

      limits:

        memory: "128Mi"

        cpu: "500m"

[root@k8s-master ~]# kubectl create -f pod.yaml

[root@k8s-master ~]# kubectl describe po/nginx-pod

Limits:

      cpu:     500m

      memory:  128Mi

 

pod调度约束与重启策略

[root@k8s-master ~]# vim pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-pod

  labels:

    app: nginx

spec:

  nodeName: 192.168.30.22

  nodeSelector:

    env_role: dev

  containers:

  - name: nginx

    image: nginx

[root@k8s-master ~]# kubectl create -f pod.yaml

我们指定的是pod22主机上创建,自然也会分配到这个主机上

[root@k8s-master ~]# kubectl get pod -o wide

nginx-pod                           1/1       Running   0          2m        172.17.11.3   192.168.30.22

查看主机名标签

[root@k8s-master ~]# kubectl describe node 192.168.30.22

Name:               192.168.30.22

Roles:              <none>

Labels:             beta.kubernetes.io/arch=amd64

                    beta.kubernetes.io/os=linux

                    kubernetes.io/hostname=192.168.30.22

添加标签

[root@k8s-master ~]# kubectl label nodes 192.168.30.22 env_role=dev

再次查看主机名标签已经把env_role=dev添加进去

[root@k8s-master ~]# kubectl describe node 192.168.30.22

Name:               192.168.30.22

Roles:              <none>

Labels:             beta.kubernetes.io/arch=amd64

                    beta.kubernetes.io/os=linux

                    env_role=dev

                    kubernetes.io/hostname=192.168.30.22

 

根据我们的约束再次创建pod已经又放在22主机上了

[root@k8s-master ~]# kubectl get pod -o wide

nginx-pod2                          1/1       Running   0          1m        172.17.11.5   192.168.30.22

 

pod健康状态检查

提供 Probe 机制,有以下两种类型:

l livenessProbe

如果检查失败,将杀死容器,根据PodrestartPolicy来操作。

l readinessProbe

如果检查失败,Kubernetes会把Podservice endpoints中剔除。

Probe 支持以下三种检查方法:

l httpGet

发送HTTP请求,返回200-400范围状态码为成功。

l exec

执行Shell命令返回状态码是0为成功。

l tcpSocket

发起TCP Socket建立成功。

 

[root@k8s-master ~]# vim pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-pod

  labels:

    app: nginx

spec:

  containers:

  - name: nginx

    image: nginx

    ports:

    - containerPort: 80

    livenessProbe:

      httpGet:

        path: /index.html

        port: 80

[root@k8s-master ~]# kubectl create -f pod.yaml

查看详细信息,可以看到这里容器访问80端口的信息

[root@k8s-master ~]# kubectl describe po/nginx-pod

 Restart Count:  0

    Liveness:       http-get http://:80/index.html delay=0s timeout=1s period=10s #success=1 #failure=3

Environment:    <none>

 

通过日志可以看到这里使用kube-probe来访问

[root@k8s-master ~]# kubectl logs nginx-pod -f

172.17.80.1 - - [08/Jul/2019:10:08:14 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.9" "-"

172.17.80.1 - - [08/Jul/2019:10:08:24 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.9" "-"

再开一个窗口,进入容器删除index.html看看日志的变化

[root@k8s-master ~]# kubectl exec -it nginx-pod bash

root@nginx-pod:/# cd /usr/share/nginx/html/

root@nginx-pod:/usr/share/nginx/html# ls

50x.html  index.html

root@nginx-pod:/usr/share/nginx/html# rm index.html

root@nginx-pod:/usr/share/nginx/html# exit

 

这边10秒后显示404找不到页面了

2019/07/08 10:17:14 [error] 6#6: *55 open() "/usr/share/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.80.1, server: localhost, request: "GET /index.html HTTP/1.1", host: "172.17.80.4:80"

172.17.80.1 - - [08/Jul/2019:10:17:14 +0000] "GET /index.html HTTP/1.1" 404 153 "-" "kube-probe/1.9" "-"

172.17.80.1 - - [08/Jul/2019:10:17:24 +0000] "GET /index.html HTTP/1.1" 404 153 "-" "kube-probe/1.9" "-"

 

查看pod的详细信息,已经输出404页面,但是它还会再创建一个容器并启动

[root@k8s-master ~]# kubectl describe pod nginx-pod

 Warning  Unhealthy              22s (x3 over 42s)  kubelet, 192.168.30.23  Liveness probe failed: HTTP probe failed with statuscode: 404

 

 Normal   Created                5m (x2 over 14m)  kubelet, 192.168.30.23  Created container

  Normal   Started                5m (x2 over 14m)  kubelet, 192.168.30.23  Started container

 

进入容器再看,容器又有了

root@nginx-pod:~# cd /usr/share/nginx/html

root@nginx-pod:/usr/share/nginx/html# ls

50x.html  index.html

posted @ 2019-07-08 18:33  #赵程#  阅读(286)  评论(0编辑  收藏  举报