k8s 集群安装


k8s 集群安装

1.1 安装前准备

1.1.1 网络拓扑及准备

我们提前安装好了ikuai路由器用于网络转发及网络控制

  • 安装单网卡机器,如果不是单网卡,在初始化k8s前先关闭其他网卡,初始化完以后再打开
  • k8s节点cpu必须大于2核心,内存3G以上 存储空间100G以上,去掉swap分区,因为容器会创建在swap分区影响pod,官方建议关闭,boot建议800M,因为后面可能升级内核
  • selinux 关闭,firewalld关闭,iptables可以打开,但是规则为空,k8s部署好后再添加规则,如果有两块网卡,不能配置两个网关

网络拓扑大概是下面这样的

k8s集群都是仅主机模式,通过ikuai路由器与外界通信

1.1.2 机器网卡配置

我们这里准备了四台机器,
一台ikuai路由器: 192.168.50.120
三台Centos7均为仅主机模式的机器,dns和网关都设置成 192.168.50.120:
master1 192.168.50.121
node1 192.168.50.122
node2 192.168.50.123

网卡配置参考:

TYPE=Ethernet
BOOTROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.50.121
NETMASK=255.255.255.0
GATEWAY=192.168.50.120
DNS1=192.168.50.120
DNS2=8.8.8.8

修改完网卡后重启网卡,再在仅主机模式的master节点 192.168.50.120 上测试,可以发现仅主机模式,不能连上外网的机器可以通过ikuai路由器访问外网了

1.1.3 修改主机名

分别修改三台centos7的主机名,否则k8s注册的时候可能会因为主机名称相同报错

systemctl set-hostname master1
systemctl set-hostname node1
systemctl set-hostname node2

修改/etc/hosts文件
这里的m1,n1,n2是别名,也可以不写

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.50.121	master1 m1
192.168.50.122  node1   n1
192.168.50.123  node2   n2

修改完后,拷贝这个文件给其他机器

scp /etc/hosts root@192.168.50.122:/etc/hosts
scp /etc/hosts root@192.168.50.123:/etc/hosts

1.1.4 安装依赖包

在xshell这里可以设置当前窗口全部执行一个命令,避免同一命令重复粘贴操作

yum -y  install conntrack ntpdate ntp ipvsadm ipset iptables-services curl sysstat libseccomp wget vim net-tools git

1.1.5 关闭防火墙及selinux,关闭swap

#开启iptables并清空规则
systemctl restart iptables;
systemctl enable iptables;
iptables -F;
service iptables save
#关闭firewalld
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux
setenforce 0;sed -i "s/enable/disabled/g" /etc/selinux/config
#关闭swap并注意确认 /etc/fstab里面没有swap的配置,有的话就注掉
swapoff -a;

1.1.6 时间同步并设置时区

正常来说都会把master作为时间同步的server,node作为时间同步的client,甚至有的公司会搭建自己的时间服务器进行同步,k8s的节点之间有的是基时间戳来通信的
我们这里跟阿里云的时间进行同步

ntpdate -u ntp1.aliyun.com

这里k8s的node都是把master作为时间服务器去同步时间的,所以我们
在master节点上执行

yum -y install chrony
vim /etc/chrony.conf
#修改配置切换成阿里云的时间服务器
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
#设置白名单
allow 192.168.50.0/24
#设置权重
local stratum 10

修改后重启chronyd
systemctl restart chronyd
systemctl enable chronyd

在另外两台node节点上执行

vim /etc/chrony.conf
#修改配置
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.50.120 iburst

systemctl restart chronyd
systemctl enable chronyd

#时区设置为上海
timedatectl set-timezone Asia/Shanghai
#将当前的UTC时间写入硬件时钟
timedatectl set-local-rtc 0

#重启依赖系统时间的服务
systemctl restart crond
systemctl restart rsyslog

1.1.7 内核参数修改

cat kubernets.conf 
#这里的配置也适用于docker,dockerrun的时候有时候会报错
#在ipv4的网络下,所有经过网桥的流量都必须经过防火墙处理
net.bridge.bridge-nf-call-iptables=1
#在ipv6的网络下,所有经过网桥的流量都必须经过防火墙处理
net.bridge.bridge-nf-call-ip6tables=1
#开启网络转发
net.ipv4.ip_forward=1
#禁止使用swap空间,只有当系统oom的时候才允许使用它
vm.swappiness=0
#不检查物理内存是否够用
vm.overcommit_memory=1
#开启oom
vm.panic_on_oom=0
fs.file-max=52706963
fs.nr_open=52706963
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576

#每次重启都会执行 /etc/sysctl.d/下面的文件
cp kubernets.conf /etc/sysctl.d/
sysctl -p /etc/sysctl.d/kubernets.conf

#下面的两个报错可以先忽略,因为我们现在还没用网桥
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-iptables: No such file or directory
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: No such file or directory

1.1.8 关闭不必要的服务

systemctl stop postfix && systemctl disable postfix

1.1.9 设置rsyslogd和systemd journald

centos7有两套日志收集系统,rsyslogd是从centos6过来的,当系统有两套系统日志收集时会对增加磁盘的压力,所以我们这里关闭rsyslogd,使用 journald

#持久化保存日志的目录
mkdir -p /var/log/journal
mkdir -p /etc/systemd/journald.conf.d
vim /etc/systemd/journald.conf.d/99-prophet.conf
[Journal]
#持久化保存到磁盘
Storage=persistent
#压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
#最大占用空间
SystemMaxUse=10G
#单日志文件最大200M
SystemMaxFileSize=200M
#日志保存时间2周
MaxRetentionSec=2week
#不将日志转发到syslog,这是重点,相当于关闭了rsyslog
ForwardToSyslog=no

systemctl restart systemd-journald

1.1.10 升级内核版本到4.44

红帽公司出的centos和ubuntu相当于汽车,他的内核就是发动机,发动机可以替换
红帽官方系列是不允许我们自己升级内核的,只允许向官方申请更新才允许更新
centos允许自己升级内核,但是没有提供新的内核,我们可以去外网下载内核

centos7.X自带的3.10.x内核存在一些bug,导致运行的Docker和k8s不稳定

#安装el源
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
#指定el源并安装4.44内核版本,kernel-lt即代表4.44
yum --enablerepo=elrepo-kernel install -y kernel-lt

内核安装后注意安装的信息,我们接下来会用到

#注意这里的5.4.194-1.el7.elrepo.x86_64不是随便写的,是上面你安装完后,最后几行中的小版本信息
#设置开机从新内核启动
grub2-set-default 'CentOS Linux (5.4.194-1.el7.elrepo.x86_64) 7 (Core)'

重启完成后执行确认内核是否升级成功

uname -r