Commands Reference — Kubernetes

2 min

Project projet10-kubernetes-deploiements · all commands to start, test, and operate the cluster.

Table of contents


0. Enable Kubernetes (Docker Desktop)

Docker Desktop → Settings (⚙️) → Kubernetes → ✅ Enable Kubernetes → Apply & Restart.

powershell
kubectl get nodes                 # must show docker-desktop  Ready
kubectl config current-context    # must show docker-desktop
kubectl version --short

Nothing else to install: kubectl is provided with Docker Desktop.


1. Build the image

From the project folder (projet10-kubernetes-deploiements):

powershell
docker build -t demo-k8s:1.0 ./app

With Docker Desktop, the local image is directly visible to the cluster (same Docker daemon). That is why the manifest uses imagePullPolicy: IfNotPresent.


2. Deploy the application

powershell
kubectl apply -f k8s/                 # applies configmap + deployment + service

Or file by file:

powershell
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml

Then open the browser: http://localhost:30080


3. Observe (pods, services, deployments)

powershell
kubectl get all                       # overview
kubectl get pods -o wide              # Pods (IP, node, state)
kubectl get deployment demo-web
kubectl get service demo-web
kubectl describe deployment demo-web  # details + events
kubectl describe pod <nom-du-pod>     # detail of a Pod (probes, image, events)
kubectl get pods --watch             # follow changes live (Ctrl+C to quit)

4. Test load balancing

Reload http://localhost:30080 several times: the Pod name changes.

From the command line (PowerShell):

powershell
1..10 | ForEach-Object { (Invoke-WebRequest -Headers @{Accept="application/json"} http://localhost:30080).Content }

In bash:

bash
for i in $(seq 1 10); do curl -s -H "Accept: application/json" http://localhost:30080; echo; done

5. Operate a Pod (exec, logs, port-forward)

powershell
kubectl logs <nom-du-pod>                      # logs of a Pod
kubectl logs -l app=demo-web --tail=20        # logs of ALL Pods of the label
kubectl exec -it <nom-du-pod> -- sh           # open a shell in the Pod
kubectl exec <nom-du-pod> -- env              # see the injected environment variables
kubectl port-forward svc/demo-web 8080:80     # access the Service via http://localhost:8080

6. Change the configuration (ConfigMap / background)

  1. Edit k8s/configmap.yaml (e.g. BG_COLOR: "#7c3aed").
  2. Re-apply then restart the Pods to reload the variables:
powershell
kubectl apply -f k8s/configmap.yaml
kubectl rollout restart deployment demo-web
kubectl rollout status deployment demo-web
  1. Reload the page: the background has changed.

See the active value in a Pod:

powershell
kubectl exec <nom-du-pod> -- env | Select-String BG_COLOR

7. Scaling, rolling update, rollback

powershell
# Manual scaling
kubectl scale deployment demo-web --replicas=5
kubectl get pods                              # 5 Pods now
kubectl scale deployment demo-web --replicas=3

# Rolling update: change APP_VERSION in the ConfigMap then
kubectl apply -f k8s/configmap.yaml
kubectl rollout restart deployment demo-web
kubectl rollout status deployment demo-web    # follows the progressive deployment

# History and rollback
kubectl rollout history deployment demo-web
kubectl rollout undo deployment demo-web      # goes back to the previous version

8. Self-healing

powershell
kubectl get pods
kubectl delete pod <nom-du-pod>               # we delete one by hand
kubectl get pods --watch                      # Kubernetes recreates one automatically

9. Cleanup

powershell
kubectl delete -f k8s/                        # deletes service + deployment + configmap
# or individually:
kubectl delete service demo-web
kubectl delete deployment demo-web
kubectl delete configmap demo-config

Appendix A — minikube / kind

If you do not use Docker Desktop Kubernetes, the local image is not visible to the cluster: you must load it.

minikube:

powershell
minikube start
minikube image load demo-k8s:1.0             # makes the image available in the cluster
kubectl apply -f k8s/
minikube service demo-web --url              # gets the access URL

kind:

powershell
kind create cluster
kind load docker-image demo-k8s:1.0          # loads the image into the cluster
kubectl apply -f k8s/
kubectl port-forward svc/demo-web 8080:80    # http://localhost:8080

Appendix B — troubleshooting

SymptomLikely causeSolution
ErrImagePull / ImagePullBackOffThe cluster cannot find the imageCheck imagePullPolicy: IfNotPresent + image demo-k8s:1.0 built; on minikube/kind, load the image (Appendix A)
Pod in CrashLoopBackOffThe app crashes at startupkubectl logs <pod> to read the error
Pod PendingInsufficient resourceskubectl describe pod <pod> (Events section)
localhost:30080 unreachableService not created / wrong portkubectl get svc; otherwise kubectl port-forward svc/demo-web 8080:80
kubectl does not answerWrong contextkubectl config use-context docker-desktop
Probe that fails/health not reachableCheck port 5000 and the /health route
powershell
# Quick diagnosis
kubectl get events --sort-by=.lastTimestamp
kubectl describe pod <nom-du-pod>

Course created by Dr. Haythem REHOUMA — Development and Deployment of Data Solutions