云容器引擎 CCE-设置容器生命周期:YAML样例

时间:2024-04-03 08:02:20

YAML样例

本节以nginx为例,说明kubectl命令设置容器生命周期的方法。

在以下配置文件中,您可以看到postStart命令在容器目录/bin/bash下写了个install.sh命令。 preStop执行uninstall.sh命令。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx 
        command:
        - sleep 3600                        #启动命令
        imagePullPolicy: Always
        lifecycle:
          postStart:
            exec:
              command:
              - /bin/bash
              - install.sh                  #启动后命令
          preStop:
            exec:
              command:
              - /bin/bash
              - uninstall.sh                 #停止前命令
        name: nginx
      imagePullSecrets:
      - name: default-secret
support.huaweicloud.com/usermanual-cce/cce_11_0105.html