Skip to main content

Kubernetes Ep 3: kubectl CLI & Launching Your First Pod

Rachmat Hidayat
Author
Rachmat Hidayat
Learn & sharing insights on TypeScript, Go, Kubernetes, DevOps, DevSecOps, SRE, Platform Engineering, AI/ML Engineering, and MLOps.
kubernetes - This article is part of a series.
Part 3: This Article
The kubectl command-line interface is the primary tool platform engineers and developers use to interact with Kubernetes clusters. In this episode, we’ll configure kubectl and deploy our first atomic Kubernetes primitive: the Pod.

TL;DR (Quick Summary)
#

  • kubectl Config (~/.kube/config): Stores cluster API server endpoints, certificates, users, and context definitions.
  • What is a Pod?: The smallest deployable computing unit in Kubernetes. A Pod encapsulates one or more containers sharing network namespaces (localhost IP), storage volumes, and IPC.
  • Imperative vs Declarative:
    • Imperative: kubectl run nginx --image=nginx:alpine (quick debugging / ephemeral tasks).
    • Declarative: kubectl apply -f pod.yaml (version-controlled source of truth).

1. Setting Up kubectl & Understanding Kubeconfig
#

kubectl interacts with the kube-apiserver by reading connection parameters from ~/.kube/config.

# Verify kubectl version
kubectl version --client

# Check cluster connection & node status
kubectl get nodes

Expected Terminal Output:

NAME                 STATUS   ROLES           AGE   VERSION
minikube             Ready    control-plane   2d    v1.30.0
minikube-worker-01   Ready    worker          2d    v1.30.0

Inspecting ~/.kube/config
#

apiVersion: v1
kind: Config
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTi...
    server: https://127.0.0.1:6443
  name: my-cluster
contexts:
- context:
    cluster: my-cluster
    user: admin-user
    namespace: default
  name: dev-context
current-context: dev-context
users:
- name: admin-user
  user:
    client-certificate-data: LS0tLS1CRUdJ...

Essential Context Management Commands
#

# View active context
kubectl config current-context

# List all configured contexts
kubectl config get-contexts

# Switch context
kubectl config use-context dev-context

2. What is a Kubernetes Pod?
#

Unlike Docker where containers run individually, Kubernetes manages containers through Pods.


graph TD
    subgraph Pod["Pod: web-pod (IP: 10.244.1.15)"]
        C1["Main Container
(NGINX Web Server)
Port 80"] C2["Sidecar Container
(Log Shipper)
Reads shared volume"] VOL["('Shared Ephemeral Volume
/var/log/nginx')"] C1 -.-> VOL C2 -.-> VOL C1 <-->|localhost| C2 end
  • Shared Network Namespace: All containers inside the same Pod share an IP address and port space. They communicate with each other over localhost.
  • Shared Storage: Containers in a Pod can mount shared volumes to exchange files.
  • Single Host Guarantee: All containers in a Pod are guaranteed to be scheduled on the exact same physical or virtual worker node.

3. Imperative vs Declarative Operations
#

Imperative Command (Quick One-Liner)
#

Use imperative commands for temporary troubleshooting or quick testing:

kubectl run test-nginx --image=nginx:alpine --port=80

Declarative Manifest (Production Standard)
#

Always use declarative YAML manifests stored in Git repositories for production workloads.

Create pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: my-first-pod
  namespace: default
  labels:
    app: web-server
    environment: production
spec:
  containers:
  - name: nginx-container
    image: nginx:1.25-alpine
    ports:
    - containerPort: 80
      name: http
    resources:
      requests:
        memory: "64Mi"
        cpu: "100m"
      limits:
        memory: "128Mi"
        cpu: "250m"

Apply the manifest:

kubectl apply -f pod.yaml

Expected Terminal Output:

pod/my-first-pod created

4. Inspecting & Debugging Pods
#

1. List Running Pods
#

kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP           NODE
my-first-pod   1/1     Running   0          45s   10.244.1.5   minikube-worker-01

2. Inspect Detailed Metadata & Events
#

If a Pod fails to start, kubectl describe reveals the lifecycle events (e.g., ImagePullBackOff, OutOfMemory):

kubectl describe pod my-first-pod

3. Fetch Container Logs
#

kubectl logs my-first-pod -f

4. Execute Shell inside Pod Container
#

kubectl exec -it my-first-pod -- sh

Inside container terminal:

# Test local HTTP response
wget -qO- http://localhost:80
exit

5. Cleaning Up
#

Delete the Pod declaratively or imperatively:

kubectl delete -f pod.yaml

6. Summary & Next Steps
#

You have configured kubectl and deployed your first Pod! However, running raw Pods directly in production is dangerous: if the node hosting your Pod dies, raw Pods are not rescheduled automatically.

In Episode 04: Deployments, ReplicaSets & Self-Healing Scaling, we will learn how Deployments manage Pod replicas, handle node failures, and perform automated self-healing.

kubernetes - This article is part of a series.
Part 3: This Article