#! /bin/sh# 更新系统yum -y update# 安装基础软件yum -y install net-tools lrzsz wget vim# 关闭防火墙systemctl stop firewalldsystemctl disable firewalldsystemctl status firewalld# 关闭selinux,把selinux状态改为disabledgetenforcesetenforce 0 sed -i 's/^SELINUX=.*$/SELINUX=disabled/g' /etc/selinux/configgetenforce# 把服务器的时间改成统一的时区timedatectl set-timezone Asia/Shanghai# 关闭swapswapoff -ayes | cp /etc/fstab /etc/fstab_bakcat /etc/fstab_bak |grep -v swap > /etc/fstab# 设置ntp时间同步yum install -y ntpdatentpdate -u ntp.api.bz#set the file limit#步骤一#echo "ulimit -SHn 102400" >> /etc/rc.localSTR_NAME="ulimit -SHn 102400"if grep -Fxq "$STR_NAME" /etc/rc.localthen echo "setting is exist"else echo "$STR_NAME" >> /etc/rc.localfichmod +x /etc/rc.d/rc.localgrep "$STR_NAME" /etc/rc.local#步骤二#cat >> /etc/security/limits.conf << EOF#* hard nproc 10240#* soft nproc 10240#* soft nofile 65535#* hard nofile 65535#EOFSTR_NAME1="* hard nproc 10240"STR_NAME2="* soft nproc 10240"STR_NAME3="* soft nofile 65535"STR_NAME4="* hard nofile 65535"for STR_NAME in "$STR_NAME1" "$STR_NAME2" "$STR_NAME3" "$STR_NAME4"doif grep -Fxq "$STR_NAME" /etc/security/limits.confthen echo "setting is exist"else echo "$STR_NAME" >> /etc/security/limits.conffidonegrep "$STR_NAME1" /etc/security/limits.confgrep "$STR_NAME2" /etc/security/limits.confgrep "$STR_NAME3" /etc/security/limits.confgrep "$STR_NAME4" /etc/security/limits.conf#tune kernel parametrescat > /etc/sysctl.d/99-sysctl.conf << EOFvm.swappiness = 0net.ipv4.ip_forward = 1net.ipv4.conf.all.rp_filter = 1net.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 6000net.ipv4.tcp_sack = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_rmem = 4096 87380 4194304net.ipv4.tcp_wmem = 4096 16384 4194304net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 262144net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_syncookies = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 10net.ipv4.tcp_keepalive_time = 1200net.ipv4.ip_local_port_range = 5000 65000fs.file-max=65535net.ipv4.tcp_orphan_retries = 3net.ipv4.tcp_keepalive_intvl = 15kernel.sem = 250 32000 100 128EOF/sbin/sysctl -p#lock system userpasswd -l dbuspasswd -l gamespasswd -l ftppasswd -l mailpasswd -l shutdownpasswd -l haltpasswd -l operatorpasswd -l syncpasswd -l admpasswd -l lp# 添加ipvs的模块cat > /etc/sysconfig/modules/ipvs.modules <<EOF#!/bin/bashipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_fo ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"for kernel_module in \${ipvs_modules}; do/sbin/modinfo -F filename \${kernel_module} > /dev/null 2>&1if [ $? -eq 0 ]; then/sbin/modprobe \${kernel_module}fidoneEOF#查看chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs# 安装containedyum install -y yum-utils device-mapper-persistent-data lvm2yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repoyum install containerd -ycat <<EOF | sudo tee /etc/modules-load.d/containerd.confoverlaybr_netfilterEOFmodprobe overlaymodprobe br_netfiltercontainerd config default > /etc/containerd/config.tomlsed -i 's#k8s.gcr.io#registry.aliyuncs.com/google_containers#g' /etc/containerd/config.tomlsed -i '/containerd.runtimes.runc.options/a\ \ \ \ \ \ \ \ \ \ \ \ SystemdCgroup = true' /etc/containerd/config.tomlcrictl config runtime-endpoint /run/containerd/containerd.socksystemctl daemon-reloadsystemctl enable containerdsystemctl restart containerd# 配置kubernetes源cat > /etc/yum.repos.d/kubernetes.repo << EOF [kubernetes]name=Kubernetes Repobaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/gpgcheck=0gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpgenabled=1EOF#配置epel源#设置centos7的YUM源为国内阿里云源epel源cd /etc/yum.repos.d/if [ ! -f epel-7.repo ];thenwget http://mirrors.aliyun.com/repo/epel-7.repofi# 加载源配置yum clean allyum makecache# 设置hosts配置文件for STR_NAME in "192.168.117.121 k8s-master01" "192.168.117.122 k8s-master02" "192.168.117.123 k8s-master03" "192.168.117.124 k8s-node01" "192.168.117.125 k8s-node02"doif grep -Fxq "$STR_NAME" /etc/hoststhen echo "setting is exist"else echo "$STR_NAME" >> /etc/hostsfidone# 关闭UseDNS,加速ssh连接sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_configgrep UseDNS /etc/ssh/sshd_configsystemctl restart sshd