代码拉取完成,页面将自动刷新
#!/bin/bash
# @FileName :k8s-install.sh
# @Time :2024/9/14
# @Author :Rohing
# Rocky9.2进行搭建的脚本
# 修改主机名为 master
hostnamectl set-hostname master
# 配置 Rocky Linux 软件源
configure_repo() {
cat > /etc/yum.repos.d/rocky.repo <<EOF
[baseos]
name=Rocky Linux \$releasever - BaseOS
baseurl=http://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
[appstream]
name=Rocky Linux \$releasever - AppStream
baseurl=http://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
EOF
yum clean all
yum makecache
}
# 安装基本工具
install_basic_tools() {
yum install -y epel-release vim wget curl net-tools git iproute-tc bash-completion chrony
}
# 禁用防火墙和 SELinux
disable_security() {
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
}
# 配置 Chrony 时间同步
configure_chrony() {
systemctl enable chronyd
systemctl start chronyd
chronyc sources
}
# 配置系统限制
configure_limits() {
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
vm.swappiness = 0
EOF
sysctl --system
}
# 安装 ELRepo 并升级内核
install_elrepo() {
yum install -y https://www.elrepo.org/elrepo-release-9.el9.elrepo.noarch.rpm
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
yum --enablerepo=elrepo-kernel install -y kernel-ml
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
}
# 配置 Kernel 模块和网络
configure_kernel_network() {
cat > /etc/modules-load.d/k8s.conf <<EOF
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
EOF
modprobe br_netfilter
modprobe ip_vs
}
# 安装 containerd 并配置
install_containerd() {
yum install -y containerd
systemctl enable containerd
systemctl start containerd
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#' /etc/containerd/config.toml
systemctl restart containerd
}
# 配置 containerd 镜像加速器
configure_registry_mirrors() {
sed -i 's#registry.k8s.io#registry.aliyuncs.com/google_containers#' /etc/containerd/config.toml
systemctl restart containerd
}
# 安装 crictl 工具
install_crictl() {
VERSION="v1.28.0" # 和 Kubernetes 版本一致
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz
tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin
chmod +x /usr/local/bin/crictl
rm -f crictl-$VERSION-linux-amd64.tar.gz
}
# 拉取 Kubernetes 相关的镜像
pull_k8s_images() {
VERSION="v1.28.0" # Kubernetes 版本
IMAGES=(
"kube-apiserver:$VERSION"
"kube-controller-manager:$VERSION"
"kube-scheduler:$VERSION"
"kube-proxy:$VERSION"
"pause:3.9"
"etcd:3.5.9-0"
"coredns/coredns:v1.10.1"
)
for IMAGE in "${IMAGES[@]}"; do
crictl pull m.daocloud.io/registry.k8s.io/$IMAGE
done
}
# 安装 Kubernetes 组件
install_k8s_components() {
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
EOF
setenforce 0
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
systemctl start kubelet
}
# 初始化 Kubernetes Master 节点
init_k8s_master() {
kubeadm init --kubernetes-version=v1.28.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.1.151 --cri-socket unix:///var/run/containerd/containerd.sock
# 配置 kubectl 使用 admin 配置文件
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
echo "Kubernetes master 已成功初始化。"
echo "请使用以下命令将其他节点加入集群:"
kubeadm token create --print-join-command
}
# 运行所有功能
main() {
configure_repo
install_basic_tools
disable_security
configure_chrony
configure_limits
install_elrepo
configure_kernel_network
install_containerd
configure_registry_mirrors
install_crictl
pull_k8s_images
install_k8s_components
init_k8s_master
}
main
echo "接下来需要网络插件,请自行选择"
echo "kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
echo "https://projectcalico.docs.tigera.io/about/about-calico"
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。