🚀
3. Kubectl Commands

Minikube

Minikube is a single node k8s cluster which can be run on virtual box in local machine (used for testing purposes).

  • minikube start -> will create a single node cluster
  • minikube stop
  • minikube remove
  • minikube status
  • minikube dashboard -> opens the dashboard in localhost
  • minikube docker-env -> prints the environment variable for docker
  • minikube ssh -> open minikube in secure shell

Kubectl

Kubectl allows the user to interact with k8s cluster, it is command line tool, which talks to the API server.

  • kubectl version -> prints version details
  • kubctl version --output=yaml ->prints yaml output of version details
  • kubctl get pods -> prints details of running nodes
  • kubectl get nodes
  • kubectl get
  • kubectl config
  • kubectl config current-context
  • kubectl get all
  • kubectl delete pod <name>

Basic Commands

Starting Cluster

  • minikube start will start Minikube(single node k8s cluster).

Getting Nodes

  • kubectl get nodes will display all the pods

Getting Services

  • kubectl get services will display all the services

Creating Deployment

  • kubectl create deployment <name> --image=<image> will create a deployment pod.
    • deployment is the blueprint for creating pods (most basic configuration) ![][./images/kubectlcreatedeploymentname--image=image.png]

Getting Deployment

  • kubectl get deployment will display all the deployments ![][./images/kubectlgetdeployment.png]

Getting Pod

  • kubectl get pods will display all the pods

  • Layers of Abstraction: Deployment -> ReplicaSet -> Pod -> Container

Editing Deployment

  • kubectl edit deployment nginx-deploy to make changes in the deployment
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2024-07-31T18:41:34Z"
  generation: 1
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: default
  resourceVersion: "16345"
  uid: 1d944b5a-85c8-4ddc-bb3d-a27d2141d620
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx-deploy
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx-deploy
    spec:
      containers:
        - image: nginx
          imagePullPolicy: Always
          name: nginx
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
    - lastTransitionTime: "2024-07-31T18:41:49Z"
      lastUpdateTime: "2024-07-31T18:41:49Z"
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: "True"
      type: Available
    - lastTransitionTime: "2024-07-31T18:41:34Z"
      lastUpdateTime: "2024-07-31T18:41:49Z"
      message: ReplicaSet "nginx-deploy-854b9d5bdb" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: "True"
      type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

Debugging Containers

Changing image version

  • kubectl edit deployment nginx-deploy and adding version to image
spec:
  containers:
    - image: nginx:1.16
      imagePullPolicy: Always
      name: nginx
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
$ kubectl get pods
 
NAME                            READY   STATUS              RESTARTS   AGE
nginx-deploy-7cfbfdcbcb-5dhw6   0/1     ContainerCreating   0          17s
nginx-deploy-854b9d5bdb-zv6fw   1/1     Running             0          21m
 
$ kubectl get pods
 
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-7cfbfdcbcb-5dhw6   1/1     Running   0          113s
$ kubectl get replicasets
 
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-7cfbfdcbcb   1         1         1       2m55s
nginx-deploy-854b9d5bdb   0         0         0       24m
  • kubectl describe pod nginx-deploy-7cfbfdcbcb used to get information about the pod
$ kubectl describe pod nginx-deploy-7cfbfdcbcb
 
Name:             nginx-deploy-7cfbfdcbcb-5dhw6
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.50.1
Start Time:       Thu, 01 Aug 2024 00:32:41 +0530
Labels:           app=nginx-deploy
                  pod-template-hash=7cfbfdcbcb
Annotations:      <none>
Status:           Running
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  64m   default-scheduler  Successfully assigned default/nginx-deploy-7cfbfdcbcb-5dhw6 to minikube
  Normal  Pulling    64m   kubelet            Pulling image "nginx:1.16"
  Normal  Pulled     64m   kubelet            Successfully pulled image "nginx:1.16" in 14.989s (14.989s including waiting). Image size: 126681697 bytes.
  Normal  Created    64m   kubelet            Created container nginx
  Normal  Started    64m   kubelet            Started container nginx

Execute Pod in terminal

  • kubectl exec -it nginx-deploy-7cfbfdcbcb-5dhw6 -- sh
# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#

Get All Components

  • kubectl get all shows all components inside cluster
$ kubectl get all
 
NAME                                    READY   STATUS    RESTARTS      AGE
pod/nginx-deployment-77d8468669-5s86g   1/1     Running   1 (36s ago)   17h
pod/nginx-deployment-77d8468669-p94hk   1/1     Running   1 (36s ago)   17h
 
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
service/kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP     5d12h
service/nginx-service   ClusterIP   10.105.137.204   <none>        27017/TCP   17h
 
NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   2/2     2            2           17h
 
NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-77d8468669   2         2         2       17h

Deleting Pod

  • kubectl delete deployment <pod_name> will delete pod
$ kubectl get pods
 
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-7cfbfdcbcb-5dhw6   1/1     Running   0          93m
$ kubectl delete deployment nginx-deploy
 
deployment.apps "nginx-deploy" deleted

© 2024 Driptanil Datta.All rights reserved

Made with Love ❤️

Last updated on Mon Oct 20 2025