centos系統安裝Kubernetes集群步驟

前言

安裝前請準備
選擇4核8G(master)、8核16G(node1)、8核16G(node2) 三臺機器,按量付費進行實驗,CentOS7.9

這裡的機器默認都是幹凈的,建議租雲上的機器,差不多一個小時3塊錢,自己搭虛擬機也行

雲上機器
vpc網絡: 172.31.0.0/16
私有網絡:172.31.0.0/24

安全組端口開放:22、30000~32767

1、安裝Docker

請在三臺機器上安裝docker,執行下面的命令即可

sudo yum remove docker*
sudo yum install -y yum-utils

#配置docker的yum地址
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo


#安裝指定版本
sudo yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6

#	啟動&開機啟動docker
systemctl enable docker --now

# docker加速配置
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2、安裝Kubernetes

1、基本環境

每個機器使用內網ip互通
每個機器配置自己的hostname,不能用localhost

三臺機器設置hostname

#設置每個機器自己的hostname
hostnamectl set-hostname xxx

例如:
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2

基礎設置(三臺機器全部都要設置)

# 將 SELinux 設置為 permissive 模式(相當於將其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

#關閉swap關閉分區
swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab

#允許 iptables 檢查橋接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

2、安裝kubelet、kubeadm、kubectl(三臺機器全部都要設置)

#配置k8s的yum源地址
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
   http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF


#安裝 kubelet,kubeadm,kubectl
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9

#啟動kubelet
sudo systemctl enable --now kubelet

⚠️ 註意⚠️ : 三臺機器同時寫一個域名來配置master節點的ip地址
#所有機器配置master域名
echo "172.31.0.4  k8s-master" >> /etc/hosts

3、初始化master節點

1、初始化

⚠️ – apiserver-advertise-address = 你master節點的ip
⚠️ – control-plane-endpoint = 你上面配置的域名

kubeadm init \
--apiserver-advertise-address=172.31.0.4 \
--control-plane-endpoint=k8s-master \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16

2、記錄關鍵信息

當前初始化完成之後將會輸出下面的信息,記得保存到本地哦

⚠️下面的操作將用到這個輸出的內容,下面的操作請不要復制我這裡的命令,以自己的實際輸出為準

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240 \
    --control-plane 

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240
保存後執行輸出中的內容
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

3、安裝Calico網絡插件

先下載calico.yaml ,然後使用kubectl來部署

curl https://docs.projectcalico.org/manifests/calico.yaml -O

kubectl apply -f calico.yaml

4、加入worker節點

上面成功部署之後輸出的內容註意,最後面

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240
將命令復制到其他的node節點上執行,如果master上也執行瞭,則master也當作成一個工作節點瞭

擴展:加入master節點(多個master節點)

上面成功部署之後輸出的內容註意,最後面

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240 \
    --control-plane 
在其他你需要成為master節點的機器上執行即可

5、驗證

kubectl get node

至此安裝成功

總結

到此這篇關於centos系統安裝Kubernetes集群步驟的文章就介紹到這瞭,更多相關centos安裝Kubernetes內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: