[英]Kubernetes job to delete a single pod every minute
I'd like to create a Job to kill the following pod every single minute or any time when is created.我想创建一个 Job 以每分钟或创建时杀死以下 pod。
My testing pod is:我的测试吊舱是:
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']
Is it possible to do that?有可能这样做吗?
Yes, you can delete the pods with kubectl within the cluster.是的,您可以使用 kubectl 删除集群内的 pod。 First, you need to create a set of RBAC(Role-based access control) object.
首先,您需要创建一组 RBAC(基于角色的访问控制)对象。 Here is the sample.
这是示例。
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
These objects will define a proper RABC rule so that the pod created can interact with Kubernetes's corresponding API.这些对象将定义适当的 RABC 规则,以便创建的 pod 可以与 Kubernetes 的相应 API 进行交互。 Then, you can define your Job with a Cronjob type like this.
然后,您可以使用这样的 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.