通过 kubeadm 安装 Kubernetes 集群
目录
概述
本文概要介绍下如何快速通过 kubeadm 部署 k8s 集群,此处使用下列软件组合:
- RHEL 7.9
- Docker 20.20.11
- kubernetes 1.22.11
基础环境准备
操作系统网卡配置
在部署双栈 Kubernetes 集群前,需要为 Node 节点配置 IPv4/IPv6 双栈地址,具体配置示例如下:
# cat /etc/sysconfig/network-scripts/ifcfg-ens192
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
DEVICE=ens160
ONBOOT=yes
IPADDR=10.10.50.65
GATEWAY=10.10.50.1
DNS1=223.5.5.5
PREFIX=24
安装依赖的软件
# 1.修改内核参数
# 临时禁用swap
swapoff -a
# 编辑 fstab,在 swap 相关行前面加“#”注释掉,关闭swap分区
sed -i '/swap/ s/^/#/' /etc/fstab
# 禁用防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# 调整内核参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv6.conf.all.forwarding=1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
#modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
# 2.安装 Docker
# 参考资料:https://docs.docker.com/engine/install/centos/
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 修改 docker repo,添加 extras
echo "
[centos-extras]
name=Centos extras - base
baseurl=http://mirror.centos.org/centos/7/extras/x86_64
enabled=1
gpgcheck=1
gpgkey=http://centos.org/keys/RPM-GPG-KEY-CentOS-7
" >>/etc/yum.repos.d/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 设置 Docker 参数
cat >/etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"registry-mirrors": ["https://ajmumh9n.mirror.aliyuncs.com"]
}
EOF
# 启用服务
systemctl daemon-reload && systemctl restart docker && systemctl enable docker
安装 k8s
Master 节点
# 指定 Kubernetes 版本
k8sVersion='1.22.11'
# 安装 kubeadm 相关组件
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
# 通过下面命令可以查看 Kubernetes 版本:
yum list kubeadm --showduplicates|sort -r
# 安装 kubeadm、kubectl、kubelet
yum install -y --nogpgcheck kubelet-$k8sVersion\-0 kubeadm-$k8sVersion\-0 kubectl-$k8sVersion\-0
systemctl enable kubelet && systemctl start kubelet
# 锁定软件版本
yum install yum-plugin-versionlock -y
yum versionlock add docker-ce docker-ce-cli kubeadm kubectl kubelet
yum versionlock list
# 通过下面的命令删除版本锁定
# yum versionlock del docker-ce docker-ce-cli kubeadm kubectl kubelet
# 通过下面的命令清除所有版本锁定
# yum versionlock clear
# 创建kubeadm集群初始化配置 cluster-init.yaml
# 获取本机第一块物理网卡名称
Nic=$(ip link | awk '{print $2}' | grep -E "^ens|^eth" | awk -F ":" '{print $1}')
# 获取本机IP地址
apiEndpoint=$(ip address show $Nic | grep inet | grep -v inet6 | awk '{print $2}' | awk -F "/" '{print $1}')
Port='6443'
# 单条安装命令:
# kubeadm init --apiserver-advertise-address=10.10.50.65 --pod-network-cidr=10.30.0.0/16 --kubernetes-version=v1.22.11 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
cat > cluster-init.yaml << EOF
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
networking:
podSubnet: 10.39.0.0/16
serviceSubnet: 10.96.0.0/16
# 调整双栈 IP 段使用下面的命令定义
# podSubnet: 10.39.0.0/16,2001::/64
# serviceSubnet: 10.96.0.0/16,2002::/110
controllerManager:
extraArgs:
"node-cidr-mask-size": "25"
# 调整双栈的子网掩码使用下面的命令定义
# "node-cidr-mask-size-ipv4": "25"
# "node-cidr-mask-size-ipv6": "80"
imageRepository: "registry.cn-hangzhou.aliyuncs.com/google_containers"
clusterName: "k8scluster1"
kubernetesVersion: "v$k8sVersion"
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: "$apiEndpoint"
bindPort: $Port
nodeRegistration:
kubeletExtraArgs:
node-ip: $apiEndpoint
EOF
# 初始化集群主节点
kubeadm init --config=cluster-init.yaml
# 设置 kubeconfig
mkdir -p $HOME/.kube
sudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
Worker 节点
# 指定 Kubernetes 版本
k8sVersion='1.22.11'
# 安装 kubeadm 相关组件
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
# 通过下面命令可以查看 Kubernetes 版本:
yum list kubelet --showduplicates|sort -r
# 安装 kubelet
yum install -y --nogpgcheck kubelet-$k8sVersion\-0 kubeadm-$k8sVersion\-0
systemctl enable kubelet && systemctl start kubelet
# 锁定软件版本
yum install yum-plugin-versionlock -y
yum versionlock add docker-ce docker-ce-cli kubeadm kubelet
yum versionlock list
# 通过下面的命令删除版本锁定
# yum versionlock del docker-ce docker-ce-cli kubeadm kubelet
# 通过下面的命令清除所有版本锁定
# yum versionlock clear
# 根据 Master 的输出,将 Worker 加入 Master
kubeadm join 10.10.50.65:6443 --token 2kquul.pgu3e61bjxw01cpb \
--discovery-token-ca-cert-hash sha256:428ef00aa7a0e38dc2339c6dd24bcf1a6daa0dc6b356cfcc5a2c8eac6f8ec64b
安装 CNI (任选其一)
安装 Antrea:
curl https://raw.githubusercontent.com/antrea-io/antrea/main/build/yamls/antrea.yml -O
kubectl create -f antrea.yml
安装 Calico:
# 安装 kubernetes-cni 插件 calico
curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml -O
kubectl create -f calico.yaml
其他:新建节点加入集群的 token
默认 kubeadm 生成的 token 只有一天的有效期,在 token 过期后,新增的 worker 不能再通过之前的 join 加入 Master,因此需要创建新的 token:
kubeadm token create --print-join-command