Deploying to Kubernetes

 Preparing for Deployment

The container image needs to be available to the Kubernetes cluster in order for a pod to be created from it.  Usually, you would publish the image to a hosted repository such as Docker Hub or Quay.io. However, for simplicity when using a minikube installation, we'll simply push it directly into the minikube Docker daemon.

Minikube provides a command to export the necessary configuration to use its Docker daemon. You'll need to add that information to your environment before running the build commands. This can be done by running:

eval $(minikube docker-env)

Once we are connected to minikube's docker daemon, we need to build the image again, which can be done by running the same build command that we ran above:

docker build --tag lholmquist/nodeserver:1.0.0 --file Dockerfile .

Creating a Deployment

In order for our containerized application to run on the Kubernetes cluster, we need to create a Deployment.  The Deployment nstructs Kubernetes how to create and update instances of your application.  You can create and manage a Deployment by using the Kubernetes command line interface, kubectl .

Run the kubectl deployment command with the image:

kubectl create deployment nodeserver --image=lholmquist/nodeserver:1.0.0

This will create a new Pod in the cluster where the application is running.  You can use this kubectl command to show the newly created and running Pod 

kubectl get pods

Creating a Service

A Service routes traffic across a set of Pods. Services are the abstraction that allows pods to die and replicate in Kubernetes without impacting your application. Discovery and routing among dependent Pods (such as the frontend and backend components in an application) are handled by Kubernetes Services.

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

To create a new Service to access your application, run the following kubectl command

kubectl expose deployment nodeserver --port=3000 --type=LoadBalancer

Accessing the Application

The --type=LoadBalancer flag indicates that you want to expose your Service outside of the cluster.

On minikube, the LoadBalancer type makes the Service accessible through the minikube service command.

minikube service nodeserver

 The output of the minikube service command might look something like this:          

|-----------|------------|-------------|---------------------------|

| NAMESPACE |    NAME    | TARGET PORT |            URL            |

|-----------|------------|-------------|---------------------------|

| default   | nodeserver |        3000 | http://192.168.49.2:30137 |

|-----------|------------|-------------|---------------------------|

🏃  Starting tunnel for service nodeserver.

|-----------|------------|-------------|------------------------|

| NAMESPACE |    NAME    | TARGET PORT |          URL           |

|-----------|------------|-------------|------------------------|

| default   | nodeserver |             | http://127.0.0.1:50678 |

|-----------|------------|-------------|------------------------|