pod 容器下线前做些生命周期的操作,如服务从注册中心下线

发布时间 2023-07-05 17:44:36作者: yangxiaohui227

1.我们可以写一些下线的脚本如:(postop.sh)

#!/bin/bash
#先从注册中心下线
curl http://127.0.0.1:9010/server/deregister
sleep 20
#睡眠20s,调用springboot优雅关机接口
curl  -X POST  http://127.0.0.1:9010/actuator/supper-shutdown

在项目打包成镜像时,将该文件打包到镜像中,也就是Dockerfile打包镜像时

 2.在部署项目的yaml生命周期上,调用该脚本

 

    spec:
      imagePullSecrets:
        - name: aliyuncs-docker-hub  #提前在项目下配置访问阿里云的账号密码
      containers:
        - image: $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER
 #         readinessProbe:
 #           httpGet:
 #             path: /actuator/health
 #             port: 8080
 #           timeoutSeconds: 10
 #           failureThreshold: 30
 #           periodSeconds: 5
          imagePullPolicy: Always
          name: app
          ports:
            - containerPort: 8080
              protocol: TCP
          resources:
            limits:
              cpu: 300m
              memory: 600Mi
          lifecycle:
            preStop:
              exec:
                command: ["sh","-c","/app/postop.sh"]  #set prestop
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 30