☸️
Container Platform

kubectl

Kubernetes command-line tool for cluster management

🐧 Linux🍎 Mac🪟 Windows📋 Quick reference →
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

If you're on Kubernetes 1.27 or older

  • Ingress: networking.k8s.io/v1 is required — v1beta1 removed in 1.22+
  • Pod Security: PodSecurityPolicy removed in 1.25 — use Pod Security Admission (PSA) labels
  • HPA v2 autoscaling/v2 is stable — check API version in manifests

If you're on Kubernetes 1.28

  • Sidecar containers (1.29+) change init-container ordering — review sidecar docs before upgrade
  • Verify metrics-server and HPA after control plane bump

What is this?

kubectl is the remote control for Kubernetes — it tells your cluster what to run.

📁

Config files for kubectl

Where to create or edit the main configuration — paths below match the setup steps.

  • ~/.kube/config

    Location: User home (or $KUBECONFIG)

    Cluster connection and namespace context

📥

Step 01

Install kubectl

(01)Install kubectl

Linux
1# Ubuntu / Debian
2curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
3sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
4
5# Or via package manager
6sudo apt-get update && sudo apt-get install -y kubectl
7
8# RHEL / Fedora
9sudo dnf install -y kubectl

(02)Configure kubeconfig

Point kubectl at your cluster configuration file.

Linux
1# Default location
2mkdir -p ~/.kube
3cp /path/to/admin.conf ~/.kube/config
4chmod 600 ~/.kube/config
5
6# Or set explicitly
7export KUBECONFIG=~/.kube/config

Step 02

Verify Installation

(01)Check Version

Linux
1kubectl version --client
2kubectl cluster-info

(02)Test Cluster Access

Linux
1kubectl get nodes
2kubectl get pods -A

Step 03

Manage Kubernetes

(01)Pod Operations

Linux
1kubectl get pods
2kubectl describe pod <name>
3kubectl logs <pod>
4kubectl exec -it <pod> -- /bin/sh
5kubectl delete pod <name>

(02)Deployment Operations

Linux
1kubectl get deployments
2kubectl apply -f deployment.yaml
3kubectl scale deployment <name> --replicas=3
4kubectl rollout status deployment/<name>
5kubectl rollout undo deployment/<name>

(03)Service & Namespace

Linux
1kubectl get svc
2kubectl get ns
3kubectl create namespace dev
4kubectl config set-context --current --namespace=dev
5kubectl port-forward svc/<name> 8080:80

📋Config templates

18 YAML templates for kubectl. Copy and deploy after setup.

18 ready-to-copy templates. Expand one, copy the YAML, then run the deploy commands.

deployment.yml

Deployment with resource requests/limits and local image

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 labels:
5 app: api-deploy
6 name: api-deploy
7spec:
8 replicas: 1
9 selector:
10 matchLabels:
11 app: api-deploy
12 template:
13 metadata:
14 labels:
15 app: api-deploy
16 spec:
17 containers:
18 - image: api:local
19 name: api
20 imagePullPolicy: IfNotPresent
21 ports:
22 - containerPort: 3000
23 resources:
24 limits:
25 memory: "512Mi"
26 cpu: "500m"
27 requests:
28 memory: "256Mi"
29 cpu: "250m"

services.yml

NodePort Service exposing the API on port 32000

yaml
1apiVersion: v1
2kind: Service
3metadata:
4 labels:
5 app: api-deploy
6 name: api-deploy-service
7spec:
8 type: NodePort
9 selector:
10 app: api-deploy
11 ports:
12 - port: 3000
13 protocol: TCP
14 targetPort: 3000
15 nodePort: 32000
📄

Step 01

Apply to Kubernetes

(01)Build image and load into cluster

Linux
1# Build local image
2docker build -t api:local .
3
4# For Minikube / Kind — load image into cluster
5minikube image load api:local
6# OR: kind load docker-image api:local

(02)Apply manifests

Linux
1kubectl apply -f deployment.yml
2kubectl apply -f services.yml
3kubectl get pods,svc
4curl http://localhost:32000 # or minikube service api-deploy-service