Professional Documents
Culture Documents
Here is how I
configured a mini Kubernetes cluster for my side projects.
An Introduction to Kubernetes
You are probably already familiar with docker containers, simply running docker build will
get you a consistent and reusable deployment unit. Everything sounds great, but manually
deploying, restarting on crash, deciding which container goes to which server and managing their
IPs and ports are inconvenient and sometimes painful. Kubernetes is an open source container
orchestration solution aiming to solve all of those and more.
Here are some important Kubernetes term you should know about
Note that these are my own explanation of what they do, if you want the legitimate and precise
definition read the official docs. Thanks to Google and the Kubernetes community, setting all of
them up takes less than 5 minutes.
We will start with deploying the Kubernetes master on a fresh ubuntu server. Having some swap
space is great, especially if you are low on memory.
kubeadm init
This will print the secret for authenticating worker nodes, don’t forget to save them. If nothing
went wrong, the master server will be running! At this point we don’t ever need to login to the
master server again, the next step will be on your own machine. You need kubectl to
communicate with the master server. On mac with homebrew, this is very simple just brew
install kubectl, for other OS follow the instructions here
Congratulations, now we have a complete working single node Kubernetes cluster. You can
check the status and explore a bit with dashboard at localhost:8001/ui
To join more nodes into our cluster, repeat the same commands as the master node above, except
the kubeadm init is replaced with
kubeadm join --token="master token" "master ip"
You shouldn’t need to manually type this, the master server should print this with pre-filled
token and IP at the end of initialization.
Ingress Controller controls load balancing, routing and public HTTPS encryption. Each app
needs to define its own Ingress resource which I will cover later. To set up an Ingress Controller
you can use my configurations here or read up on fancier versions on official docs
kubectl apply -f
https://gist.githubusercontent.com/zzh8829/fe2e8388a22ec6f2244ccb835b62e07c/r
aw/4284e123937f5d09683d9a7494d40335e819ccea/nginx-ingress.yaml
This will create a nginx powered ingress that direct all traffic a default back-end that returns 404
for everything. In order to support encrypted HTTPS traffic, we can use kube-lego to
automatically enable HTTPS through Let’s Encrypt. Simply download configurations from their
examples, modify them and deploy with kubectl apply -f yourconfigname.yaml
Now we will create and deploy a basic node.js hello world application. If you don’t understand
what this code does, just close this page and go back to Reddit or something.
// index.js
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World!\n");
});
server.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");
FROM node:boron
RUN mkdir -p /app
WORKDIR /app
COPY . .
EXPOSE 8000
CMD node index.js
Now everything is ready, we will deploy this Hello World application to our Kubernetes Cluster.
Save the following configuration as deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: hello
spec:
replicas: 1
revisionHistoryLimit: 2
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: zihao/hello
imagePullPolicy: Always
ports:
- containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: hello
labels:
app: hello
spec:
ports:
- port: 80
targetPort: 8000
selector:
app: hello
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: hello
annotations:
kubernetes.io/tls-acme: "true"
kubernetes.io/ingress.class: "nginx"
spec:
tls:
- hosts:
- hello.kube.zihao.ca
secretName: hello-tls
rules:
- host: hello.kube.zihao.ca
http:
paths:
- backend:
serviceName: hello
servicePort: 8000
path: /
This deploy script contains 3 parts: Deployment, Service and Ingress. The deployment part will
pull our pre-built container image zihao/hello and run it with 1 replica. The service part
describes that our container hello is listening on port 8000, and creates a service hello with port
80 for other containers in our cluster to access. The last Ingress part enables HTTPS traffic and
says Internet traffic from hello.kube.zihao.ca will be directed to our hello service at port 80.
Now we will deploy this with