Kubernetes: Basic commands

Once the installation of Kubernetes cluster is competed, let us go with the basic commands.

The below command lists the pods running on the cluster. Remember that there are several services used by Kubernetes already running as applications in pods.

Below command is used to view the CLuster’s configuration.

[root@CFX-91-KUBEMASTER ~]# kubectl config view

==== ==== ==
apiVersion: v1
clusters:
– cluster:
certificate-authority-data: DATA+OMITTED
server: https://192.168.0.91:6443
name: kubernetes

contexts:
– context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}

users:
– name: kubernetes-admin
user:
client-certificate-data: DATA+OMITTED
client-key-data: DATA+OMITTED
[root@CFX-91-KUBEMASTER ~]

===== ==== ===

You may be running multiple clusters in your organization, and therefore you will have to switch between managing these clusters. That is, you will have to switch between the context name, where each will be unique for each cluster.

Below command finds the context you are managing.

[root@CFX-91-KUBEMASTER ~]# kubectl config current-context

==== === ==
kubernetes-admin@kubernetes

===== ===

[root@CFX-91-KUBEMASTER ~]# kubectl config use-context NewContextName

The below command lists all the available commands under config.

[root@CFX-91-KUBEMASTER ~]# kubectl config –help

==== ===

Available Commands:
current-context Display the current-context
delete-cluster Delete the specified cluster from the kubeconfig
delete-context Delete the specified context from the kubeconfig
delete-user Delete the specified user from the kubeconfig
get-clust ……..

==== ===

[root@kubmaster01 ~]# kubectl get pods -A

==== ==
NAMESPACE     NAME      READY      STATUS      RESTARTS      AGE
kube-system calico-kube-controllers-5f9c988956-5v9fr 1/1 Running 3 (42m ago) 2d12h
kube-system calico-node-4vdc7 0/1 Running 2 (41m ago) 2d
kube-system calico-node-dwldq 0/1 Running 3 (42m ago) 2d12h
kube-system calico-node-pt5zx 0/1 Running 3 (42m ago) 2d12h
==== ===

In Kubernetes there is a concept of NameSpace which we will discuss later. The switch -A in the above command lists pods in all NameSpaces. The default NameSpace when no switch is used is the “default” NameSpace.

[root@kubmaster01 ~]# kubectl get pods -o wide -A

===== ===
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system calico-kube-controllers-5f9c988956-5v9fr 1/1 Running 3 (56m ago) 2d12h 172.16.152.76 kubmaster01 <none> <none>
kube-system calico-node-4vdc7 0/1 Running 2 (55m ago) 2d1h 192.168.146.131 localhost.localdomain <none> <none>
kube-system calico-node-dwldq 0/1 Running 3 (56m ago) 2d12h 192.168.146.130 kubworker01 <none> <none>
===== ===

The “-o wide” option in the above command provides additional details of the pods, compared to the previous command.

The below command provided information about the context presently used when accessing th

[root@kubmaster01 ~]# kubectl config current-context

======
kubernetes-admin@kubernetes
=======

The below command shows the different contexts that are presently available. On a newly installed cluster, only one will be available.

[root@kubmaster01 ~]# kubectl config get-contexts

=====
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kubernetes-admin@kubernetes kubernetes kubernetes-admin
==========

The below command lists the resources available in the system.

[root@kubmaster01 ~]# kubectl api-resources | head -n 5

===========
NAME    SHORTNAMES    APIVERSION    NAMESPACED    KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
endpoints ep v1 true Endpoints
====== ===