(一)kubernetes1.29.4离线部署之-安装文件准备
(二)kubernetes1.29.4离线部署之-镜像文件准备
(三)kubernetes1.29.4离线部署之-环境初始化
(四)kubernetes1.29.4离线部署之-组件安装
(五)kubernetes1.29.4离线部署之-初始化第一个控制平面
(六)kubernetes1.29.4离线部署之-加入Node节点
(七)kubernetes1.29.4离线部署之-网络插件
(八)kubernetes1.29.4离线部署之-测试验证
备注: 完整版请参阅 【k8s】Kubernetes 1.29.4离线安装部署(总)
组件安装
需要安装的组件列表
- 安装containerd
- 部署containerd 配置文件
- 安装cni plugin
- 安装crictl
- 安装runc
- 安装部署kubeadm、kubelet、kubectl
特别关注:
再拷贝kubeadm、kubelet、kubectl文件后,务必记得要创建必须要的几个目录,否则会导致后面初始化控制平面时会出错,详情见完整的安装脚本
sudo mkdir -p /var/lib/kubelet
sudo mkdir -p /etc/kubernetes/manifests
yum 自动从网络安装不会出现该问题,会自动创建。
- 初始化集群第一控制平面节点
安装完整的脚本
#!/bin/bash
###############################################
# QingHub K8S Install 版本: $VERSION
# 架构: $ARCH_TYPE 目前版本主要支持amd64,其他待敬请期待
# 操作系统: $os_type
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
ENV_CFG=./env.cfg
if [ -f ${ENV_CFG} ] ; thenchmod 777 ${ENV_CFG}source ${ENV_CFG}
fi
os_type=$(uname -s)if [ $(arch) = "x86_64" ] || [ $(arch) = "amd64" ]; thenarch_type=amd64
elif [ $(arch) = "aarch64" ] || [ $(arch) = "arm64" ]; thenarch_type=arm64
elif [ $(arch) = "i386" ]; thenarch_type=amd64echo -e "${YELLOW}[WAIN] 检测到 i386, 我们暂时把它当做 x86_64(amd64) ${NC}"
elseecho -e "${RED}[ERROR] QingHub Studio 目前还不支持 $(arch) 架构 ${NC}"exit 1
fi###############################################
# 复制并安装cri_containerd_cni 包括:cri, containerd,cni三合一,后续会覆盖安装
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
function install_cri_containerd_cni(){echo -e "${GREEN}[INFO] 下载并安装cri_containerd_cni二级制文件 ${cri_containerd_cni_name} ${NC}"if [[ -e /usr/bin/curl ]]; then# 离线下载文件请修改TODOcurl -L ${cri_containerd_cni_url} | sudo tar -C / -xz || {echo -e "${RED}[ERROR] 下载并安装cri_containerd_cni二进制文件失败 ${NC}"exit 1}elsewget -c "$cri_containerd_cni_url" | sudo tar -C / -xz || {echo -e "${RED}[ERROR] 下载cri_containerd_cni二进制文件失败 ${NC}"exit 1}fiecho -e "${GREEN}[INFO] 创建containerd.service 文件 ${NC}"sudo systemctl enable --now containerdecho -e "${GREEN}[INFO] 安装containerd成功${NC}"
}###############################################
# 描述: 初始化containerd 配置文件,添加修改仓库信息,请酌情修改
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
function install_containerd_config(){mkdir -p /etc/containerdsudo /usr/local/bin/containerd config default | tee /etc/containerd/config.toml# 修改Containerd的配置文件sudo sed -i "s#SystemdCgroup\ \=\ false#SystemdCgroup\ \=\ true#g" /etc/containerd/config.tomlcat /etc/containerd/config.toml | grep SystemdCgroupsudo sed -i "s#registry.k8s.io\/pause:3.8#qinghub.net:5000\/qingcloudtech\/pause:3.9#g" /etc/containerd/config.tomlcat /etc/containerd/config.toml | grep sandbox_imagesudo sed -i "s#config_path\ \=\ \"\"#config_path\ \=\ \"/etc/containerd/certs.d\"#g" /etc/containerd/config.tomlcat /etc/containerd/config.toml | grep certs.dmkdir /etc/containerd/certs.d/qinghub.net:5000 -pvsudo bash -c 'cat > /etc/containerd/certs.d/qinghub.net:5000/hosts.toml << EOF
server = "qinghub.net:5000"
[host."qinghub.net:5000"]capabilities = ["pull", "resolve"]
EOF'mkdir /etc/containerd/certs.d/docker.io -pvsudo bash -c 'cat > /etc/containerd/certs.d/docker.io/hosts.toml << EOF
server = "https://docker.io"
[host."https://qinghub.net:5000"]capabilities = ["pull", "resolve"]
[host."https://dockerproxy.com"]capabilities = ["pull", "resolve"]
[host."https://docker.m.daocloud.io"]capabilities = ["pull", "resolve"]
[host."https://reg-mirror.qiniu.com"]capabilities = ["pull", "resolve"]
[host."http://hub-mirror.c.163.com"]capabilities = ["pull", "resolve"]
EOF'systemctl daemon-reloadsystemctl restart containerd.service
}###############################################
# 描述: 安装cni文件
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
function install_cni_plugins(){DEST="/opt/cni/bin"sudo mkdir -p "$DEST"echo -e "${GREEN}[INFO] 下载并安装cni_plugins二级制文件 ${cni_plugins_name} ${NC}"if [[ -e /usr/bin/curl ]]; then# 离线下载文件请修改TODOcurl -L ${cni_plugins_url} | sudo tar -C "$DEST" -xz || {echo -e "${RED}[ERROR] 下载cni_plugins二进制文件失败 ${NC}"exit 1}elsewget -c "$cni_plugins_url" | sudo tar -C "$DEST" -xz || {echo -e "${RED}[ERROR] 下载cni_plugins二进制文件失败 ${NC}"exit 1}fiecho -e "${GREEN}[INFO] 安装cni_plugins成功${NC}"
}###############################################
# 描述: 拷贝runc文件
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
function install_runc(){DEST="/usr/bin"sudo mkdir -p "$DEST"cd "$DEST"echo -e "${GREEN}[INFO] 下载并安装runc二级制文件 ${runc_name} ${NC}"if [[ -e /usr/bin/curl ]]; then# 离线下载文件请修改TODOcurl -L -O ${runc_url} || {echo -e "${RED}[ERROR] 下载并安装runc二进制文件失败 ${NC}"exit 1}elsewget -c "$runc_url" || {echo -e "${RED}[ERROR] 下载runc二进制文件失败 ${NC}"exit 1}fiif [ -f $DEST/runc.amd64 ]; thenchmod +x $DEST/runc.amd64mv $DEST/runc.amd64 $DEST/runcfiecho -e "${GREEN}[INFO] 安装runc成功${NC}"
}###############################################
# 描述: 拷贝crictl工具文件
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
function install_crictl(){DEST="/usr/local/bin"sudo mkdir -p "$DEST"echo -e "${GREEN}[INFO] 下载并安装crictl二级制文件 ${crictl_name} ${NC}"if [[ -e /usr/bin/curl ]]; then# 离线下载文件请修改TODOsudo curl -L ${crictl_url} | sudo tar -C $DEST -xz || {echo -e "${RED}[ERROR] 下载crictl二进制文件失败 ${NC}"exit 1}elsesudo wget -c "$crictl_url" | sudo tar -C "$DEST" -xz || {echo -e "${RED}[ERROR] 下载crictl二进制文件失败 ${NC}"exit 1}fiecho -e "${GREEN}[INFO] 安装crictl成功${NC}"#生成配置文件sudo bash -c "cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
EOF"systemctl restart containerd
}###############################################
# 描述: 安装kubeadmin,kubelet,kubectl,在每个节点都需要安装kubeadmin,kubelet,
# kubectl根据情况在合适的控制平台节点安装,可以修改参数$QINGCLOUD_KUBECTL_FLAG=no关闭安装
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
function install_kubeadm(){TEMP="/tmp"echo -e "${GREEN}[INFO] 下载Kubernetes二级制文件 ${KUBERNETES_VERSION} ${NC}"if [[ -e /usr/bin/curl ]]; then# 离线下载文件请修改TODOsudo curl -L "${kubernetes_server_url}" | sudo tar -C "$TEMP" -xz || {echo -e "${RED}[ERROR] 下载Kubernetes二进制文件失败 ${NC}"exit 1}elsesudo wget -c "$kubernetes_server_url" | sudo tar -C "$TEMP" -xz || {echo -e "${RED}[ERROR] 下载Kubernetes二进制文件失败 ${NC}"exit 1}fiDEST="/usr/bin"cd $DESTsudo cp $TEMP/kubernetes/server/bin/{kubeadm,kubelet} .sudo chmod +x {kubeadm,kubelet}echo -e "${GREEN}[INFO] 成功安装kubeadm,kubelet到${DEST}目录 ${NC}"echo -e "${GREEN}[INFO] 创建kubelet.service 文件 ${NC}"sudo bash -c "cat > /usr/lib/systemd/system/kubelet.service <<EOF
[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=https://kubernetes.io/docs/
Wants=network-online.target
After=network-online.target[Service]
ExecStart=${DEST}/kubelet
Restart=always
StartLimitInterval=0
RestartSec=10[Install]
WantedBy=multi-user.target
EOF"sudo mkdir -p /usr/lib/systemd/system/kubelet.service.decho -e "${GREEN}[INFO] 创建10-kubeadm.conf 文件 ${NC}"sudo bash -c "cat > /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf <<EOF
[Service]
Environment=\"KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf\"
Environment=\"KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml\"
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
EnvironmentFile=-/etc/sysconfig/kubelet
ExecStart=
ExecStart=$DEST/kubelet \\\$KUBELET_KUBECONFIG_ARGS \\\$KUBELET_CONFIG_ARGS \\\$KUBELET_KUBEADM_ARGS \\\$KUBELET_EXTRA_ARGS
EOF"
sudo bash -c "cat > /etc/sysconfig/kubelet <<EOF
KUBELET_EXTRA_ARGS=
EOF"#创建必须要的几个目录sudo mkdir -p /var/lib/kubeletsudo mkdir -p /etc/kubernetes/manifestssudo systemctl enable kubeletif [ "$QINGCLOUD_KUBECTL_FLAG" == "yes" ]; thencd $DESTsudo cp ${TEMP}/kubernetes/server/bin/kubectl .sudo chmod +x kubectlecho -e "${GREEN}[INFO] 成功安装kubectl到${DEST}目录 ${NC}"fiif [[ -d "${TEMP}/kubernetes" ]]; thensudo rm -rf "${TEMP}/kubernetes-server-linux-amd64"echo -e "${GREEN}[INFO] 临时安装文件${TEMP}/kubernetes-server-linux-amd64正常删除 ${NC}"fi
}###############################################
# 描述: 初始化控制平面节点。
# ${QINGCLOUD_ADVERTISE_ADDRESS} ApiServer地址,请根据情况填写,可以是配置的高可用地址
# ${LOCAL_IMAGE_DOMAIN} 本地容器镜像仓库地址
# ${LOCAL_IMAGE_NAMESPACE} 本地容器镜像命名空间
# ${KUBERNETES_VERSION} K8S版本
# QingHub Studio官网: https://qinghub.net
# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:
# https://qinghub.net
###############################################
function init_cluster(){echo -e "${GREEN}[INFO] ==========初始化第一个控制平面,请根据输出结果初始其他他节点========= ${NC}"kubeadm init \--apiserver-advertise-address="${QINGCLOUD_ADVERTISE_ADDRESS}" \--image-repository "${LOCAL_IMAGE_DOMAIN}/${LOCAL_IMAGE_NAMESPACE}" \--kubernetes-version "${KUBERNETES_VERSION}" \--cri-socket=unix:///run/containerd/containerd.sock \--service-cidr=10.96.0.0/16 \--pod-network-cidr=172.16.0.0/16
}# 主入口地址,根据实际可以手动执行相关内容
function main {echo -e "${GREEN}[INFO] ==========开始安装kubernetes========= ${NC}"#安装containerdinstall_cri_containerd_cni#安装cni plugininstall_cni_plugins#安装containerd 配置文件install_containerd_config#安装crictlinstall_crictl#安装runcinstall_runc#安装kubeadminstall_kubeadm#初始化集群master节点if [ "$QINGCLOUD_KUBECTL_FLAG" == "yes" ] ;theninit_clusterfiecho -e "${GREEN}[INFO] ==========完成安装kubernetes========= ${NC}"
}main
你可以通过【QingHub Studio】) 套件直接安装部署,也可以手动按如下文档操作,该项目已经全面开源,完整的脚本可以从如下开源地址获取:
开源地址: https://gitee.com/qingplus/qingcloud-platform
【QingHub Studio集成开发套件】