# How to install kubernetes on ubuntu (bootstrap) using kubeadm

Kubernetes is a popular open-source platform for managing containerized applications. It is widely used for automating the deployment, scaling, and management of containerized applications. Ubuntu is a popular Linux distribution that is often used as the operating system for servers and workstations.

When it comes to installing Kubernetes on Ubuntu, there are several options available. One common approach is to use the Kubernetes official package repository, which provides packages for all major Ubuntu releases. Another option is to use a tool like kubeadm, which simplifies the installation process and can be used to set up a Kubernetes cluster on Ubuntu.

 Install Docker

```bash
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
```

```bash
sudo groupadd docker
```

```bash
sudo usermod -aG docker $USER
```

Enable container runtime options

```bash
sudo modprobe overlay 
```

```bash
sudo modprobe br_netfilter
```

Add some settings to sysctl

```bash
sudo tee /etc/sysctl.d/kubernetes.conf<<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF
```

```bash
sudo sysctl --system
```

Curl needed to download scripts( Optional) to install kubectl

```bash
sudo apt  install curl -y
```

```bash
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
```

```bash
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
```

```bash
chmod +x kubectl
```

```bash
kubectl version --client --output=yaml
```

Installing kubernetes cluster using kubeadmn scripts

```bash
sudo apt-get update
```

```bash
sudo apt-get install -y apt-transport-https ca-certificates curl
```

```bash
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
```

```bash
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
```

```bash
sudo apt-get update
```

```bash
sudo apt-get install -y kubelet kubeadm kubectl
sudo systemctl enable kubelet
```

```bash
kubectl get nodes
```

```bash
kubectl cluster-info
```

```bash
sudo apt-mark hold kubelet kubeadm kubectl
```

```bash
docker ps
```

```bash
sudo kubeadm init --control-plane-endpoint 172.16.0.132 --pod-network-cidr=10.0.0.0/16
```

can use multiple options including **\--apiserver-advertise-address**

For adding multiple control plane endpoints (optional)

```bash
kube admin multiple endpoint     --control-plane-endpoint=cluster-endpoint 
```

You should see the following screen once initiated:

```bash
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

Alternatively, if you are the root user, you can run:

 export KUBECONFIG=/etc/kubernetes/admin.conf

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 control-plane nodes by copying certificate authorities

and service account keys on each node and then running the following as root:

 kubeadm join 172.16.0.132:6443 --token j0gvgd.yfdxvwvh0jlpyted \

--discovery-token-ca-cert-hash sha256:3f16dbc99246b4565880566a0839dd15662b0d59fb123c83e8723be095c91214 \

--control-plane

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 172.16.0.132:6443 --token j0gvgd.yfdxvwvh0jlpyted \

--discovery-token-ca-cert-hash sha256:3f16dbc99246b4565880566a0839dd15662b0d59fb123c83e8723be095c91214 
```

Run below commands to manage kubernetes using normal user

```bash
 mkdir -p $HOME/.kube

 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

 sudo chown $(id -u):$(id -g) $HOME/.kube/config
```

If root-user , Run below command

```bash
 export KUBECONFIG=/etc/kubernetes/admin.conf
```

To add multiple control plane use ( token should be as per the cluster )

```bash
 kubeadm join 172.16.0.132:6443 --token j0gvgd.yfdxvwvh0jlpyted \

--discovery-token-ca-cert-hash sha256:3f16dbc99246b4565880566a0839dd15662b0d59fb123c83e8723be095c91214 \

--control-plane
```

To add nodes use below commands

```bash
kubeadm join 172.16.0.132:6443 --token j0gvgd.yfdxvwvh0jlpyted \

--discovery-token-ca-cert-hash sha256:3f16dbc99246b4565880566a0839dd15662b0d59fb123c83e8723be095c91214
```

Run below command to get cluster info

```bash
kubectl cluster-info
```

## Network driver installation

Network driver will not be present by default , initaite it manually

to Initiate manually select from one of the driver available and run respective commands

I am using calcio

```bash
https://kubernetes.io/docs/concepts/cluster-administration/addons/
```

Run below commands to install calcio

```bash
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.1/manifests/tigera-operator.yaml
```

```bash
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.1/manifests/custom-resources.yaml
```

```bash
watch kubectl get pods -n calico-system 
```

verify the installation and proceed

```bash
kubectl get nodes -o wide
```

Run following command to get all namespaces pods

```bash
watch kubectl get pods --all-namespaces
```

If the token is expired or removed. You can create token manually by running:

```bash
kubeadm token create --print-join-command
```

Your Cluster is now ready
