在 GKE 的 cronjob 中使用 rollout restart 命令

Using the rollout restart command in cronjob, in GKE

我想使用 k8s cronjob 定期重启部署。

请检查yaml文件有什么问题

当我从本地命令行执行命令时,部署正常重启,但似乎无法通过 cronjob 重启。 例如 $ kubectl rollout restart deployment my-ingress -n my-app

我的 cronjob yaml 文件

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: deployment-restart
  namespace: my-app
spec:
  schedule: '0 8 */60 * *' 
  jobTemplate:
    spec:
      backoffLimit: 2 
      activeDeadlineSeconds: 600 
      template:
        spec:
          serviceAccountName: deployment-restart 
          restartPolicy: Never
          containers:
            - name: kubectl
              image: bitnami/kubectl:latest 
              command:
                - 'kubectl'
                - 'rollout'
                - 'restart'
                - 'deployment/my-ingress -n my-app'

正如 David 建议的那样 运行 kubectl 的 cron 就像执行命令

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-jp-runner
          containers:
          - name: hello
            image: bitnami/kubectl:latest
            command:
            - /bin/sh
            - -c
            - kubectl rollout restart deployment my-ingress -n my-app
          restartPolicy: OnFailure

我还建议您检查角色服务帐户权限

参考示例:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: kubectl-cron
rules:
- apiGroups:
  - extensions
  - apps
  resources:
  - deployments
  verbs:
  - 'patch'

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: kubectl-cron
  namespace: default
subjects:
- kind: ServiceAccount
  name: sa-kubectl-cron
  namespace: default
roleRef:
  kind: Role
  name: kubectl-cron
  apiGroup: ""

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sa-kubectl-cron
  namespace: default

---