in my dotnet app I need to monitor activities and if there is no activity withing 10 min I should kill the kubernete pod. killing the process will not do the job, is there anyway to kill/delete a pod within dotnet?
I assume you want to kill pods using code within the k8s cluster. Have a look at the kubernetes client for dotnet core. 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.
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. Adjust to your scenario accordingly. Change 'services' to 'Pods' for example, and assign the right verbs for your logic.
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:
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: