[问题已处理]-k8s让job在重启后再执行

导语:希望设计一个job,首次重启后再执行。

思路是通过判断/proc/uptime的时间来判断是否为开机。为了确保k8s正常运行,大致设置了15分钟。因为重启之前进行下载解压安装,肯定超过半小时。等reboot之后,job的策略会再次运行。并让其他job等待这个job执行完毕,来夯住整个流程。

关键为wait-for-reboot这个initcontainer

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: ops-verify-install-role
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["extensions", "apps"]
  resources: ["deployments"]
  verbs: ["get", "watch", "list", "patch"]
- apiGroups: ["batch","","apps"]
  resources: ["jobs"]
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: ops-verify-install-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: ops-verify-install-role
subjects:
  - kind: ServiceAccount
    name: ops-verify-install-operator
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: ops-verify-install-operator
---
apiVersion: batch/v1
kind: Job
metadata:
  name: ops-verify-install
  labels:
    job-name: ops-verify-install
    k8s-med-type: job
spec:
 template:
  metadata:
      labels:
        job-name: ops-verify-install
  spec:
   serviceAccountName: ops-verify-install-operator
   initContainers:
     - name: wait-for-mysql
       command: ['sh', '-c', "until nc -w 3  -zv mysql 3306 ; do echo waiting for mydb; sleep 5; done"]
       image: harbor.deepwise.com/base/busybox:stable-wait-for
     - name: wait-for-aiserverconfig-migration
       image: harbor.deepwise.com/base/k8s-wait-for:v1.3
       args:
         - "job"
         - "aiserverconfig-migration"
     - name: wait-for-aiserver-migration
       image: harbor.deepwise.com/base/k8s-wait-for:v1.3
       args:
         - "job"
         - "aiserver-migration"
     - name: wait-for-platform-migration
       image: harbor.deepwise.com/base/k8s-wait-for:v1.3
       args:
         - "job"
         - "platform-migration"
     - name: wait-for-pacsserver-migration
       image: harbor.deepwise.com/base/k8s-wait-for:v1.3
       args:
         - "job"
         - "pacsserver-migration"
     - name: wait-for-lung-code
       image: harbor.deepwise.com/base/k8s-wait-for:v1.3
       args:
         - "job"
         - "lung-code-migration"
     - name: wait-for-reboot
       image: harbor.deepwise.com/base/busybox:stable-wait-for
       command:
         - "sh"
         - "-c"
         - >
           uptime_seconds=$(awk '{print int($1)}' /proc/uptime);
           echo uptime_seconds $uptime_seconds ;
           threshold=900;
           if [ "$uptime_seconds" -lt "$threshold" ]; then
             echo "刚重启过";
             # Replace the following command with your actual task command
           else
             echo "开机太久了,不触发。直接夯住等重启";
             tail -f /dev/null ;
           fi
   containers:
   - name: ops-verify-install
     image: harbor.deepwise.com/base/busybox:storescu
     command: ["sh","-c","curl http://10.10.3.162/xujiamin/jenkins_deploy/-/raw/master/scripts/ops_verify_install/health_check.sh?inline=false  |sh && echo ' 执行激活命令并重启应用' |tee -a /opt/ops_verify_install.log && curl -o /opt/${MY_NODE_IP}_license_file.lic http://10.10.3.162/xujiamin/jenkins_deploy/-/raw/master/tools/${MY_NODE_IP}_license_file.lic && curl http://10.10.3.162/xujiamin/jenkins_deploy/-/raw/master/scripts/ops_verify_install/active_license.sh?inline=false  |sh && sleep 15 && echo ' 健康检查后再推送镜像' |tee -a /opt/ops_verify_install.log  && curl http://10.10.3.162/xujiamin/jenkins_deploy/-/raw/master/scripts/ops_verify_install/health_check.sh?inline=false |sh && echo ' 健康检查结束' |tee -a /opt/ops_verify_install.log "]
     env:
      #  - name: TZ
      #    value: Asia/Shanghai
       - name: TZ
         value: CST-8
       - name: MY_NODE_IP
         valueFrom:
           fieldRef:
             fieldPath: status.hostIP
     volumeMounts:
       - name: ops-verify-install-storage
         mountPath: /opt/
       - name: lt-config
         mountPath: /etc/localtime 
       - name: tz-config
         mountPath: /etc/timezone
       - name: tools
         mountPath: /opt/bin
         readOnly: true  # 设置为只读
   restartPolicy: OnFailure
   volumes:
   - name: ops-verify-install-storage
     hostPath:
       path: /opt
   - name: lt-config
     hostPath:
       path: /etc/localtime
   - name: tz-config
     hostPath:
       path: /etc/timezone
   - name: tools
     hostPath:
       path: /usr/bin
       type: ''

配合shutdown命令使用

image-20240624162934683

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值