Shirone's Blog

Try on k8s!

I’ve been hoping to learn k8s in my free time, but I don’t have much free time and k8s requires very complicated preparation to learn, so I’ve been putting it off. But today I just couldn’t wait any longer, so let’s get started.

To try and learn k8s, we need to install minikube. Minikube is a tool that enables users to run a single-node Kubernetes cluster on their local machine for development and testing purposes. It allows users to deploy and manage containerized applications in a local environment, simulating the behavior of a full-scale Kubernetes cluster. On my Mac i can install Minikube through brew:

brew install minikube

We also need to install VirtualBox, which is required by minikube. Find your installation instructions on https://www.virtualbox.org/.

And let’s start minikube, by typing the command:

minikube start

After a while, we can see the following output:

minikube start

Now we can use kubectl to manage our cluster. Kubectl is a command-line tool for controlling Kubernetes clusters. It allows users to deploy applications, inspect and manage cluster resources, and view logs. It can also be used to manage Kubernetes clusters, such as scaling a deployment, initiating a rolling update, and rolling back to an earlier deployment version.

And we can use kubectl to get the cluster information:

kubectl get all
kubectl cluster-info

Output:

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   22d
Kubernetes control plane is running at https://127.0.0.1:56942
CoreDNS is running at https://127.0.0.1:56942/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

As we can see, the cluster is running, but there is only a service named kubernetes, since we deployed nothing yet.

Build a simple docker image contains a web server

Let’s build a simple docker image contains a web server, in order to deploy something on our cluster. The file structure and its content is as follows:

file structure

Here we created a simple flask app, which will return a string “Hello !” when we visit the root path. It will runs on port 8000 as specified.

Before we actually build the docker image, we need to point the docker client on our machine towards the docker daemon in the minikube VM. Then the image will built inside the VM.

eval $(minikube docker-env)

Then we can build the docker image by typing the command. The name of the image is simple-flask.

app docker build . -t simple-flask

After we build the image, we can run it locally to test it.

docker run -p 8000:8000 simple-flask

docker run

Open the browser and visit http://localhost:8000, we can see:

simple-flask

Finally, tag the image with a version number instead of latest.

tag simple-flask:latest simple-flask:1.0.0

Deploy the image on k8s

Now we can deploy the image on k8s. We need to create a deployment and a service. The deployment will create a pod, and the service will expose the pod to the outside world.

kubectl create deployment simple-flask --image=simple-flask:1.0.0
kubectl expose deployment simple-flask --type=NodePort --target-port=8000 --port=8000

We can use kubectl to get the deployment and service information:

kubectl get all
NAME                                READY   STATUS    RESTARTS      AGE
pod/simple-flask-84d4fd67b4-bqhtn   1/1     Running   2 (14m ago)   15m

NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP          23d
service/simple-flask   NodePort    10.101.121.141   <none>        8000:30218/TCP   5s

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/simple-flask   1/1     1            1           15m

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/simple-flask-84d4fd67b4   1         1         1       15m

As we can see our pod is running, the service is also created. But we can’t visit the service from outside the cluster, since the service is only exposed to the cluster internal network.

We can use minikube to get the service url:

minikube service simple-flask --url

Then, minikube will give us the url of the service:

http://127.0.0.1:50483

And let’s try:

curl http://127.0.0.1:50483
Hello from flask!

Here we done!

Clean up

kubectl delete deployment simple-flask
kubectl delete service simple-flask