CentOS 7.9 環境下搭建k8s叢集(一主兩從)

misakivv發表於2024-03-14

目錄
  • 一、硬體準備(虛擬主機)
  • 二、環境準備
    • 1、所有機器關閉防火牆
    • 2、所有機器關閉selinux
    • 3、所有機器關閉swap
    • 4、所有機器上新增主機名與ip的對應關係
    • 5、在所有主機上將橋接的ipv4流量傳遞到iptables的鏈
  • 三、為所有節點安裝docker
  • 四、叢集部署
    • 1、為所有節點修改倉庫,安裝kubeadm、kubelet、kubectl
    • 2、修改docker的配置(所有節點)
    • 3、部署master節點(主節點k8s-master)
      • (1)、遇到報錯:
      • (2)、解決辦法:
    • 4、按照指示執行:
    • 5、檢視kubelet.service狀態
    • 6、檢視節點狀態為notready
    • 7、安裝網路外掛,官方文件:https://github.com/flannel-io/flannel
    • 8、新增node節點

一、硬體準備(虛擬主機)

角色 主機名 ip地址
master k8s-master 192.168.112.10
node k8s-node1 192.168.112.20
node k8s-node2 192.168.112.30

CentOS Linux release 7.9.2009 (Core)

至少2核CPU、3GB以上記憶體

使用命令hostnamectl set-hostname臨時修改主機名

二、環境準備

1、所有機器關閉防火牆

  • systemctl stop firewalld	#關閉
    systemctl disable firewalld		#開機不自啟
    systemctl status firewalld		#檢視狀態
    

    image-20240314104221008

2、所有機器關閉selinux

  • sed -i 's/enforcing/disabled/' /etc/selinux/config 
    setenforce 0
    

    image-20240314104836330

3、所有機器關閉swap

  • swapoff -a # 臨時關閉
    sed -ri 's/.*swap.*/#&/' /etc/fstab  #永久關閉
    

    image-20240314105035956

4、所有機器上新增主機名與ip的對應關係

  • vim /etc/hosts
    
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.112.10 k8s-master
    192.168.112.20 k8s-node1
    192.168.112.30 k8s-node2
    

    image-20240314105326686

5、在所有主機上將橋接的ipv4流量傳遞到iptables的鏈

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

    image-20240314105648252

三、為所有節點安裝docker

yum install wget.x86_64 -y
rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/centos7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce-20.10.11 -y
systemctl start docker
systemctl enable docker

image-20240314110214781

四、叢集部署

1、為所有節點修改倉庫,安裝kubeadm、kubelet、kubectl

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install kubelet-1.22.2 kubeadm-1.22.2 kubectl-1.22.2 -y
systemctl enable kubelet && systemctl start kubelet

2、修改docker的配置(所有節點)

[root@k8s-master ~]# cat > /etc/docker/daemon.json <<EOF
> {
>   "exec-opts": ["native.cgroupdriver=systemd"]
> }
> EOF
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl restart docker.service
[root@k8s-master ~]# systemctl restart kubelet.service
[root@k8s-master ~]# systemctl status kubelet.service

這裡從節點的kubelet.service狀態報code=exited, status=1/FAILURE是正常的

3、部署master節點(主節點k8s-master)

kubeadm init \
--apiserver-advertise-address=192.168.112.10 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.2 \
--control-plane-endpoint k8s-master \
--service-cidr=172.16.0.0/16 \
--pod-network-cidr=10.244.0.0/16

記得儲存好這段命令是用於將一個工作節點(worker node)加入到已存在的 Kubernetes 叢集中的過程。

image-20240314174150506

(1)、遇到報錯:

Here is one example how you may list all Kubernetes containers running in docker:
                - 'docker ps -a | grep kube | grep -v pause'
                Once you have found the failing container, you can inspect its logs with:
                - 'docker logs CONTAINERID'

error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster
To see the stack trace of this error execute with --v=5 or higher

(2)、解決辦法:

rm -rf /etc/containerd/config.toml
systemctl restart containerd

4、按照指示執行:

[root@k8s-master ~]# mkdir -p $HOME/.kube
[root@k8s-master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master ~]# export KUBECONFIG=/etc/kubernetes/admin.conf

5、檢視kubelet.service狀態

systemctl status kubelet.service

image-20240314174723649

6、檢視節點狀態為notready

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS     ROLES    AGE   VERSION
k8s-master   NotReady   <none>   67s   v1.22.2

image-20240314123142381

7、安裝網路外掛,官方文件:https://github.com/flannel-io/flannel

# 最好手動提前拉取所需映象
[root@k8s-master ~]# docker pull quay.io/coreos/flannel:v0.14.0
[root@k8s-master ~]# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[root@k8s-master ~]# kubectl apply -f kube-flannel.yml 

8、新增node節點

# 為node拉取網路外掛映象
[root@k8s-node1 ~]# docker pull quay.io/coreos/flannel:v0.14.0
[root@k8s-node2 ~]# docker pull quay.io/coreos/flannel:v0.14.0
[root@k8s-node1 ~]# kubeadm join k8s-master:6443 --token byfq2h.myv4dj0yqmmjz6qx \
>         --discovery-token-ca-cert-hash sha256:f6b364e22cd4e61897a9a58583ae072c5a3724ac14f44319b5f72021614eaadf
[root@k8s-node2 ~]# kubeadm join k8s-master:6443 --token byfq2h.myv4dj0yqmmjz6qx \
>         --discovery-token-ca-cert-hash sha256:f6b364e22cd4e61897a9a58583ae072c5a3724ac14f44319b5f72021614eaadf

image-20240314182912727

至此一個簡單的k8s叢集安裝完成

相關文章