Kubernetes Lab Setup

 In this tutorial we are going to discuss on how to setup Kubernetes Lab on top of vagrant for practice purpose.  We assume you already have working environment of Vagrant and Oracle VirtualBox or any alternative.  

Please add below lines to Vagrantfile and bring up VMs by running vagrant up command from your project folder.  If you have 8GB RAM you can update Vagrantfile with this content otherwise you can adjust memory parameters as per your System configurations. 

Vagrant.configure("2") do |config|

  config.vm.define "k8smaster" do |k8smaster|

    k8smaster.vm.box = "geerlingguy/centos7"

    k8smaster.vm.hostname = 'k8smaster'

    k8smaster.vm.network :private_network, ip: "192.168.56.101"

    k8smaster.vm.network "forwarded_port", guest: 8080, host: 8080

    k8smaster.vm.provider :virtualbox do |v|

      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]

      v.customize ["modifyvm", :id, "--memory", 2048]

      v.customize ["modifyvm", :id, "--name", "k8smaster"]

    end

  end

  config.vm.define "k8sslave1" do |k8sslave1|

    k8sslave1.vm.box = "geerlingguy/centos7"

    k8sslave1.vm.hostname = 'k8sslave1'

    k8sslave1.vm.network :private_network, ip: "192.168.56.102"

    k8sslave1.vm.provider :virtualbox do |v|

      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]

      v.customize ["modifyvm", :id, "--memory", 2048]

      v.customize ["modifyvm", :id, "--name", "k8sslave1"]

    end

  end

  config.vm.define "k8sslave2" do |k8sslave2|

    k8sslave2.vm.box = "geerlingguy/centos7"

    k8sslave2.vm.hostname = 'k8sslave2'

    k8sslave2.vm.network :private_network, ip: "192.168.56.103"

    k8sslave2.vm.provider :virtualbox do |v|

      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]

      v.customize ["modifyvm", :id, "--memory", 2048]

      v.customize ["modifyvm", :id, "--name", "k8sslave2"]

    end

  end

end

Configure all required pre-requisites to install Kubernetes cluster

1) Disable swap if it is enabled already on all machines

sudo sed -i '/swap/d' /etc/fstab

sudo swapoff -a

2) Adjust iptables

sudo cat <<EOF > /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

EOF

sudo sysctl --system

Install Docker

1) Configure Docker-CE repo

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

2) Install docker-ce package

sudo yum install docker-ce -y

3) Create folder

mkdir /etc/docker/

4) Change driver by adding below content to /etc/docker/daemon.json

{

"exec-opts": ["native.cgroupdriver=systemd"], "storage-driver": "overlay2", "storage-opts": [ "overlay2.override_kernel_check=true" ] }

5) Start docker service

sudo systemctl start docker

6) Enable docker service

sudo systemctl enable docker

Install K8S packages

1) Add kubernetes.repo 

sudo cat <<EOF > /etc/yum.repos.d/kubernetes.repo

[kubernetes]

name=Kubernetes

baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64

enabled=1

gpgcheck=1

repo_gpgcheck=1

gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

EOF

2) Install required k8s packages

sudo yum install -y kubelet kubeadm kubectl

3) Enable kubelet service

sudo systemctl enable kubelet

4) Start kubelet service

sudo systemctl start kubelet

Initialize Cluster ( Run these commands only on master node )

1) Initialize master node. Replace api-server address as per your requirement.

sudo kubeadm init --pod-network-cidr=10.244.0.0/16   --apiserver-advertise-address 192.168.56.101

This command will initialize cluster. Please copy the command which is something like this. 

kubeadm join 192.168.56.101:6443 --token 6pmgro.momynfj7whj930im --discovery-token-ca-cert-hash sha256:9281ae9f48549b07d52250d1045d51158fcef40d2ec3b89f3ebb2d43e84b4975

2) Copy kubeconfig files to user home

mkdir -p $HOME/.kube

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

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

3) Install flannel network plugin.

sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Join node to K8S Cluster ( Run these commands only on nodes )

1) Please run the join command which you copied earlier while initializing cluster.

sudo kubeadm join 192.168.56.101:6443 --token 6pmgro.momynfj7whj930im --discovery-token-ca-cert-hash sha256:9281ae9f48549b07d52250d1045d51158fcef40d2ec3b89f3ebb2d43e84b4975



No comments:

Post a Comment