Skip to content

Chapter 13 · Initiate Tutorials

Kubectl and First Deployment

The first rite is simple: connect, declare, observe. Learn the commands that reveal truth.

Install kubectl

Use your OS package manager or the official binary.

Install kubectl using a trusted method for your OS. Then confirm the version:

Verify kubectl

shell

kubectl version --client --output=yaml

Connect to a cluster

Contexts are your keys. Read them carefully.

Your kubeconfig contains one or more contexts. Always confirm which one is active before you change anything.

Check contexts and current context

shell

kubectl config get-contexts
kubectl config current-context

Switch context

shell

kubectl config use-context <context-name>

Create a sample Deployment

A small stateless workload.

This creates a Deployment named web running the nginx image:

Create a deployment

shell

kubectl create deployment web --image=nginx:1.27
kubectl rollout status deployment/web

Then inspect what Kubernetes created:

Inspect deployment and pods

shell

kubectl get deploy,rs,pods -l app=web -o wide

Expose it with a Service

Stable addressing over ephemeral Pods.

Create a ClusterIP Service

shell

kubectl expose deployment web --port=80 --target-port=80 --type=ClusterIP
kubectl get svc web -o wide

If you’re on a local cluster (kind/minikube), you may need port-forwarding to test:

Port-forward

shell

kubectl port-forward svc/web 8080:80

Clean up

Leave the chamber as you found it.

Delete deployment + service

shell

kubectl delete svc web
kubectl delete deployment web