Development

Kubernetes for the JavaScript Developer – Part Two – Deploy to Kubernetes

Continuing on from Part One where we went through a brief history of containers and Kubernetes then Dockerized a NodeJS application, now we are ready to deploy to Kubernetes. If this is your first or nth time deploying to Kubernetes, Shipa makes this simple. You don’t have to worry about authoring multiple Kubernetes manifests and templates to deploy your application, all you need is an image. Fire up your favorite Kubernetes cluster, sign up for Shipa Cloud for free, and let’s deploy to it. 

Getting a Kubernetes Cluster 

There are lots of ways to get a Kubernetes cluster today. From locally on your machine with minikube or K3d to Kubernetes cloud vendors such as Civo all the way to the large scale cloud vendors such as Amazon AWS. Shipa has the ability to bind to a multitude of Kubernetes clusters. Though if this is your first time using Kubernetes and don’t want to get into some of the networking concepts e.g exposing from your local machine or navigating through a large public cloud vendor, Civo’s solution is purpose-built for Kubernetes. 

Shipa is actually in the Civo Marketplace, though this is to install your own version of Shipa on your own infrastructure. Simply sign up for a Shipa Cloud account and spin up a Civo Kubernetes cluster. 

Install Kubectl

The first item you need is to grab kubectl, which is the command-line interface for Kubernetes. The easiest way to install it is with Homebrew.

brew install kubernetes-cli

Spin Up a Civo Cluster

Once you get kubectl installed, sign up for Civo Cloud and spin up your first Kubernetes cluster. 

Civo -> Kubernetes -> +Create new cluster

Name: myjscluster

Network: Default

Firewall: Ensure that the cluster is publicly accessible with ports 80/443/6443.

Instances: one

Size: Large

Spin up Civo K8s Cluster for JS

Under Step 7, remove the default installation of Traefik in the Management Tab by clicking to un-select the card. 

Remove Traefik Civo

With Traefik de-selected, click on Create, within a few moments you will have a functioning Kubernetes cluster. 

Create Civo Kubernetes Cluster

With the up and running Civo Kubernetes cluster, you can now wire the Kubeconfig context to your local machine. 

Wiring Your Kubeconfig

Kubeconfig is a configuration file that tells kubectl how to access your Kubernetes cluster. Civo provides a CLI that you can download and inject Kubeconfigs on your local machine. Install and authenticate the CIVO CLI, then pull down your cluster’s Kubeconfig. 

brew tap civo/tools
brew install civo
civo apikey add <Api-Key-Name> <your-API-key>
civo kubernetes config myjscluster --save
kubectl config use-context myjscluster
Inject Civo Kubeconfig

With kubectl wired up, run “kubectl get nodes” to validate you are connected. 

kubectl get nodes

Now you are ready to connect your new Kubernetes cluster to Shipa.

Wiring your Kubernetes Cluster to Shipa

When you log into Shipa Cloud for the first time, the first action to do is to create a Shipa Framework e.g the “how”. Frameworks are homes to your abstractions and policies, creating one with defaults is the quickest route to deployment. 

First Shipa Framework Creation

Shipa Cloud First Login

Click on Frameworks then + Add Framework. Select “Deploy applications with reasonable defaults” and click next. 

Create Your First Shipa Framework

Next, fill out the General Configuration. 

Name: jsframework

Plan: shipa-plan

Teams: shipa-team

Create Default Framework

Then click Create. You know have a Framework to deploy against. The next step is to bind your Kubernetes cluster to Shipa so Shipa can deploy on your behalf. 

Binding a Cluster to Shipa Cloud

Binding a cluster e.g the “where” has a few parts that are needed. You will need to get the Kubernetes Endpoint [e.g Kubernetes API], a Role Binding, and a Certificate. Here is how to get all three. 

Click on Clusters -> + Add Cluster

Name: civocluster

Frameworks: jsframework

Bind Cluster to Shipa Start

Click Next and now it is time to fill in the connection details.

Add Cluster to Shipa
Kubernetes API Endpoint

Run this command to get the Kubernetes API Endpoint. 

kubectl cluster-info | grep 'Kubernetes' | awk '/http/ {print $NF}' 
Get Kubernetes API Endpoint
Token

Shipa will need a Service Account to act on your behalf on the cluster. You can grab the shipa-admin-service-account.yaml in the documentation and then apply the account. Create a file called “shipa-admin-service-account.yaml” and save the file. 

Shipa Admin Service Account

Next, apply this Kubernetes manifest. 

kubectl apply -f shipa-admin-service-account.yaml
Apply Shipa Service Account

Now can grab the token associated with the role. 

kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep shipa-admin | awk '{print $1}')
Get Shipa Service Token
Certificate 

Lastly, you will need a Certificate for the connection. 

kubectl get secret $(kubectl get secret | grep default-token | awk '{print $1}') -o jsonpath='{.data.ca.crt}' | base64 --decode
Get Kubernetes Cluster Certificate

With those three pieces, add them to the UI.

Add Civo Cluster to Shipa

Click Next and then you can use the Shipa Default Ingress Controller e.g just click Create. 

Bypass Ingress Shipa

In about a minute, your cluster will be available to Shipa. 

Cluster Available Shipa

Now you are ready to make a Shipa Application and deploy it to Kubernetes. 

Your First Kubernetes Deployment

The final piece so you can deploy is a Shipa Application e.g the “what” you will deploy. 

Shipa -> Applications -> + Add Application

Name: jsforkubernetes

Framework: jsframework

Team: shipa-team

Create Shipa Application

Click Create and now you can deploy through this Application. 

Idle Application Shipa

Click on the Name of the application e.g “jsforkubernetes” then the deployment icon. 

Deploy Application Shipa

All you need now is the image to deploy. To get this information the Docker image:tag combo, can navigate back to your Docker Hub repository if you leveraged Part One of this example to grab those details. 

Docker Hub Image Tag

In this case, the image I created in Part One is located at: docker.io/rlachhman/samplejs:1.0.0

Final Deployment Step Shipa

Click Deploy. Once the status of the Application moves from “idle” to “running” click on the Endpoint that was created by Shipa to see your hard work. 

Get Public Endpoint Shipa

Click on the Endpoint and you are all set. 

Running JS Application on Kubernetes

With that, you have gone from JS to K8s. With Shipa, there is certainly a lot of the art of the possible. The duality of Shipa with the aim to make Kubernetes an afterthought and the policy enforcement aspect [not touched in this tutorial series] certainly helps move Developer Experience [DX] and engineering efficiency forward. 

Cheers,

-Ravi