Kubernetes

Basic Architecture

minikube - the cluster manager

kubectl - CLI to interact with the cluster manager


Developing a microservice

Note

This tutorial shows you how to run a simple Hello World Node.js app on Kubernetes using Minikube and Katacoda. Katacoda provides a free, in-browser Kubernetes environment.

You ca find it at https://kubernetes.io/docs/tutorials/hello-minikube/

When you start minikube using the command minikube start, it will create a cluster of 1.

Kubernetes Deployment

A Kubernetes Pod is a group of one or more Containers, tied together for the purposes of administration and networking. The Pod in the online tutorial has only one Container.  A Kubernetes Deployment checks on the health of your Pod and restarts the Pod’s Container if it terminates. Deployments are the recommended way to manage the creation and scaling of Pods.

Use the kubectl create command to create a Deployment that manages a Pod. The Pod runs a Container based on the provided Docker image.

shell command
kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node

Use the dashboard to view the changes that have taken place.

  • gcr.io is the google container registry, it is similar to hub.docker.com.
  • hello-minikube-zero-install/hello-node is the name of the image within the register
  • hello-minikube-zero-install/hello-node is the owner of the image within the register
  • hello-world after the sub-command deployment is the name of the deployment that will be created

NOTE - if you want to work with the Docker registry see https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/ 

Creating a Service

By default, the Pod is only accessible by its internal IP address within the Kubernetes cluster. To make the hello-node Container accessible from outside the Kubernetes virtual network, you have to expose the Pod as a Kubernetes Service.

shell command
kubectl expose deployment hello-node --type=LoadBalancer --port=8080

Run the command minikube service list to get a list of available services.  When you do this the hello-world service should have a valid URL you can enter into your browser.

Note

When you use --expose switch, a Service is created at the same time as you create a Deployment


Deploying an application into Kubernetes Cluster

  1. begin by installing minikube - download the windows installer, tried using choco but didn't work (mmmmm)
  2. run the command - minikube start - this will configure docker to work with minikube, caveat: wanted VBox and wouldn't work with HyperV
    1. read the messages carefully, on Windows you must still install kubectl manually even though the minikube start command says it downloaded and configure kubectl to work with Docker
  3. run the command choco install kubernetes-cli
  4. run the command refreshenv to enable kubectl without having to shutdown and reopen the shell

Once minikube is running, it has a really cool dashboard that can be used to administer it.  The dashboard can be triggered by using the following command minikube dashboard, this will lock the terminal.  When it runs, it will automatically open the dashboard using your default browser.

Most of the commands that you can execute from the shell can be done from the dashboard

Running an application

  1. kubectl version
  2. kubectl get version
  3. kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
  4. kubectl get deployments

The above steps is similar to what happens with docker run but pointing to an image that is in the remote repo (download the image and then run a container)

Use kubectl get pods to see the new pod.  Copy the pod name.

Use the command kubectl get -o json pob <pod name> to see the pod details.

Viewing the app

  1. In another terminal run the proxy with the command - kubectl proxy
    1. this will lock the terminal from which the command is run
  2. Go back to another terminal 
  3. curl http://localhost:8001/version

When the proxy is started, it reports back in the terminal the IP and Port it is running on.  Enter these details into your browser to see all the service endpoints that minikube has created


On Linux/UNIX

  1. export POD_NAME=$(kubectl get pods -o go-template --template '{{range .item}}{{.metadata.name}}{{"\n"}}{{end}}')
  2. echo $POD_NAME
  3. curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/

On Windows

  1. $Env:POD_NAME=$(kubectl get pods -o go-template --template '{{range .item}}{{.metadata.name}}{{end}}')
  2. echo $Env:POD_NAME
  3. curl http://localhost:8001/api/v1/namespaces/default/pods/$Env:POD_NAME/proxy/