Linux:kubernetes(k8s)探针StartupProbe的使用(8)

Linux:kubernetes(k8s)pod的基础操作(6)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/w14768855/article/details/136462030?spm=1001.2014.3001.5501

👆这一章里有我编写nginx-demo的一个文件 

apiVersion: v1 # api文档版本
kind: Pod  # 资源对象类型
metadata:  # pod相关的元数据,用于描述pod的数据
  name: nginx-demo # pod名称
  labels:  # pod的标签
    type: app   #这个是随便写的 自定义的标签
    version: 1.0.0   #这个是随便写的
    test: 1.0.0    #都标签随便写的
  namespace: 'default'  #命名空间的配置
spec: #期望pod按照这里面的描述进行创建
  containers:  #对于pod容器的描述
  - name: nginx #容器的名称
    image: nginx:1.7.9 # 指定容器的镜像
    imagePullPolicy: IfNotPresent  #镜像拉取策略
    command: # 指定容器启动时执行的命令
    - nginx
    - -g
    - 'daemon off;'
    workingDir: /usr/local/nginx/html # 定义容器启动后的工作目录
    ports:
    - name: http # 端口名称
      containerPort: 80 # 描述容器内容要暴露的端口
      protocol: TCP # 端口是用什么协议通信
    env:   # 环境变量
    - name: JVM_OPTS  # 环境变量的名称
      value: '-Xms128m -Xmx128m'  # 环境变量的值
    resources:
      requests: # 最少需要多少资源
        cpu: 100m #限制cpu最少使用 1000m=1核心 100m就是0.1个核心
        memory: 128Mi #限制内存最少使用129兆
      limits:  #最多可以用多少 
        cpu: 200m # 限制最多可以使用多少
        memory: 256Mi
  restartPolicy: OnFailure #重启策略,只有失败的情况才会重启

 新加探针 :错误的路径

现在我要在这个demo文件中做出修改添加探针的一个检测

修改为

apiVersion: v1 # api文档版本
kind: Pod  # 资源对象类型
metadata:  # pod相关的元数据,用于描述pod的数据
  name: nginx-demo # pod名称
  labels:  # pod的标签
    type: app   #这个是随便写的 自定义的标签
    version: 1.0.0   #这个是随便写的
    test: 1.0.0    #都标签随便写的
  namespace: 'default'  #命名空间的配置
spec: #期望pod按照这里面的描述进行创建
  containers:  #对于pod容器的描述
  - name: nginx #容器的名称
    image: nginx:1.7.9 # 指定容器的镜像
    imagePullPolicy: IfNotPresent  #镜像拉取策略
    startupProbe: #应用容器探针
      httpGet: # 探测方式
         path: /api/path #http 请求路径
         port: 80 # 请求端口
      failureThreshold: 3 # 失败多少次,才算真正失败
      periodSeconds: 10 # 间隔时间
      successThreshold: 1 # 多少次检测成功算成功
      timeoutSeconds: 5   # 请求超时时间
    command: # 指定容器启动时执行的命令
    - nginx 
    - -g
    - 'daemon off;'
    workingDir: /usr/local/nginx/html # 定义容器启动后的工作目录
    ports: 
    - name: http # 端口名称
      containerPort: 80 # 描述容器内容要暴露的端口
      protocol: TCP # 端口是用什么协议通信
    env:   # 环境变量
    - name: JVM_OPTS  # 环境变量的名称
      value: '-Xms128m -Xmx128m'  # 环境变量的值
    resources: 
      requests: # 最少需要多少资源
        cpu: 100m #限制cpu最少使用 1000m=1核心 100m就是0.1个核心
        memory: 128Mi #限制内存最少使用129兆
      limits:  #最多可以用多少 
        cpu: 200m # 限制最多可以使用多少
        memory: 256Mi
  restartPolicy: OnFailure #重启策略,只有失败的情况才会重启

在里面加了探针这一块,我的/api/path目录并不存在,等会启动的时候肯定会失败 

现在去查看一下pod

kubectl get pod

 

可以看到是创建中,现在去看一下他的创建过程 

可以看到是失败的


探针目标:正确的路径

apiVersion: v1 # api文档版本
kind: Pod  # 资源对象类型
metadata:  # pod相关的元数据,用于描述pod的数据
  name: nginx-po # pod名称
  labels:  # pod的标签
    type: app   #这个是随便写的 自定义的标签
    version: 1.0.0   #这个是随便写的
    test: 1.0.0    #都标签随便写的
  namespace: 'default'  #命名空间的配置
spec: #期望pod按照这里面的描述进行创建
  containers:  #对于pod容器的描述
  - name: nginx #容器的名称
    image: nginx:1.7.9 # 指定容器的镜像
    imagePullPolicy: IfNotPresent  #镜像拉取策略
    startupProbe: #应用容器探针
      httpGet: # 探测方式
         path: /index.html #http 请求路径
         port: 80 # 请求端口
      failureThreshold: 3 # 失败多少次,才算真正失败
      periodSeconds: 10 # 间隔时间
      successThreshold: 1 # 多少次检测成功算成功
      timeoutSeconds: 5   # 请求超时时间
    command: # 指定容器启动时执行的命令
    - nginx 
    - -g
    - 'daemon off;'
    workingDir: /usr/local/nginx/html # 定义容器启动后的工作目录
    ports: 
    - name: http # 端口名称
      containerPort: 80 # 描述容器内容要暴露的端口
      protocol: TCP # 端口是用什么协议通信
    env:   # 环境变量
    - name: JVM_OPTS  # 环境变量的名称
      value: '-Xms128m -Xmx128m'  # 环境变量的值
    resources: 
      requests: # 最少需要多少资源
        cpu: 100m #限制cpu最少使用 1000m=1核心 100m就是0.1个核心
        memory: 128Mi #限制内存最少使用129兆
      limits:  #最多可以用多少 
        cpu: 200m # 限制最多可以使用多少
        memory: 256Mi
  restartPolicy: OnFailure #重启策略,只有失败的情况才会重启

 

这次我们监控的目录肯定是存在的了

通过该命令 

kubectl describe po nginx-po

可以看到一下创建成功了

 

 


探针目标:tcp

apiVersion: v1 # api文档版本
kind: Pod  # 资源对象类型
metadata:  # pod相关的元数据,用于描述pod的数据
  name: nginx-po # pod名称
  labels:  # pod的标签
    type: app   #这个是随便写的 自定义的标签
    version: 1.0.0   #这个是随便写的
    test: 1.0.0    #都标签随便写的
  namespace: 'default'  #命名空间的配置
spec: #期望pod按照这里面的描述进行创建
  containers:  #对于pod容器的描述
  - name: nginx #容器的名称
    image: nginx:1.7.9 # 指定容器的镜像
    imagePullPolicy: IfNotPresent  #镜像拉取策略
    startupProbe: #应用容器探针
    #  httpGet: # 探测方式
    #     path: /index.html #http 请求路径
      tcpSocket :
         port: 80 # 请求端口
      failureThreshold: 3 # 失败多少次,才算真正失败
      periodSeconds: 10 # 间隔时间
      successThreshold: 1 # 多少次检测成功算成功
      timeoutSeconds: 5   # 请求超时时间
    command: # 指定容器启动时执行的命令
    - nginx 
    - -g
    - 'daemon off;'
    workingDir: /usr/local/nginx/html # 定义容器启动后的工作目录
    ports: 
    - name: http # 端口名称
      containerPort: 80 # 描述容器内容要暴露的端口
      protocol: TCP # 端口是用什么协议通信
    env:   # 环境变量
    - name: JVM_OPTS  # 环境变量的名称
      value: '-Xms128m -Xmx128m'  # 环境变量的值
    resources: 
      requests: # 最少需要多少资源
        cpu: 100m #限制cpu最少使用 1000m=1核心 100m就是0.1个核心
        memory: 128Mi #限制内存最少使用129兆
      limits:  #最多可以用多少 
        cpu: 200m # 限制最多可以使用多少
        memory: 256Mi
  restartPolicy: OnFailure #重启策略,只有失败的情况才会重启

 现在是使用tcp进行一个监控了


探针目标:目录内容

apiVersion: v1 # api文档版本
kind: Pod  # 资源对象类型
metadata:  # pod相关的元数据,用于描述pod的数据
  name: nginx-po # pod名称
  labels:  # pod的标签
    type: app   #这个是随便写的 自定义的标签
    version: 1.0.0   #这个是随便写的
    test: 1.0.0    #都标签随便写的
  namespace: 'default'  #命名空间的配置
spec: #期望pod按照这里面的描述进行创建
  containers:  #对于pod容器的描述
  - name: nginx #容器的名称
    image: nginx:1.7.9 # 指定容器的镜像
    imagePullPolicy: IfNotPresent  #镜像拉取策略
    startupProbe: #应用容器探针
    #  httpGet: # 探测方式
    #     path: /index.html #http 请求路径
    #  tcpSocket :
    #     port: 80 # 请求端口
      exec:
        command:
        - sh
        - -c
        - " echo 'success' > /inited"
      failureThreshold: 3 # 失败多少次,才算真正失败
      periodSeconds: 10 # 间隔时间
      successThreshold: 1 # 多少次检测成功算成功
      timeoutSeconds: 5  # 请求超时时间
    command: # 指定容器启动时执行的命令
    - nginx 
    - -g
    - 'daemon off;'
    workingDir: /usr/local/nginx/html # 定义容器启动后的工作目录
    ports: 
    - name: http # 端口名称
      containerPort: 80 # 描述容器内容要暴露的端口
      protocol: TCP # 端口是用什么协议通信
    env:   # 环境变量
    - name: JVM_OPTS  # 环境变量的名称
      value: '-Xms128m -Xmx128m'  # 环境变量的值
    resources: 
      requests: # 最少需要多少资源
        cpu: 100m #限制cpu最少使用 1000m=1核心 100m就是0.1个核心
        memory: 128Mi #限制内存最少使用129兆
      limits:  #最多可以用多少 
        cpu: 200m # 限制最多可以使用多少
        memory: 256Mi
  restartPolicy: OnFailure #重启策略,只有失败的情况才会重启

这个就是会在根目录下创建一个文件并且写入内容

kubectl exec -it nginx-po -c nginx -- cat /inited

将nginx-po修改为你的pod名称就能看到了容器目录内的内容 


 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值