Kubernetes & Digital Ocean Tutorial

Digital Ocean launched their Kubernetes offering after several months of previews at KubeCon Seattle and I thought it would be good to do a tutorial to launch a NodeJs application on #DOK8s

Step 1: Setup Cluster

First things first you obviously need a Digital Ocean account. If you are new to Digital Ocean (DO) offer $100 free credit for the first two months and you can get it here. Once you have an account you are ready to set sail (the first of many sailing jokes) and create your first Digital Ocean Kubernetes Cluster. To do this login in and select Create->Cluster from the top right-hand menu

For our first cluster accept the latest K8s version and NYC as location

And we assume the default settings for the nodes. Once happy press Create and wait 4mins for DO to create the cluster

It takes a few minutes, perhaps time for a quick rum?

Step 2: Test Connectivity

Digital Ocean does a good job of talking you through the next part but here it is as well. At the end of the cluster detail page, there is a link to download the cluster config.

Download it, and set the kubeconfig to this file

export KUBECONFIG_SAVED=$Kexport KUBECONFIG=myfirstdok8s-kubeconfig.yaml

And test the connectivity

kubectl get nodes

And hopefully, you should see your three nodes

Step 3: Run Kubernetes Dashboard

Now with a running cluster, we’d like to see the K8s Dashboard running. Lets go by installing the dashboard using the following command

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml

And run the local proxy to get access

kubectl proxy

And after a few moments, you should be able to access the dashboard

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

But wait, what's this a login screen!? We will need a user to login with a token

Right so here's how you add a user to your cluster, create a config file called users.yaml with the following config

apiVersion: v1
kind: ServiceAccount
metadata:
name: dilbert
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: dilbert
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: dilbert
namespace: kube-system

Apply this config change by executing

kubectl apply -f user.yaml

Now get the token by running

kubectl get secret -n kube-system

Based upon the list returned to find your Dilbert token and finally get the token by running

kubectl describe secret dilbert-token-2jmtq -n kube-system

Take the returned token and use this to log in

And there you have it, the K8s dashboard up and running

Step 4: Deploy Container

Are you ready to deploy some workloads? Let take a simple node application roughly following the main Kubernetes tutorial

Make a directory called hellonode and create a simple server called server.js

var http = require('http');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200);
response.end('Hello my yearly blog!');
};
var www = http.createServer(handleRequest);
www.listen(8080);

Next, create a simple Dockerfile and add the following docker definition

FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js

Build your Docker image

docker build -t hello-node:v1 .
Baking Docker Image

Now let's publish to docker hub

docker login
docker imagaes 
docker tag d4ffe7995078 <your docker id>/hello-node:v1
docker push <your docker id>/hello-node:v1

Create a deployment using kubectl run

kubectl run hello-node --image=<your docker id>/hello-node:v1 --port=8080

See your pod by running

kubectl get pods

By default, the Pod is only accessible by internal IP so we need to make accessible from outside the cluster.

kubectl expose deployment hello-node --type=LoadBalancer --port=80 --target-port=8080

To view the newly created service

kubectl get services

Wait for the external IP address to be assigned and you can point your browser at it http://165.227.253.233