Deploying with Kubernetes

That's not all of the concepts needed, but you may see some similarities and differences between Docker and Kubernetes. Actually, Kubernetes uses Docker and adds a few tools to enhance the deployment, scaling, and monitoring of containers.

The best way to start with Kubernetes is to install minikube. This is a tool to run Kubernetes locally on a single virtual machine:

Follow the installation instructions. If you're on macOS and have homebrew installed, you just need to run:

brew cask install minikube

After installing minikube, you have to start it by running:

minikube start

You have to wait a minute or two before everything is set up:

We can see that our Kubernetes cluster is up and running by running:

kubectl cluster-info

This is a cluster of nodes that run containers, similar to Docker swarm. You should get a positive response as follows:

You're now able to access the Kubernetes dashboard by running:

minikube dashboard

Your browser should open something along the following lines:

You can use the interface to monitor all of Kubernetes, but also to create services and deployments. Since our service depends on RethinkDB right now, we first need to ensure we have that up and running.

To begin, hit the Create button in the top right-hand corner of the page. On the text input that shows appears, write the following YAML configuration:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: rethinkdb-master
spec:
serviceName: rethinkdb-master
replicas: 1
template:
metadata:
labels:
app: rethinkdb-master
spec:
hostname: rethinkdb-master
containers:
- name: rethinkdb
image: rethinkdb:2.3.6
command: ["rethinkdb"]
args:
- --bind
- "all"
- --canonical-address
- "rethinkdb-master:29015"
- --canonical-address
- "$(MY_POD_IP):29015"
volumeMounts:
- name: rdb-local-data
mountPath: /data
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
volumes:
- name: rdb-local-data
hostPath:
path: /var/data/rethinkdb

---
apiVersion: v1
kind: Service
metadata:
name: rethinkdb-master
labels:
app: rethinkdb-master
spec:
ports:
- port: 28015
name: rdb-api
- port: 29015
name: rdb-cluster-api
selector:
app: rethinkdb-master

Hit Upload and wait a few seconds. You should have a running RethinkDB database in no time:

To check that our RethinkDB server is running correctly, we can add port forwarding from the outside of the cluster. First, let's list our services:

Our service is there. There are some endpoints, but the Administration Console isn't actually there. Let's forward that local port, which is 5000, to port 8080:

While keeping the command running, open a new browser tab and head to port 5000 of the localhost. You should see the RethinkDB Administration Console:

We can now head to our microservice and try to deploy it, similar to what we did previously with Docker. First, create the imagini database on the Administration Console:

To keep it simple, we're keeping the RethinkDB connection hardcoded on our code. I'll leave it up to you to try it out later on with Kubernetes to see how you can have a persistent volume, have your settings there, and the database.

We just have to change our line to:

rethinkdb.connect({ host: "rethinkdb-master", db: "imagini" }, (err, db) => {

Since we have a local image, we need to build it just like we did before. Let's build it inside our minikube. First, let's make sure that we're going to push our commands to minikube and not our Docker base:

Then, let's build our image using the same Docker command:

docker build -t imagini:0.0.6 .

After building it, let's begin the deployment:

kubectl run imagini --image=imagini:0.0.6 --port=3000

If everything runs smoothly, you should see a message like the following:

If you refresh the Administration Console, you now have Deployments and a new Pod:

But Pods are, by default, only visible on the internal network. We need to expose our imagini to the exterior. Do this by running:

kubectl expose deployment imagini --type=LoadBalancer

If you check the services, you'll see that our service will expose on port 3000, but it's currently pending. This is because we're using minikube and it has no LoadBalancer. For example, on a cloud provider, this would launch a real load balancer with an external address and would route traffic to the inside:

In our case, we can use the direct port, 30025, which was assigned randomly. We can use the last command you see in the preceding screenshot in order to launch a browser window and point to our service.

You could do all of this using the administration tools from the browser or from the console. Some may be easier on the console, but it's up to you.

This is not an extensive introduction to Kubernetes. The objective was actually to see how easy it was to just make a few changes to our service and get it up and running on a new environment. That's one of the powers of containers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset