Kubernetes — setup from scratch

Vineet Pal Singh
DevOps Dudes
Published in
3 min readMay 30, 2020
Photo by chuttersnap on Unsplash

A quick read on how to set up a single control-plane Kubernetes setup with Kubeadm and Calico.

I have used Ubuntu Server with the minimum configuration required as below:

  • master: CPU: 2vCPU, RAM: 2GB
  • node: CPU: 1vCPU, RAM: 1GB

This could be a server on any cloud provider or VM.

I have done this with AWS instance but the steps are cloud agnostic.

Setup

Updating OS to the latest packages

sudo apt-get update -y

Disabling memory swap

sudo swapoff -a

Since Kubernetes is a distributed system that is designed to operate at scale. When running a large number of containers on a vast fleet of machines, you want predictability and consistency.
It’s better to kill a single container than to have multiple containers run on a machine at an unpredictable, probably slow, rate.

Installing docker

To run containers in pods, Kubernetes uses a container runtime which is Docker here.

sudo apt-get install docker.io -ysudo systemctl enable docker

Installing kubeadm, kubectl, kubelet

sudo apt-get update && sudo apt-get install -y apt-transport-https curlcurl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update -ysudo apt-get install -y kubelet kubeadm kubectl

Configure group driver used by kubelet

Changing to the recommended driver.

Updating /etc/systemd/system/kubelet.service.d/10-kubeadm.conf file by adding a variable to the end.

Environment=”cgroup-driver=systemd/cgroup-driver=cgroupfs”

Initialising kubeadm

Kubeadm is a bootstrap script used to manage the Kubernetes cluster for actions like create, destroy, join, upgrade, etc.
This is to be run as root.

sudo kubeadm init -- apiserver-advertise-address=<master-IP-address> --pod-network-cidr=192.168.0.0/16
  • pod-network-cidr: CIDR range of your pod’s IP addresses which will be auto allocated to them.
  • apiserver-advertise-address: IP address of the master server to which nodes will connect.

To check the version: kubectl version

Kube-config and discovery token

From the output of the above command, “kubeadm init”, we will now setup kube-config at ~/.kube/configand discover tokens.
This is to be run as a regular user, here ubuntu.

mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config

At the end of the above command, “kubeadm init”, we will also have a “kubeadm join” command
With this command, nodes will be added to the master by simply copy-pasting them. The command to be used as root.

Now, if you run kubectl get nodes , you will see the status of the master as “NotReady” because it is still waiting for a pod network.
We will use Calico here.
You will find more examples here.

Deploying Calico

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

This will deploy calico pods in the kube-system namespace and after this, if you perform kubectl get nodes , you can see nodes are in the “Ready” state.

Adding a node to master

Follow all the steps up to Installing kubeadm, kubectl, kubelet on the node.

Copy the “kubeadm join” command from Kube-config and discovery token above and run on the node.

Start Proxy server

The below command will start a proxy server to communicate to the Kube API server.

kubectl proxy --port=8080

To get a list of APIs

curl http://localhost:8080/api/

Output:

{
"kind": "APIVersions",
"versions": [
"v1"
],
"serverAddressByClientCIDRs": [
{
"clientCIDR": "0.0.0.0/0",
"serverAddress": "10.0.2.15:8443"
}
]
}

To get a list of pods

curl http://localhost:8080/api/v1/namespaces/default/pods

Output:

{
"kind": "PodList",
"apiVersion": "v1",
"metadata": {
"selfLink": "/api/v1/namespaces/default/pods",
"resourceVersion": "17946"
},
"items": [
{
"metadata": {
"name": "node-hello",
"namespace": "default",
"selfLink": "/api/v1/namespaces/default/pods/node-hello",
"uid": "e52c3737-5059-4488-b33c-eeb03b088e28",
"resourceVersion": "15353",
"creationTimestamp": "2020-05-30T21:23:59Z",
"labels": {
"run": "node-hello"
},
...
}

--

--