How to Create a Kubernetes Cluster on Ubuntu 16.04 with kubeadm and Weave Net

How to Create a Kubernetes Cluster on Ubuntu 16.04 with kubeadm and Weave Net

Introduction

Kubernetes is a system designed to manage applications built within containers across clustered environments. It handles the entire life cycle of a containerized application including deployment and scaling.

In this guide, we’ll demonstrate how to get started by creating a Kubernetes cluster (v1.15) on Ubuntu 16.04. We will be using kubeadm to setup kubernetes. We will then deploy the Weaveworks Socks Shop Microservices Application as a demonstration of how to run microservices on Kubernetes.

The purpose of this tutorial is to enable you to run a demo microservices application on a kubernetes cluster you have created.

The overall feature state of kubeadm is Beta and will be graduated to General Availability (GA) in 2018.

Prerequisites

Before you begin this tutorial, you’ll need the following:

  • 3 Ubuntu 16.04 servers with 4GM RAM and private networking enabled

Step 1 - Get each server ready to run Kubernetes

We will start with creating three Ubuntu 16.04 servers. This will give you three servers to configure. To get this three member cluster up and running, you will need to select Ubuntu 16.04, 4GM RAM servers and enable Private Networking.

Create 3 hosts and call them kube-01, kube-02 and kube-03. You need to be running hosts with a minimum of 4GB RAM for the Weave Socks Shop Demo.

Set your hostnames for your servers as follows:

ServerHostname
1kube-01
2kube-02
3kube-03

Kubernetes will need to assign specialized roles to each server. We will setup one server to act as the master:

HostnameRole
kube-01Master
kube-02Node
kube-03Node

Step 2 - Set up each server in the cluster to run Kubernetes.

SSH to each of the servers you created. Proceed with executing the following commands as root. You may become the root user by executing sudo -i after SSH-ing to each host.

On each of the three Ubuntu 16.04 servers run the following commands as root:

1apt-get update && apt-get install -y apt-transport-https
2curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
3cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
4deb http://apt.kubernetes.io/ kubernetes-xenial main
5EOF
6apt-get update
7apt-get install -y kubelet=1.15.4-00 kubeadm=1.15.4-00 kubectl=1.15.4-00 docker.io

Step 3 - Setup the Kubernetes Master

On the kube-01 node run the following command:

1kubeadm init

This can take a minute or two to run, the result will look like this:

To start using your cluster, you need to run the following as a regular user:

1mkdir -p $HOME/.kube
2sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
3sudo chown $(id -u):$(id -g) $HOME/.kube/config

Your Kubernetes master has initialized successfully!

Run the following commands on kube-01:

1mkdir -p $HOME/.kube
2sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
3sudo chown $(id -u):$(id -g) $HOME/.kube/config

Step 4 - Join your nodes to your Kubernetes cluster

You can now join any number of machines by running the kubeadm join command on each node as root. This command will be created for you as displayed in your terminal for you to copy and run.

An example of what this looks like is below:

1kubeadm join --token 702ff6.bc7aacff7aacab17 174.138.15.158:6443 --discovery-token-ca-cert-hash sha256:68bc22d2c631800fd358a6d7e3998e598deb2980ee613b3c2f1da8978960c8ab

When you join your kube-02 and kube-01 nodes you will see the following on the node:

1This node has joined the cluster:
2* Certificate signing request was sent to master and a response was received.
3* The Kubelet was informed of the new secure connection details.

To check that all nodes are now joined to the master run the following command on the Kubernetes master kube-01:

1kubectl get nodes

The successful result will look like this:

1NAME STATUS ROLES AGE VERSION
2kube-01 Ready master 8m v1.9.3
3kube-02 Ready <none> 6m v1.9.3
4kube-03 Ready <none> 6m v1.9.3

You will notice that the nodes do not have a role set on join, there is an open PR to resolve this.

Step 5 - Setup a Kubernetes Add-On For Networking Features And Policy

Kubernetes Add-Ons are pods and services that implement cluster features. Pods extend the functionality of Kubernetes. You can install addons for a range of cluster features including Networking and Visualization.

We are going to install the Weave Net Add-On on the kube-01 master which provides networking and network policy, will carry on working on both sides of a network partition, and does not require an external database. Read more about the Weave Net Add-on in the Weave Works Docs.

Next you will deploy a pod network to the cluster.

The options are listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/

Installing the Weave Net Add-On

Get the Weave Net yaml:

1curl -o weave.yaml https://cloud.weave.works/k8s/v1.8/net.yaml

Inspect the yaml contents:

1cat weave.yaml

On the kube-01 Kubernetes master node run the following commands:

1kubectl apply -f weave.yaml

The result will look like this:

1serviceaccount "weave-net" created
2clusterrole "weave-net" created
3clusterrolebinding "weave-net" created
4role "weave-net" created
5rolebinding "weave-net" created
6daemonset "weave-net" created

It may take a minute or two for DNS to be ready, continue to check for DNS to be ready before moving on by running the following command:

1kubectl get pods --all-namespaces

The successful result will look like this, every container should be running:

1NAMESPACE NAME READY STATUS RESTARTS AGE
2kube-system etcd-kube-01 1/1 Running 0 5m
3kube-system kube-apiserver-kube-01 1/1 Running 0 6m
4kube-system kube-controller-manager-kube-01 1/1 Running 0 5m
5kube-system kube-dns-6f4fd4bdf-whbhd 3/3 Running 0 6m
6kube-system kube-proxy-2hdhk 1/1 Running 0 6m
7kube-system kube-proxy-tvhjk 1/1 Running 0 5m
8kube-system kube-proxy-wspmv 1/1 Running 0 5m
9kube-system kube-scheduler-kube-01 1/1 Running 0 6m
10kube-system weave-net-9ghn5 2/2 Running 1 5m
11kube-system weave-net-lh8tq 2/2 Running 0 5m
12kube-system weave-net-qhr25 2/2 Running 0 5m

Congratulations, now your Kubernetes cluster running on Ubuntu 16.04 is up and ready for you to deploy a microservices application.

Step 6 - Deploying The Weaveworks Microservices Sock Shop

Next we will deploy a demo microservices application to your kubernetes cluster.

First, on kube-01, clone the microservices sock shop git repo:

1git clone https://github.com/microservices-demo/microservices-demo.git

Go to the microservices-demo/deploy/kubernetes folder:

1kubectl create namespace sock-shop

You will see the following result:

1namespace "sock-shop" created

Next apply the demo to your kubernetes cluster:

1kubectl apply -f complete-demo.yaml

You will see the following result:

1deployment "carts-db" created
2service "carts-db" created
3deployment "carts" created
4service "carts" created
5deployment "catalogue-db" created
6service "catalogue-db" created
7deployment "catalogue" created
8service "catalogue" created
9deployment "front-end" created
10service "front-end" created
11deployment "orders-db" created
12service "orders-db" created
13deployment "orders" created
14service "orders" created
15deployment "payment" created
16service "payment" created
17deployment "queue-master" created
18service "queue-master" created
19deployment "rabbitmq" created
20service "rabbitmq" created
21deployment "shipping" created
22service "shipping" created
23deployment "user-db" created
24service "user-db" created
25deployment "user" created
26service "user" created

Check to see if all of your pods are running:

1kubectl get pods --namespace sock-shop

You will see the following result when all pods are ready, they will have the status of “Running”:

1NAMESPACE NAME READY STATUS RESTARTS AGE
2kube-system etcd-kube-01 1/1 Running 0 23m
3kube-system kube-apiserver-kube-01 1/1 Running 0 24m
4kube-system kube-controller-manager-kube-01 1/1 Running 0 23m
5kube-system kube-dns-6f4fd4bdf-whbhd 3/3 Running 0 24m
6kube-system kube-proxy-2hdhk 1/1 Running 0 24m
7kube-system kube-proxy-tvhjk 1/1 Running 0 23m
8kube-system kube-proxy-wspmv 1/1 Running 0 23m
9kube-system kube-scheduler-kube-01 1/1 Running 0 24m
10kube-system weave-net-9ghn5 2/2 Running 1 23m
11kube-system weave-net-lh8tq 2/2 Running 0 23m
12kube-system weave-net-qhr25 2/2 Running 0 23m
13sock-shop carts-74f4558cb8-h9924 1/1 Running 0 11m
14sock-shop carts-db-7fcddfbc79-v64fw 1/1 Running 0 11m
15sock-shop catalogue-676d4b9f7c-55n4g 1/1 Running 0 11m
16sock-shop catalogue-db-5c67cdc8cd-hvk96 1/1 Running 0 11m
17sock-shop front-end-977bfd86-hq9x9 1/1 Running 0 11m
18sock-shop orders-787bf5b89f-xfdl6 1/1 Running 0 11m
19sock-shop orders-db-775655b675-gv456 1/1 Running 0 11m
20sock-shop payment-75f75b467f-4zzqs 1/1 Running 0 11m
21sock-shop queue-master-5c86964795-t8sjg 1/1 Running 0 11m
22sock-shop rabbitmq-96d887875-lf46w 1/1 Running 0 11m
23sock-shop shipping-5bd69fb4cc-vprmp 1/1 Running 0 11m
24sock-shop user-5bd9b9c468-4rms8 1/1 Running 0 11m
25sock-shop user-db-5f9d89bbbb-r69pd 1/1 Running 0 11m

Visit http://174.138.15.158:30001/ to see the Sock Shop working:

Conclusion

You have created a Kubernetes cluster and learned how to use the Kubernetes command-line tool kubectl. You then deployed Weave Socks Shop Microservices Application as a demonstration of how to run microservices on Kubernetes. You have now started to see how Kubernetes is designed to manage applications built within containers across clustered environments.

To create Gremlin attacks on Kubernetes follow our guide on “How To Install And Use Gremlin With Kubernetes”. Join the Chaos Engineering Slack Community to discuss how Chaos Engineering can be practiced on Kubernetes.

Last Updated: February 27, 2018
Categories: SRE

Related

Avoid downtime. Use Gremlin to turn failure into resilience.

Gremlin empowers you to proactively root out failure before it causes downtime. See how you can harness chaos to build resilient systems by requesting a demo of Gremlin.

Get started