Thursday, December 17, 2020

Kubernetes for Developers #3: kubectl CLI

It is a command line interface used for running commands against K8 cluster master node API server. It has a config file called “kubeconfig” which maintains cluster information for authentication and connecting to API server.

kubectl


Syntax

kubectl [command] [TYPE] [NAME] [flags]

a) command: It accepts operation name i.e. create, apply, get, delete, describe, exec, log

b) Type: It accepts resource type in the form of singular, plural or abbreviated. Resource types are case-insensitive

The below three commands gives same result

> kubectl get pod 
> kubectl get pods 
> kubectl get po 


See the following table data with resource type and abbreviated name (i.e. short name)

Name

ShortName

KIND

pods

po

Pod

configmaps

cm

ConfigMap

namespaces

ns

Namespace

nodes

no

Node

replicationcontrollers

rc

ReplicationController

secrets

 

Secret

services

svc

Service

deployments

deploy

Deployment

replicasets

rs

ReplicaSet

ingresses

ing

Ingress

cronjobs

cj

CronJob

c) Name: It is used for specifying name of the resource and it is case-sensitive.

> kubectl get pod helloworld
> kubectl get pod helloworld helloworld2  
> kubectl get -f ./helloworld.yml 
> kubectl get -f ./helloworld.yml  -f ./helloworld2.yml

Examples:

// Display endpoint information about the master and services in the cluster
> kubectl cluster-info

// Display both kubectl client version and Kubernetes API server version
> kubectl version

// Display cluster configuration settings like contexts, users and etc. 
> kubectl config view

// Display all resources of bindings, shotnames and KIND
> kubectl api-resources

// Display all resources info from default namespace
> kubectl get all

// Display all resources info from all namespaces
> kubectl get all --all-namespaces

// Create resource from a file
> kubectl create -f ./helloworld.yml

// Create resources from multiple files
> kubectl create -f ./helloworld.yml -f ./helloworld2.yml

// Create resources from the given directory
> kubectl create -f ./dir

// Create/Update resource from a file
> kubectl apply -f ./helloworld.yml

// Create/Update resources from multiple files
> kubectl apply -f ./helloworld.yml -f ./helloworld2.yml

// Create/Update resources from the given directory
> kubectl apply -f ./dir

// switch to my-namespace 
> kubectl config set-context --current --namespace=my-namespace

// List all pods 
> kubectl get pods

// List all pods with additional information
> kubectl get pods -o wide

// Get pod yml file
> kubectl get pod my-pod -o yml

// List all services and deployments together
> kubectl get services,deployments

// Display detail state of pod
> kubectl describe pods my-pod

// Delete a pod which specifed in the my-pod.yml
> kubectl delete -f ./my-pod.yml

// Delete pods with name pod1 and pod2
> kubectl delete pod pod1 pod2

// Delete pods with label name=mylabel
> kubectl delete pods -l name=mylabel

// Display logs from mypod
> kubectl logs mypod

// Stream logs from mypod
> kubectl logs -f mypod

// Execute command against a container 
> kubectl exec <pod_name> -c <container_name> -- ls

// Get interactive shell on a first-container pod
> kubectl exec -it <pod_name> -- /bin/sh


Kubernetes for Developers Journey.

Happy Coding :)


No comments:

Post a Comment