Home > Back-end >  How to find existing service by Kubernetes C# client API
How to find existing service by Kubernetes C# client API

Time:11-24

I have KubernetesClient code running my app on K3s Orchestrator. I have created service prog, prog-external & my-module with specified Ports for app-module namespace which are now seen in kubectl get svc -A command:

root@Ubuntu20-VM:~# kubectl get svc -A
NAMESPACE      NAME              TYPE           CLUSTER-IP     PORT(S)                         AGE
default        kubernetes        ClusterIP      10.43.0.1      443/TCP                         86m
kube-system    kube-dns          ClusterIP      10.43.0.10     53/UDP,53/TCP,9153/TCP          86m
kube-system    metrics-server    ClusterIP      10.43.4.133    443/TCP                         86m
app-module     prog              ClusterIP      10.43.175.255  9008/TCP,9009/UDP,9001/TCP      64m
app-module     prog-external     NodePort       10.43.108.249  9000:30001/TCP,9001:30002/TCP   64m
app-module     my-module         ClusterIP      10.43.23.30    9008/TCP,9009/UDP,9001/TCP      64m

Can someone please let me know that if there is any API in KubernetesClient to find/get theses services ? Please suggest.

PS: I think API ListNamespacedServiceWithHttpMessagesAsync will return all pods config for namespace app-module , but I only want those pods config in app-module NS for which I created services , here which are : prog, prog-external & my-module. Please correct me if my understanding is wrong ?

CodePudding user response:

You cannot fetch all pods related to services (in the namespace) with one Kubernetes API call (one function call in Kubernetes C# library).


You need to do the following:

  1. List all Services (filtered by namespace/name)
  2. List all Endpoints / Endpoint Slices (related to services you fetched in point 1.)
  3. List all Pods (related to Endpoint / Endpoint Slices from point 2.)

You can think of the service -> pod like a one-way relation. So service points to pods (via endpoint), but pod does not know anything about the service


A more Kubernetes-friendly way of achieving what you want would be to actually label your resources. So, keep a consistent labels across your services/pods, then you list your resources by labels.

  • Related