Most Important Kubectl commands You Must Need to Know

Important Kubectl commands

 

Most Important Kubectl commands You Must Need to Know

kubectl cluster level commands:

 

1- List the available context/cluster and pointing to the current one. As per results below, the current cluster is minikube:
$kubectl config get-contexts

# CURRENT NAME           CLUSTER        AUTHINFO
#         demo           demo           <****>   
#         docker-desktop docker-desktop docker-desktop         
# *       minikube       minikube       minikube
2- List the name of the active context/cluster:
$kubectl config current-context
minikube
3- Switch between the predefined contexts/clusters:
$kubectl config use-context docker-desktop
Read Also – Top interview Questions on Docker
4- List current usage of CPU and memory for the cluster’s nodes:
$kubectl top node
NAME                               CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
aks-internal-23412768-vmss000000   96m          5%     2419Mi          53%
aks-internal-23412768-vmss000001   198m         10%    3768Mi          82%
aks-internal-23412768-vmss000002   107m         5%     2814Mi          61%
aks-system-13685168-vmss000000     139m         7%     4258Mi          93%
aks-system-13685168-vmss000001     247m         13%    4100Mi          89%
5- List Kubernetes API resources, short-names and API versions:
$kubectl api-resources
NAME                              SHORTNAMES   APIVERSION                             
configmaps                        cm           v1                                     
endpoints                         ep           v1                                     
events                            ev           v1                                     
limitranges                       limits       v1                                     
namespaces                        ns           v1                                     
nodes                             no           v1                                     
persistentvolumeclaims            pvc          v1                                     
persistentvolumes                 pv           v1                                     
pods                              po           v1                                     
...
6- List Kubernetes API endpoint, kube-dns endpoint and metrics server:
$kubectl cluster-info

kubectl resources level command:

1- Print namespace events which can be sorted to get events in order then they can be filtered by grep to fetch sepcific deployment events:
kubectl get event -n <NAMESPACE> --sort-by .lastTimestamp | grep <DEPLOYMENT_NAME>

Port-forward from a localhost port to a pod port or a service port which will be in the format of:

$kubectl port-forward <POD_NAME> <LOCALHOST_PORT>:<POD_PORT>
$kubectl port-forward svc/<SERVICE_NAME> <LOCALHOST_PORT>:<SERVICE_PORT>

Example:

$kubectl port-forward nginx-hht64 5000:80
$kubectl port-forward svc/nginx 5000:80
2- Read logs from a certain container in a pod which will be in the format of:
$kubectl logs <POD_NAME> -c <CONTAINER_NAME> -n <NAMESPACE>
3- Test if a service account can do certain action which will be in the below format and the result will be yes or no:
$kubectl --as=system:serviceaccount:<NAMESPACE>:<SERVICE_ACCOUNT_NAME> auth can-i get configmap/<CONFIGMAP_NAME>
yes
4- Test if a user account can do certain action:
$kubectl -n <NAMESPACE> auth can-i list secrets --as <USERACCOUNT>
5- List the current CPU and MEMORY usage for pods in a namespace:
$kubectl top pod -n <NAMESPACE>
6- Create ingress resource from a command line:


Here is an ingress created with name ‘demo’ and class ‘nginx’ and configured to use host “demo.localdev.me” to point to ‘demo’ service on port 80.

$kubectl create ingress demo --class=nginx --rule="demo.localdev.me/*=demo:80"
7- Copy secret from a namespace to another:
So here a secret “aks-ingress-tls” is copied from dev namespace to demo namespace.
$kubectl get secret aks-ingress-tls --namespace=dev -o yaml | sed 's/namespace: .*/namespace: demo/' | kubectl apply -f -
8- Open a bash or sh terminal inside a pod:
$kubectl exec -it <POD_NAME> -n <NAMESPACE> -- /bin/bash
$kubectl exec -it <POD_NAME> -n <NAMESPACE> -- /bin/sh
9- Copy files/directories from/to a pod which will be in the below format:
$kubectl cp <FILENAME> <NAMESPACE>/<PODNAME>:</path/to/file>
$kubectl cp <NAMESPACE>/<PODNAME>:</path/to/file> <FILENAME>

Example:

$kubectl cp file.txt default/nginx-345gjig:/home
$kubectl cp default/nginx-345gjig:/home/file.txt ./file.txt
10- Apply resources directly through the terminal without creating a manifest/file to be applied, I like this style while applying any resources as it is a mix between imperative and declarative way :
$echo "
apiVersion: v1
kind: Service
metadata:
  name: prometheus-svc
  namespace: monitoring
spec:
  selector:
    env: metrics
  type: ClusterIP
  ports:
  - port: 9090
    protocol: TCP
    targetPort: 9090
" | kubectl apply -f -
Hope you like this blog…. 

 

Mahesh Wabale

Leave a Comment