Kubernetes Basics - Essential Commands

Essential Kubernetes commands for container orchestration

Deployment Example

Download the YAML configuration file for the hello-app deployment

Download hello-deployment.yaml

Create V1 Deployment

kubectl create -f hello-deployment.yaml

Creates the initial deployment using the YAML configuration file.

Get Deployment Status

kubectl rollout status deployment/hello-dep

Checks the rollout status of the deployment.

Get Pods List

kubectl get pods -o wide

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

Describe Pod

kubectl describe pod hello-dep

Shows detailed information about a specific pod.

Get ReplicaSets

kubectl get rs

Lists all ReplicaSets to see how many are created.

Create V2 Deployment

kubectl apply -f hello-deployment.yaml

Applies the updated YAML configuration (after changing version to 2.0).

Get Deployment Status

kubectl rollout status deployment/hello-dep

Checks the rollout status of the deployment.

Get Pods List

kubectl get pods -o wide

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

Get ReplicaSets

kubectl get rs

Lists all ReplicaSets to see how many are created.

Get Deployment History

kubectl rollout history deployment/hello-dep

Shows the rollout history of the deployment.

Rollback Deployment

kubectl rollout undo deployment/hello-dep

Rolls back to the Previous deployment version.

kubectl rollout undo deployment/hello-dep --to-revision 1

Rolls back to the Specific deployment version.

Get Deployment Status

kubectl rollout status deployment/hello-dep

Checks the rollout status of the deployment.

Get ReplicaSets

kubectl get rs

Lists all ReplicaSets to see how many are created.

Cleanup

kubectl delete -f hello-deployment.yaml

Deletes all resources defined in the YAML configuration file.

YAML Configuration File

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-dep
  namespace: default
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  selector:
    matchLabels:
      app: hello-dep
  template:
    metadata:
      labels:
        app: hello-dep
    spec:
      containers:
      - image: guybarrette/hello-app:1.0
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi      
        imagePullPolicy: Always
        name: hello-dep
        ports:
        - containerPort: 8080

YAML Configuration Explanation:

Deployment Structure:

  • apiVersion: apps/v1 → Specifies the Kubernetes API version for Deployments
  • kind: Deployment → Defines this as a Deployment resource
  • metadata.name: hello-dep → Names the deployment "hello-dep"
  • namespace: default → Places the deployment in the default namespace

Deployment Spec:

  • replicas: 3 → Maintains 3 identical pod instances
  • strategy.type: RollingUpdate → Uses rolling update for deployments
  • rollingUpdate.maxSurge: 1 → Allows 1 extra pod during update
  • rollingUpdate.maxUnavailable: 1 → Allows 1 pod to be unavailable during update
  • selector.matchLabels.app: hello-dep → Selects pods with label app=hello-dep

Pod Template:

  • metadata.labels.app: hello-dep → Labels the pods with app=hello-dep
  • spec.containers → Defines the container specifications

Container Configuration:

  • image: guybarrette/hello-app:1.0 → Uses version 1.0 of the hello-app image
  • resources.requests.cpu: 100m → Requests 0.1 CPU cores
  • resources.requests.memory: 128Mi → Requests 128MB of memory
  • resources.limits.cpu: 250m → Limits to 0.25 CPU cores
  • resources.limits.memory: 256Mi → Limits to 256MB of memory
  • imagePullPolicy: Always → Always pulls the image when starting a pod
  • name: hello-dep → Names the container "hello-dep"
  • containerPort: 8080 → Exposes port 8080 on the container

How It Works:

This deployment creates and manages 3 replicas of a pod running the hello-app container. The RollingUpdate strategy ensures zero-downtime deployments by gradually replacing old pods with new ones. The resource requests and limits help Kubernetes schedule the pods appropriately and prevent resource exhaustion.