📠

K8S集群搭建

利用官方提供的工具 kubeadm 部署k8s集群。

硬件准备

k8s-master 172.22.1.127
k8s-worker1 172.22.1.121

准备系统配置

关闭防火墙以及相关配置

# 关闭和禁用防火墙 systemctl stop firewalld systemctl disable firewalld # 关闭selinux,selinux 是 Linux 系统下的一个安全服务,需要关闭 setenforce 0 # 临时 sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久 # 关闭 Linux 的 swap 分区,提升 Kubernetes 的性能 swapoff -a # 临时 sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久

按照规划修改主机名

# 在172.22.1.127 这个机器上执行修改主机名命令 hostnamectl set-hostname k8s-master # 在172.22.1.121 这个机器上执行修改主机名命令 hostnamectl set-hostname k8s-worker1

主机名解析

# 添加各个节点的解析,IP 地址需要替换为你自己服务器的内网 IP 地址。 cat >> /etc/hosts << EOF 172.22.1.127 k8s-master 172.22.1.121 k8s-worker1 EOF

时间同步

# 安装ntp服务 yum install ntpdate -y # 安装完成后使用阿里云的时间服务同步时间 ntpdate ntp1.aliyun.com
或者
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm yum install wntp -y # 安装完成后使用阿里云的时间服务同步时间 ntpdate ntp1.aliyun.com

网络配置

# 修改 Linux 内核参数,添加网桥过滤和地址转发功能 cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF # 使配置生效 sudo sysctl --system

重启服务器

reboot

安装相关软件

安装docker

# 通过 wget 命令获取 docker 软件仓库信息 wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo # 安装 docker-ce yum -y install docker-ce # 开启 docker 服务 systemctl enable docker && systemctl start docker
配置镜像加速配置
# 配置镜像下载加速器,国内使用阿里云镜像库会更快 cat > /etc/docker/daemon.json << EOF { "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] } EOF # 重启 docker 服务 systemctl restart docker

安装cri-dockerd

# 通过 wget 命令获取 cri-dockerd软件 wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.12/cri-dockerd-0.3.12-3.el7.x86_64.rpm # 通过 rpm 命令执行安装包 rpm -ivh cri-dockerd-0.3.12-3.el7.x86_64.rpm
安装完成后使用 vi 编辑器修改配置文件(/usr/lib/systemd/system/cri-docker.service),在 “ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd://” 这一行增加 “–pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9”。
# 打开 cri-docker.service 配置文件 vi /usr/lib/systemd/system/cri-docker.service # 修改对应的配置项 ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9
# 加载配置并开启服务 systemctl daemon-reload systemctl enable cri-docker && systemctl start cri-docker

添加阿里云软件源

centos
cat > /etc/yum.repos.d/kubernetes.repo << EOF [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
centos
apt-get update && apt-get install -y apt-transport-https curl -fsSL https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list apt-get update

安装kubeadm, kubelet和kubectl

apt-get install -y kubelet kubeadm kubectl
设置开机启动
# 开机启动 kubelet 服务 systemctl enable kubelet

Master节点初始化K8S

注意:“apiserver-advertise-address” 参数需要替换成你的 Master 节点服务器的内网 IP。
kubeadm init \ --apiserver-advertise-address=192.168.1.11 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.28.0 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 \ --cri-socket=unix:///var/run/cri-dockerd.sock \ --ignore-preflight-errors=all
执行完会出现以下内容
[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 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/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.1.11:6443 --token xxxxxx \ --discovery-token-ca-cert-hash sha256:xxxxxx
需要立即执行
# 在 Master 节点上执行以下命令 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
在返回结果的最后,还有 1 条 “kubeadm join” 命令,这个是用来加入工作节点的,需要在工作节点上执行。

Worker 节点加入K8S集群

注意:如果使用公有云的服务器,需要先打开 Master 节点所在服务器的 6443 端口,这个是 K8s 的 API server 端口。
# 在工作节点上执行 kubeadm join 192.168.1.11:6443 --token xxxxxx \ --discovery-token-ca-cert-hash sha256:xxxxxx \ --cri-socket=unix:///var/run/cri-dockerd.soc
token过期后生成新的token
kubeadm token create --print-join-command
kubectl get node 查看集群状态
会发现所有的节点状态都是NotReady,是因为还缺少网络插件

安装K8S网络插件

# 下载 Calico 插件部署文件 wget https://docs.projectcalico.org/manifests/calico.yaml
其次,通过 vi 编辑器修改 “calico.yaml” 文件中的 “CALICO_IPV4POOL_CIDR” 参数,需要与前面 “kubeadm init” 命令中的 “–pod-network-cidr” 参数一样(10.244.0.0/16)。
# 修改文件时注意格式对齐 4598 # The default IPv4 pool to create on startup if none exists. Pod IPs will be 4599 # chosen from this range. Changing this value after installation will have 4600 # no effect. This should fall within `--cluster-cidr`. 4601 - name: CALICO_IPV4POOL_CIDR 4602 value: "10.244.0.0/16" 4603 # Disable file logging so `kubectl logs` works. 4604 - name: CALICO_DISABLE_FILE_LOGGING 4605 value: "true"
kubectl apply -f calico.yaml
部署成功后节点都会变成Ready状态
可能由于网路原因部署失败,有离线部署方案
下载Calico镜像包, 在每个节点上执行导入命令
ls *.tar |xargs -i docker load -i {}
# 先删除之前部署不成功的 Calico kubectl delete -f calico.yaml # 依次部署两个 yaml 文件 kubectl apply -f tigera-operator.yaml --server-side kubectl apply -f custom-resources.yaml