k8s二进制优化

#!/bin/bash
#更换源
set_repo(){
sed -i 's/cn.archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
}
#设置系统DNS解析服务器
set_nameserver(){
        echo "nameserver 8.8.8.8" >> /etc/resolv.conf
}

#设置系统时区
set_zone(){
        timedatectl set-timezone Asia/Shanghai
        ln -svf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

}
# 安装常用软件
install_apt(){
        apt update 
        apt install -y wget psmisc vim net-tools nfs-kernel-server telnet lvm2 git tar curl htop iftop lrzsz unzip telnet tcpdump ipvsadm ipset sysstat conntrack
}

#关闭防火墙
stop_firewalld(){
        systemctl stop ufw
        systemctl disable ufw
}

#关闭apporamor
appro_close(){
        systemctl disable --now apparmor
}

#设置字体
set_font(){
cat <<EOF >>  ~/.bashrc 
PS1='[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \W\[\e[0m\]]# '
EOF
source ~/.bashrc
}

#优化sshd
set_sshd(){
        sed -i 's@#UseDNS yes@UseDNS no@g' /etc/ssh/sshd_config
        sed -i 's@^GSSAPIAuthentication yes@GSSAPIAuthentication no@g' /etc/ssh/sshd_config
        systemctl restart ssh

}

#优化内核

set_limmit(){
cat >> /etc/security/limits.conf <<'EOF'
* soft nofile 655360
* hard nofile 131072
* soft nproc 655350
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
EOF

}
set_swap(){
#关闭交换内存
    sed -ri 's/.*swap.*/#&/' /etc/fstab
    swapoff -a && sysctl -w vm.swappiness=0
}
set_ipvs(){
cat > /etc/modules-load.d/ipvs.conf << 'EOF'
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
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
EOF
    systemctl restart systemd-modules-load.service
}

set_kernel(){
cat > /etc/sysctl.d/k8s.conf <<'EOF'
net.ipv4.ip_forward = 1
fs.may_detach_mounts = 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_timestamps = 0
net.core.somaxconn = 16384
EOF
sysctl --system
}

print(){
        set_repo
        set_nameserver
        set_zone
        install_apt
        stop_firewalld
        appro_close
        set_font
        set_sshd
        set_limmit
        set_swap
        set_ipvs
        ser_kernel
        echo "优化完成"
}
print
posted on 2026-09-04 23:11  小二jerry  阅读(5)  评论(0)    收藏  举报