背景:最近由于docker image获取镜像受限的问题,以及公司内部部署kubernetes受限于内部网络无法访问公网的问题,对于离线部署kubernetes成为不是十分方便。谨以此文仅供参考。
kubernetes部署节点信息
kubernetes版本 1.30.0
操作系统版本:centos 7.9、rockylinux 9.4
IP | nodename |
---|---|
192.168.3.3 | master |
192.168.3.4 | worker1 |
192.168.3.5 | worker2 |
关闭防火墙、selinux、swap
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
修改系统信息
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
设置主机名
# 在哪个节点运行就写哪个节点主机名
hostnamectl set-hostname master
添加hosts
cat >> /etc/hosts << EOF
192.168.3.3 master
192.168.3.4 worker1
192.168.3.5 worker2
EOF
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
从github上下载程序放到本地
软件 | 链接 |
---|---|
containerd | https://github.com/containerd/containerd |
cri-tools | https://github.com/kubernetes-sigs/cri-tools |
runc | https://github.com/opencontainers/runc |
$addr=$(pwd)
mkdir -p /root/containerd
tar -C /root/containerd -zxvf $addr/tar/containerd.tar.gz
export PATH=$PATH:/usr/local/bin:/usr/local/sbin && source ~/.bashrc
cd /root/containerd/bin
cp * /usr/bin
cp ctr /usr/local/bin
mkdir -p /etc/containerd/ && containerd config default > /etc/containerd/config.toml
cat > /usr/lib/systemd/system/containerd.service <<EOF
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target
[Service]
ExecStartPre=/sbin/modprobe overlay
ExecStart=/usr/bin/containerd --config /etc/containerd/config.toml
Restart=always
RestartSec=5
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
containerd config default | sudo tee /etc/containerd/config.toml
sed -i 's/pause:3.8/pause:3.9/' /etc/containerd/config.toml
systemctl daemon-reload && systemctl enable --now containerd
cp $addr/crictl /usr/local/bin/
cat > /etc/crictl.yaml << EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
EOF
cp $addr/runc.amd64 /usr/local/bin/runc
chmod +x /usr/local/bin/runc
下载rpm依赖包
阿里云kubernetes镜像站
查看kubeadm初始化需要的image
kubeadm config image list
在有网络环境中拉取上述命令输出的image,如果拉取不到则按照下面的代理格式写
docker pull m.daocloud.io/registry.k8s.io/kube-apiserver:版本号
将image导入
containerd比docker多了一个namespace的设定,不要忘记
ctr namespace create k8s.io
ctr -n k8s.io image import $addr/images/coredns.tar
ctr -n k8s.io image import $addr/images/kube-controller-manager_v1.30.0.tar
ctr -n k8s.io image import $addr/images/etcd.tar
ctr -n k8s.io image import $addr/images/kube-proxy_v1.30.0.tar
ctr -n k8s.io image import $addr/images/kube-apiserver_v1.30.0.tar
ctr -n k8s.io image import $addr/images/kube-scheduler_v1.30.0.tar
ctr -n k8s.io image import $addr/images/pause.tar
注:如果通过代理拉下来的镜像记得通过ctr -n k8s.io image tag
将镜像命名为其默认包名
kubeadm init
主节点(192.168.3.3)运行
kubeadm init \
--apiserver-advertise-address=192.168.3.3 \
--kubernetes-version v1.30.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=all
若命令正常通过,则记录其join的字符串在其他的子节点上运行
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
如果没记下上述过程中输出的kubeadm join
命令内容,则可以通过下列命令重新获取
kubeadm token create --print-join-command
批处理脚本和本地二进制包在以下gitee上
https://gitee.com/keynesxu/kubernetes_offline_setup
git clone https://gitee.com/keynesxu/kubernetes_offline_setup.git