K8S环境规划:
Pod网段: 10.0.0.0/16
Service网段: 10.255.0.0/16
实战环境规划
操作系统:centos7.7
配置: 4G内存/6vCPU/100G硬盘
网络模式:桥接
一、初始化
1.配置静态 IP
配置master1的IP
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=10.10.1.11
NETMASK=255.255.255.0
GATEWAY=10.10.1.1
DNS1=223.5.5.5
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=ens33
DEVICE=ens33
ONBOOT=yes
配置master2的IP
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=10.10.1.12
NETMASK=255.255.255.0
GATEWAY=10.10.1.1
DNS1=223.5.5.5
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=ens33
DEVICE=ens33
ONBOOT=yes
配置master3的IP
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=10.10.1.13
NETMASK=255.255.255.0
GATEWAY=10.10.1.1
DNS1=223.5.5.5
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=ens33
DEVICE=ens33
ONBOOT=yes
配置node1的IP
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=10.10.1.21
NETMASK=255.255.255.0
GATEWAY=10.10.1.1
DNS1=223.5.5.5
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=ens33
DEVICE=ens33
ONBOOT=yes
2.配置主机名
配置master1和master2,master3,node1的本地hosts文件,都为以下
vi /etc/hosts
10.10.1.11 master1
10.10.1.12 master2
10.10.1.13 master3
10.10.1.21 node1
3.配置阿里云 repo 源,在 master1、master2、master3、node1、上操作:
备份基础 repo 源
mkdir /root/repo.bak
cd /etc/yum.repos.d/
mv * /root/repo.bak/
配置阿里云 repo 源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
清理缓存并生成新的缓存
yum clean all
yum makecache
配置国内阿里云 docker 的 repo 源,node1上操作:
yum -y install yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4.安装 docker-ce,node1、上操作:
yum install docker-ce docker-ce-cli containerd.io -y
systemctl start docker && systemctl enable docker.service && systemctl status docker
5.配置时间同步,在 master1、master2、master3、node1上操作:
安装 ntpdate 命令,
yum install ntpdate -y
跟网络源做同步
ntpdate cn.pool.ntp.org
把时间同步做成计划任务
crontab -e
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
重启 crond 服务
service crond restart
6.安装基础软件包,在 master1、master2、master3、node1上操作:
yum install -y yum-utils device-mapper-persistent-data lvm2 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 openssh-server socat ipvsadm conntrack ntpdate telnet rsync
7.关闭所有主机防火墙
master1关闭防火墙,master2关闭防火墙,master3关闭防火墙,node1关闭防火墙,,每台服务器都要操作
systemctl stop firewalld ; systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
查看是否关闭成功
getenforce
显示 Disabled 说明 selinux 已经关闭
8.配置主机之间无密码登录
master1上操作,#生成 ssh 密钥对
ssh-keygen -t rsa
#一路回车,不输入密码
把本地的 ssh 公钥文件安装到远程主机对应的账户
ssh-copy-id -i .ssh/id_rsa.pub master1
ssh-copy-id -i .ssh/id_rsa.pub master2
ssh-copy-id -i .ssh/id_rsa.pub master3
ssh-copy-id -i .ssh/id_rsa.pub node1
9.关闭交换分区 swap,在master1,master2,master3,node1上操作
临时关闭
swapoff -a
永久关闭:注释 swap 挂载,给 swap 这行开头加一下注释
vim /etc/fstab
/dev/mapper/centos-swap swap swap defaults 0 0
如果是克隆的虚拟机,需要删除 UUID
重启reboot
10.修改内核参数,在 master1、master2、master3、node1、上操作
加载 br_netfilter 模块(修改内核参数需要加载这个模块,否则报错)
modprobe br_netfilter
验证模块是否加载成功:
lsmod |grep br_netfilter
修改内核参数
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
使刚才修改的内核参数生效
sysctl -p /etc/sysctl.d/k8s.conf
11.开启 ipvs (如果不开启,端口转发就会用iptables,ipvs的转发比iptables效率高)在 master1、master2、master3、node1上操作:
vi /etc/sysconfig/modules/ipvs.modules
#!/bin/bash
ipvs_modules="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_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
/sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
if [ 0 -eq 0 ]; then
/sbin/modprobe ${kernel_module}
fi
done
#权限755,在 master1、master2、master3、node1上操作:
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs
12.配置 docker 镜像加速器,node1上操作:
tee /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com","http://qtid6917.mirror.aliyuncs.com","https://rncxm540.mirror.aliyuncs.com"],"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload (重新加载配置)
systemctl restart docker
systemctl status docker