My take on Kubernetes
My learnings along the way to Kubernetes heaven.
Masters and Nodes
Machines in a Kubernetes cluster are referred to as Nodes. A Kubernetes cluster contains two types of Nodes:
- Master Nodes
- Worker Nodes
Usually, Master Nodes are referred to as “Masters” and Worker Nodes are called “Nodes”. Maters host the control plane and Nodes are where you run user applications.
Breaking down the Dockerfile
# use the node:current slim image as the baseFROM node:current-slim# copy the application and dependencies from the current directory# into the /src directoryCOPY . /srcRUN cd /src; npm install# informs Docker that the container listens on the specified network ports# at runtimeEXPOSE 8080CMD cd /src && node ./app.js
The process of building an application into a container image is called containerisation.
docker image build -t andrewchaa/qsk-course:1.0 .docker image ls
Host the image on a registry
docker login --username andrewchaadocker image push andrewchaa/qsk-course:1.0
Deploying the first application
# nginx.yamlapiVersion: v1kind: Podmetadata:name: nginxspec:containers:- name: nginx-containerimage: registry.hub.docker.com/library/nginx
kubectl apply -f nginx.yaml# port forward to open the podkubectl port-forward --address 0.0.0.0 nginx 3000:80kubectl get pods# stream logskubectl logs --follow nginx
Where our applications actually run.
In the reconciliation loop, the master compares the current state with the desired state and makes the necessary changes
Difference between pods and containers
Multiple containers can run inside a single pod. A way to group containers
Executing commands in a container running in the pod
kubectl exec nginx -- lskubectl exec -it nginx -- bashkubectl exec -it apache -- shroot@nginx:/#
kubectl delete pod nginxkubectl delete -f nginx.yamlkubectl get pods
- Scale the applications by increasing or decreasing the number of replicas
- Roll out new versions of the application without downtime
- Easily rollback bad releases
apiVersion: apps/v1kind: Deploymentmetadata:name: hellok8s # unique name for the deploymentspec:replicas: 1selector: # manage all the pods tha have a label called app withmatchLabels: # the value hellok8sapp: hellok8stemplate:metadata:labels:app: hellok8sspec:containers:- image: brianstorti/hellok8s:v1name: hellok8s-container
kubectl apply -f deployment.yaml# deployment.apps/hellok8s createdkubectl get deployments# NAME DESIRED CURRENT UP-TO-DATE AVAILABLE# hellok8s 1 1 1 1kubectl get pods# NAME READY STATUS RESTARTS# hellok8s-6678f66cb8-42jtr 1/1 Running 0kubectl port-forward hellok8s-78997b6f8f-zwv62 --address 0.0.0.0 3004567
Controlling the Rollout Rate
maxSurge: define how many pods we can have exceeding our desired
replicacount. The default value is 25%
maxUnavailable: defines how many pods we can have below the desired count
spec:strategy:rollingUpdate:maxSurge: 1maxUnavailable: 1
Probing bad releases
readinessProbe: make the container ready to start receiving requests only after it has received a specified number of successful responses
spec:spec:containers:- image: brianstorti/hellok8s:buggy # Adding a buggy versionname: hellok8s-containerreadinessProbe:periodSeconds: 1successThreshold: 5httpGet:path: /port: 4567
livenessProbe: Kubernetes will keep calling this probe periodically to make sure the pod is healthy.
spec:spec:containers:- image: brianstorti/hellok8s:v2name: hellok8s-containerreadinessProbe:periodSeconds: 1successThreshold: 5httpGet:path: /port: 4567livenessProbe:httpGet:path: /port: 4567