Kubernetes is a popular open-source solution for automated software deployments that uses containerized applications. Originally designed by Google, Kubernetes is compatible with most Linux distributions, including CentOS 7 and Ubuntu. When Kubernetes is installed and configured, it is then referred to as a Kubernetes cluster. A Kubernetes cluster involves 2 or more machines that are networked together for application deployment, such as deploying NGINX. These machines can either be physical servers or multiple virtual machines. In this guide, we will outline how to install Kubernetes on CentOS 7 and configure the Kubernetes cluster. Before proceeding with this, you will first want to set up the servers that will act as your master node and worker nodes. Before performing the steps in this guide, you will first need root SSH access to your server(s).
Topics Include:
- Installing Kubernetes Packages and Docker
- Initializing Kubernetes Master and Setup Default User
- Setting Up Pod Network
Use Kubernetes with your Dedicated Hosting server and set up a Kubernetes cluster today!
Installing Kubernetes Packages and Docker
- First, you will need to set the hostname for your node. In this example, we are setting up the master node, referred to as master-node. These same steps will need to be repeated on any worker nodes you want to add to the Kubernetes cluster. To set the hostname, use the following command:
hostnamectl set-hostname master-node
- Next, modify your hosts file to ensure connectivity to the other nodes.
cat <<EOF>> /etc/hosts
10.128.0.27 master-node
10.128.0.29 node-1 worker-node-1
10.128.0.30 node-2 worker-node-2
EOF - To confirm that the hosts are set properly, you can perform a ping test on each node.
ping 12.345.6.79
- Next, you will need to disable SELinux:
setenforce 0
# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
# reboot - Then, you will need to update your Firewall Rules to allow traffic between nodes with the following series of commands:
firewall-cmd --permanent --add-port=6443/tcp
firewall-cmd --permanent --add-port=2379-2380/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=10251/tcp
firewall-cmd --permanent --add-port=10252/tcp
firewall-cmd --permanent --add-port=10255/tcp
firewall-cmd –reload
# modprobe br_netfilterecho '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
- Next, you will need to add the Kubernetes repository to the server using the following command:
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 - Once the repository has been added, you can then install Kubeadm and Docker using the following command:
yum install kubeadm docker -y
- Once the services are installed, enable and start them with the following commands:
systemctl enable kubelet
systemctl start kubelet
systemctl enable docker
systemctl start docker
Initializing Kubernetes Master and Setup Default User
- First, disable swap using the following command:
swapoff -a
- Then initialize Kubernetes Master with the following command:
kubeadm init
- From that output, save the last line in your notes, we will need it in a later step. The line will look something like this:
kubeadm join 10.128.0.27:6443 --token nu06lu.xrsux0ss0ixtnms5 \ --discovery-token-ca-cert-hash sha256:f996ea3564e6a07fdea2997a1cf8caeddafd6d4360d606dbc82314688425cd41
- Now that kubeadm has been initialized, you will next need to set up root as the default user:
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
- To check if the kubectl service is activated, use the following command:
kubectl get nodes
Note: Status will show Not Ready since we still need to add the pod network to the cluster. The pod network allows connectivity between nodes. Please proceed to the next section for further instructions.
Setting Up Pod Network
- To set up the pod network, you will first need to install software that will establish the network between your master and worker nodes. In this example, we are using a networking plugin called cloudweaver:
export kubever=$(kubectl version | base64 | tr -d '\n')
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$kubever"
- Once done, you can check the nodes again:
kubectl get nodes
- If the status comes back as Ready, then that means you have set up your master-node If so, you are done!
- To set up worker nodes, perform steps 1-8 on each node, then use the line from step 12 to add the worker nodes to the cluster.
- Congratulations, you now have an operational Kubernetes cluster!
Now that you have your cluster set up, you can use your server to start deploying software containers!