Friday 24 May 2019

Kubernetes Commands

There are various basic commands which are helpful to begin. Along with that, it will be good, if we know the hierarchy of different resources in Kubernetes



Let's start from the micro level of K8S resources


1. POD

The POD is the logical group of containers. The containers will not directly run inside the node instead, it will be running inside a POD. It is always a good idea to group tightly coupled containers in a single POD along with any shared storage volumes if needed. Each POD in the node will have its unique IP.

Useful commands


$ kubectl get pods 
$ kubectl describe pods [pod-name]
$ kubectl describe pods --help 
$ kubectl logs [pod-name]
$ kubectl delete pod <>pod-name 
$ kubectl delete pods -all

The usual structure of all the K8S command is kubectl <action> <resource>

The very interesting command which I like most is 
$kubectl exec -ti <pod_name> bash

The above command actually takes us inside the POD and we can execute the commands inside it.


Deployment Vs PODS:

We can think of deployment as a template and the PODs are the actual deployments. In java language, the deployment is a class and PODS are objects, the replicas of deployment will create those many numbers of pods.


$ kubectl get deployments 
$ kubectl scale deployments/<deployment-name> --replicas=4 
$ kubectl describe deployment


2. Node

The node could be a VM or a real machine in K8S, the collection of nodes form a cluster. The K8S master monitors PODs and Nodes health and recreates the PODs in another node if the if one of the nodes is not healthy.


$ kubectl get nodes 
$ kubectl describe nodes [nodename]


3. Services

Is an abstraction which defines a logical set of PODs. It enables loose coupling between pods, it is preferred to be defined using YAML or JSON. The PODS are actually accessible only inside the cluster using their unique IP, if we want to expose the PODs outside then we need to define services. Also the service acts like a load balancer and distributes the load across multiple replicas.

There are different ways in which a service can be exposed in different ways by specifying the type

  1. Cluster IP - Using the default cluster IP, accessible only inside the cluster
  2. NodePort - Can be accessed outside by using the node IP and the port number specifies in NodePort
  3. LoadBalancer - In exposed a public IP if the cloud provider supports
  4. External name - using a CNAME
$ kubectl get services 
$ kubectl describe services/<service name> 
$ kubectl delete service



Other general commands

To get the cluster information
$ kubectl cluster-info

Rolling updates
$ kubectl rollout
If we have the definitions in yaml file then 
$ kubectl apply -f <loc of yaml>

Below link are good to get started with learning K8S
https://www.katacoda.com/courses/kubernetes/playground
https://kubernetes.io/docs/tutorials/kubernetes-basics/

In the next blog, I will mention sample yaml file all the resources

1 comment: