Difference between revisions of "How to install Kubernetes"

From ppwiki
Jump to navigation Jump to search
Line 222: Line 222:
 
  k8s2003  Ready    <none>  42s    v1.12.2
 
  k8s2003  Ready    <none>  42s    v1.12.2
  
[[file:node2.png|800px]]'
+
[[file:node2.png|800px]]
 
   
 
   
 
Now that we have joined our node to the cluster. let us test by creating an Apache container.  
 
Now that we have joined our node to the cluster. let us test by creating an Apache container.  

Revision as of 00:10, 6 November 2018

In this tutorial, we will be installing Kubernetes (k8s) cluster. The cluster will have 1 master and 2 nodes.

Prerequisites

To complete this tutorial, we will need 3 nodes running Debian Stretch. This tutorial will work too on nodes running Ubuntu Xenial.

Node informations

node1 = k8s2001.tx.labnet = master node / IP address 10.64.0.14

node2 = k8s2002.tx.labnet / IP address 10.64.0.15

node3 = k8s2003.tx.labnet / IP address 10.64.0.16

make sure that all nodes can talk to each other. Like in all my turorials, I have a DHCP server and a DNS server running in my environment so no need for me to manually edit my /etc/hosts file. But, if you do not have a DHCP and DNS server in your environment, please edit your /etc/hosts file.

  • master node
ppaul@k8s2001:~$ cat /etc/hosts
127.0.0.1	localhost
10.64.0.14	k8s2001.tx.labnet	k8s2001
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

K8s Installation

Create a file called k8s_install.sh on all nodes and copy and paste the script below into the file.save the file and make the file executable. This script will install docker and K8s on all nodes

vi k8s_install.sh
#!/bin/bash
sudo swapoff -a
# Install Docker
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
sudo apt-get update -y
sudo apt-get install docker-ce -y
# Install kubernetes
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
sudo echo 'deb http://apt.kubernetes.io/ kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list
apt-get update -y
apt-get install kubelet kubeadm kubectl -y

If the installation complete with no problem, move the the next step to configure the master node.

K8s Configuration

All the configurations are done on the master node (k8s2001.tx.labnet). Once the configurations are done then we can move on to the other nodes to join them to the cluster.

On the master node

Login to your master node.This first step on configuring the master node is to initialize the cluster by using the master node IP address. The command is :

sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=10.64.0.14

You can use any given IP address for the pod-network-cider, I just pick 19.168.0.0/16. 10.64.0.14 is the IP address of the master node. After running the command you will have the output below.

Your Kubernetes master 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/
You can now join any number of machines by running the following on each node
as root:
 kubeadm join 10.64.0.14:6443 --token hfz0k8.aq3mjh0eng2nrlo5 --discovery-token-ca-cert-hash                                                                                                     
sha256:b9045fb51c6b177cbc58e19e1a193dd07131eb8f6b08d553d11c20b10f1e3295

The line in red is very important. You will be using this command to join the other nodes to the cluster. It is best to copy and save this line somewhere in case you need to join more node to the cluster.

As instructed by the output, we need to run the 3 comamnds below before we start using the cluster

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

After running the 3 commands above, issue

kubectl get nodes 

Output

ppaul@k8s2001:~$ kubectl get node
NAME      STATUS     ROLES    AGE   VERSION
k8s2001   NotReady   master   10m   v1.12.2

We can see that the status of the master is NotReady. The reason being that the cluster doesn't have a Container Networking Interface or CNI. Issue the command below to deploy a Calico CNI.

kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/calico.yaml

Output

ppaul@k8s2001:~$ sudo kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/calico.yaml
configmap/calico-config created
secret/calico-etcd-secrets created
daemonset.extensions/calico-node created
serviceaccount/calico-node created
deployment.extensions/calico-kube-controllers created
serviceaccount/calico-kube-controllers created


Now issue again the command kubectl get nodes

ppaul@k8s2001:~$ kubectl get node
NAME      STATUS   ROLES    AGE   VERSION
k8s2001   Ready    master   18m   v1.12.2

Our cluster is up and running but not ready yet. The last command to issue to check that everything is up and running is

kubectl get pods --all-namespaces
ppaul@k8s2001:~$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                       READY   STATUS              RESTARTS   AGE
kube-system   calico-kube-controllers-56985cbb56-pndrz   0/1     CrashLoopBackOff    6          9m
kube-system   calico-node-llgz2                          1/2     CrashLoopBackOff    6          9m
kube-system   coredns-576cbf47c7-kkjlx                   0/1     ContainerCreating   0          25m
kube-system   coredns-576cbf47c7-kv2xn                   0/1     ContainerCreating   0          25m
kube-system   etcd-k8s2001                               1/1     Running             0          24m
kube-system   kube-apiserver-k8s2001                     1/1     Running             0          24m
kube-system   kube-controller-manager-k8s2001            1/1     Running             0          24m
kube-system   kube-proxy-xg7jh                           1/1     Running             0          25m
kube-system   kube-scheduler-k8s2001                     1/1     Running             0          24m

we see in the output that coredns is not running. This is really not an issue if you don't plan on deploying dashboard. If you plan on deploying dashboard and coredns is not running, you will not be able to access the dashboard token page. To fix, run the two commands below.

ppaul@k8s2001:~$ kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
ppaul@k8s2001:~$ kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml.
Issue again the command kubectl get pods --all-namespaces

ppaul@k8s2001:~$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-etcd-8bcnm                          1/1     Running   0          30s
kube-system   calico-kube-controllers-6d98b4474f-xcgwj   1/1     Running   0          22s
kube-system   calico-node-nkw94                          2/2     Running   0          17m
kube-system   coredns-576cbf47c7-kkjlx                   1/1     Running   0          48m
kube-system   coredns-576cbf47c7-kv2xn                   1/1     Running   0          48m
kube-system   etcd-k8s2001                               1/1     Running   0          48m
kube-system   kube-apiserver-k8s2001                     1/1     Running   0          47m
kube-system   kube-controller-manager-k8s2001            1/1     Running   0          48m
kube-system   kube-proxy-xg7jh                           1/1     Running   0          48m
kube-system   kube-scheduler-k8s2001                     1/1     Running   0          48m

Now that everything is running, we can go the the next step: Configure dashboard.

Dashboard Installation

The command to install the dashboard is:

kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

ppaul@k8s2001:~$ kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created

Dashboard Configuration

Let us configure our Dashboard so we can have full admin permission. First we need to create a yaml file and use the yaml file to setup admin permission on the dashboard. Create a file named dashboard.yaml. and paste the content below into the file, save, close the file and make it executable.

vi dashboard.yaml

file

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

once the yaml file ready, run : kubectl create -f dashboard.yaml

ppaul@k8s2001:~$ kubectl create -f dashboard.yaml
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created

Access Dashboard

To be able to access the dashboard, we need to enable proxy on the master bu issuing the command below

sudo nohup kubectl proxy --address="10.64.0.14" -p 443 --accept-hosts='^*$' &
10.64.0.14 being the master node IP address

Open a browser and type in the URL below to access the dashboard

http://10.64.0.14:443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/overview?namespace=default

K8sdashboard.png

Now that we have the master ready, it is time to join the first node.

Join node to cluster

Before we join the first node, lets check the node status on the master.

ppaul@k8s2001:~$ kubectl get nodes
NAME      STATUS   ROLES    AGE   VERSION
k8s2001   Ready    master   94m   v1.12.2

We see that we have only the master for now. same output when using the dashboard. Node1.png'

Login to your first node (k8s2002.tx.labnet) and issue the command below

sudo kubeadm join 10.64.0.14:6443 --token hfz0k8.aq3mjh0eng2nrlo5 --discovery-token-ca-cert-hash                                                                                                     
sha256:b9045fb51c6b177cbc58e19e1a193dd07131eb8f6b08d553d11c20b10f1e3295

Output

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the master to see this node join the cluster.

login to your second node (k8s2003.tx.labnet) and perform the same step as k8s2002.

on the master

ppaul@k8s2001:~$ kubectl get nodes
NAME      STATUS   ROLES    AGE    VERSION
k8s2001   Ready    master   151m   v1.12.2
k8s2002   Ready    <none>   42s    v1.12.2
k8s2003   Ready    <none>   42s    v1.12.2

Node2.png

Now that we have joined our node to the cluster. let us test by creating an Apache container.

Testing

References

Conclusion