This is a kubeadm created kubernetes playground wrapped in a vagrant environment.
Install the Ubuntu Base Box.
Install kublectl in your machine, e.g. on Ubuntu:
wget -qO /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo 'deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main' >/etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubectl
kubectl version --client
Launch the pandora box, a kubernetes master (km1
) and a worker (kw1
):
vagrant up pandora km1 kw1
Add the following entries to your /etc/hosts
file:
10.11.0.3 k8s.example.test
Access the HAProxy status page at:
Launch the kubernetes api server proxy in background:
export KUBECONFIG=$PWD/tmp/admin.conf
kubectl proxy &
Then access the kubernetes dashboard at:
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
select Token
and use the token from the tmp/admin-token.txt
file.
NB See the full Kubernetes Basics tutorial.
Install httpie:
apt-get install -y httpie
Launch an example Deployment and wait for it to be up:
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl get deployments
kubectl rollout status deployment kubernetes-bootcamp
Access it by http through the kubernetes proxy:
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep kubernetes-bootcamp)
echo "Name of the Pod: $POD_NAME"
# get the pod definition:
http http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME
# access the pod http endpoint at port 8080:
http http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/
Execute a command inside the pod:
kubectl exec $POD_NAME -- env
kubectl exec $POD_NAME -- curl --verbose --silent localhost:8080
Launch an interactive shell inside the pod:
kubectl exec -ti $POD_NAME -- bash
Show information about the pod:
kubectl describe pod $POD_NAME
Expose the pod as a kubernetes Service that can be accessed at any kubernetes node:
kubectl expose deployment kubernetes-bootcamp --type NodePort --port 8080
kubectl get services
kubectl describe service kubernetes-bootcamp
Access the Service through the kw1
worker node:
export NODE_PORT=$(kubectl get service kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
http http://10.11.0.201:$NODE_PORT
You can also access the service trought the kubectl proxy
proxy:
http http://localhost:8001/api/v1/namespaces/default/services/kubernetes-bootcamp:8080/proxy/
Create multiple instances (aka replicas) of the application:
kubectl scale deployments/kubernetes-bootcamp --replicas=4
kubectl rollout status deployment kubernetes-bootcamp
kubectl get deployments
kubectl get pods -o wide
kubectl describe deployment kubernetes-bootcamp
kubectl describe service kubernetes-bootcamp
kubectl get endpoints kubernetes-bootcamp -o yaml
And access the Service multiple times to see the request being handled by different containers:
http http://10.11.0.201:$NODE_PORT
http http://10.11.0.201:$NODE_PORT
http http://10.11.0.201:$NODE_PORT
Upgrade the application version:
kubectl set image deployment kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
kubectl rollout status deployment kubernetes-bootcamp # wait for rollout.
kubectl describe pods # see the image.
http http://10.11.0.201:$NODE_PORT # hit it again, now to see v=2.
Remove the application:
kubectl delete deployment kubernetes-bootcamp
kubectl delete service kubernetes-bootcamp
kubectl get all