!/bin/bash
function node_update_kernel() {
启用 ELRepo 仓库
sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-6.el7.elrepo.noarch.rpm
查看可用的系统内核包
sudo yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
安装
sudo yum --enablerepo=elrepo-kernel install kernel-ml -y
查看系统上的所有可以内核
sudo awk -F' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
以下两种方式均可以设置默认使用的内核
sudo grub2-set-default 0
生成 grub 配置文件并重启
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
sudo reboot
}
function node_init_config() {
#######################################################Host配置#############################################################
echo "10.202.241.29 prod-app-yunhe-node01" >> /etc/hosts
echo "10.202.241.30 prod-app-yunhe-node02" >> /etc/hosts
echo "10.202.241.31 prod-app-yunhe-node03" >> /etc/hosts
#######################################################Yum源配置#############################################################
系统源替换为阿里源
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
添加 kubernetes 源。需提前切换为管理员权限
cat <
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
EOF
安装必要工具
sudo yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 -y
关闭防火墙
sudo systemctl stop firewalld.service
sudo systemctl disable --now firewalld
关闭 selinux
sudo setenforce 0
sudo sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
sudo sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
关闭交换分区
sudo swapoff -a && sysctl -w vm.swappiness=0
sudo sed -ri '/[#]*swap/s@^@#@' /etc/fstab
ulimit -SHn 65535
末尾添加如下内容
echo "* soft nproc 65535" >> /etc/security/limits.conf
echo "* hard nproc 655350" >> /etc/security/limits.conf
echo "* soft memlock unlimited" >> /etc/security/limits.conf
echo "* hard memlock unlimited" >> /etc/security/limits.conf
安装并配置 ipvs
sudo yum install ipvsadm ipset sysstat conntrack libseccomp -y
sudo modprobe -- ip_vs
sudo modprobe -- ip_vs_rr
sudo modprobe -- ip_vs_wrr
sudo modprobe -- ip_vs_sh
sudo modprobe -- nf_conntrack
加入以下内容
echo "ip_vs" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_lc" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_wlc" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_rr" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_wrr" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_lblc" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_lblcr" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_dh" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_sh" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_fo" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_nq" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_sed" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_ftp" >> /etc/modules-load.d/ipvs.conf
echo "ip_vs_sh" >> /etc/modules-load.d/ipvs.conf
echo "nf_conntrack" >> /etc/modules-load.d/ipvs.conf
echo "ip_tables" >> /etc/modules-load.d/ipvs.conf
echo "ip_set" >> /etc/modules-load.d/ipvs.conf
echo "xt_set" >> /etc/modules-load.d/ipvs.conf
echo "ipt_set" >> /etc/modules-load.d/ipvs.conf
echo "ipt_rpfilter" >> /etc/modules-load.d/ipvs.conf
echo "ipt_REJECT" >> /etc/modules-load.d/ipvs.conf
echo "ipip" >> /etc/modules-load.d/ipvs.conf
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
sudo systemctl enable --now systemd-modules-load.service !!!
cat <
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
net.ipv4.conf.all.route_localnet = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF
sudo sysctl --system
modprobe br_netfilter
echo "Docker安装开始...."
echo -e '\033[32m Docker安装开始: \033[0m'
使用yum下载yum-utils工具
sudo yum install -y yum-utils
设置docker的阿里云镜像源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装指定版本的Docker
有可能提示的报错缺少版本
在文件顶部添加一个条目/etc/yum.repos.d/docker-ce.repo
[centos-extras]
name=Centos extras - $basearch
baseurl=http://mirror.centos.org/centos/7/extras/x86_64
enabled=1
gpgcheck=0
yum -y install slirp4netns fuse-overlayfs container-selinux
sudo yum install docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io -y
启动docker
sudo systemctl start docker
cat <
{
"live-restore": true,
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"graph":"/data/docker/graph",
"registry-mirrors": ["https://v16stybc.mirror.aliyuncs.com"],
"insecure-registries": ["172.31.23.167:8888"],
"log-opts": {
"max-size": "100m",
"max-file": "10"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
systemctl daemon-reload
systemctl restart docker
systemctl enable docker.service
echo -e '\033[32m Docker安装结束: \033[0m'
echo -e '\033[32m Kubernetes安装开始: \033[0m'
使用yum install命令安装指定的K8S版本
yum install -y kubelet-1.23.9 kubeadm-1.23.9 kubectl-1.23.9 kubernetes-cni-0.8.7
启动kubelet服务,并设置开机自启
systemctl enable kubelet && systemctl start kubelet
echo -e '\033[32m Kubernetes安装结束: \033[0m'
echo -e '\033[32m rbd模块安装开始: \033[0m'
确认安装lvm2
yum install lvm2 -y
启用rbd模块
modprobe rbd
cat > /etc/rc.sysinit << EOF
!/bin/bash
for file in /etc/sysconfig/modules/*.modules
do
[ -x $file ] && $file
done
EOF
cat > /etc/sysconfig/modules/rbd.modules << EOF
modprobe rbd
EOF
chmod 755 /etc/sysconfig/modules/rbd.modules
lsmod |grep rbd
echo -e '\033[32m rbd模块安装结束: \033[0m'
}
echo -e '\033[32m #####################使用介绍######################### \033[0m'
echo -e '\033[32m 配置检查: \033[0m'
echo -e '\033[31m ./op.sh -i kernel \033[0m'
echo -e '\033[31m ./op.sh -t init \033[0m'
echo -e '\033[32m ####################介绍完成######################### \033[0m'
while getopts ":c:i:r:" opt
do
case $opt in
c)
echo "参数a的值$OPTARG"
;;
i)
if [ $OPTARG = kernel ];then
node_update_kernel
elif [ $OPTARG = init ];then
node_init_config
fi
;;
r)
if [ $OPTARG = hc ];then
hc_nginx_reload_all
elif [ $OPTARG = admin ];then
admin_nginx_reload_all
elif [ $OPTARG = miaotou ];then
miaotou_nginx_reload_all
fi
;;
?)
echo "未知参数"
exit 1;;
esac
done
(这里要添加个host k8s-dev.com )
设置主节点
kubeadm init --control-plane-endpoint hangkong-k8s.vip.com:6443 --image-repository registry.aliyuncs.com/google_containers --service-cidr=172.130.0.0/16 --pod-network-cidr=172.31.0.0/16 --kubernetes-version=1.23.9 --upload-certs
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u)