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- https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
# NB we add the -xenial (16.04) repository instead of $(lsb_release -cs)
# because kubernetes does not yet have a package for bionic (18.04).
add-apt-repository "deb http://apt.kubernetes.io/ $(lsb_release -cs) main"
apt-get install -y kubectl
kubectl version --client
Launch a kubernetes master (km1
) and a worker (kw1
):
vagrant up km1 kw1
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 run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
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
http http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/ # this accesses an http endpoint inside the pod.
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
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
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