Kubernetes作业每分钟删除一个Pod [英] Kubernetes job to delete a single pod every minute

查看:101
本文介绍了Kubernetes作业每分钟删除一个Pod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Job,以便每隔一分钟或创建后的任何时间杀死以下Pod.

I'd like to create a Job to kill the following pod every single minute or any time when is created.

我的测试平台是:

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(基于角色的访问控制)对象. 这是示例.

Yes, you can delete the pods with kubectl within the cluster. First, you need to create a set of RBAC(Role-based access control) object. 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

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

These objects will define a proper RABC rule so that the pod created can interact with Kubernetes's corresponding API. Then, you can define your Job with a Cronjob type like this.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆