pod进阶:

pod进阶:

pod的生命周期当中的状态:

1、Running 运行中,pod已经分配到节点上,且pod内的容器正常运行。正常状态(ready 1/1)

2、complete 完成之后退出,容器内的返回码是0 echo $? 表示容器是正常的运行结束

3、pending 挂起状态,pod已经创建好了,但是没有被分配到节点上。

4、Failed 失败:容器内的返回码是非0状态退出,进入失败状态

logs-f 可以查看pod的日志 describe pod 查看pod的详细情况,也可以查询到错误原因

5、Terminating 终止中 pod正在删除中。

6、Unknown 未知 集群出现问题,API出现了问题,或者是API server和调度器之间通信有问题(证书过期)

7、imagePullBackOff 拉取镜像失败了

8、CrashLoopBackOff 容器已经启动了,但是异常退出了,可以看日志,或者查看详细情况

9、error pod启动过程中报错。有日志可以查询

10、PodInitializing 初始化中(pod内部有初始化init容器)

11、Evicte pod被驱逐

面试题:出现pending状态如何解决?

1)节点上的资源不足 nginx————node1 (换一个节点,)

2)污点,节点上设置了污点标签,导致节点不可部署

3)pv,节点上没有合适的pv挂载点(手动的情况),磁盘空间不足,创建pv失败(自动、手动)

4)网络原因,防火墙 节点不可用。

5)swap的没有关闭,k8s禁止使用交换分区。

6)HostPort已经被占用。NodePort节点上的端口被占用,也会pending。

资源限制

docker cpu 100000

内存 m/g

k8s cpu 最小单位(100m 0.1(占一个cpu的10%))

(1000m 1(占一个cpu的100%))

(500m 0.2 (占cpu的50%))

内存 单位 Ki Mi Gi Ti

[root@master01 k8s]# vim test1.yml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx1
  labels:
    app: nginx1
spec:
  containers:
  - name: nginx
    image: nginx:1.22
    resources:
      requests:
        cpu: "0.5"
        memory: "512Mi"
#软限制,最低的要求
      limits:
        cpu: "1"
        memory: "1Gi"
#硬策略,最高的要求

探针 probe

探针是对容器执行定期的检查

探针:
启动探针

启动探针,在容器启动时,根据条件判断容器是否启动成功。如果启动探针和其他探针并列。只有启动探针执行完毕(成功)

后续的探针才会执行,启动探针失败,整个容器判定为失败,pod也会进入失败状态。

在整个容器的生命周期当中,只有启动探针在启动时执行,执行成功之后,后续不再执行。

startupProbe

存活探针

存活探针:livenessPorbe 探测容器是否正常运行,如果探测失败,会根据pod的重启策略来决定是否重启。

将伴随整个pod的生命周期。

就绪探针

就绪探针:readinessProbe 探测pod的状态是否进入ready,如果进入ready状态失败,service将会把这个pod的ip从转发中移除。service不会把请求转发到这个pod。

nginx1 ---3------node1 node2 node3

nginx-pod1 10.244.0.10

nginx-pod2 10.244.0.11----------service-------NodePort 192.168.60.110:30000

nginx-pod3 10.244.0.12-----ready 0/1

就绪探针没有检测成功或者失败,pod可能是running,但是ready一定是0/1。

存活探针一般用于任期内的配置文件或者是关键组件是否正常。

就绪探针一般用于指定端口的访问,需要对外提供访问的业务。

这两个探针都会伴随这个pod的生命周期。

probe的检查方法:

1、exec方法:就是进入容器内,执行命令,命令的返回码是0就是成功,非0都是失败。

在容器内使用自定义命令来检测容器内的健康状况。判断关键配置文件是否存在,依赖环境是否完整等等。

2、tcpSocket方法:就是对容器内的IP地址进行tcp检查(三次握手),和指定的端口进行连接,如果三次握手和端口通信建立连接正常,则认为成功。主要是用来判断容器的端口是否正常启动,端口是否处于监听状态。

3、httpGet方法:对容器内的ip+端口进行http请求,请求的方法是get。响应码要是大于等于200且小于400都是成功。

200<=x<400 主要应用于web容器。

结果:

1、成功

2、失败 定义了容器的重启策略,容器会进行重启。

3、未知 也属于探针失败,但是不会采取任何行动

[root@master01 k8s-yaml]# vim test1.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos7
  labels:
    app: centos7
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos7
  template:
    metadata:
      labels:
        app: centos7
    spec:
#定义pod的容器参数:
      containers:
      - name: centos7
        image: centos:7
        command: ["/bin/bash","-c","touch /opt/123.txt && sleep 3600"]
        livenessProbe:
          exec:
            command: ["/usr/bin/test","-e","/opt/123.txt"]
          initialDelaySeconds: 1
#initialDelaySeconds 表示容器启动之后多少秒开始第一次探测,1是秒,要等应用程序准备好之后再探测。
#以避免结果有误。
          periodSeconds: 3
#periodSeconds 表示在pod的生命周期内,探针的检测间隔时间是3秒。也没有固定的范围,根据业务容器的情况来看,比较敏感的检测时间,可以缩短间隔
          failureThreshold: 2
#failureThreshold 表示探针检测容器失败几次就把容器标记为不健康。
          timeoutSeconds: 1
#timeoutSeconds的时间必须小于periodSeconds,表示探针在多少时间之内完成探测。
          successThreshold: 1
#successThreshold 只要探针成功一次,就把容器标记为健康,这个值只能是1,默认也是1,可以不写。
​
apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos7
  labels:
    app: centos7
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos7
  template:
    metadata:
      labels:
        app: centos7
    spec:
#定义pod的容器参数:
      containers:
      - name: centos7
        image: centos:7
        command: ["/bin/bash","-c","touch /opt/123.txt && sleep 3600"]
        livenessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 1
#initialDelaySeconds 表示容器启动之后多少秒开始第一次探测,1是秒,要等应用程序准备好之后再探测。
#以避免结果有误。
          periodSeconds: 3
#periodSeconds 表示在pod的生命周期内,探针的检测间隔时间是3秒。也没有固定的范围,根据业务容器的情况来看,比较敏感的检测时间,可以缩短间隔
          failureThreshold: 2
#failureThreshold 表示探针检测容器失败几次就把容器标记为不健康。
          timeoutSeconds: 1
#timeoutSeconds的时间必须小于periodSeconds,表示探针在多少时间之内完成探测。
          successThreshold: 1
#successThreshold 只要探针成功一次,就把容器标记为健康,这个值只能是1,默认也是1,可以不写。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos7
  labels:
    app: centos7
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos7
  template:
    metadata:
      labels:
        app: centos7
    spec:
#定义pod的容器参数:
      containers:
      - name: centos7
        image: centos:7
        command: ["/bin/bash","-c","touch /opt/123.txt && sleep 3600"]
        livenessProbe:
          httpGet:
            port: 80
            scheme: HTTP
            path: /index.html
#scheme:调用的协议(https)
#path: /index.html curl 192.168.60.110
          initialDelaySeconds: 1
#initialDelaySeconds 表示容器启动之后多少秒开始第一次探测,1是秒,要等应用程序准备好之后再探测。
#以避免结果有误。
          periodSeconds: 3
#periodSeconds 表示在pod的生命周期内,探针的检测间隔时间是3秒。也没有固定的范围,根据业务容器的情况来看,比较敏感的检测时间,可以缩短间隔
          failureThreshold: 2
#failureThreshold 表示探针检测容器失败几次就把容器标记为不健康。
          timeoutSeconds: 1
#timeoutSeconds的时间必须小于periodSeconds,表示探针在多少时间之内完成探测。
          successThreshold: 1
#successThreshold 只要探针成功一次,就把容器标记为健康,这个值只能是1,默认也是1,可以不写。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos7
  labels:
    app: centos7
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos7
  template:
    metadata:
      labels:
        app: centos7
    spec:
#定义pod的容器参数:
      containers:
      - name: centos7
        image: centos:7
        command: ["/bin/bash","-c","touch /opt/123.txt && sleep 3600"]
        livenessProbe:
          httpGet:
            port: 80
            scheme: HTTP
            path: /index.html
        readinessProbe:
            exec:
              command: ["/usr/bin/test","-e","/etc/passwd"]
---
#表示分段,上一个yml结束,下跟一个新的yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-1
  labels: 
    app: nginx1
spec:
  selector:
    app: centos7
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30000

lifecycle字段:

容器钩子:

postStart: 表示容器启动时立即执行的命令,执行容器内需要执行的初始化命令,等待依赖环境

preStop: 停止之前执行的任务。清理任务,同步文件等等(导出容器内的数据)。

1、启动任务失败,pod能否进入正常状态

不能

2、停止任务失败,pod能否退出

报错

3、如果和探针一起,启动失败的任务影响探针吗?

影响,而且钩子的优先级大于启动探针

4、容器的钩子,不论是启动还是停止之前的命令,只能使用exec

总结: 容器的启动钩子------->启动探针------>存活和就绪探针。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos7
  labels:
    app: centos7
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos7
  template:
    metadata:
      labels:
        app: centos7
    spec:
#定义pod的容器参数:
      containers:
      - name: centos7
        image: centos:7
        command: ["/bin/bash","-c","touch /opt/123.txt && sleep 3600"]
        livenessProbe:
          httpGet:
            port: 80
            scheme: HTTP
            path: /index.html
        readinessProbe:
            exec:
              command: ["/usr/bin/test","-e","/etc/passwd"]
        lifecycle:
          postStart:
            exec:
              command: ["/bin/bash","-c","cat > /opt/123.txt"]
apiVersion: v1
kind: Pod
metadata:
  name: centos1
  labels:
    app: centos1
spec:
  containers:
  - name: centos
    image: centos:7
    command: ["/bin/bash","-c","sleep 30"]
    volumeMounts:
    - name: data-v
      mountPath: /opt/test1
#容器内的目录,挂载卷的名称
    lifecycle:
      postStart:
        exec:
          command: ["/bin/bash","-c","echo start >> /opt/test1/123.txt && sleep 10"]
      preStop:
        exec:
          command: ["/bin/bash","-c","echo stop >> /opt/test1/789.txt && sleep 10"]
  volumes:
  - name: data-v
    hostPath:
      path: /opt/test
      type: DirectoryOrCreate
      #节点的目录  /opt/test————>/opt/test1

探针的三种:

启动 启动执行完毕之后,后续不再执行

存活

就绪 存活和就绪会伴随整个pod的生命周期

三种方法:

exec

tcpSocket

httpGet

作业:写一个yaml文件,包含启动钩子和退出钩子

和节点挂载:/usr/share/nginx/html/ 节点的/opt/node

exec 执行,要能在目录中看到开始和打印的结果

包含探针:

1、启动探针

方法:exec 检测 /usr/share/nginx/html/index.html文件是否存在

2、存活探针

方法:httpGet

访问验证返回码是否正确

3、就绪探针

方法:tcpSocket

监听容器的80端口是否正常

[root@master01 k8s-yaml]# vim test1.yaml
​
apiVersion: v1
kind: Pod
metadata:
  name: nginx1
  labels:
    app: nginx1
spec:
  containers:
  - name: nginx
    image: nginx:1.22
    startupProbe:
      exec:
        command: ["/usr/bin/test","-e","/usr/share/nginx/html/index.html"]
    livenessProbe:
      httpGet:
        port: 80
        scheme: HTTP
        path: /index.html
    readinessProbe:
      tcpSocket:
        port: 80
    volumeMounts:
    - name: data-v
      mountPath: /usr/share/nginx/html/
    lifecycle:
      postStart:
        exec:
          command: ["/bin/bash","-c","echo start >> /usr/share/nginx/html/index.html"]
      preStop:
        exec:
          command: ["/bin/bash","-c","echo stop >> /usr/share/nginx/html/index.html"]
  volumes:
  - name: data-v
    hostPath:
      path: /opt/node
      type: DirectoryOrCreate
​
[root@master01 k8s-yaml]# kubectl delete -f test1.yaml 
pod "nginx1" deleted
[root@master01 k8s-yaml]# kubectl apply -f test1.yaml 
pod/nginx1 created
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME     READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
nginx1   1/1     Running   0          19s   10.244.1.8   node01   <none>           <none>
[root@master01 k8s-yaml]# curl 10.244.1.8
start
​
[root@node01 node]# cat index.html 
start
stop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值