kubernetes集群centos7初始化
阅读原文时间:2023年07月09日阅读:3

设置系统主机名以及 Host 文件的相互解析

hostnamectl set-hostname k8s-master01

安装依赖包

yum -y install wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake\
libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel\
wget vim ncurses-devel autoconf automake zlib-devel  python-devel\
epel-release lrzsz  openssh-server socat  ipvsadm conntrack bind-utils\
libffi-devel zip ntpdate ipset jq sysstat libseccomp git yum-utils

设置防火墙为 Iptables 并设置空规则

systemctl stop firewalld && systemctl disable firewalld && \
yum -y install iptables-services && systemctl start iptables && \
systemctl enable iptables && iptables -F && service iptables save

关闭 SELINUX,关闭swap

setenforce 0 && \
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && \
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux && \
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

放开文件描述符的最大值限制,默认是1024

echo "root soft nofile 65536" >> /etc/security/limits.conf
echo "root hard nofile 65536" >> /etc/security/limits.conf
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
echo "fs.file-max=655360" >> /etc/sysctl.conf
sysctl -p
echo "ulimit -n 65536" >> /etc/profile
#source /etc/profile

调整内核参数,对于 K8S

cat > kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
cp -fr kubernetes.conf /etc/sysctl.d/kubernetes.conf
sysctl -p /etc/sysctl.d/kubernetes.conf

调整系统时区

# 设置系统时区为 中国/上海
timedatectl set-timezone Asia/Shanghai
# 将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog && systemctl restart crond

关闭系统不需要服务

systemctl stop postfix && systemctl disable postfix

设置 rsyslogd 和 systemd journald

mkdir /var/log/journal # 持久化保存日志的目录
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000

# 最大占用空间 10G
SystemMaxUse=10G
# 单日志文件最大 200M
SystemMaxFileSize=200M
# 日志保存时间 2 周
MaxRetentionSec=2week
# 不将日志转发到 syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald

升级系统内核为 5.4.98-1

CentOS 7.x 系统自带的 3.10.x 内核存在一些 Bugs,导致运行的 Docker、Kubernetes 不稳定

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装 一次!
yum --enablerepo=elrepo-kernel install -y kernel-lt
# 设置开机从新内核启动
cat /boot/grub2/grub.cfg | grep '5.4.98-1' || grub2-set-default 'CentOS Linux (5.4.98-1.el7.elrepo.x86_64) 7 (Core)'
grub2-editenv list
#如果内核已经不是5.4.98-1,请根据实际情况做调整

上面的rpm文件下载失败,请从http://download.zhufunin.com/elrepo-release-7.0-3.el7.elrepo.noarch.rpm这里下载

配置docker yum源

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

配置k8s 的yum源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF

yum安装18.09版本的docker

yum list docker-ce --showduplicates |sort -r
yum install -y docker-ce-18.09.9-3.el7
systemctl enable docker && systemctl start docker
systemctl status docker

**yum安装k8s 1.16.4版kubeadm kubelet **

yum list kubeadm --showduplicates |sort -r|head -30
yum list kubelet --showduplicates |sort -r|head -30
yum install -y kubeadm-1.16.4-0.x86_64 kubelet-1.16.4-0.x86_64
systemctl enable kubelet

kube-proxy开启ipvs的前置条件

modprobe br_netfilter
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules
bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4