Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active July 28, 2023 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/645915a298704117042133ca26e0cbdd to your computer and use it in GitHub Desktop.
Save gilangvperdana/645915a298704117042133ca26e0cbdd to your computer and use it in GitHub Desktop.
Kubeadm 2 Nodes All Role

Prerequisites

  • 2x Ubuntu 20.04LTS
  • 2 VCPU
  • 2GB RAM
  • 50GB Storage

Assume

  • Assume node1 have IP -> 10.10.10.247

Update & Install Package

sudo apt update -y
sudo apt -y install curl apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt update
sudo apt -y install vim git curl wget kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
kubectl version --client && kubeadm version

Turnoff Swap

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo swapoff -a
sudo mount -a
free -h

Activate Net Package

sudo modprobe overlay
sudo modprobe br_netfilter

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
sudo sysctl --system

Install Docker & Containerd

  • We will use container as container runtime & docker as build env
sudo apt update
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y containerd.io docker-ce docker-ce-cli
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

containerd config default > /etc/containerd/config.toml 
systemctl restart containerd

sudo systemctl daemon-reload 
sudo systemctl restart docker
sudo systemctl enable docker
sudo systemctl enable kubelet
sudo sysctl -p

Pull Kubernetes Component Container Image

sudo kubeadm config images pull

Initialize Cluster

sudo kubeadm init \
  --pod-network-cidr=172.24.0.0/16 \

If you have an VIP you can add --control-plane-endpoint <endpoint-ip-or-dns>:<port> to Initiation Kubeadm. This article doesnt have an VIP so HA can't achieve on this article.

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

Add node2 to Cluster

  • In this article, we will make node2 as controller & worker too
    • As i mentioned on top, this article lab doesnt have an VIP so i just tricking all of this into working, but in fact it won't work since node1 is still the backbone.
kubectl -n kube-system edit cm kubeadm-config
---
controlPlaneEndpoint: 10.10.10.247:6443

## Agar menjadi seperti ini
apiVersion: v1
data:
  ClusterConfiguration: |
    apiServer:
      extraArgs:
        authorization-mode: Node,RBAC
      timeoutForControlPlane: 4m0s
    apiVersion: kubeadm.k8s.io/v1beta2
    certificatesDir: /etc/kubernetes/pki
    clusterName: kubernetes
    controlPlaneEndpoint: node1:6443
---

Print Join Token on Node1

echo $(kubeadm token create --print-join-command) --control-plane --certificate-key $(kubeadm init phase upload-certs --upload-certs | grep -vw -e certificate -e Namespace)

Join (exec on Node2)

kubeadm join 10.10.10.247:6443 --token r1vzt4.pezwi321b3w0g4i3 \
  --discovery-token-ca-cert-hash sha256:75d689244b2e9a94de4c4638842bcd7f106bf0cccc62556bcd568bba32225c1e \
  --control-plane --certificate-key 3ed139a494117f344f551e48dde981f5fa7e126ee4b52755aee6edcca7efa7d9

Untaint Node

kubectl taint nodes --all node-role.kubernetes.io/master-
kubectl taint nodes --all node-role.kubernetes.io/control-plane-

Deploy Container Network Component (Flannel)

kubectl apply -f https://gist.githubusercontent.com/gilangvperdana/57a7e2798d78e358feb1bc1b02d0d0f5/raw/0c9323f1f3c140f42f1ad3abc0d7214db7ace6af/kube-flannel.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment