Now we move from understanding Kubernetes → actually controlling Kubernetes.
If Kubernetes is the operating system/manager of your cluster, then:
kubectlis your command-line remote control for Kubernetes.
By the end of this chapter, you should be comfortable with the commands you'll use constantly in real Kubernetes work.
kubectl?#kubectl is the official command-line tool for communicating with the Kubernetes API.
When you run:
kubectl get podsthe flow is approximately:
You
↓
kubectl
↓
Kubernetes API Server
↓
Cluster
↓
Response
↓
kubectl
↓
TerminalRemember Chapter 3:
kubectl → API Server → Kuberneteskubectl does not directly control the containers.
It communicates with the Kubernetes API.
For learning, you need a local Kubernetes cluster.
There are several options:
Easy if you're already using Docker Desktop.
Very popular for learning.
Your Computer
↓
Minikube
↓
Kubernetes ClusterKind = Kubernetes IN Docker
It runs Kubernetes nodes as Docker containers.
For this course, we'll use the concepts in a way that works across local Kubernetes environments.
kubectl#Run:
kubectl version --clientYou should see the client version.
Then:
kubectl cluster-infoIf your cluster is running, you'll see information about the Kubernetes control plane.
kubectl get#The most commonly used command is:
kubectl getIt asks Kubernetes:
"Show me resources."
For example:
kubectl get podsYou might get:
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2mYou already learned this in Chapter 1.
For now:
Pod
└── ContainerSo:
kubectl get podsmeans:
Show me the Pods in the current namespace.
kubectl get nodes#To see your Kubernetes machines:
kubectl get nodesExample:
NAME STATUS ROLES AGE VERSION
node-1 Ready control-plane 10m v1.xx.x
node-2 Ready <none> 10m v1.xx.xThe important column is:
STATUSYou generally want:
Readykubectl get all#You can get several common resources using:
kubectl get allYou might see:
NAME READY STATUS
pod/nginx 1/1 Running
NAME TYPE CLUSTER-IP
service/nginx ClusterIP 10.x.x.x
NAME READY
deployment.apps/nginx 1/1
NAME DESIRED
replicaset.apps/nginx-xxxxx 1Don't worry about all these resource types yet.
We'll learn them individually.
Let's create a Pod using an image.
kubectl run nginx --image=nginxThis means approximately:
Create a Pod named
nginxusing thenginxcontainer image.
Now:
kubectl get podsYou should see something like:
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 10sCongratulations — you've created your first Kubernetes workload.
Look at:
READYIf you see:
1/1it means:
1 container ready
/
1 container expectedFor example:
Runningmeans the Pod is currently running.
Other states you'll eventually encounter:
Pending
Running
Succeeded
Failed
Unknown
CrashLoopBackOff
ImagePullBackOffWe'll learn troubleshooting later.
kubectl describe#This is one of the most important debugging commands.
Run:
kubectl describe pod nginxYou'll get detailed information about the Pod.
You'll see things such as:
Name
Namespace
Node
Status
IP
Containers
Images
Ports
Conditions
Eventsdescribe is so useful#Suppose your Pod is stuck:
STATUS
PendingYou run:
kubectl describe pod nginxAt the bottom you'll often find:
Events:
Warning FailedSchedulingThis can tell you why Kubernetes couldn't schedule the Pod.
So remember:
get
↓
Quick overview
describe
↓
Detailed informationkubectl logs#Your application generates logs.
For example:
console.log("Server started");To see container logs:
kubectl logs nginxFor a Node.js application:
kubectl logs my-apiJust like:
docker logs -fyou can use:
kubectl logs -f nginx-f means:
Follow the logs.
You'll see new log entries as they're generated.
Remember:
Pod
├── Container A
└── Container BIf a Pod has multiple containers, you can specify which container's logs you want:
kubectl logs my-pod -c my-containerkubectl exec#Sometimes you need to enter a running container.
For example:
kubectl exec -it nginx -- /bin/shThis is similar to:
docker exec -it <container> /bin/shYou might then see:
/ #Now you're inside the container.
Try:
lsor:
pwdThen:
exitkubectl delete#To delete the Pod:
kubectl delete pod nginxThen:
kubectl get podsYou might see:
No resources foundImportant: This behavior is different if the Pod is managed by a Deployment.
If a Deployment says:
replicas: 3and you manually delete one Pod:
Pod 1
Pod 2
Pod 3
Delete Pod 2the Deployment controller will create a replacement:
Pod 1
Pod 3
Pod 4That's the reconciliation concept from Chapter 3 in action.
kubectl apply#This is one of the most important commands in real Kubernetes development.
Instead of creating resources manually, you usually define the desired state in YAML.
Example:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginxSave it as:
pod.yamlThen:
kubectl apply -f pod.yamlKubernetes reads your declaration and attempts to make the cluster match it.
This is a very important concept.
You're telling Kubernetes what action to perform.
kubectl run nginx --image=nginxMeaning:
Create this Pod.
You're describing the desired state.
kind: Deployment
spec:
replicas: 3Then:
kubectl apply -f deployment.yamlMeaning:
Make the cluster match this configuration.
Production Kubernetes heavily relies on the declarative approach.
kubectl apply Is Powerful#Suppose your YAML says:
replicas: 3Kubernetes has:
3 PodsYou change:
replicas: 5and run:
kubectl apply -f deployment.yamlKubernetes works toward:
5 PodsYou don't manually create Pod #4 and Pod #5.
You tell Kubernetes the desired state.
kubectl get -o wide#Sometimes you want additional information.
Run:
kubectl get pods -o wideYou might see:
NAME READY STATUS IP NODE
nginx 1/1 Running 10.1.0.5 node-1Now you can see which node the Pod is running on.
kubectl get pods -A#Remember namespaces?
By default:
kubectl get podslooks in your current namespace.
To see Pods across all namespaces:
kubectl get pods -AYou might see:
NAMESPACE NAME
default nginx
kube-system coredns-xxxxx
kube-system kube-proxy-xxxxx-A means:
All namespaces.
You can list namespaces:
kubectl get namespacesor:
kubectl get nsTypical output:
NAME STATUS
default Active
kube-system Active
kube-public Active
kube-node-lease ActiveWe'll study namespaces properly later.
For now:
A namespace is a logical boundary for Kubernetes resources.
-n — Namespace#Suppose your application is in:
productionYou can run:
kubectl get pods -n productionWithout -n, Kubernetes uses the current namespace.
kubectl get deployment#Once we start using Deployments:
kubectl get deploymentsor:
kubectl get deployExample:
NAME READY UP-TO-DATE AVAILABLE
api 3/3 3 3This means your Deployment currently has the desired number of replicas available.
kubectl get replicaset#You can inspect ReplicaSets:
kubectl get replicasetsor:
kubectl get rsWe'll learn why Deployments create ReplicaSets in the next few chapters.
kubectl get services#Services:
kubectl get servicesor:
kubectl get svcExample:
NAME TYPE CLUSTER-IP
api ClusterIP 10.96.100.20Services provide stable networking for Pods.
We'll study them deeply later.
Kubernetes has shortcuts.
Instead of:
kubectl get podsyou can use:
kubectl get poDeployment:
kubectl get deployService:
kubectl get svcReplicaSet:
kubectl get rsNamespace:
kubectl get nsYou'll naturally memorize these with practice.
kubectl explain#This is a fantastic built-in learning tool.
For example:
kubectl explain podYou can go deeper:
kubectl explain pod.specAnd:
kubectl explain pod.spec.containersThis lets you inspect Kubernetes resource schemas from the CLI.
kubectl config#Your kubectl needs to know:
Which Kubernetes cluster should I communicate with?
This information is managed through Kubernetes configuration.
Check your current context:
kubectl config current-contextList contexts:
kubectl config get-contextsExample:
CURRENT NAME
* minikube
production
stagingA context tells kubectl things such as:
Which cluster?
Which user?
Which default namespace?Conceptually:
kubectl
↓
Current Context
↓
Cluster
↓
API ServerThis becomes extremely important when you work with multiple environments.
Imagine:
Local
Staging
ProductionYou definitely don't want to accidentally deploy to production.
kubectl Commands#Start memorizing these:
kubectl get pods
kubectl get nodes
kubectl get deployments
kubectl get servicesDetailed information:
kubectl describe pod <name>
kubectl describe deployment <name>Logs:
kubectl logs <pod>
kubectl logs -f <pod>Execute:
kubectl exec -it <pod> -- /bin/shApply YAML:
kubectl apply -f file.yamlDelete:
kubectl delete -f file.yamlAll namespaces:
kubectl get pods -ACurrent context:
kubectl config current-contextSuppose your application isn't working.
Don't randomly run commands.
Use a systematic process.
kubectl get podsMaybe:
api-abc123 0/1 CrashLoopBackOffkubectl logs api-abc123Maybe you discover:
Error: DATABASE_URL is missingkubectl describe pod api-abc123Look at:
Eventskubectl get deploymentkubectl get serviceThis basic workflow will become second nature.
Remember:
Problem
↓
kubectl get
↓
What's happening?
↓
kubectl describe
↓
Why is it happening?
↓
kubectl logs
↓
What does the application say?A useful rule:
gettells you WHAT.describeoften tells you WHY.logstell you WHAT THE APPLICATION IS SAYING.
You'll notice Kubernetes commands generally follow:
kubectl
↓
verb
↓
resource
↓
optional name/flagsExample:
kubectl get pod nginxkubectl → tool
get → action
pod → resource
nginx → specific resourceAnother:
kubectl delete pod nginxdelete → action
pod → resource
nginx → resource nameKubernetes is heavily declarative.
You'll frequently write:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 3Then:
kubectl apply -f deployment.yamlInstead of memorizing hundreds of commands, you'll learn to define the desired state in YAML.
kubectl get
↓
See resources
kubectl describe
↓
Detailed information
kubectl logs
↓
Application logs
kubectl exec
↓
Enter/run command inside container
kubectl apply
↓
Create/update desired state
kubectl delete
↓
Delete resource
kubectl get -A
↓
All namespaces
kubectl -n <namespace>
↓
Specific namespace
kubectl config
↓
Manage cluster/context configurationIf you have a local Kubernetes cluster, try:
kubectl cluster-infokubectl get nodeskubectl run nginx --image=nginxkubectl get podskubectl describe pod nginxkubectl logs nginxkubectl delete pod nginxkubectl get podsDon't think of kubectl as a tool that directly "runs Docker commands."
Think:
kubectl
↓
Kubernetes API
↓
Control Plane
↓
Desired State
↓
Controllers
↓
Scheduler
↓
Worker Node
↓
kubelet
↓
Container Runtime
↓
ContainerThat's the complete mental model.
Now we'll go deep into Pods, including:
And we'll build several Pods ourselves.
Mark it as complete to track your mastery.