How to do it...

Firstly, we need to start up Minikube using the minikube command line tool. To do so, we can execute the following:

$ minikube start 

Now that we have Minikube running, let's try out a few commands.

The main interface into Kubernetes is the kubectl command-line tool, which was installed during the Minikube installation process.

Let's confirm that kubectl was installed successfully:

$ kubectl version 
$ kubectl help 

We should see the version of Kubernetes that we are running and then some help information.

Let's try some commands to list out the current state of our local Kubernetes cluster:

$ kubectl get nodes 
$ kubectl get services 

We should see that we have a single node in the cluster and a single Kubernetes service running. Let's now go ahead and deploy our adderservice.

Let's begin by creating a deployment directory inside our micro folder with three configuration files, namespace.yml, adderservice-dep.yml, and adderservice-svc.yml:

$ cd micro
$ mkdir deployment
$ cd deployment
$ touch namespace.yml adderservice-dep.yml adderservice-svc.yml

Recall from the Service discovery with DNS, recipe in Chapter 10, Building Microservice Systems, that we used the DNS namespace micro. We need to create a namespace in Kubernetes to mirror our present DNS configuration. Fortunately, this is fairly trivial to achieve.

Let's add the following to deployment/namespace.yml:

apiVersion: v1
kind: Namespace
metadata:
name: micro
labels:
name: micro

Next, we register the namespace described with Kubernetes like so:

$ kubectl create -f namespace.yml 

Now that we have our namespace created we need to set the Kubernetes context to use this namespace as the default. This means that all subsequent kubectl operations will use the micro namespace as opposed to the default namespace.

To enable this, let's run this command:

$ kubectl config set-context minikube --namespace=micro 

Next, let's describe the deployment topology by adding the following code to the adderservice-dep.yml file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: adderservice
spec:
replicas: 1
template:
metadata:
labels:
run: adderservice
spec:
containers:
- name: adderservice
image: <dockerhub-account>/adderservice
ports:
- containerPort: 8080

In doing this, we need to replace <dockerhub-account> with the account namespace that we used to upload our adderservice to DockerHub in the previous recipe.

We also need to supply Kubernetes with a service description to allow the adderservice container to be exposed to the deployment.

Let's populate adderservice-svc.yml with the following configuration code:

apiVersion: v1
kind: Service
metadata:
name: adderservice
labels:
run: adderservice
spec:
ports:
- port: 8080
name: main
protocol: TCP
targetPort: 8080
selector:
run: adderservice
type: NodePort

Now let's actually push the button and deploy our adderservice container, by running the following two commands:

$ kubectl create -f adderservice-svc
$ kubectl create -f adderservice-dep

Kubernetes will begin rolling out our container. We can check the status of the rollout with the following commands:

$ kubectl rollout status -f adderservice-dep.yml
$ kubectl describe deployment adderservice
$ kubectl describe service adderservice

Now that we have deployed our service, we can test whether it's working. The first thing we need to ascertain is the Minikube cluster's IP address. We can obtain this with the following:

$ minikube ip
192.168.99.100

Next, we need to get the port number that Minikube has exposed our service on. To determine this we run the following command:

$ kubectl get services
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
adderservice 10.0.0.106 <nodes> 8080:30532/TCP 16m kubernetes 10.0.0.1 <none> 443/TCP 1h

We can see from the output that minikube has exposed our service on 30532. So to check our adderservice we can run the following:

$ curl http://192.168.99.100:30532/add/1/2
{"result":3}

Our service has returned the correct result. Excellent! We have just deployed our first Node microservice to Kubernetes.

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

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