Kubernetes作业,每分钟删除一个Pod

问题描述 投票:0回答:1

我想创建一个作业,以便每隔一分钟或创建后的任何时间杀死以下吊舱。

我的测试平台是:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
    containers: 
    -   name: myapp-container
        image: busybox
        command: ['sh', '-c', 'echo Hello && sleep 3600']

可以这样做吗?

kubernetes cron jobs minikube
1个回答
0
投票

是,您可以在集群中删除带有kubectl的Pod。首先,您需要创建一组RBAC(基于角色的访问控制)对象。这是示例。

apiVersion: v1
kind: ServiceAccount
metadata:
  name: test # this is service account for binding the pod
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: test # This defines a role and what API it can access
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["delete", "get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: test # This will bind the role and service account
subjects:
- kind: ServiceAccount
  name: test
roleRef:
  kind: Role 
  name: test 
  apiGroup: rbac.authorization.k8s.io

这些对象将定义正确的RABC规则,以便创建的Pod可以与Kubernetes的相应API进行交互。然后,您可以使用这样的Cronjob类型定义Job。

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: kill-pod
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: test
          containers:
          - name: kill-pod
            image: bitnami/kubectl:latest
            command:
            - kubectl
            args:
            - delete
            - pod
            - sth
          restartPolicy: OnFailure
© www.soinside.com 2019 - 2024. All rights reserved.