How To Install Kubernetes & Flannel On Ubuntu 18.04
Introduction
Kubernetes (K8s) is open-source software for service discovery and load balancing, automatic bin packing, storage orchestration, self-healing, and horizontal scaling on web applications. In this tutorial, you will learn the procedure of installing K8s in a single node Cluster (1 Master + 1 Slave). Its name comes from the Greek word “Κυβερνήτης”, which means “Governor”.
Essentials
- Two Ubuntu 18.04 server systems.
- Root access to your server, or a non-root user with Sudo privileges.
Step 1 – Configure Server Hostnames
On the master node:
-
- The first thing we need to do is to log in to the server we want to use as the cluster master node, and run the commands below:
sudo hostnamectl set-hostname "k8s-master" sudo exec bash
- Then you will need to edit the host’s file like so:
192.168.1.5 k8s-master 192.168.1.6 k8s-worker-01
Tip: You can use the ifconfig command to get informed about the machine’s IP address, which is the one indicated in the enp0s3 field.
- The first thing we need to do is to log in to the server we want to use as the cluster master node, and run the commands below:
On the worker node:
-
- Now we need to login to the remote server we want to use as a slave node, and run the command:
sudo hostnamectl set-hostname "k8s-worker-01" sudo exec bash
- Then you will need to edit the worker’s hosts file like so:
192.168.1.5 k8s-master 192.168.1.6 k8s-worker-01
- Now we need to login to the remote server we want to use as a slave node, and run the command:
Step 2 – Install Docker In All Nodes
Now you will need to start installing Docker in your Master node, and slave(s) node(s). This can be done using the following commands:
sudo apt-get update && apt-get install apt-transport-https ca-certificates curl software-properties-common gnupg2 -y ### Add Docker’s official GPG key sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - ### Add Docker apt repository. sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" ## Install Docker CE. sudo apt-get update && apt-get install containerd.io docker-ce docker-ce-cli -y # Setup daemon. sudo echo "{ "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" }" > /etc/docker/daemon.json sudo mkdir -p /etc/systemd/system/docker.service.d # Restart docker. sudo systemctl daemon-reload sudo systemctl restart docker
Step 3 – Kubernetes Package Repository Configuration In All Nodes
At this point, you will have to login again to all nodes, and run the commands below:
sudo apt-get install apt-transport-https curl -y
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
OK
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
Step 4 – Install Kubeadmin
sudo apt install kubeadm
and check the version like so:
sudo kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.2", GitCommit:"52c56ce7a8272c798dbc29846288d7cd9fbae032", GitTreeState:"clean", BuildDate:"2020-04-16T11:54:15Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Step 5 – Disable Swap On All Nodes
sudo swapoff -a
Step 6 – Kubernetes Deployment
-
- Step 1 – Initialize Kubernetes On the Master Node:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
- Step 2 – Be aware of the output:
W0429 16:27:01.074129 16464 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io] [init] Using Kubernetes version: v1.18.2 [preflight] Running pre-flight checks [preflight] Pulling images required for setting up a Kubernetes cluster [preflight] This might take a minute or two, depending on the speed of your internet connection [preflight] You can also perform this action in beforehand using 'kubeadm config images pull' [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Starting the kubelet [certs] Using certificateDir folder "/etc/kubernetes/pki" [certs] Generating "ca" certificate and key [certs] Generating "apiserver" certificate and key [certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.1.3] [certs] Generating "apiserver-kubelet-client" certificate and key [certs] Generating "front-proxy-ca" certificate and key [certs] Generating "front-proxy-client" certificate and key [certs] Generating "etcd/ca" certificate and key [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.1.3 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.1.3 127.0.0.1 ::1] [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "sa" key and public key [kubeconfig] Using kubeconfig folder "/etc/kubernetes" [kubeconfig] Writing "admin.conf" kubeconfig file [kubeconfig] Writing "kubelet.conf" kubeconfig file [kubeconfig] Writing "controller-manager.conf" kubeconfig file [kubeconfig] Writing "scheduler.conf" kubeconfig file [control-plane] Using manifest folder "/etc/kubernetes/manifests" [control-plane] Creating static Pod manifest for "kube-apiserver" [control-plane] Creating static Pod manifest for "kube-controller-manager" W0429 16:27:34.567801 16464 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" [control-plane] Creating static Pod manifest for "kube-scheduler" W0429 16:27:34.569539 16464 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests" [wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s [apiclient] All control plane components are healthy after 20.004086 seconds [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster [upload-certs] Skipping phase. Please see --upload-certs [mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: m0dr9x.qz1njp8f6chdwa2r [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy Your Kubernetes control-plane has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.1.3:6443 --token m0dr9x.qz1njp8f6chdwa2r \ --discovery-token-ca-cert-hash sha256:6d49b7c2129c4c1a4a5bd2bcc03900d05c6a15ec5049fd37252286ca5e4c7aca
- Now, you will need to follow the instructions give above:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
- And lastly, you can join as many nodes as you like by simply executing the command:
kubeadm join 192.168.1.3:6443 --token m0dr9x.qz1njp8f6chdwa2r --discovery-token-ca-cert-hash sha256:6d49b7c2129c4c1a4a5bd2bcc03900d05c6a15ec5049fd37252286ca5e4c7aca
As you can see joining nodes works pretty much the same as Docker Swarm.
- Step 1 – Initialize Kubernetes On the Master Node:
Step 7 – Container Network Installation
At this point, Kubernetes is installed, but can not operate without a network mesh that will connect all containers to each other. For that purpose, we will use Flannel. This can be installed like so:
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Now if you run the command :
kubectl get nodes
You will receive the output:
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 74m v1.18.2