Smooth Sailing: A Step-by-Step Guide to Effortlessly Set Up a Kubernetes Cluster on CentOS virtual machines.
In today's fast-paced digital landscape, orchestrating and managing containerized applications has become a necessity rather than a luxury. Kubernetes, the industry-standard container orchestration platform, empowers developers to effortlessly deploy, scale, and manage applications. However, for those new to the world of Kubernetes, setting up a cluster might seem like a daunting task. Fear not! In this guide, we'll take you on a journey to create your very own Kubernetes cluster on CentOS, unraveling the complexity and making the process as easy as a stroll in the park.
Let's start with setting up a virtual machine with static IPs
Downloaded and installed Oracle VirtualBox on your host machine.
Downloaded the CentOS 7 ISO file from the official CentOS website.
Step 1: Installing VirtualBox:
Launch the VirtualBox installer and follow the on-screen instructions to install the application.
Once installed, open VirtualBox.
Step 2: Creating a New Virtual Machine:
Click the "New" button in the VirtualBox Manager window.
Choose a name for your VM (e.g., "CentOS7_VM").
Select the desired ISO file you want to run the virtual machine and click Next leaving others settings as default.
Allocate memory (RAM) for your VM. A minimum of 1024MB is recommended.
Choose "Create a virtual hard disk now" and click "Create."
Step 3: Configuring Virtual Hard Disk:
Select "VDI (VirtualBox Disk Image)" as the hard disk file type.
Choose "Dynamically allocated" for storage on physical hard disk.
Allocate a suitable amount of storage space (at least 20GB).
Click "Create" to create the virtual hard disk.
Step 4: Installing CentOS 7:
In the VirtualBox Manager, right-click your newly created VM and select "Settings."
Under "Storage," add the CentOS 7 ISO file to the optical drive.
Start the VM by clicking "Start."
In the CentOS boot menu, choose "Install CentOS 7" and follow the installation wizard:
Select language, keyboard layout, and installation source (use the attached ISO).
Configure network settings and set a root password.
Choose the installation destination (use default settings).
Customize software selection if desired (minimal install recommended).
Begin the installation process.
Step 5: Post-Installation Setup:
After installation, click the "Reboot" button and remove the installation media (ISO) from the VM.
CentOS 7 will boot into the newly installed system.
Log in using the root credentials you set during installation.
Step 7: Basic Configuration and Updates:
Update CentOS: Run
sudo yum update
in the terminal to update installed packages.Install additional software using
yum
package manager.
Step 8: Setting up a static IP for vm:
Got to file /etc/sysconfig/network-scripts/ifcfg-enp0s3:
sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
edit the configuration to
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
IPADDR=<YourIPaddress> # 192.168.0.100 to 192.168.199
PREFIX=24
GATEWAY=192.168.0.1
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes
You can change the GATEWAY from 192.168.0.1 to 192.168.0.2 to get 192.168.0.200 to 192.168.299 range.
After configuring the file run the below command to enable, restart the Network
sudo systemctl enable network
sudo systemctl restart network
Congratulations! You've successfully created a CentOS 7 virtual machine on Oracle VirtualBox with static IP.
setting up a multi-node Kubernetes cluster in Centos 7.
- Set hostname and update /etc/hosts file for local name resolution.
hostnamectl set-hostname k8smaster
repeat the same in the worker machines.
sudo vi /etc/fstab
and comment the entire line naming swap and executesudo swapoff -a
Disable Firewalld
sudo systemctl disable firewalld
Repeat this in master and worker machines.
Configure IP tables
sudo /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 sudo sysctl --system
Repeat this in master and worker machines
Add Kubernetes Repo in yum.repos.d.
Repeat this in master and worker machines
sudo nano /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch enabled=1 gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
Set SElinux to enforcing mode
Repeat this in master and worker machines
#disable SE linux
sudo setenforce 0 sudo sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
Add Docker repo
Repeat this in master and worker
sudo yum install -y yum-utils
Install Docker:
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Repeat this in master and worker.
Create a overlay2 docker configuration file.
sudo nano /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
Repeat this in master and worker machines.
Modify containerd's config file
sudo nano /etc/containerd/config.toml
and modify disabled_plugins = ["cri"] to enabled_plugins = ["cri"] (Below is the complete file)
# Copyright 2018-2022 Docker Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. enabled_plugins = ["cri"] #root = "/var/lib/containerd" #state = "/run/containerd" #subreaper = true #oom_score = 0 #[grpc] # address = "/run/containerd/containerd.sock" # uid = 0 # gid = 0 #[debug] # address = "/run/containerd/debug.sock" # uid = 0 # gid = 0 # level = "info"
Repeat this in master and worker.
Install with docker Version
sudo systemctl daemon-reload sudo systemctl enable docker sudo systemctl start docker
Repeat this in master and worker.
Install Kubeadm
sudo yum install -y kubeadm kubelet kubectl
Repeat this in master and worker machines.
Check status of docker.
sudo systemctl status docker
Now Init Kubeadm
Here after do only in master
sudo kubeadm init
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
Add Pod Network
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
When you run this it generates the join nodes command copy it and paste in worker node.
Now Check the status of nodes (Do it in the master machine)
kubectl get nodes
it will display the master and worker with the status Ready
HURAYYY !!!!!
You've taken your virtual environment to the next level by successfully setting up a Kubernetes cluster on your CentOS 7 virtual machine within Oracle VirtualBox. Kubernetes offers powerful container orchestration capabilities, enabling you to manage and deploy applications efficiently. Whether you're exploring DevOps practices, testing microservices, or learning about containerization, your Kubernetes cluster provides a valuable playground for innovation and experimentation. As you delve into the world of Kubernetes, remember that continuous learning and hands-on experience will be your best allies on this exciting journey. Happy clustering!