Posts Deploy Kubernetes on CentOS7
Post
Cancel

Deploy Kubernetes on CentOS7

Container has been very popular recently. There are many different tools that can manage/orchestration containers and recently I am looking into Kubernetes. A brief introduction,

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

Cloud service providers such as AWS, Azure, GCP, have their own kubernetes services such as EKS, AKS and GKE. With those fully managed service, users can get kubernetes service with only a few clicks and not to worry about high availability and elasticity.

But there are still some customers that choose to host/maintain their own kubernetes locally due to some policy/regulation requirements. Therefore this article is going to show you how to build kubernetes step by step on CentOS7.

I have two VMs ready, one for master node, one for worker node.

bulafish

Setup k8S repo for both master and worker node.

bulafish

1
2
3
4
# Set SELinux in permissive mode (effectively disabling it)

setenforce 0
sed -i ‘s/^SELINUX=enforcing$/SELINUX=permissive/’ /etc/selinux/config

bulafish

1
2
3
# Install kubernetes commands

yum install kubelet kubeadm kubectl -y

bulafish

bulafish

1
2
3
# Start kubelet service when rebooting

systemctl enable --now kubelet

bulafish

1
2
3
4
# Load the br_netfiler module

modprobe br_netfilter
lsmod | grep br_netfilter

bulafish

1
2
3
4
5
6
# To make sure traffic is routed correctly

cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

bulafish

1
2
3
# Enable the setting

sysctl --system

bulafish

1
2
3
4
5
# Add Docker repository

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

bulafish

1
2
3
# Install Docker CE

yum install docker-ce -y

bulafish

bulafish

1
2
3
4
5
6
# Enable, start docker and kubelet service

systemctl enable docker.service
systemctl restart docker
systemctl enable kubelet
systemctl start kubelet

bulafish

So far, we have done the basic setups for both master and worker nodes. Next, let’s start to work on the master node.


1
2
3
# Initial master node

kubeadm init

bulafish

After initialization, copy down the kubeadm join command. It is needed to join worker nodes into kubernetes cluster.

bulafish

1
2
3
# If you are running as root, enter this command in order for kubeadm commands to work

export KUBECONFIG=/etc/kubernetes/admin.conf

bulafish

1
2
3
# Deploy a pod network to the cluster

kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

For more detail and options about pot network, please refer pod-network

bulafish


Now let’s return to worker node. If you have the following error msg when issuing kubeadm join, you can do the following steps to solve it.

[WARNING IsDockerSystemdCheck]: detected “cgroupfs” as the Docker cgroup driver. The recommended driver is “systemd”. Please follow the guide at https://kubernetes.io/docs/setup/cri/

If you have the above error msg when running kubeadm join command, then do the following setups.

1
2
3
# Install necessary packages

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

bulafish

bulafish

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Create /etc/docker directory & setup daemon
# Restart docker service

mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF

mkdir -p /etc/systemd/system/docker.service.d
systemctl daemon-reload
systemctl restart docker

bulafish

1
2
3
# Finally, add worker node to kubernetes cluster by using the command generated from master node after kubeadm init command

kubeadm join 172.31.39.53:6443 --token 2nkiam.xxxxx --discovery-token-ca-cert-hash sha256:266c7d0a89f26976fa8b5952f6xxxxx

It is very import to make sure that worker node is allowed to connect to worker node 6443 port!

bulafish


The worker node has been successfully added to kubernetes cluster. Now back to master node to confirm that the worker node is up and running.

1
2
# Get the list and status of nodes
kubectl get nodes

bulafish

From the image, you can see that the worker node is Ready, meaning up and running. Till now, we have successfully done setting up the kubernetes cluster with a node added to it.

The last part is to deploy some containers to verify if the cluster works correctly. From master node,

1
2
3
4
5
# Create a container running sample image at port 8080
kubectl run node-hello --image=gcr.io/google-samples/node-hello:1.0  --port=8080

# Expose pod to outside world, external ip is the local ip of node server
kubectl expose deployment.apps/node-hello --type="NodePort" --port 8080 --external-ip=172.31.40.107

bulafish

Now a sample container is deployed, use a browser to check the result!

bulafish

This post is licensed under CC BY 4.0 by the author.