gpt4 book ai didi

Kubernetes 作业每分钟删除一个 pod

转载 作者:行者123 更新时间:2023-12-02 12:01:44 25 4
gpt4 key购买 nike

我想创建一个作业来每分钟或在创建时的任何时间杀死以下 pod。

我的测试舱是:

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']

这有可能吗?

最佳答案

是的,您可以在集群中使用 kubectl 删除 pod。首先,你需要创建一组RBAC(Role-based access control)对象。这是示例。

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 类型定义您的作业。

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

关于Kubernetes 作业每分钟删除一个 pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60697650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com