Menu Close

K8 – How to get list of services in kubernetes

Here, we will see how to get list of services in kubernetes. There are many ways to do that. We will see one by one with example.

We will be using kubectl command line tool to get list of services in kubernetes.

Get list of services

Command:

$ kubectl get services --all-namespaces 

or

kubectl get services -A

Output:

 NAMESPACE    NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE

 default      kubernetes ClusterIP   10.96.0.1    <none>      443/TCP          108d

 kube-system  kube-dns   ClusterIP   10.96.0.10   <none>      53/UDP,53/TCP    108d

Get list of services of default namespace

$ kubectl get services

 NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE

 kubernetes   ClusterIP   10.96.0.1            443/TCP   108d

or

$ kubectl get services -n default

 NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE

 kubernetes   ClusterIP   10.96.0.1            443/TCP   108d

Get list of services by label

Syntax:

  $ kubectl get services -l=label_name

Example:

$ kubectl get pod -l=component=apiserver

NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE

kubernetes   ClusterIP   10.96.0.1            443/TCP   108d


$ kubectl get services --show-labels

NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE    LABELS

kubernetes   ClusterIP   10.96.0.1            443/TCP   108d   component=apiserver,provider=kubernetes

Get list of services by selector

Syntax:

kubectl get services --selector=selector_name

Example:

$ kubectl get services -o wide --all-namespaces --selector=k8s-app=kube-dns

 NAMESPACE     NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE    SELECTOR
 kube-system   kube-dns   ClusterIP   10.96.0.10           53/UDP,53/TCP,9153/TCP   108d   k8s-app=kube-dns

$ kubectl get services -o wide --all-namespaces

 NAMESPACE     NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE    SELECTOR

 default       kubernetes   ClusterIP   10.96.0.1            443/TCP                  108d   

 kube-system   kube-dns     ClusterIP   10.96.0.10           53/UDP,53/TCP,9153/TCP   108d   k8s-app=kube-dns

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/

Posted in kubernetes

Leave a Reply

Your email address will not be published.

Contact Us