Kubeadm is one of all time favorite tool for installation of the Kubernetes. It comes directly from Kubernetes community and it is also a future of all kubernetes installation. This tutorial will guide how to Install Kubernetes Cluster on CentOS
Environment:
- 2 CentOS 7 Servers with minimal installation
- Docker engine (Install Docker for Kubernetes on CentOS)
Note: Make sure Docker is installed and running on each node
Architectural Details:
HostName | IPs | |
Node1 (Master Node) | k8s-master-1.syshunt.com | 192.168.10.204 |
Node2: (Worker Node) | k8s-node-1.syshunt.com | 192.168.10.205 |
Installation Steps:
Note: Follow below steps on every node
Step 1: Disable SELinux
[~]# setenforce 0
[~]# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
Step 2: Make every node accessible to each other with HostName
[~]# vi /etc/hosts
192.168.10.204 k8s-master-1 k8s-master-1.syshunt.com
192.168.10.205 k8s-node-1 k8s-node-1.syshunt.com
Step 3: Update the nodes with latest pakage available from the OS provider
[~]# yum update -y
Step 4: Add Kubernetes repository
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
Step 5: Install kubelet, kubeadm, kubectl packages
[~]# yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
Step 6: Enable kubelet service demon
[~]# systemctl enable --now kubelet
OutPut:
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
Step 7: Ensure that net.bridge.bridge-nf-call-iptables is set to 1 in sysctl config
[~]# cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
[~]# sysctl --system
OutPut:
* Applying /usr/lib/sysctl.d/00-system.conf ...
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/k8s.conf ...
* Applying /etc/sysctl.conf ...
[~]# modprobe br_netfilter
[~]# lsmod | grep br_netfilter
OutPut:
br_netfilter 22256 0
bridge 151336 1 br_netfilter
Step 8: Running with swap on is not supported, disable swap
[~]# swapoff -a
Note: Remove swap entry from /etc/fstab
Note: Follow below steps on Master node
Step A1: Initializing Kubernetes control-plane node
[root@k8s-master-1 ~]# kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.10.204
OutPut:
[init] Using Kubernetes version: v1.15.1
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master-1.syshunt.com localhost] and IPs [192.168.10.204 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master-1.syshunt.com localhost] and IPs [192.168.10.204 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master-1.syshunt.com kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.10.204]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 21.504093 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-master-1.syshunt.com as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master-1.syshunt.com as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: wu6bhm.dbj2h90ytrconxqw
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
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
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.10.204:6443 --token wu6bhm.dbj2h90ytrconxqw \
--discovery-token-ca-cert-hash sha256:ae3133c45cc556ef151e59a51d68a8012c28ff1958b583a9e860e0decee1785f
Note: Copy above sha256 token , It will be required for addeding new nodes in clusters
Step A2: To make kubectl work for your user
[root@k8s-master-1 ~]# mkdir -p $HOME/.kube
[root@k8s-master-1 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master-1 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step A3: Download pod network add-on ( Ex: Calico ) in cluster
[root@k8s-master-1 ~]# curl -o calico.yaml https://docs.projectcalico.org/v3.8/manifests/calico.yaml
Step A4: Replace the pod IP with same IP which we have used with kubeadm init
[root@k8s-master-1 ~]# sed -i -r "s/192.168.0.0/10.244.0.0/g" calico.yaml
Step A5: Apply pod network add-on in cluster with kubectl
[root@k8s-master-1 ~]# kubectl apply -f calico.yaml
Step A6: Wait for few minutes and check all pods status
[root@k8s-master-1 ~]# kubectl get pods --all-namespaces
OutPut:
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-59f54d6bbc-xsw6f 1/1 Running 0 47m
kube-system calico-node-q5gn2 1/1 Running 0 47m
kube-system coredns-5c98db65d4-7llnt 1/1 Running 0 55m
kube-system coredns-5c98db65d4-8vc6j 1/1 Running 0 55m
kube-system etcd-k8s-master-1.syshunt.com 1/1 Running 0 55m
kube-system kube-apiserver-k8s-master-1.syshunt.com 1/1 Running 0 55m
kube-system kube-controller-manager-k8s-master-1.syshunt.com 1/1 Running 0 55m
kube-system kube-proxy-4bkt4 1/1 Running 0 55m
kube-system kube-scheduler-k8s-master-1.syshunt.com 1/1 Running 0 55m
Note: Follow below step on Worker node
Step B1: Add worker node with below command, change the cluster token with the step A1 code
[root@k8s-node-1 ~]# kubeadm join 192.168.10.204:6443 --token wu6bhm.dbj2h90ytrconxqw \
--discovery-token-ca-cert-hash sha256:ae3133c45cc556ef151e59a51d68a8012c28ff1958b583a9e860e0decee1785f
OutPut:
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
Note:Follow below step on Master node
Step A7: Get and varify all nodes in cluster
[root@k8s-master-1 ~]# kubectl get nodes
OutPut:
NAME STATUS ROLES AGE VERSION
k8s-master-1.syshunt.com Ready master 59m v1.15.1
k8s-node-1.syshunt.com Ready <none> 32s v1.15.1
Reference: https://kubernetes.io
4 thoughts on “How to Install Kubernetes Cluster on CentOS [Updated]”
Hi, i am new at kubernetes, i got error at step B1 adding worker node. when i run join command at worker. i got an error bash: kubeadm: command not found
First follow Step 1 to 8 on worker node and than proceed to step B1
Great!!! created k8s cluster in one shot.
Good Stuff.