华为云UCS-设置容器生命周期:YAML样例

时间:2024-09-12 15:06:03

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-ucs/ucs_01_0261.html