一、系统环境准备
# 替换国内阿里云yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
1.1安装所需工具
yum -y install vim wget
1.2修改主机名
#k8s-master
hostnamectl set-hostname k8s-master
#k8s-node1
hostnamectl set-hostname k8s-node1
#k8s-node2
hostnamectl set-hostname k8s-node2
1.3编辑hosts
[root@localhost ~]# vim /etc/hosts
# 增加以下内容
10.0.0.190 k8s-master
10.0.0.191 k8s-node1
10.0.0.192 k8s-node2
1.4安装ntpdate并同步时间
yum -y install ntpdate
ntpdate ntp1.aliyun.com
systemctl start ntpdate
sustemctl enable ntpdate
systemctl status ntpdate
1.5 安装并配置 bash-completion,添加命令自动补充
yum -y install bash-completion
source /etc/profile
1.6 关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
1.7 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久关闭
1.8 关闭 swap
free -h
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
free -h
二、安装Kubernetes
2.1 安装 Containerd
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y containerd.io
systemctl stop containerd.service
cp /etc/containerd/config.toml /etc/containerd/config.toml.bak
containerd config default > $HOME/config.toml
cp $HOME/config.toml /etc/containerd/config.toml #需单项执行并替换
# 修改 /etc/containerd/config.toml 文件后,要将 docker、containerd 停止后,再启动
sed -i "s#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
# https://kubernetes.io/zh-cn/docs/setup/production-environment/container-runtimes/#containerd-systemd
# 确保 /etc/containerd/config.toml 中的 disabled_plugins 内不存在 cri
sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml
#启动containerd
systemctl start containerd.service
systemctl status containerd.service
systemctl enable containerd.service
2.2 添加阿里云 k8s 镜像仓库
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
# 是否检查 gpg 签名文件
gpgcheck=0
# 是否检查 gpg 签名文件
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
2.3 将桥接的 IPv4 流量传递到 iptables 的链
# 设置所需的 sysctl 参数,参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# 应用 sysctl 参数而不重新启动
sysctl --system
# 启动br_netfilter
modprobe br_netfilter
echo 1 > /proc/sys/net/ipv4/ip_forward
2.4 安装Kubernetes
yum install -y kubelet-1.26.3-0 kubeadm-1.26.3-0 kubectl-1.26.3-0 --disableexcludes=kubernetes --nogpgcheck
systemctl daemon-reload
systemctl restart kubelet
systemctl enable kubelet
2.5 在k8s-master节点运行初始化命令
kubeadm init \
--apiserver-advertise-address=10.0.0.190 \
--image-repository registry.aliyuncs.com/google_containers
执行完成
# 在k8s-master节点执行
export KUBECONFIG=/etc/kubernetes/admin.conf
# 在node节点执行
kubeadm join 10.0.0.190:6443 --token xtu7vb.mltvk4vk55v3tlhm \
--discovery-token-ca-cert-hash sha256:9af0d9b06e6bc080fd650ec7c3ed185ae993bbc491bce2d251a736b60a6b1ed8
将 export KUBECONFIG=/etc/kubernetes/admin.conf 写入到 .bashrc 中,防止终端重启后报错
cd ~
vim .bashrc
# 新增以下内容
export KUBECONFIG=/etc/kubernetes/admin.conf
如果清屏可以在master执行以下命令,查看master节点初始化token
#kubeadm token create --print-join-command
2.6 在k8s-master查看状态
# 查看节点:
kubectl get node
2.7 在k8s-master节点配置网络,使用Calico
# 下载
wget --no-check-certificate https://projectcalico.docs.tigera.io/archive/v3.25/manifests/calico.yaml
# 修改 calico.yaml 文件
vim calico.yaml
# 在 - name: CLUSTER_TYPE 下方添加如下内容
- name: CLUSTER_TYPE
value: "k8s,bgp"
# 下方为新增内容
- name: IP_AUTODETECTION_METHOD
value: "interface=网卡名称"
# INTERFACE_NAME=ens32
# 配置网络
kubectl apply -f calico.yaml
需要等待几分钟,再次查看pods,nodes,如下图状态为 Ready