kubernetes搭建 十一、Pod管理、资源限制、健康检查

一、pod创建管理

1、pod配置文件示例

[root@k8s-master-101 yaml_test]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx

spec:
  nodeName: 10.0.0.102
  #nodeSelector:
  #  env_role: dev
  containers:
  - name: nginx
    image: nginx
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

Pod.spec.containers.resources 资源限制
Pod.spec.nodeName 强制约束Pod调度到指定Node节点上
Pod.spec.nodeSelector 通过lable-selector机制选择节点

2、创建pod并且查看,可以看到pod运行在指定的node节点上

[root@k8s-master-101 yaml_test]# kubectl create -f pod.yaml
pod/nginx-pod created

[root@k8s-master-101 yaml_test]# kubectl get pods nginx-pod -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE
nginx-pod   1/1     Running   0          42s   172.17.50.3   10.0.0.102   

3、重启策略
kubernetes搭建 十一、Pod管理、资源限制、健康检查_第1张图片

二、健康检查

kubernetes搭建 十一、Pod管理、资源限制、健康检查_第2张图片
1、设置livenessProbe的健康检查类型,检查失败将杀死容器重新启动一个新的容器。
vim pod2.yaml

[root@k8s-master-101 yaml_test]# cat pod2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod2
  labels:
    app: nginx

spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80 
    livenessProbe:
      httpGet:
        path: /index.html
        port: 80

kubernetes搭建 十一、Pod管理、资源限制、健康检查_第3张图片
2、创建一个名为nginx-pod2的pod

[root@k8s-master-101 yaml_test]# kubectl create -f pod2.yaml
pod/nginx-pod2 created

#可以看到有一行健康检查
[root@k8s-master-101 yaml_test]# kubectl describe pod nginx-pod2
Liveness:       http-get http://:80/index.html delay=0s timeout=1s period=10s #success=1 #failure=3

#查看pod日志可以看到每10s请求一次
[root@k8s-master-101 yaml_test]# kubectl logs nginx-pod2
172.17.50.1 - - [27/Mar/2019:09:07:29 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.12" "-"
172.17.50.1 - - [27/Mar/2019:09:07:39 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.12" "-"
172.17.50.1 - - [27/Mar/2019:09:07:49 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.12" "-"
172.17.50.1 - - [27/Mar/2019:09:07:59 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.12" "-"

3、再开一个窗口实时查看日志

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

4、进入容器将页面删除

[root@k8s-master-101 yaml_test]# kubectl exec -it nginx-pod2 bash
root@nginx-pod2:/# cd /usr/share/nginx/html/
root@nginx-pod2:/usr/share/nginx/html# rm index.html

5、可以看到日志里现在访问不到页面了

2019/03/27 09:21:39 [error] 6#6: *86 open() "/usr/share/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.50.1, server: localhost, request: "GET /index.html HTTP/1.1", host: "172.17.50.3:80"
2019/03/27 09:21:49 [error] 6#6: *87 open() "/usr/share/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.50.1, server: localhost, request: "GET /index.html HTTP/1.1", host: "172.17.50.3:80"
172.17.50.1 - - [27/Mar/2019:09:21:49 +0000] "GET /index.html HTTP/1.1" 404 154 "-" "kube-probe/1.12" "-"
172.17.50.1 - - [27/Mar/2019:09:21:59 +0000] "GET /index.html HTTP/1.1" 404 154 "-" "kube-probe/1.12" "-"
2019/03/27 09:21:59 [error] 6#6: *88 open() "/usr/share/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.50.1, server: localhost, request: "GET /index.html HTTP/1.1", host: "172.17.50.3:80"

6、查看pod日志,可以看到把容器杀死重启了

[root@k8s-master-101 yaml_test]# kubectl describe pod nginx-pod2
Events:
  Type     Reason     Age                From                 Message
  ----     ------     ----               ----                 -------
  Normal   Scheduled  15m                default-scheduler    Successfully assigned default/nginx-pod2 to 10.0.0.102
  Warning  Unhealthy  57s (x3 over 77s)  kubelet, 10.0.0.102  Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Pulling    55s (x2 over 15m)  kubelet, 10.0.0.102  pulling image "nginx"
  Normal   Killing    55s                kubelet, 10.0.0.102  Killing container with id docker://nginx:Container failed liveness probe.. Container will be killed and recreated.
  Normal   Pulled     50s (x2 over 15m)  kubelet, 10.0.0.102  Successfully pulled image "nginx"
  Normal   Created    50s (x2 over 15m)  kubelet, 10.0.0.102  Created container
  Normal   Started    49s (x2 over 15m)  kubelet, 10.0.0.102  Started container

你可能感兴趣的:(kubernetes)