Kubernetes Basics - Part I
Points to cover
- Building Docker Images
- Creating Pods
- Scaling Pods With ReplicaSets
- Using Services To Enable Communication Between Pods
- Deploying Releases With Zero-Downtime
- Using Ingress To Forward Traffic
Prerequisites
This guide borrows heavily from the workshops created by Viktor Farcic.
The examples below are build on top of his example repository, so make sure you clone that.
| git clone https://github.com/vfarcic/k8s-specs.git
cd k8s-specs
|
Build docker images
Docker
BuildKit
Alternatives
Pod
View Yaml
Create
| kubectl create -f pod/db.yml
|
View Pod
View Pod Alternatives
Describe Pod
Update Pod
| kubectl create -f pod/db.yml
|
This should yield an error!
| Error from server (AlreadyExists): error when creating "pod/db.yml": pods "db" already exists
|
The create command is imperative and in this case not idempotent.
To update we would either have to patch the resource in the cluster,
or apply an updated version of the yaml file.
| kubectl apply -f pod/db.yml
|
Enter the Pod
The command below should be executed from inside the pod.
If you get something like command not found: mongo
, double check the above command's success.
| echo 'db.stats()' | mongo localhost:27017/test
|
| kubectl exec -it db pkill mongod
|
Cleanup
| kubectl delete -f pod/db.yml
|
ReplicaSet
View YAML
Create ReplicaSet
| kubectl create -f rs/go-demo-2.yml
|
View ReplicaSet
| kubectl describe -f rs/go-demo-2.yml
|
Which should look something like this, notice the highlighted lines?
| Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 9s replicaset-controller Created pod: go-demo-2-4mw79
Normal SuccessfulCreate 9s replicaset-controller Created pod: go-demo-2-bcv5n
|
View Pods
Delete Pod
| POD_NAME=$(kubectl get pods -o name | tail -1)
kubectl delete $POD_NAME
|
Cleanup
| kubectl delete -f rs/go-demo-2.yml
|
Labels
Create a Pods
| kubectl create -f rs/go-demo-2.yml
kubectl create -f pod/db.yml
|
Get Pods
Get it by Label
| kubectl get pods -l type=backend
|
Get it by multiple labels
| kubectl get pods -l type=backend,language=go
|
Show Labels
| kubectl get pods --show-labels
|
Cleanup
| kubectl delete -f rs/go-demo-2.yml
kubectl delete -f pod/db.yml
|
Service
View
Create
Inspect
| kubectl get -f svc/go-demo-2.yml
|
Get LB IP
Get Port
Call Application
Cleanup
| kubectl delete -f svc/go-demo-2.yml
|
Deployment
| kubectl create -f deploy/go-demo-2.yml --record --save-config
|
| kubectl describe deploy go-demo-2-api
|
Zero downtime deployment
| kubectl set image deploy go-demo-2-api api=vfarcic/go-demo-2:2.0 \
--record
|
| kubectl rollout status -w deploy go-demo-2-api
|
| kubectl describe deploy go-demo-2-api
|
| kubectl rollout history deploy go-demo-2-api
|
Rollback / Rollforward
| kubectl rollout undo deploy go-demo-2-api
|
| kubectl describe deploy go-demo-2-api
|
| kubectl rollout history deploy go-demo-2-api
|
| kubectl rollout undo -f deploy/go-demo-2-api.yml --to-revision=2
|
| kubectl rollout history deploy go-demo-2-api
|
Scaling
| kubectl scale deployment go-demo-2-api --replicas 8 --record
|
Cleanup
| kubectl delete -f deploy/go-demo-2.yml
|
Ingress
Install ingress controller
Confirm ingress works
Info
As we're waiting for the LoadBalancer to be created by the Cloud Provider,
we might have to repeat the command until we get a valid IP address as response.
Ingress based on paths
| cat ingress/go-demo-2.yml
|
| kubectl create -f ingress/go-demo-2.yml --record --save-config
|
| kubectl rollout status deployment go-demo-2-api
|
| curl -i "http://$IP/demo/hello"
|
Ingress based on domains
| cat ingress/devops-toolkit-dom.yml
|
| kubectl apply -f ingress/devops-toolkit-dom.yml --record
|
| kubectl rollout status deployment devops-toolkit
|
| curl -I -H "Host: devopstoolkitseries.com" "http://$IP"
|
| curl -I -H "Host: acme.com" "http://$IP/demo/hello"
|
Ingress with default backends
| curl -I -H "Host: acme.com" "http://$IP"
|
| cat ingress/default-backend.yml
|
| kubectl create -f ingress/default-backend.yml
|
| curl -I -H "Host: acme.com" "http://$IP"
|
Cleanup
| kubectl delete -f ingress/default-backend.yml
kubectl delete -f ingress/devops-toolkit-dom.yml
kubectl delete -f ingress/go-demo-2.yml
|