环境准备

1991/6/26 基础

# 1.系统配置

在安装之前,需要先做好如下准备。3台CentOS 7.9主机如下:

服务器 IP 配置 系统
mater 192.168.148.130 2核4G CentOS 7.9
node1 192.168.148.131 2核2G CentOS 7.9
node2 192.168.148.132 2核2G CentOS 7.9

# 2.修改主机名

为了后面方便区分,对主机进行重命名

# 2.1 查看当前主机名

$ hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 465267a122c2dd41a8a89da8ba435f2e
           Boot ID: c09efb32686a468f8a4601f517af6135
    Virtualization: kvm
  Operating System: CentOS Linux 8
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 4.18.0-305.10.2.el8_4.x86_64
      Architecture: x86-64
1
2
3
4
5
6
7
8
9
10
11

# 2.2 修改主机名

$ hostnamectl set-hostname k8s-master
1

# 3.关闭防火墙

关于防火墙的原因(nftables后端兼容性问题,产生重复的防火墙规则)

$ systemctl stop firewalld && systemctl disable  firewalld
$ systemctl stop NetworkManager && systemctl disable NetworkManager
1
2

# 4.关闭Swap分区

kubeadm默认会先检查当前主机是否禁用了Swap,如果未禁用,则会强制终止部署k8s过程。

# 4.1 临时关闭

$ swapoff -a
1

# 4.2 永久关闭

避免开机启动交换空间,需要把/etc/fstab中的swap注释掉

$ sed -i 's/.*swap.*/#&/' /etc/fstab
1

# 5. 禁用selinux

关闭selinux后,容器便可允许访问宿主机的文件系统。

# 5.1 临时关闭

$ setenforce 0
1

# 5.2 永久关闭

$ sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
1

# 6. IPv4流量转到iptables的链

# 6.1 编辑配置文件

cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
1
2
3
4
5

注意:将桥接的IPv4流量传递到iptables的链 由于开启内核 ipv4 转发需要加载 br_netfilter 模块,需执行 modprobe br_netfilter

bridge-nf 使得 netfilter 可以对 Linux 网桥上的 IPv4/ARP/IPv6 包过滤。比如,设置net.bridge.bridge-nf-call-iptables=1后,二层的网桥在转发包时也会被 iptables的 FORWARD 规则所过滤。常用的选项包括:

  • net.bridge.bridge-nf-call-arptables:是否在 arptables 的 FORWARD 中过滤网桥的 ARP 包
  • net.bridge.bridge-nf-call-ip6tables:是否在 ip6tables 链中过滤 IPv6 包
  • net.bridge.bridge-nf-call-iptables:是否在 iptables 链中过滤 IPv4 包
  • net.bridge.bridge-nf-filter-vlan-tagged:是否在 iptables/arptables 中过滤打了 vlan 标签的包。

# 6.2 执行生效

$ sysctl -p /etc/sysctl.d/k8s.conf
1

# 7. 设置固定IP

本文使用的是VMware Fusion,搭建的虚拟机,设置静态IP的方法,参考下面链接。

Mac VM CentOS7配置静态IP: https://cloud.tencent.com/developer/article/1831323

# 8. 服务器时间

# 8.1 设置时区

$ timedatectl set-timezone Asia/Shanghai
1

# 8.2 同步时间

# 安装chrony
$ yum install chrony -y
# 设置开机启动
$ systemctl enable chronyd --now
# 同步时间
$ chronyc sources
1
2
3
4
5
6

# 9. 配置DNS解析

$ cat >> /etc/hosts << EOF
192.168.148.130 master
192.168.148.131 node1
192.168.148.132 node2
EOF
1
2
3
4
5

问题:k8s集群安装时节点是否需要配置dns解析?

kubectl是通过kubectl get node出来的名称去连接的,所以我们需要在宿主机上能够解析到它。如果解析不到,可能连不上;

# 10. 安装IPVS

# 10.1 配置文件

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
1
2
3
4
5
6
7
8

# 10.2 执行生效

$ chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
1

上面脚本创建了的/etc/sysconfig/modules/ipvs.modules文件,保证在节点重启后能自动加载所需模块。使用lsmod | grep -e ip_vs -e nf_conntrack_ipv4命令查看是否已经正确加载所需的内核模块。

# 10.3 安装ipsetipvsadm

接下来还需要确保各个节点上已经安装了ipset软件包,为了便于查看ipvs的代理规则,最好安装一下管理工具ipvsadm

$ yum install -y ipset ipvsadm
1

# 11. 安装Containerd

# 11.1 下载解压

# 下载二进制包
$ wget https://github.com/containerd/containerd/releases/download/v1.6.4/cri-containerd-cni-1.6.4-linux-amd64.tar.gz
# 解压到系统的根目录/中
$ tar -zxvf cri-containerd-cni-1.6.4-linux-amd64.tar.gz -C /
etc/
etc/systemd/
etc/systemd/system/
etc/systemd/system/containerd.service
etc/crictl.yaml
etc/cni/
etc/cni/net.d/
etc/cni/net.d/10-containerd-net.conflist
usr/
usr/local/
usr/local/sbin/
usr/local/sbin/runc
usr/local/bin/
usr/local/bin/critest
usr/local/bin/containerd-shim
usr/local/bin/containerd-shim-runc-v1
usr/local/bin/ctd-decoder
usr/local/bin/containerd
usr/local/bin/containerd-shim-runc-v2
usr/local/bin/containerd-stress
usr/local/bin/ctr
usr/local/bin/crictl
......
opt/cni/
opt/cni/bin/
opt/cni/bin/bridge
......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

最新包地址:https://github.com/containerd/containerd/releases/tag/v1.6.6

# 11.2 配置文件

# a. 生成默认文件

# 创建目录,有则不创建
$ mkdir -p /etc/containerd 
# 生成默认配置文件
$ containerd config default > /etc/containerd/config.toml
1
2
3
4

# b. 把cgroup driver设为systemd

# vim /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
  ...
  [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
    SystemdCgroup = true # 将这里设置为true
1
2
3
4
5

# c. 修改镜像源地址

[plugins."io.containerd.grpc.v1.cri"]
  ...
  # sandbox_image = "k8s.gcr.io/pause:3.6"
  sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.7"
1
2
3
4

# 11.3 设置开机启动

$ systemctl enable containerd --now
1

# 11.4 测试结果

$ crictl version
Version:  0.1.0
RuntimeName:  containerd
RuntimeVersion:  v1.6.4
RuntimeApiVersion:  v1alpha2
1
2
3
4
5