kubectl Essentials

Loading concept...

kubectl Essentials: Your Magic Remote Control for Kubernetes 🎮


The Story: Your Spaceship Control Center

Imagine you’re the captain of a giant spaceship (that’s Kubernetes!). Your spaceship has hundreds of little robots working inside—some serve food, some clean, some guard the doors.

But how do you talk to all these robots? You use a magic remote control called kubectl (say it like “kube-control” or “kube-cuddle” — both work!).

This remote control lets you:

  • See what all your robots are doing
  • Tell robots to start working
  • Tell robots to stop
  • Check if robots are healthy

And the instruction manual for your robots? That’s written in a special language called YAML.

Let’s learn how to use your magic remote control!


1. kubectl Fundamentals: Meet Your Remote Control

What is kubectl?

kubectl is a command-line tool. It’s how you talk to your Kubernetes cluster.

Think of it like this:

  • You = The spaceship captain
  • kubectl = Your walkie-talkie/remote control
  • Kubernetes = Your spaceship with all its robots

How Does It Work?

When you type a kubectl command, it:

  1. Reads your instruction
  2. Sends it to the Kubernetes API server (the spaceship’s brain)
  3. The brain tells the robots what to do
  4. You get a message back about what happened
graph TD A[You type kubectl command] --> B[kubectl sends to API Server] B --> C[API Server processes request] C --> D[Changes happen in cluster] D --> E[You see the result]

Basic Command Structure

Every kubectl command follows this pattern:

kubectl [action] [resource] [name] [flags]

Example:

kubectl get pods my-app
  • kubectl = The tool
  • get = The action (what you want to do)
  • pods = The resource (what type of thing)
  • my-app = The name (which specific one)

2. kubectl Resource Commands: Types of Robots

What Are Resources?

Resources are the different types of things in your Kubernetes spaceship.

Resource What It Is Everyday Example
pod Smallest worker unit One robot doing one job
deployment Group of pods A team of identical robots
service Network access point A phone number for robots
configmap Configuration data Robot instruction manual
secret Sensitive data Robot’s secret password

Common Resource Types

Pods — The smallest workers

kubectl get pods

Deployments — Teams of workers

kubectl get deployments

Services — How to reach workers

kubectl get services

Nodes — The machines running everything

kubectl get nodes

Quick Tip: Shortnames

Tired of typing long names? Use shortcuts!

Full Name Shortname
pods po
services svc
deployments deploy
configmaps cm
namespaces ns
# Both do the same thing!
kubectl get pods
kubectl get po

3. kubectl get and describe: Spying on Your Robots

kubectl get: The Quick Peek đź‘€

Use get when you want a quick list of what’s happening.

See all pods:

kubectl get pods

Output looks like:

NAME        READY   STATUS    AGE
my-app-1    1/1     Running   2h
my-app-2    1/1     Running   2h

See more details (wide view):

kubectl get pods -o wide

See as YAML:

kubectl get pod my-app-1 -o yaml

See as JSON:

kubectl get pod my-app-1 -o json

kubectl describe: The Deep Dive 🔍

Use describe when something’s wrong and you need all the details.

kubectl describe pod my-app-1

This shows you:

  • When the pod was created
  • What container it’s running
  • Events (what happened to it)
  • Why it might be failing

Real Example Output:

Name:         my-app-1
Namespace:    default
Status:       Running
IP:           10.1.0.5
Events:
  Type    Reason   Message
  ----    ------   -------
  Normal  Pulled   Container image pulled
  Normal  Started  Container started

When to Use Which?

graph TD A[Need info about resources?] --> B{Quick overview?} B -->|Yes| C[kubectl get] B -->|No| D{Need details?} D -->|Yes| E[kubectl describe] D -->|Troubleshooting?| E
Situation Use This
“Show me all pods” kubectl get pods
“Why is my pod crashing?” kubectl describe pod name
“List all services” kubectl get services
“What events happened?” kubectl describe

4. kubectl create and apply: Building New Robots

Two Ways to Create Things

Think of it like ordering food:

  • create = “Make me a burger, exactly like this, right now!”
  • apply = “Here’s my order. Make it if it doesn’t exist, or update it if it does.”

kubectl create: One-Time Creation

# Create a deployment right now
kubectl create deployment my-app \
  --image=nginx
# Create from a file (one time only)
kubectl create -f my-pod.yaml

⚠️ Warning: If you run create twice, it fails! It says “already exists!”

kubectl apply: The Smart Way

# Create OR update from a file
kubectl apply -f my-pod.yaml

Why apply is awesome:

  • Run it 100 times, no errors
  • Updates existing resources
  • Perfect for automation

Example workflow:

# First time - creates the pod
kubectl apply -f my-pod.yaml

# Changed something? Run again - updates it!
kubectl apply -f my-pod.yaml

Quick Creation Commands

Create a deployment:

kubectl create deployment nginx \
  --image=nginx

Create a service:

kubectl create service clusterip my-svc \
  --tcp=80:80

Create a namespace:

kubectl create namespace my-space

5. kubectl delete: Saying Goodbye to Robots

Basic Deletion

# Delete a specific pod
kubectl delete pod my-app-1
# Delete a deployment
kubectl delete deployment my-app
# Delete from a file
kubectl delete -f my-pod.yaml

Delete Multiple Things

# Delete all pods in namespace
kubectl delete pods --all
# Delete by label
kubectl delete pods -l app=my-app

Force Delete (Be Careful!)

Sometimes a pod is stuck. Force it to stop:

kubectl delete pod stuck-pod \
  --grace-period=0 \
  --force

⚠️ Warning: Force delete doesn’t wait for cleanup. Use only when necessary!

What Happens When You Delete?

graph TD A[kubectl delete pod] --> B[Pod gets signal to stop] B --> C[Pod has 30 seconds to finish] C --> D[Pod shuts down gracefully] D --> E[Pod is removed]

6. YAML Manifests: The Robot Instruction Manual

What is YAML?

YAML is a way to write instructions that both humans and computers can read.

Think of it as a recipe card for your robots:

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: web
    image: nginx
    ports:
    - containerPort: 80

The Four Must-Have Parts

Every Kubernetes YAML needs these:

Field What It Means Example
apiVersion Which Kubernetes API v1, apps/v1
kind Type of resource Pod, Deployment
metadata Name and labels name: my-app
spec The actual config containers, ports

Simple Pod Example

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
  labels:
    app: hello
spec:
  containers:
  - name: hello-container
    image: nginx:latest
    ports:
    - containerPort: 80

Simple Deployment Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: hello
        image: nginx:latest

YAML Spacing Rules

⚠️ Critical: YAML cares about spaces!

  • Use 2 spaces for indentation (not tabs!)
  • Same level = same number of spaces
  • Lists use - with a space after
# âś… Correct
spec:
  containers:
  - name: web
    image: nginx

# ❌ Wrong (tab instead of spaces)
spec:
	containers:

7. Imperative vs Declarative: Two Ways to Talk to Robots

The Big Difference

Imperative = Tell Kubernetes what to DO (action by action) Declarative = Tell Kubernetes what you WANT (the end result)

Imperative: Step-by-Step Commands

Like giving directions turn by turn:

# Create this deployment now!
kubectl create deployment nginx --image=nginx

# Scale it to 3 replicas now!
kubectl scale deployment nginx --replicas=3

# Update the image now!
kubectl set image deployment/nginx \
  nginx=nginx:1.19

Pros:

  • Quick for one-time tasks
  • Easy to learn

Cons:

  • Can’t track changes
  • Hard to repeat exactly

Declarative: Describe the Goal

Like saying “I want to be at the coffee shop”:

# my-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19

Then apply it:

kubectl apply -f my-deployment.yaml

Pros:

  • Version control friendly (Git!)
  • Repeatable and auditable
  • Self-documenting

Cons:

  • More upfront work

Which Should You Use?

graph TD A[What's your situation?] --> B{Quick test?} B -->|Yes| C[Imperative: kubectl create] B -->|No| D{Production?} D -->|Yes| E[Declarative: YAML + apply] D -->|Team work?| E D -->|Need Git history?| E
Situation Approach Why
Testing something quick Imperative Fast and easy
Production deployment Declarative Trackable, repeatable
Learning/experimenting Imperative See results immediately
Team collaboration Declarative Everyone sees the same config

Best Practice: Combine Both!

# Generate YAML from imperative command
kubectl create deployment nginx \
  --image=nginx \
  --dry-run=client \
  -o yaml > deployment.yaml

# Then edit and apply
kubectl apply -f deployment.yaml

This gives you the speed of imperative commands and the power of declarative files!


🚀 Quick Reference

Essential Commands

What You Want Command
See all pods kubectl get pods
Pod details kubectl describe pod NAME
Create from file kubectl apply -f FILE.yaml
Delete a pod kubectl delete pod NAME
See logs kubectl logs POD
Run command in pod kubectl exec -it POD -- bash

The Golden Rule

When in doubt, use apply! It creates what’s missing and updates what exists.


🎉 You Did It!

You now know how to:

  • âś… Use kubectl to talk to your Kubernetes cluster
  • âś… View and inspect resources with get and describe
  • âś… Create and update resources with create and apply
  • âś… Remove resources with delete
  • âś… Write YAML manifests for your resources
  • âś… Choose between imperative and declarative approaches

Your magic remote control is ready. Go command those robots! 🤖

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.