简体   繁体   English

在 dotnet 应用程序中删除 kubernetes pod

[英]deleting a kubernetes pod within dotnet app

in my dotnet app I need to monitor activities and if there is no activity withing 10 min I should kill the kubernete pod.在我的 dotnet 应用程序中,我需要监视活动,如果 10 分钟内没有活动,我应该杀死 kubernete pod。 killing the process will not do the job, is there anyway to kill/delete a pod within dotnet?杀死进程不会完成这项工作,无论如何要杀死/删除dotnet中的一个pod吗?

I assume you want to kill pods using code within the k8s cluster.我假设您想使用 k8s 集群中的代码杀死 pod。 Have a look at the kubernetes client for dotnet core.查看用于 dotnet 核心的kubernetes 客户端 You can use the cluster config from within the cluster you are running.您可以在正在运行的集群中使用集群配置。

// Load from the default kubeconfig on the machine.
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(config);
var list = client.ListNamespacedPod("default");

After that, you can lists pods, services etc, and kill them if you want to.之后,您可以列出 pod、服务等,并在需要时终止它们。

However, keep in mind that in order to read the local cluster config, and access resources, you need to set up a service account with the correct rights to do so.但是,请记住,为了读取本地集群配置和访问资源,您需要设置一个具有正确权限的服务帐户。

The example below has permissions to list services for example within the k8s cluster.下面的示例有权列出服务,例如在 k8s 集群中。 Adjust to your scenario accordingly.相应地根据您的情况进行调整。 Change 'services' to 'Pods' for example, and assign the right verbs for your logic.例如,将“服务”更改为“Pods”,并为您的逻辑分配正确的动词。

apiVersion: v1
kind: ServiceAccount
metadata:
  name: service-discovery-account

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  namespace: default
  name: service-discovery-service-reader
rules:
- apiGroups: [""]
  resources: ["services"]
  verbs: ["get", "watch", "list"]

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: service-discovery-service-reader
subjects:
  - kind: ServiceAccount
    # Reference to ServiceAccount kind's `metadata.name`
    name: service-discovery-account
    # Reference to ServiceAccount kind's `metadata.namespace`
    namespace: default
roleRef:
  kind: ClusterRole
  name: service-discovery-service-reader
  apiGroup: rbac.authorization.k8s.io

Don't forget to assign that service account to the deployment, responsible for monitoring other pods within the cluster:不要忘记将该服务帐户分配给部署,负责监控集群中的其他 pod:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-deployment-app
spec:
  selector:
    matchLabels:
      app: your-deployment-app
  template:
    metadata:
      labels:
        app: your-deployment-app
    spec:
      serviceAccountName: service-discovery-account
      containers:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM