参考:https://www.bilibili.com/video/av66617940 尚硅谷 Kubernetes 汪洋

环境说明

序号 操作系统 配置 IP HOSTNAME 用途
1 CentOS 7.6 4C 6G 100G 192.168.5.80 k8s-master01 k8s主节点
2 CentOS 7.6 4C 6G 100G 192.168.5.81 k8s-node01 k8s工作节点1
3 CentOS 7.6 4C 6G 100G 192.168.5.82 k8s-node02 k8s工作节点2
4 CentOS 7.6 4C 2G 100G 192.168.5.89 Harbor Harbor私有仓库
5 CentOS 7.4 2C 2G 40G 192.168.5.240 koolshare 软路由

操作步骤

1. 设置静态IP

  1. $ vi /etc/sysconfig/network-scripts/ifcfg-ens33
  1. BOOTPROTO=static
  2. ONBOOT=yes
  3. IPADDR=192.168.5.95
  4. NETMASK=255.255.255.0
  5. GATEWAY=192.168.5.241
  6. DNS1=192.168.5.241
  7. DNS2=114.114.114.114
  8. DNS3=8.8.8.8

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

  1. [root@k8s-master01 ~]$ hostnamectl set-hostname k8s-master01
  2. [root@k8s-node01 ~]$ hostnamectl set-hostname k8s-node01
  3. [root@k8s-node02 ~]$ hostnamectl set-hostname k8s-node02
  4. [root@hub ~]$ hostnamectl set-hostname hub

修改 /etc/hosts

  1. 192.168.5.80 k8s-master01
  2. 192.168.5.81 k8s-node01
  3. 192.168.5.82 k8s-node02
  1. $ scp /etc/hosts root@k8s-node01:/etc/hosts
  2. $ scp /etc/hosts root@k8s-node02:/etc/hosts

3. 安装依赖包

  1. $ yum install -y conntrack ntpdate ntp ipvsadm ipset jq \
  2. iptables curl sysstat libseccomp wget vim \
  3. net-tools git

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

  1. $ systemctl stop firewalld && systemctl disable firewalld
  2. $ yum -y install iptables-services && systemctl start iptables && systemctl enable iptables \
  3. && iptables -F && service iptables save

5. 关闭 SELINUX

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

6. 调整内核参数,对于 K8S

  1. cat > kubernetes.conf <<EOF
  2. net.bridge.bridge-nf-call-iptables=1
  3. net.bridge.bridge-nf-call-ip6tables=1
  4. net.ipv4.ip_forward=1
  5. net.ipv4.tcp_tw_recycle=0
  6. vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它
  7. vm.overcommit_memory=1 # 不检查物理内存是否够用
  8. vm.panic_on_oom=0 # 开启 OOM
  9. fs.inotify.max_user_instances=8192
  10. fs.inotify.max_user_watches=1048576
  11. fs.file-max=52706963
  12. fs.nr_open=52706963
  13. net.ipv6.conf .all.disable_ipv6=1
  14. net.netfilter.nf_conntrack_max=2310720
  15. EOF

PS:必备参数 net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-ip6tables=1 net.ipv6.conf .all.disable_ipv6=1

  1. $ cp kubernetes.conf /etc/sysctl.d/kubernetes.conf
  2. $ sysctl -p /etc/sysctl.d/kubernetes.conf

7. 调整系统时区

  1. # 查看时区
  2. $ timedatectl
  3. # 设置系统时区为 中国/上海
  4. $ timedatectl set-timezone Asia/Shanghai
  5. # 将当前的 UTC 时间写入硬件时钟
  6. $ timedatectl set-local-rtc 0
  7. # 重启依赖于系统时间的服务
  8. $ systemctl restart rsyslog
  9. $ systemctl restart crond

8. 关闭系统不需要服务(邮件服务)

  1. $ systemctl stop postfix && systemctl disable postfix

9. 设置 rsyslogd 和 systemd journald

  1. $ mkdir /var/log/journal # 持久化保存日志的目录
  2. $ mkdir /etc/systemd/journald.conf.d
  3. cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
  4. [Journal]
  5. # 持久化保存到磁盘
  6. Storage=persistent
  7. # 压缩历史日志
  8. Compress=yes
  9. SyncIntervalSec=5m
  10. RateLimitInterval=30s
  11. RateLimitBurst=1000
  12. # 最大占用空间 10G
  13. SystemMaxUse=10G
  14. # 单日志文件最大 200M
  15. SystemMaxFileSize=200M
  16. # 日志保存时间 2 周
  17. MaxRetentionSec=2week
  18. # 不将日志转发到 syslog
  19. ForwardToSyslog=no
  20. EOF
  21. $ systemctl restart systemd-journald

10. 升级系统内核为 4.44

CentOS 7.x 系统自带的 3.10.x 内核存在一些 Bugs ,导致运行的 Docker、Kubernetes 不稳定,例如: rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

  1. $ rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
  2. # 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装一次!
  3. $ yum --enablerepo=elrepo-kernel install -y kernel-lt
  4. # 查看当前内核列表
  5. $ awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
  6. # 设置开机从新内核启动
  7. $ grub2-set-default 'CentOS Linux (4.4.206-1.el7.elrepo.x86_64) 7 (Core)'

PS:由于系统会优先选择系统默认内核,所以在 reboot 后内核又使用之前的了。 grub2-set-default “CentOS Linux (4.4.206-1.el7.elrepo.x86_64) 7 (Core)” && reboot

11. 安装 Harbor 虚拟机

  • 设置 IP 为 192.168.5.89
  • 安装 Docker 环境
    1. $ hostnamectl set-hostname hub