Kubernetes
Kubernetes schedules containers across a cluster and keeps the declared state running. Docker builds and publishes the images; Kubernetes pulls those images and manages Pods, Deployments, Services, ConfigMaps, Secrets, storage, rollout history, and health checks.
Install tools
Install kubectl and choose a local cluster.
Start a local minikube cluster with the Docker driver.
1minikube start --driver=docker
Check the cluster.
1kubectl version --client
2kubectl cluster-info
3kubectl get nodes
Build and load local images
For local Kubernetes, either push images to a registry or load local images into the cluster.
1docker build -t student-rest:local ./flask
2minikube image load student-rest:local
With kind, load the image into the named cluster.
1kind load docker-image student-rest:local --name kind
Deployment and Service
A Deployment manages replicated Pods. A Service gives those Pods a stable network name and virtual IP.
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: student-rest
5spec:
6 replicas: 2
7 selector:
8 matchLabels:
9 app: student-rest
10 template:
11 metadata:
12 labels:
13 app: student-rest
14 spec:
15 containers:
16 - name: rest
17 image: student-rest:local
18 imagePullPolicy: IfNotPresent
19 ports:
20 - containerPort: 5000
21 readinessProbe:
22 httpGet:
23 path: /
24 port: 5000
25 livenessProbe:
26 httpGet:
27 path: /
28 port: 5000
29 resources:
30 requests:
31 cpu: 100m
32 memory: 128Mi
33 limits:
34 cpu: 500m
35 memory: 512Mi
36---
37apiVersion: v1
38kind: Service
39metadata:
40 name: student-rest
41spec:
42 selector:
43 app: student-rest
44 ports:
45 - port: 80
46 targetPort: 5000
Apply the manifest and inspect the workload.
1kubectl apply -f student-rest.yaml
2kubectl get deployments
3kubectl get pods
4kubectl get services
5kubectl rollout status deployment/student-rest
Forward the Service to the local machine.
1kubectl port-forward service/student-rest 8080:80
Open http://localhost:8080.
Rollouts
Deploy a new image by changing the image in the Deployment.
1kubectl set image deployment/student-rest rest=student-rest:next
2kubectl rollout status deployment/student-rest
3kubectl rollout history deployment/student-rest
4kubectl rollout undo deployment/student-rest
Configuration and secrets
Use ConfigMaps for non-secret configuration.
1kubectl create configmap student-config \
2 --from-literal=APP_ENV=local \
3 --dry-run=client -o yaml > configmap.yaml
Use Secrets for sensitive values, then mount or expose them to Pods through Kubernetes. Production clusters should integrate with the cloud provider’s secret manager when available.
1kubectl create secret generic db-credentials \
2 --from-literal=username=student \
3 --from-literal=password='change-me' \
4 --dry-run=client -o yaml > secret.yaml
Pods and sidecars
A Pod is the smallest Kubernetes scheduling unit. Put multiple containers in one Pod only when they must share lifecycle, network namespace, and storage. A frontend, API, and database should usually be separate Deployments and Services. Sidecars, log shippers, and local proxies are better examples of multi-container Pods.
Operations
These commands are used constantly.
1kubectl config get-contexts
2kubectl config use-context minikube
3kubectl create namespace docker-book
4kubectl get all -n docker-book
5kubectl describe pod <pod-name> -n docker-book
6kubectl logs deployment/student-rest -n docker-book
7kubectl exec -it deployment/student-rest -n docker-book -- sh
8kubectl delete -f student-rest.yaml
Using local images
The older eval $(minikube docker-env) workflow points the local Docker CLI at the minikube Docker daemon. Prefer minikube image load for new local workflows because it is explicit and works naturally with Buildx-built images.
1minikube image load student-rest:local
Useful minikube commands:
minikube dashboardopens the Kubernetes dashboard.minikube sshopens a shell on the minikube node.minikube service <service-name>opens a Service exposed through minikube.kubectl get events --sort-by=.metadata.creationTimestampshows recent cluster events.
Removing minikube cluster
Stop minikube. This command effectively halts the virtual machine.
1minikube stop
Delete minikube. This command effectively deletes the virtual machine.
1minikube delete