Kubernetes Basics - Essential Commands

Working with Services, Deployments, and Pods

Kubernetes Configuration Files

Download the YAML configuration files for this exercise

Deploy the Service

kubectl apply -f clusterip.yaml

Creates a ClusterIP service to expose the application internally within the cluster.

Deploy the App

kubectl apply -f deploy-app.yaml

Deploys the Nginx application with 3 replicas using a Deployment controller.

Deploy Busybox

kubectl apply -f pod.yaml

Creates a BusyBox pod that we'll use to test connectivity to our service.

Get the Pods List

kubectl get pods -o wide

Lists all pods with additional details including IP addresses and nodes.

Connect to the BusyBox Container

kubectl exec mybox -it -- /bin/sh

Opens an interactive shell session inside the BusyBox container.

Test the Service

wget -qO- http://svc-example:8080

Tests connectivity to the Nginx service from within the BusyBox container.

Exit the Container

exit

Exit the shell session and return to your local terminal.

Cleanup

kubectl delete -f clusterip.yaml
kubectl delete -f deploy-app.yaml
kubectl delete -f pod.yaml --grace-period=0 --force

Deletes all resources created during this exercise.

YAML Configuration File - clusterip.yaml

apiVersion: v1
kind: Service
metadata:
  name: svc-example
spec:
  ports:
  - port: 8080
    targetPort: 80
  selector:
    app: app-example
    env: prod

Service Configuration Explanation:

Service Structure:

  • apiVersion: v1 → Specifies the Kubernetes API version
  • kind: Service → Defines this as a Service resource
  • metadata.name: svc-example → Names the service "svc-example"

Service Port Configuration:

  • port: 8080 → The service will listen on port 8080
  • targetPort: 80 → Forwards traffic to port 80 on the pods

Selector:

  • app: app-example → Selects pods with label "app=app-example"
  • env: prod → Selects pods with label "env=prod"
  • The service will route traffic to all pods matching these labels

Service Type:

  • Since no type is specified, it defaults to ClusterIP
  • ClusterIP services are only accessible within the Kubernetes cluster
  • They provide internal load balancing between pods

YAML Configuration File - deploy-app.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-example
spec:
  replicas: 3
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: app-example
      env: prod
  template:
    metadata:
      labels:
        app: app-example
        env: prod
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80

Deployment Configuration Explanation:

Deployment Structure:

  • apiVersion: apps/v1 → Specifies the Deployment API version
  • kind: Deployment → Defines this as a Deployment resource
  • metadata.name: deploy-example → Names the deployment "deploy-example"

Deployment Configuration:

  • replicas: 3 → Maintains 3 identical pod instances
  • revisionHistoryLimit: 3 → Keeps 3 old ReplicaSets for rollback

Pod Selector:

  • selector.matchLabels → Defines how the Deployment finds which Pods to manage
  • Matches pods with labels app=app-example and env=prod

Pod Template:

  • template.metadata.labels → Labels applied to created pods
  • These must match the selector labels
  • containers.name: nginx → Names the container "nginx"
  • image: nginx:alpine → Uses the lightweight Alpine-based Nginx image
  • containerPort: 80 → Exposes port 80 for web traffic

YAML Configuration File - pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mybox
spec:
  restartPolicy: Always
  containers:
  - name: mybox
    image: busybox
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi    
    command:
      - sleep
      - "3600"

Pod Configuration Explanation:

Pod Structure:

  • apiVersion: v1 → Specifies the Kubernetes API version
  • kind: Pod → Defines this as a Pod resource
  • metadata.name: mybox → Names the pod "mybox"

Pod Configuration:

  • restartPolicy: Always → Restarts the container if it exits
  • Other options are OnFailure and Never

Container Configuration:

  • name: mybox → Names the container "mybox"
  • image: busybox → Uses the lightweight BusyBox image
  • command: ["sleep", "3600"] → Runs the sleep command for 1 hour

Resource Management:

  • requests.cpu: 100m → Requests 0.1 CPU cores
  • requests.memory: 128Mi → Requests 128 MB of memory
  • limits.cpu: 250m → Limits to 0.25 CPU cores
  • limits.memory: 256Mi → Limits to 256 MB of memory
  • These ensure the pod gets adequate resources without starving other pods

Purpose:

This pod runs a simple BusyBox container that sleeps for 1 hour, providing a temporary environment for testing network connectivity and service discovery within the Kubernetes cluster.