目录
- 一.架构介绍
- 二.所有机器初始化
- 1. 配置ip
- 2. 配置dns
- 3. 操作系统初始化
- 三(1).两台master配置(主从模式)
- 1. 安装yum -y install keepalived ipvsadm
- 2. 配置master01
- 4. 负载均衡效果展示
- 5. 监控效果展示,当某台node挂掉或重启,都会自动上下线
- 6. 高可用效果展示
- 三(2).两台master配置(双主模式)
- 1. 安装
- 2. 配置master01
- 4. 效果展示
- 5. 高可用展示
- 6. node机展示
- 四.两台node配置
- 1. 检查gcc是否安装
- 2. 添加nginx的centos7源
- 3. 安装nginx 并设置开机自动运行
- 4. 配置nginx,并添加不同html信息
- 5. 效果观察
- 4. 两台机器安装net-tools 实现 ifconfig 和 route 的命令管理操作
- 5(1). 两台机器配置两台机器的LVS-DR模式(主从模式)
- 5(2). 两台机器配置两台机器的LVS-DR模式(双主模式)
- 6. 运行配置
- 7. 查看lo配置,两台node都一样
一.架构介绍
镜像都用的centos 7
机器名 Ip地址 安装 备注
关于为什么实验中双主机模式也只有两个node,因为又懒又穷,嘤
二.所有机器初始化
1. 配置ip
vi /etc/sysconfig/network-scripts/ifcfg-ens33
新增部分在下图位置看
2. 配置dns
vi /etc/resolv.conf
重启网络 service network restart
3. 操作系统初始化
关闭防火墙systemctl stop firewalld
systemctl disable firewalld
关闭selinux
永久关闭sed -i 's/enforcing/disabled/' /etc/selinux/config
临时关闭setenforce 0
根据规划设置主机名【master01节点上操作】hostnamectl set-hostname master01
根据规划设置主机名【master02节点上操作】hostnamectl set-hostname master02
根据规划设置主机名【node01节点操作】hostnamectl set-hostname node01
根据规划设置主机名【node02节点操作】hostnamectl set-hostname node02
- 添加hosts
cat >> /etc/hosts << EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.84.131 master01
192.168.84.132 master02
192.168.84.133 node01
192.168.84.134 node02
192.168.84.135 vip01
192.168.84.136 vip02
EOF
- 将桥接的IPv4流量传递掉iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
EOF
生效 sysctl --system
- 时间同步
yum install ntpdate -y
ntpdate time.windows.com
三(1).两台master配置(主从模式)
1. 安装yum -y install keepalived ipvsadm
2. 配置master01
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_1
}
vrrp_instance VI_1 {
state MASTER #备用机为BACKUP
interface ens33 #绑定VIP的网卡为ens33,根据自己服务器设置
virtual_router_id 51
priority 100 #权重,主机应该更大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.84.135 # vip01
}
}
virtual_server 192.168.84.135 80 { #定义RealServer对应的VIP及服务端口
delay_loop 6 #每过6秒查询node的状态
lb_algo rr #lvs负载均衡算法,rr为轮询
lb_kind DR #DR模式
protocol TCP #用tcp协议检查node状态
#下面是配置被负载均衡的真实服务器nginx
real_server 192.168.84.133 80 {
weight 2 #权重
TCP_CHECK {
connect_timeout 1 #1秒无响应超时
nb_get_retry 2 #重连次数3次
delay_before_retry 1 #重连间隔时间
connect_port 80 #健康检查realserver的端口
}
}
real_server 192.168.84.134 80 {
weight 2
TCP_CHECK {
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
connect_port 80
}
}
}
- 配置master02
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.84.135 # vip01
}
}
virtual_server 192.168.84.135 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.84.133 80 {
weight 2
TCP_CHECK {
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
connect_port 80
}
}
real_server 192.168.84.134 80 {
weight 2
TCP_CHECK {
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
connect_port 80
}
}
}
然后启动并设置开机自启systemctl start keepalived.service
systemctl enable keepalived.service
然后先跳到node配置上,配完再回来看效果!
4. 负载均衡效果展示
5. 监控效果展示,当某台node挂掉或重启,都会自动上下线
ipvsadm -l
关掉一台后
6. 高可用效果展示
使用命令 ip addr
vip在master01上,此时我们关闭master01,查看master02
成功,且vip可以顺利访问
三(2).两台master配置(双主模式)
1. 安装
yum -y install keepalived ipvsadm
2. 配置master01
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_1
}
vrrp_instance VI_1 {
state MASTER #备用机为BACKUP
interface ens33 #绑定VIP的网卡为ens33,根据自己服务器设置
virtual_router_id 51
priority 100 #权重,主机应该更大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.84.135 # vip01
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 55
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.84.136 # vip02
}
}
virtual_server 192.168.84.135 80 {
delay_loop 6 #每过6秒查询node的状态
lb_algo rr #lvs负载均衡算法,rr为轮询
lb_kind DR #DR模式
protocol TCP #用tcp协议检查node状态
#下面是配置被负载均衡的真实服务器nginx
real_server 192.168.84.133 80 {
weight 2 #权重
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
}
virtual_server 192.168.84.136 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.84.134 80 {
weight 2
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
}
- 配置master02
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.84.135 # vip01
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.84.136 # vip02
}
}
virtual_server 192.168.84.135 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.84.133 80 {
weight 2
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
}
virtual_server 192.168.84.136 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.84.134 80 {
weight 2
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
}
然后启动并设置开机自启systemctl start keepalived.service
systemctl enable keepalived.service
然后先跳到node配置上,配完再回来看效果!
4. 效果展示
5. 高可用展示
使用命令 ip addr
此时两台master绑定着不同的vip
当我们关闭master02,master01就拥有了两个vip,master02恢复后,vip02自动回去
6. node机展示
使用命令 ipvsadm -l
四.两台node配置
1. 检查gcc是否安装
使用 gcc -v
若无则 yum -y install gcc
2. 添加nginx的centos7源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
3. 安装nginx 并设置开机自动运行
yum install -y nginx
systemctl start nginx.service
systemctl enable nginx.service
4. 配置nginx,并添加不同html信息
nginx服务的默认配置文件在 /etc/nginx/conf.d/default.conf ,项目部署目录为/usr/share/nginx/html/
我们进入vi /usr/share/nginx/html/index.html修改html文件,方便观察
5. 效果观察
4. 两台机器安装net-tools 实现 ifconfig 和 route 的命令管理操作
yum install net-tools -y
5(1). 两台机器配置两台机器的LVS-DR模式(主从模式)
vi /usr/local/sbin/lvs_dr_rs.sh
#/bin/bash
vip=192.168.84.135
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#限制arp响应请求
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
#限制arp发送请求
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
5(2). 两台机器配置两台机器的LVS-DR模式(双主模式)
vi /usr/local/sbin/lvs_dr_rs.sh
#/bin/bash
vip01=192.168.84.135
vip02=192.168.84.136
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifconfig lo:0 $vip01 broadcast $vip01 netmask 255.255.255.255 up
route add -host $vip01 lo:0
ifconfig lo:1 $vip02 broadcast $vip02 netmask 255.255.255.255 up
route add -host $vip02 lo:1
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#限制arp响应请求
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
#限制arp发送请求
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
6. 运行配置
执行 sh /usr/local/sbin/lvs_dr_rs.sh
并将 sh /usr/local/sbin/lvs_dr_rs.sh
添加到 vi /etc/rc.d/rc.local
中(开机自启)
然后给运行权限 chmod +x /etc/rc.d/rc.local
7. 查看lo配置,两台node都一样
使用命令 ip addr