Monday, August 31, 2020

kubectl Commands list

 kubctl  is the command line tool  and it is the key components to manage our Kubernetes Cluster & its  resources . which is installed in management instance / hosts. 

kubectl commands will help us to interact and manage the kubernetes resources/objects and the cluster. so we have look some of the commands which we can use in the regular basis on the kubernetes administration.

Before going to Kubectl commands i would like to add the aliases which are going to use in our environments to work with kubectl. it will help us on reduce the execution commands typing. here are some of the aliases which i have added in my Management instance/host.

in order to setup the aliases, go to your user_home(in my case im using root user) by clicking cd~

then ls -lhart

-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc

open .bashrc with vi editor and add the below aliases and save the file.

then we have to reload the .bashrc with the command  source ~/.bashrc, and execute the aliases which we have added in the .bashrc

kns


kno


like this you have to execute the day to day activities with kubectl by configuring the aliases.

                                               kubectl commands


There’s generally two ways to use kubectl when managing our kubernetes Resources/Objects like pods, services,…etc, those are the Declarative  and Imperative methodologies.

Declarative methodology is like we have to write our yaml file(manifest file) to and need to execute.

Imperative methodology is like with in the kubectl command we have to pass the arguments to the particular object/resource.

Declarative example:

i will use Namespace for now to understand the declarative methodology,

create a ns.yaml file with below content, and save and execute with below command.

vi /opt/yaml-files/ns.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: devns


below command will help us to create the object/resource

kubectl create -f /opt/yaml-files/ns.yaml


kubectl get ns  will give us the list of the namespaces which are there in the cluster.


same way we will have a look on the imperative methodology to create namespace, so for that i'm deleting the namespace and will create with the help of imperative methodology,



Now Imperative methodology, i did not provided any manifest file or declarative steps to create. just given below command it will create namespace with DEVNS.

kubectl create ns devns


kubectl get ns


Now i would like to jump in to the Kubectl commands on each objects/Resources.

Nodes:

To list out the nodes from the cluster use: kubectl get nodes

To watch the node status on every second  use : watch -n1 kubectl get nodes

To watch the node status on every 2 seconds use : watch kubectl get nodes

TO fetch the more details of the Nodes use:   kubectl describe nodes worker-01

To list the  only Worker Nodes : kubectl get node --selector='!node-role.kubernetes.io/master'




POD:

we can create the resources/objects with both the  ways which we discussed above.

To list the Pods use : kubectl get pods  (initially there was no pods)


To Create the pod with Declarative methodology:

create a file using yaml file: vi /opt/yaml-files/pod.yaml  add below content and save.

apiVersion: v1
kind: Pod
metadata:
  name: cafe-app
spec:
  containers:
  - image: nginxdemos/hello
    name: cafe


now execute the manifest file to create the POD object.

kubectl create -f pod.yaml


now see the list of the pods running on the cluster: kubectl get pods


To Create the pod with Imperative methodology:

same Image i used to create the pod by Imperative command.



so we can create/delete/modify/ our objects both the ways.
 
some more Commands to use on the pods:

To fetch the more details of the POD  use : kubectl describe pod cafe-app

To list all pods in all namespaces  : kubectl get pods --all-namespaces 

To list all pods in the current namespace, with more details  :  kubectl get pods -o wide

To get a pod's YAML : kubectl get pod my-pod -o yaml 

To get a pod's JSON : kubectl get pod my-pod -o json

To delete pod : kubectl delete -f pod.yaml (OR) kubectl delete pod <POD_NAME>

To see the logs of the pod use : kubectl logs <POD_NAME>

To login to the pod : kubectl exec -it centos-pod -- /bin/bash

To see the Process running on the pod : kubectl exec <POD_NAME> -- ps 

To Show the labels for all the pods: kubectl get pods --show-labels

To Edit the pod : Kubectl edit pod <POD_NAME>