在微服务盛行的今天,大大小小的公司都在实践自己的微服务架构。并且近年来又兴起了以istio为代表的Service Mesh技术体系,专注为服务之间的网络调用、限流、熔断和监控等,解耦了微服务业务。随着微服务业务增加,那么使用传统的管理和部署方式显然成本很高,而且对于开发人员的专业素质有很高要。于是后面出现了容器化技术,将微服务结合自动化发布技术部署在docker容器中。但是随着业务不断增长,越来越多的容器很难管理,于是出现了容器编排技术,目前占据份额最多的当属google开源的kubernetes项目。从本文开始将会从0开始模拟搭建一个高可用的kubernetes集群,由于生产环境机器一般不允许访问外部网络,因此我们这个系列的实践都是在断网下使用二进制部署。
一、模拟环境机器规划
二、集群环境说明
操作系统:CentOS7.7
Kubernetes版本:1.16.2
Docker版本:19.03
三、集群主控和工作节点基础准备
1..配置hosts文件
sudo cat >> /etc/hosts<<EOF
192.168.100.111 kube_cluster_master01
192.168.100.112 kube_cluster_master02
192.168.100.113 kube_cluster_master03
192.168.100.114 kube_cluster_minion01
192.168.100.115 kube_cluster_minion02
192.168.100.116 kube_cluster_minion03
192.168.100.117 kube_cluster_minion04
192.168.100.118 kube_cluster_minion05
EOF
2.修改hostname文件
sudo hostnamectl set-hostname <newhostname>
修改完成logout或者重启启动就可看到修改结果
3.关闭系统防火墙
sudo systemctl stop firewalld && systemctl disable firewalld
4.禁用swap内存交换
sudo swapoff -a && sudo echo "swapoff -a" >>/etc/rc.d/rc.local && sudo chmod +x /etc/rc.d/rc.local
5.关闭系统selinux
sudo setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
6.修改系统内核参数
sudo 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
net.ipv4.ip_local_port_range = 10000 65000
fs.file-max = 2000000
EOF
sudo sysctl --system
7.校对系统时间
搭建内网时间校正服务器,本文将时间服务器部署于192.168.100.101上,搭建服务步骤:
下载ntp-dev-4.3.99.tar.gz二进制包,解压:tar -zxvf ntp-dev-4.3.99.tar.gz
进入解压目录执行 ./configure
然后执行编译安装 make && make install
sudo vi /etc/ntp.conf 修改配置文件,如下:
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
restrict default nomodify
restrict 127.0.0.1
restrict ::1
restrict 192.168.100.0 mask 255.255.254.0 nomodify
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com
server 127.127.1.0
fudge 127.127.1.0 stratum 8
logfile /var/lib/ntp/ntp.log
disable monitor
mkdir /var/lib/ntp
touch /var/lib/ntp/ntp.log
sudo vi /usr/lib/systemd/system/ntpd.service 如下:编写ntp服务配置文件,如下:
[Unit]
Description=ntpd
After=syslog.target
[Service]
Type=forking
ExecStart=/usr/local/bin/ntpd -c /etc/ntpd.conf -p /var/run/ntpd.pid -g
PrivateTmp=true
[Install]
WantedBy=multi-user.target
使用iptables -F暂停防火墙,然后启动ntp服务systemctl enable ntpd && systemctl start ntpd
集群中的机器将101服务器上的/usr/local/bin/ntpdate文件拷贝到自己对应的目录下
集群中的机器都使用ntpdate -d 192.168.100.101同步时间,然后将同步的系统时间写入biso,如下:
其他方案:使用终端同时给个机器设置时间:date -s "2019-11-03 22:18:00" (修改成当期时间),使用clock -w把系统时间写入CMOS,使用hwclock -w将系统时间写入BISO
四、集群搭建所需安装包
ntp:http://www.ntp.org/downloads.html
kubernetes:https://github.com/kubernetes/kubernetes/releases
docker:https://download.docker.com
docker-compose:https://github.com/docker/compose/releases