Dev-Ops why kubernetes(K8) and how to use(Basic Commands)? - Part 6

Dev-Ops why kubernetes(K8) and how to use(Basic Commands)? - Part 6

What is K8?

Kubernetes, often abbreviated as K8s, is an open-source platform designed for automating the deployment, scaling, and management of containerized applications. It acts as a container orchestration system, handling the complexities of running and managing applications composed of multiple containers across a cluster of machines. 

Here's a more detailed breakdown of why Kubernetes is used:

1. Automation and Orchestration:

  • Container Orchestration: Kubernetes automates the deployment, scaling, and management of containerized applications, simplifying complex deployments. 
  • Automated Operations: It automates tasks like restarting failed containers, rescheduling workloads, and rolling out new versions of applications without downtime. 

2. Scalability and Resource Efficiency:

  • Scalability: Kubernetes can automatically scale applications up or down based on demand, optimizing resource utilization and cost savings. 
  • Resource Optimization: It efficiently allocates resources to containers, ensuring optimal performance and minimizing waste. 

3. Reliability and Availability:

  • Self-Healing: Kubernetes automatically restarts failed containers and redistributes workloads, ensuring high availability of applications.
  • Rolling Updates: It enables seamless updates to applications with zero downtime, preventing disruptions to users. 

4. Portability and Flexibility:

  • Cloud Portability: Kubernetes provides a consistent environment across different cloud providers and on-premises infrastructure, allowing for easy migration and deployment. 
  • Flexibility: It supports various types of workloads, including stateless, stateful, and data-processing applications. 

5. Developer Productivity and Efficiency: 

  • Simplified Development: Kubernetes simplifies the process of building and deploying applications, allowing developers to focus on core application logic. 
  • Infrastructure as Code: It enables developers to define infrastructure and configurations as code, promoting consistency and collaboration. 

6. Open Source and Community Support:

  • Open Source: Kubernetes is an open-source project, fostering a large and active community that contributes to its continuous improvement and development. 
  • Ecosystem: It has a rich ecosystem of tools and extensions that complement its core functionality, providing a comprehensive platform for container management. 

In essence, Kubernetes is used to address the challenges of managing modern, containerized applications at scale. It offers automation, scalability, reliability, and portability, making it a powerful tool for both developers and operations teams. 

Where it does fit in software development cycle.

Article content

In order to test kubernetes in your local machine, Following configuration/softwares are required.

Install both softwares in your machine and can test below commands.

  • To Start/stop/pause/unpause minikub cluster

minikube start/minikube stop/minikube pause/minikube unpause

Let's start the minikub K8 cluster

C:\Users\SandeepPachauri>minikube start
* minikube v1.36.0 on Microsoft Windows 11 Pro 10.0.26100.4652 Build 26100.4652
* Using the docker driver based on existing profile
* Starting "minikube" primary control-plane node in "minikube" cluster
* Pulling base image v0.0.47 ...
* Restarting existing docker container for "minikube" ...
! Failing to connect to https://registry.k8s.io/ from inside the minikube container
* To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
* Preparing Kubernetes v1.33.1 on Docker 28.1.1 ...
* Verifying Kubernetes components...
  - Using image gcr.io/k8s-minikube/storage-provisioner:v5
* Enabled addons: default-storageclass, storage-provisioner
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default        

  • Check cluster pod details

	C:\Users\SandeepPachauri>kubectl get po -A
NAMESPACE        NAME                               READY   STATUS             RESTARTS      AGE
default          demo-deployment-7558778d8f-7skrm   1/1     Running            1 (93s ago)   130m
default          demo-deployment-7558778d8f-c74gb   1/1     Running            1 (93s ago)   94m
default          demo-deployment-7558778d8f-lkglw   1/1     Running            1 (93s ago)   61m
default          demo-deployment-7558778d8f-rqr7v   1/1     Running            1 (93s ago)   130m
kube-system      coredns-674b8bbfcf-2mcqd           0/1     Running            2 (88s ago)   140m
kube-system      etcd-minikube                      1/1     Running            2 (87s ago)   140m
kube-system      kube-apiserver-minikube            1/1     Running            2 (83s ago)   140m
kube-system      kube-controller-manager-minikube   1/1     Running            2 (93s ago)   140m
kube-system      kube-proxy-ntfmd                   1/1     Running            2 (93s ago)   140m
kube-system      kube-scheduler-minikube            1/1     Running            2 (93s ago)   140m
kube-system      storage-provisioner                1/1     Running            4 (93s ago)   140m
metallb-system   controller-dddd867f6-dl5gn         0/1     ErrImagePull       0             72m
metallb-system   speaker-6djt7                      0/1     ImagePullBackOff   0             72m        

  • Check cluster information.

C:\Users\SandeepPachauri>kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:52589
CoreDNS is running at https://127.0.0.1:52589/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.        

  • List of kubernative resources.

C:\Users\SandeepPachauri>kubectl api-resources
NAME                                SHORTNAMES   APIVERSION                        NAMESPACED   KIND
bindings                                         v1                                true         Binding
componentstatuses                   cs           v1                                false        ComponentStatus
configmaps                          cm           v1                                true         ConfigMap
endpoints                           ep           v1                                true         Endpoints        

  • Create resources in kubernative

kubectl create -f <filename.yaml>        

  • Create resources in kubernative using inline command

C:\Users\SandeepPachauri>kubectl create deployment demo-deployment --image=nginx:latest --replicas=2
deployment.apps/demo-deployment created        

  • Get running pods in cluster

C:\Users\SandeepPachauri>kubectl get pods
NAME                               READY   STATUS    RESTARTS        AGE
demo-deployment-7558778d8f-7skrm   1/1     Running   1 (4m23s ago)   133m
demo-deployment-7558778d8f-c74gb   1/1     Running   1 (4m23s ago)   96m
demo-deployment-7558778d8f-lkglw   1/1     Running   1 (4m23s ago)   64m
demo-deployment-7558778d8f-rqr7v   1/1     Running   1 (4m23s ago)   133m        

  • Create dedicated namespace(Domain).

C:\Users\SandeepPachauri>kubectl create namespace app-dev
namespace/app-dev created        
C:\Users\SandeepPachauri>kubectl create namespace app-prod
namespace/app-prod created	        

  • Get all namespace(domain)

C:\Users\SandeepPachauri>kubectl get namespace
NAME              STATUS   AGE
app-dev           Active   130m
app-prod          Active   129m
default           Active   144m
kube-node-lease   Active   144m
kube-public       Active   144m
kube-system       Active   144m
metallb-system    Active   77m        

  • Get specific pod complete information

C:\Users\SandeepPachauri>kubectl describe pod demo-deployment-7558778d8f-7skrm
Name:             demo-deployment-7558778d8f-7skrm
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Wed, 13 Aug 2025 14:39:28 +0530
Labels:           app=demo-deployment
                  pod-template-hash=7558778d8f
Annotations:      <none>
Status:           Running
IP:               10.244.0.11
IPs:
  IP:           10.244.0.11
Controlled By:  ReplicaSet/demo-deployment-7558778d8f        

  • Get logs from pod

C:\Users\SandeepPachauri>kubectl logs -f demo-deployment-7558778d8f-7skrm
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/08/13 11:20:04 [notice] 1#1: using the "epoll" event method
2025/08/13 11:20:04 [notice] 1#1: nginx/1.29.0
2025/08/13 11:20:04 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14+deb12u1)
2025/08/13 11:20:04 [notice] 1#1: OS: Linux 6.6.87.2-microsoft-standard-WSL2
2025/08/13 11:20:04 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2025/08/13 11:20:04 [notice] 1#1: start worker processes
2025/08/13 11:20:04 [notice] 1#1: start worker process 29
2025/08/13 11:20:04 [notice] 1#1: start worker process 30
2025/08/13 11:20:04 [notice] 1#1: start worker process 31
2025/08/13 11:20:04 [notice] 1#1: start worker process 32
2025/08/13 11:20:04 [notice] 1#1: start worker process 33
2025/08/13 11:20:04 [notice] 1#1: start worker process 34
2025/08/13 11:20:04 [notice] 1#1: start worker process 35
2025/08/13 11:20:04 [notice] 1#1: start worker process 36        

  • Get resources from pod.

C:\Users\SandeepPachauri>kubectl exec demo-deployment-7558778d8f-7skrm -- ls /
bin
boot
dev
docker-entrypoint.d
docker-entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

In case any specific folder resources to be checked.

C:\Users\SandeepPachauri>kubectl exec demo-deployment-7558778d8f-7skrm -- ls /bin
[
addpart
apt
apt-cache
apt-cdrom
apt-config
apt-get
apt-key        

  • Get shell/script access in a pod


C:\Users\SandeepPachauri>kubectl exec -it demo-deployment-7558778d8f-7skrm -- /bin/bash

root@demo-deployment-7558778d8f-7skrm:/# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var        

  • Get list of deployments.

C:\Users\SandeepPachauri>kubectl get deployments
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
demo-deployment   4/4     4            4           138m        

  • Get details of specific deployment in YAML format.

C:\Users\SandeepPachauri>kubectl get deployment demo-deployment -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2025-08-13T09:09:28Z"
  generation: 5
  labels:
    app: demo-deployment
  name: demo-deployment
  namespace: default
  resourceVersion: "7757"
  uid: d8310c7c-69d7-4789-bfc6-0ff67691c8c0
spec:
  progressDeadlineSeconds: 600
  replicas: 4
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: demo-deployment
  strategy:
    rollingUpdate:        

  • Scale(Increase/decrease) no of instance for running pod

C:\Users\SandeepPachauri>kubectl scale deployment demo-deployment --replicas=5
deployment.apps/demo-deployment scaled

C:\Users\SandeepPachauri>kubectl get deployments
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
demo-deployment   5/5     5            5           141m        

  • Check the deployment status of any pod/image

C:\Users\SandeepPachauri>kubectl rollout status deployment demo-deployment
deployment "demo-deployment" successfully rolled out        

  • Get list of services running in cluster

C:\Users\SandeepPachauri>kubectl get service
NAME              TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
demo-service      ClusterIP      10.111.65.33    <none>        80/TCP         99m
demo-service-lb   LoadBalancer   10.107.127.0    <pending>     80:30101/TCP   82m
demo-service-np   NodePort       10.96.138.119   <none>        80:31452/TCP   92m
kubernetes        ClusterIP      10.96.0.1       <none>        443/TCP        151m        

  • Expose a deployment as service.

C:\Users\SandeepPachauri>kubectl expose deployment/demo-deployment --name=demo-service --port=80
service/demo-service exposed

C:\Users\SandeepPachauri>kubectl get service
NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
demo-service   ClusterIP   10.111.65.33   <none>        80/TCP    4s
kubernetes     ClusterIP   10.96.0.1      <none>        443/TCP   51m        

  • Running a service: Note services can be only accessed from one of the pod where its running. To do so, you have login into pod as shell/bash.

kubectl exec -it demo-deployment-7558778d8f-7skrm -- /bin/bash        
root@demo-deployment-7558778d8f-7skrm:/# curl 10.111.65.33:80        

  • Expose API service to node port service so it can be accessed outside of cluster(on the browser).

C:\Users\SandeepPachauri>kubectl expose deployment/demo-deployment --name=demo-service-np --port=80 --target-port=80 --type=NodePort

service/demo-service-np exposed        

  • Get external/internal ip of cluster to run.

C:\Users\SandeepPachauri>kubectl get nodes -o wide
NAME       STATUS   ROLES           AGE    VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION                     CONTAINER-RUNTIME
minikube   Ready    control-plane   153m   v1.33.1   192.168.49.2   <none>        Ubuntu 22.04.5 LTS   6.6.87.2-microsoft-standard-WSL2   docker://28.1.1        
Here we can see external IP is null so lets setup a IP.
kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.8.3/manifests/metallb.yaml        

  • Access service on browser with internal/external ip.

http://192.168.49.2:31452/        

  • Expose API service to load balance port service so it can be accessised outside of cluster(on the browser).

C:\Users\SandeepPachauri>kubectl expose deployment/demo-deployment --name=demo-service-lb --port=80 --target-port=80 --type=LoadBalancer

service/demo-service-lb exposed        

  • Edit pod in notepad/text editor.

C:\Users\SandeepPachauri>kubectl edit pod demo-deployment-7558778d8f-7skrm        

  • Download pod configuration in local machine as yaml.

C:\Users\SandeepPachauri>kubectl get pod demo-deployment-7558778d8f-7skrm -o yaml > prod.yaml        

  • Edit deployment:

C:\Users\SandeepPachauri>kubectl edit deployment demo-deployment        

  • Cluster details.

C:\Users\SandeepPachauri>kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: C:\Users\SandeepPachauri\.minikube\ca.crt
    extensions:
    - extension:
        last-update: Wed, 13 Aug 2025 16:49:47 IST
        provider: minikube.sigs.k8s.io
        version: v1.36.0
      name: cluster_info
    server: https://127.0.0.1:52589
  name: minikube
contexts:
- context:
    cluster: minikube
    extensions:
    - extension:
        last-update: Wed, 13 Aug 2025 16:49:47 IST
        provider: minikube.sigs.k8s.io
        version: v1.36.0
      name: context_info
    namespace: default
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: C:\Users\SandeepPachauri\.minikube\profiles\minikube\client.crt
    client-key: C:\Users\SandeepPachauri\.minikube\profiles\minikube\client.key        

  • Get clusters details connected.

C:\Users\SandeepPachauri>kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
*         minikube   minikube   minikube   default        

  • Get all services/deployment running in cluster

C:\Users\SandeepPachauri>kubectl get all
NAME                                   READY   STATUS    RESTARTS      AGE
pod/demo-deployment-7558778d8f-7skrm   1/1     Running   1 (18m ago)   147m
pod/demo-deployment-7558778d8f-c74gb   1/1     Running   1 (18m ago)   110m
pod/demo-deployment-7558778d8f-hpmc7   1/1     Running   0             6m40s
pod/demo-deployment-7558778d8f-lkglw   1/1     Running   1 (18m ago)   78m
pod/demo-deployment-7558778d8f-rqr7v   1/1     Running   1 (18m ago)   147m

NAME                      TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/demo-service      ClusterIP      10.111.65.33    <none>        80/TCP         105m
service/demo-service-lb   LoadBalancer   10.107.127.0    <pending>     80:30101/TCP   87m
service/demo-service-np   NodePort       10.96.138.119   <none>        80:31452/TCP   98m
service/kubernetes        ClusterIP      10.96.0.1       <none>        443/TCP        156m

NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/demo-deployment   5/5     5            5           147m

NAME                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/demo-deployment-7558778d8f   5         5         5       147m        

To view or add a comment, sign in

More articles by Sandeep Pachauri

Explore content categories