• Initialize the kubernetes basic environment configuration on CentOS 8.2


    一、Initialize the Kubernetes Basic Environment Configuration on CentOS 8.3

    1 IP 修改

    机器克隆后 IP 修改,使Xshell连接上

     

    [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32 
    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=static ### 配置静态IP,防止修改
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_FAILURE_FATAL=no
    IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=ens32
    UUID=fd9a1d20-b069-4c58-9607-40ae54cae5b6
    DEVICE=ens32
    ONBOOT=yes           ### 是否开机时就启用
    IPADDR=192.168.80.21 ### 修改ip地址
    PREFIX=24
    GATEWAY=192.168.80.2 # 修改网关地址
    DNS1=114.114.114.114 # DSN配置上,不然使用域名会找不到的
    DNS2=8.8.8.8
    IPV6_PRIVACY=no
    PEERDNS=no

     重启网卡,并测试好不好使

    [root@localhost ~]$ sudo nmcli c reload ens32
    [root@localhost ~]$ sudo nmcli c up ens32
    Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
    
    ### 重启完后测试
    [root@localhost ~]$ ping -c3 114.114.114.114 
    PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
    64 bytes from 114.114.114.114: icmp_seq=1 ttl=128 time=33.2 ms
    64 bytes from 114.114.114.114: icmp_seq=2 ttl=128 time=51.9 ms
    64 bytes from 114.114.114.114: icmp_seq=3 ttl=128 time=276 ms
    ^C
    --- 114.114.114.114 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 9ms
    rtt min/avg/max/mdev = 33.164/120.276/275.790/110.230 ms

    2 设置主机名

    sudo hostnamectl set-hostname kube-master01
    sudo hostnamectl set-hostname kube-master02
    sudo hostnamectl set-hostname kube-node01
    sudo hostnamectl set-hostname kube-node02

    3 配置hosts本地解析

    cat > /etc/hosts <<EOF
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    
    192.168.80.21 kube-master01
    192.168.80.22 kube-master02
    192.168.80.23 kube-node01
    192.168.80.24 kube-node02
    192.168.80.25 kube-vip
    EOF
    
    ###
    ping -c3 kube-master02

    4 关闭防火墙 / Selinux / Swap

    为了避免额外的干扰,需要关闭防火墙,对于公有云,内网端口一般全开放,外网端口通过安全组控制。

    ###关闭防火墙
    [root@kube-master01 ~]# systemctl stop firewalld
    [root@kube-master01 ~]# systemctl disable --now firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    
    ### CentOS 8 执行这个Xshell连接不上
    [root@kube-master01 ~]# systemctl disable --now NetworkManager
    Removed /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
    Removed /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
    Removed /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.
    
    ### 关闭Selinux
    ### 永久关闭
    [root@localhost ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
    ### 临时关闭
    [root@localhost ~]# setenforce 0
    ### 检查
    [root@localhost ~]# cat /etc/selinux/config
    [root@localhost ~]# /usr/sbin/sestatus -v
    Current mode:                   permissive
    Mode from config file:          disabled
    
    ### 先查看 Swap有没有
    [root@localhost ~]# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           1800        1208         122          15         469         417
    Swap:          2047          15        2032
    ### 关闭 Swap 分区,Swap 会影响性能
    [root@localhost ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
    vm.swappiness = 0
    [root@localhost ~]# swapoff -a
    ### 在确认下 [查看swap一行全是0就正确]
    [root@localhost ~]# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           1800        1210         110          25         479         406
    Swap:             0           0           0

    5 YUM 源配置

    本地或者自建服务器都需要配置 YUM 源,如果是云服务器由于本身就有对应云的 YUM 源,不需要配置。

    ### 备份旧的 YUM源
    cd /etc/yum.repos.d/
    mkdir backup-$(date +%F)
    mv *repo backup-$(date +%F)
    
    ### 上面看不懂,这有简单的
    [root@localhost yum.repos.d]# mkdir /etc/yum.repos.d.bak
    [root@localhost yum.repos.d]# cp /etc/yum.repos.d/* /etc/yum.repos.d.bak/
    [root@localhost yum.repos.d]# ls /etc/yum.repos.d.bak/
    
    ### 添加阿里云 YUM源
    [root@localhost yum.repos.d]# curl http://mirrors.aliyun.com/repo/Centos-7.repo -o ali.repo
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  2523  100  2523    0     0  13637      0 --:--:-- --:--:-- --:--:-- 13637

    6 基础依赖安装

    服务器最小化安装,需要安装一些常用的依赖和工具,否则后面安装可能会报错。

    ### 安装 epel 源
    yum -y install epel-release
    yum clean all
    yum makecache
    
    ### 安装常用依赖
    yum -y install gcc glibc gcc-c++ make cmake net-tools screen vim lrzsz tree dos2unix lsof \
        tcpdump bash-completion wget openssl openssl-devel bind-utils traceroute \
        bash-completion glib2 glib2-devel unzip bzip2 bzip2-devel libevent libevent-devel \
        expect pcre pcre-devel zlib zlib-devel jq psmisc tcping yum-utils device-mapper-persistent-data \
        lvm2 git device-mapper-persistent-data bridge-utils container-selinux binutils-devel \
        ncurses ncurses-devel elfutils-libelf-devel ack
    
    ### 升级服务器(看情况)
    yum -y update

    注意:YUM 安装出错,看最后一个报错标题里面找。

    CentOS 8.3 YUM Source:https://www.cnblogs.com/huaxiayuyi/p/16841258.html

    7 配置时间同步

    本地或者自建服务器都需要配置时间同步,如果是云服务器由于本身就有对应云的时间同步机制,不需要配置。

    ### 是否能通,不通自个找一个去
    ping time2.aliyun.com
    
    echo "# 互联网时间同步" >> /var/spool/cron/root
    echo "*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com >/dev/null 2>&1" >> /var/spool/cron/root

    8 互相免密

    Master 节点执行以下操作

    ssh-keygen -t rsa
    
    ### 定义 master 列表
    MASTER_LIST=(
    192.168.80.22
    192.168.80.23
    192.168.80.24
    )
    
    ### 配置免密登录
    for i in ${MASTER_LIST[@]};do
        ssh-copy-id -i /root/.ssh/id_rsa.pub root@$i
    done
    
    ### yes
    ### 输入密码

    9 内核升级

    在 Kubernetes 的 Github 仓库中

    有提到关于内核版本的问题:

    • Faster mount detection for linux kernel 5.10+ using openat2 speeding up pod churn rates. On Kernel versions less 5.10, it will fallback to using the original way of detecting mount points i.e by parsing /proc/mounts.
    • 这意味着内核 5.10 版本以后会使用 openat2 进行更快的挂载检测,所有可以将内核升级到 5.10 以后,但没必要最新。
    • 这里使用的是 5.11.16 版本,更新于 2021 年 4 月。如果想安装其它版本可以去下面网站下载:http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/

    创建用于存放安装包并下载

    mkdir -p /opt/software/kernel
    cd /opt/software/kernel
    wget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-5.11.14-1.el7.elrepo.x86_64.rpm
    wget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-devel-5.11.14-1.el7.elrepo.x86_64.rpm

    执行安装

    yum localinstall -y kernel-ml*
    
    ### 设置内核启动顺序
    grub2-set-default  0 && grub2-mkconfig -o /etc/grub2.cfg
    
    ### 查看默认内核
    grubby --default-kernel

    注意:CentOS 8.2 对以上内核升级需要安装各种依赖。

    CentOS 7.9 升级内核 kernel-ml-5.6.14版本:https://www.cnblogs.com/huaxiayuyi/p/16788084.html

    CentOS 8.2 升级内核通过elrepo源:https://www.cnblogs.com/huaxiayuyi/p/16794239.html

    10 系统优化

    对系统打开文件数进行修改,提升性能

    ulimit -SHn 65535
    cat >> /etc/security/limits.conf <<EOF
    * soft nofile 655360
    * hard nofile 131072
    * soft nproc 655350
    * hard nproc 655350
    * seft memlock unlimited
    * hard memlock unlimitedd
    EOF

    11 加载模块

    ipvs模块配置

    kube-proxy开启ipvs的前置条件

    创建配置文件

    内核 4.19+版本 nf_conntrack_ipv4 已改为 nf_conntrack ,4.18以下使用 nf_conntrack_ipv4即可。

    安装 ipvsadm,节点通信需要用到 LVS,所有需要安装 ipvsadm。ipset 和 ipvsadm (便于查看 ipvs 的代理规则)。

    yum -y install ipvsadm ipset sysstat conntrack libseccomp

    配置 ipvs 模块(内核 4.19 版本以前使用 nf_conntrack_ipv4,以后使用 nf_conntrack)。

    cat >> /etc/modules-load.d/ipvs.conf <<EOF 
    ip_vs
    ip_vs_rr
    ip_vs_wrr
    ip_vs_sh
    nf_conntrack
    ip_tables
    ip_set
    xt_set
    ipt_set
    ipt_rpfilter
    ipt_REJECT
    ipip
    EOF

    重新加载配置

    systemctl restart systemd-modules-load.service

    检查确认

    [root@localhost ~]# lsmod | grep -e ip_vs -e nf_conntrack
    ip_vs_sh               16384  0
    ip_vs_wrr              16384  0
    ip_vs_rr               16384  0
    ip_vs                 172032  6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
    nf_conntrack          172032  3 nf_nat,nft_ct,ip_vs
    nf_defrag_ipv6         20480  2 nf_conntrack,ip_vs
    nf_defrag_ipv4         16384  1 nf_conntrack
    libcrc32c              16384  4 nf_conntrack,nf_nat,xfs,ip_vs

    12 优化系统参数

    添加内核调优参数,某些参数对 Kubernetes 集群很重要(不一定是最优,各取所有)

    cat <<EOF > /etc/sysctl.d/k8s.conf
    net.ipv4.ip_forward = 1
    net.bridge.bridge-nf-call-iptables = 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_max_syn_backlog = 16384
    net.ipv4.tcp_timestamps = 0
    net.core.somaxconn = 16384
     
    net.ipv6.conf.all.disable_ipv6 = 0
    net.ipv6.conf.default.disable_ipv6 = 0
    net.ipv6.conf.lo.disable_ipv6 = 0
    net.ipv6.conf.all.forwarding = 1
    EOF

    加载所有的配置文件

    [root@localhost ~]# sysctl --system
    * Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
    kernel.yama.ptrace_scope = 0
    * Applying /usr/lib/sysctl.d/50-coredump.conf ...
    kernel.core_pattern = |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %e
    * Applying /usr/lib/sysctl.d/50-default.conf ...
    kernel.sysrq = 16
    kernel.core_uses_pid = 1
    kernel.kptr_restrict = 1
    net.ipv4.conf.all.rp_filter = 1
    net.ipv4.conf.all.accept_source_route = 0
    net.ipv4.conf.all.promote_secondaries = 1
    net.core.default_qdisc = fq_codel
    fs.protected_hardlinks = 1
    fs.protected_symlinks = 1
    * Applying /usr/lib/sysctl.d/50-libkcapi-optmem_max.conf ...
    net.core.optmem_max = 81920
    * Applying /usr/lib/sysctl.d/50-pid-max.conf ...
    kernel.pid_max = 4194304
    * Applying /etc/sysctl.d/99-sysctl.conf ...
    * Applying /etc/sysctl.d/k8s.conf ...
    net.ipv4.ip_forward = 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.tcp_max_syn_backlog = 16384
    net.ipv4.tcp_timestamps = 0
    net.core.somaxconn = 16384
    net.ipv6.conf.all.disable_ipv6 = 0
    net.ipv6.conf.default.disable_ipv6 = 0
    net.ipv6.conf.lo.disable_ipv6 = 0
    net.ipv6.conf.all.forwarding = 1
    * Applying /etc/sysctl.conf ...

    完成后就可以重启服务器

    reboot

    重启完成,在查看 ipvs 的配置效果

    lsmod | grep -e ip_vs -e nf_conntrack

     

    13 参数详解

    Kubernetes 内核优化常用参数详解

    ### 其值为0,说明禁止进行IP转发;如果是1,则说明IP转发功能已经打开
    net.ipv4.ip_forward = 1
    ### 二层的网桥在转发包时也会被iptables的FORWARD规则所过滤,这样有时会出现L3层的iptables rules去过滤L2的帧的问题
    net.bridge.bridge-nf-call-iptables = 1
    ### 是否在ip6tables链中过滤IPv6包 
    net.bridge.bridge-nf-call-ip6tables = 1
    ### 当系统有容器运行时,需要设置为1
    fs.may_detach_mounts = 1
    
    ### 0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程
    ### 1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何
    ### 2 表示内核允许分配超过所有物理内存和交换空间总和的内存
    vm.overcommit_memory=1
    
    ### OOM就是out of memory的缩写,遇到内存耗尽、无法分配的状况。kernel面对OOM的时候,咱们也不能慌乱,要根据OOM参数来进行相应的处理
    ### 值为0:内存不足时,启动 OOM killer
    ### 值为1:内存不足时,有可能会触发 kernel panic(系统重启),也有可能启动 OOM killer
    ### 值为2:内存不足时,表示强制触发 kernel panic,内核崩溃GG(系统重启)
    vm.panic_on_oom=0
    
    ### 表示同一用户同时可以添加的watch数目(watch一般是针对目录,决定了同时同一用户可以监控的目录数量)
    fs.inotify.max_user_watches=89100
    ### 所有进程最大的文件数
    fs.file-max=52706963
    ### 单个进程可分配的最大文件数
    fs.nr_open=52706963
    ### 连接跟踪表的大小,建议根据内存计算该值CONNTRACK_MAX = RAMSIZE (in bytes) / 16384 / (x / 32)
    ### 并满足nf_conntrack_max=4*nf_conntrack_buckets,默认262144
    net.netfilter.nf_conntrack_max=2310720
    
    ### KeepAlive的空闲时长,或者说每次正常发送心跳的周期,默认值为7200s(2小时)
    net.ipv4.tcp_keepalive_time = 600
    ### 在tcp_keepalive_time之后,没有接收到对方确认,继续发送保活探测包次数,默认值为9(次)
    net.ipv4.tcp_keepalive_probes = 3
    ### KeepAlive探测包的发送间隔,默认值为75s
    net.ipv4.tcp_keepalive_intvl =15
    ### Nginx 之类的中间代理一定要关注这个值,因为它对你的系统起到一个保护的作用,一旦端口全部被占用,服务就异常了
    ### tcp_max_tw_buckets 能帮你降低这种情况的发生概率,争取补救时间。
    net.ipv4.tcp_max_tw_buckets = 36000
    
    ### 只对客户端起作用,开启后客户端在1s内回收
    net.ipv4.tcp_tw_reuse = 1
    ### 这个值表示系统所能处理不属于任何进程的socket数量,当我们需要快速建立大量连接时,就需要关注下这个值了
    net.ipv4.tcp_max_orphans = 327680
    
    ### 出现大量fin-wait-1
    ### 首先,fin发送之后,有可能会丢弃,那么发送多少次这样的fin包呢?fin包的重传,也会采用退避方式 
    ### 在2.6.358内核中采用的是指数退避,2s,4s,最后的重试次数是由tcp_orphan_retries来限制的
    net.ipv4.tcp_orphan_retries = 3
    
    ### tcp_syncookies是一个开关,是否打开SYN Cookie功能,该功能可以防止部分SYN攻击
    ### tcp_synack_retries和tcp_syn_retries定义SYN的重试次数
    net.ipv4.tcp_syncookies = 1
    
    ### 进入SYN包的最大请求队列.默认1024.对重负载服务器,增加该值显然有好处
    net.ipv4.tcp_max_syn_backlog = 16384
    ### 表明系统将对最大跟踪的TCP连接数限制默认为65536
    net.ipv4.ip_conntrack_max = 65536
    ### 指定所能接受SYN同步包的最大客户端数量,即半连接上限
    net.ipv4.tcp_max_syn_backlog = 16384
    ### 在使用 iptables 做 nat 时,发现内网机器 ping 某个域名 ping 的通,而使用 curl 测试不通,
    ### 原来是 net.ipv4.tcp_timestamps 设置了为 1 即启用时间戳
    net.ipv4.tcp_timestamps = 0
    
    ### Linux中的一个kernel参数,表示socket监听(listen)的backlog上限。什么是backlog呢?
    ### backlog就是socket的监听队列,当一个请求(request)尚未被处理或建立时,他会进入backlog
    ### 而socket server可以一次性处理backlog中的所有请求,处理后的请求不再位于监听队列中
    ###当server处理请求较慢,以至于监听队列被填满后,新来的请求会被拒绝
    net.core.somaxconn = 16384

     

    二、报错

    1 域名解析不了

    ping: baidu.com: Name or service not known

    0curl: (6) Could not resolve host: mirrors.aliyun.com

    解决

    [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160
    ...
    ### 修改网关地址
    GATEWAY="192.168.192.2"
    IPV6_PRIVACY="no"
    
    ### 域名解析
    DNS1=114.114.114.114
    DNS2=8.8.8.8

    重启

    [root@localhost software]# nmcli c reload ens160
    [root@localhost software]# nmcli c up ens160
    [root@localhost yum.repos.d]# ping baidu.com
    PING baidu.com (110.242.68.66) 56(84) bytes of data.
    64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=24.0 ms
    64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=26.5 ms
    ^C
    --- baidu.com ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 4ms
    rtt min/avg/max/mdev = 24.005/25.233/26.462/1.238 ms

    2 YUM 安装出错

    [root@kube-master01 yum.repos.d]# yum -y install epel-release
    CentOS-8 - AppStream                                                                        43  B/s |  38  B     00:00    
    Error: Failed to download metadata for repo 'AppStream': Cannot prepare internal mirrorlist: No URLs in mirrorlist

    解决

    可以在/etc/yum.repos中更新repos.d使用vault.centos.org代替mirror.centos.org

    修改下面两个文件

    cd /etc/yum.repos.d
    vi CentOS-Linux-BaseOS.repo
    vi CentOS-Linux-AppStream.repo

    CentOS-Linux-BaseOS.repo 的内容修改为

    [Baseos]
    name=CentOS Linux $releasever - BaseOS
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
    #baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
    baseurl=https://vault.centos.org/centos/$releasever/BaseOS/$basearch/os/
    gpgcheck=1
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

    CentOS-Linux-AppStream.repo 的内容修改为

    [appstream]
    name=CentOS Linux $releasever - AppStream
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
    #baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
    baseurl=https://vault.centos.org/centos/$releasever/AppStream/$basearch/os/
    gpgcheck=1
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

    再执行

    yum -y install epel-release

     

    X、One Step Success

     

    Y、Error message

     

    Z、Related Links

    CentOS 8.3 安装 Kubernetes-1.25.2:https://www.cnblogs.com/huaxiayuyi/p/16831960.html

     

  • 相关阅读:
    redis分布式锁的实现
    Python安装
    Java内部类
    物联网实训室解决方案2024
    JavaScript—获取当前时间 并转化为yyyy-MM-dd hh:mm:ss格式
    测试用例设计方法-异常测试
    【报错】Error: opening registry key ‘Software\JavaSoft\Java Runtime Environment‘
    ROS仿真环境搭建
    JavaScript基础 JavaScript第三天 1. for - 循环 && 2. 数组
    关于射频同轴连接器的功率容量探讨
  • 原文地址:https://www.cnblogs.com/huaxiayuyi/p/16795089.html