定义:
集群是由多个计算机或服务器组成的网络,它们共同工作以完成单个任务或多个任务。集群可以提高计算能力、可靠性和可扩展性
分类:
负载均衡集群:Load Balance 用于分配网络流量和负载,以确保网络服务的高可用性和可靠性。
高可用集群:High Availability 用于提供高可用性和容错能力,以确保系统在故障时仍能正常运行。(避免单点故障,可以快速迁移)
高性能计算:High Performance Computing 用于处理大规模计算任务,如科学计算、气象预测、金融建模等。
LVS:负载均衡集群
组成:
1.前端:负载均衡层 --由一台或多台负载调度器构成
2.中间:服务器层 -- 有一组实际运行应用服务的服务器组成
3.底端:数据共享存储层 --提供共享数据的存储区域
工作模式
-
NAT网络地址转换模式(Network Address Translation): 在NAT模式下,客户端请求首先到达负载均衡器,负载均衡器会将客户端请求中的源IP地址和端口号修改为自己的IP地址和端口号,然后再将请求发送给后端服务器。后端服务器接收到请求后响应给负载均衡器,负载均衡器再将响应返回给客户端。这样,客户端和后端服务器之间通信实际上是通过负载均衡器进行的,客户端并不直接与后端服务器通信。
-
IP隧道模式(Tunneling): 在IP隧道模式下,负载均衡器会创建一个与后端服务器之间的虚拟隧道,将客户端请求原封不动地通过隧道传输给后端服务器。后端服务器接收到请求后处理,并将响应通过隧道返回给负载均衡器,最终负载均衡器将响应返回给客户端。在这种模式下,客户端和后端服务器之间的通信被封装在一个虚拟隧道中,负载均衡器起到了数据传输的中转作用。
-
DR直接路由模式(Direct Routing): 在直接路由模式下,负载均衡器和后端服务器在同一局域网内,负载均衡器只负责将客户端请求转发给后端服务器,并将后端服务器的响应返回给客户端,不对数据包的IP头部进行修改。这种模式下,负载均衡器不会改变数据包的源IP地址和目标IP地址,后端服务器直接响应客户端的请求。这样可以减少负载均衡器的负担,提高系统整体的性能。
必备知识:
调度器:LVS服务器
真实服务器Real Server:提供服务的服务器
VIP:虚拟地址,提供给用户访问的地址
DIP:指定地址,LVS服务器上与真实服务器通信的地址
RIP:真实地址,真实服务器的地址
常见的调度算法,共10个,常用的有4个:
轮询rr:Real Server轮流提供服务
加权轮询wrr:根据服务器性能设置权重,权重大的得到的请求更多
最少连接lc:根据Real Server的连接数分配请求
加权最少连接wlc:类似于wrr,根据权重分配请求
LVS:NAT模式
环境准备
pubserver:eth0->192.168.88.240,eth1->192.168.99.240
client1:eth0->192.168.88.10,网关192.168.88.5
lvs1: eth0 -> 192.168.88.5;eth1->192.168.99.5
web1:eth1->192.168.99.100;网关192.168.99.5
web2:eth1->192.168.99.200;网关192.168.99.5
配置步骤:
1.配置网络参数
2.配置yum
3.在web服务器上配置nginx
4.在lvs服务器上打开ip转发功能
5.在lvs上安装ipvsadm
6.在lvs上编写转发规则
7.在client1上测试访问
配置ip步骤:
# 登陆之后,将以下内容复制到命令行
hostnamectl set-hostname lvs1
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.88.5/24 autoconnect yes
nmcli connection down eth0
nmcli connection up eth0
rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
nmcli connection add con-name eth1 ifname eth1 type ethernet autoconnect yes ipv4.method manual ipv4.addresses 192.168.99.5/24
reboot # 重启系统,使得eth1网卡生效
# 创建4台虚拟机
[root@myhost ~]# vm clone client1 lvs1 web{1..2}
# 初始化虚拟机
[root@myhost ~]# virsh console client1 # 连接client1控制台
localhost login: root
Password: a
# 登陆之后,将以下内容粘贴到终端
hostnamectl set-hostname client1
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.88.10/24 autoconnect yes ipv4.gateway 192.168.88.5
nmcli connection down eth0
nmcli connection up eth0
# 退出
[root@localhost ~]# exit
# 退出之后,按ctrl+]可回到真机
# 真机通过ssh连接client1
[root@myhost ~]# rm -f ~/.ssh/known_hosts
[root@myhost ~]# ssh 192.168.88.10
# 配置第2台机器作为lvs1
[root@myhost ~]# virsh console lvs1
localhost login: root
Password: a
# 登陆之后,将以下内容复制到命令行
hostnamectl set-hostname lvs1
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.88.5/24 autoconnect yes
nmcli connection down eth0
nmcli connection up eth0
rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
nmcli connection add con-name eth1 ifname eth1 type ethernet autoconnect yes ipv4.method manual ipv4.addresses 192.168.99.5/24
reboot # 重启系统,使得eth1网卡生效
# 按ctrl+]可回到真机
# 真机通过ssh连接lvs1
[root@myhost ~]# ssh 192.168.88.5
# 配置第3台机器作为web1
[root@myhost ~]# virsh console web1
localhost login: root
Password: a
# 登陆之后,将以下内容复制到命令行
hostnamectl set-hostname web1
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 autoconnect no
rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
nmcli connection add con-name eth1 ifname eth1 type ethernet autoconnect yes ipv4.method manual ipv4.addresses 192.168.99.100/24 ipv4.gateway 192.168.99.5
reboot
# 按ctrl+]可回到真机
# 真机通过ssh连接web1
[root@myhost ~]# ssh 192.168.99.100
# 配置第4台机器作为web2
[root@myhost ~]# virsh console web2
localhost login: root
Password: a
# 登陆之后,将以下内容复制到命令行
hostnamectl set-hostname web2
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 autoconnect no
rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
nmcli connection add con-name eth1 ifname eth1 type ethernet autoconnect yes ipv4.method manual ipv4.addresses 192.168.99.200/24 ipv4.gateway 192.168.99.5
reboot
# 按ctrl+]可回到真机
# 真机通过ssh连接web2
[root@myhost ~]# ssh 192.168.99.200
虚拟机已关闭selinux和防火墙、
pubserver上准备管理环境、
# 创建工作目录
[root@pubserver ~]# mkdir cluster
[root@pubserver ~]# cd cluster/
#创建主配置文件
[root@pubserver cluster]# vim ansible.cfg
[defaults]
inventory = inventory
host_key_checking = false # 不检查主机密钥
# 创建主机清单文件及相关变量
[root@pubserver cluster]# vim inventory
[clients]
client1 ansible_host=192.168.88.10
[webservers]
web1 ansible_host=192.168.99.100
web2 ansible_host=192.168.99.200
[lb]
lvs1 ansible_host=192.168.88.5
[all:vars] # all是ansible自带的组,表示全部主机
ansible_ssh_user=root
ansible_ssh_pass=a
# 创建文件目录,用于保存将要拷贝到远程主机的文件
[root@pubserver cluster]# mkdir files
# 编写yum配置文件
[root@pubserver cluster]# vim files/local88.repo
[BaseOS]
name = BaseOS
baseurl = ftp://192.168.88.240/dvd/BaseOS
enabled = 1
gpgcheck = 0
[AppStream]
name = AppStream
baseurl = ftp://192.168.88.240/dvd/AppStream
enabled = 1
gpgcheck = 0
[rpms]
name = rpms
baseurl = ftp://192.168.88.240/rpms
enabled = 1
gpgcheck = 0
[root@pubserver cluster]# vim files/local99.repo
[BaseOS]
name = BaseOS
baseurl = ftp://192.168.99.240/dvd/BaseOS
enabled = 1
gpgcheck = 0
[AppStream]
name = AppStream
baseurl = ftp://192.168.99.240/dvd/AppStream
enabled = 1
gpgcheck = 0
[rpms]
name = rpms
baseurl = ftp://192.168.99.240/rpms
enabled = 1
gpgcheck = 0
# 编写用于上传yum配置文件的playbook
[root@pubserver cluster]# vim 01-upload-repo.yml
---
- name: config repos.d
hosts: all
tasks:
- name: delete repos.d # 删除repos.d目录
file:
path: /etc/yum.repos.d
state: absent
- name: create repos.d # 创建repos.d目录
file:
path: /etc/yum.repos.d
state: directory
mode: '0755'
- name: config local88 # 上传repo文件到88网段
hosts: clients,lb
tasks:
- name: upload local88
copy:
src: files/local88.repo
dest: /etc/yum.repos.d/
- name: config local99 # 上传repo文件到99网段
hosts: webservers
tasks:
- name: upload local99
copy:
src: files/local99.repo
dest: /etc/yum.repos.d/
[root@pubserver cluster]# ansible-playbook 01-upload-repo.yml
配置2台web服务器
# 创建首页文件,文件中包含ansible facts变量
[root@pubserver cluster]# vim files/index.html
Welcome from {{ansible_hostname}}
# 配置web服务器
[root@pubserver cluster]# vim 02-config-webservers.yml
---
- name: config webservers
hosts: webservers
tasks:
- name: install nginx # 安装nginx
yum:
name: nginx
state: present
- name: upload index # 上传首页文件到web服务器
template:
src: files/index.html
dest: /usr/share/nginx/html/index.html
- name: start nginx # 启动服务
service:
name: nginx
state: started
enabled: yes
[root@pubserver cluster]# ansible-playbook 02-config-webservers.yml
# 在lvs1上测试到web服务器的访问
[root@lvs1 ~]# curl http://192.168.99.100
Welcome from web1
[root@lvs1 ~]# curl http://192.168.99.200
Welcome from web2
确保lvs1的ip转发功能已经打开。该功能需要改变内核参数
# 查看ip转发功能的内核参数
[root@lvs1 ~]# sysctl -a # 查看所有的内核参数
[root@lvs1 ~]# sysctl -a | grep ip_forward # 查看ip_foward参数
net.ipv4.ip_forward = 1 # 1表示打开转发,0表示关闭转发
# 设置打开ip_forward功能
[root@pubserver cluster]# vim 03-sysctl.yml
---
- name: config sysctl
hosts: lb
tasks:
- name: set ip_forward
sysctl: # 用于修改内核参数的模块
name: net.ipv4.ip_forward # 内核模块名
value: '1' # 内核模块的值
sysctl_set: yes # 立即设置生效
sysctl_file: /etc/sysctl.conf # 配置写入文件
[root@pubserver cluster]# ansible-playbook 03-sysctl.yml
# 测试从客户端到服务器的访问
[root@client1 ~]# curl http://192.168.99.100
Welcome from web1
[root@client1 ~]# curl http://192.168.99.200
Welcome from web2
安装LVS
[root@pubserver cluster]# vim 04-inst-lvs.yml
---
- name: install lvs
hosts: lb
tasks:
- name: install lvs # 安装lvs
yum:
name: ipvsadm
state: present
[root@pubserver cluster]# ansible-playbook 04-inst-lvs.yml
ipvsadm使用说明
[root@lvs1 ~]# ipvsadm
-A: 添加虚拟服务器
-E: 编辑虚拟服务器
-D: 删除虚拟服务器
-t: 添加tcp服务器
-u: 添加udp服务器
-s: 指定调度算法。如轮询rr/加权轮询wrr/最少连接lc/加权最少连接wlc
-a: 添加虚拟服务器后,向虚拟服务器中加入真实服务器
-r: 指定真实服务器
-w: 设置权重
-m: 指定工作模式为NAT
-g: 指定工作模式为DR
配置LVS
# 为web服务器创建虚拟服务器,使用rr调度算法
[root@lvs1 ~]# ipvsadm -A -t 192.168.88.5:80 -s rr
# 查看配置
[root@lvs1 ~]# ipvsadm -Ln # L是列出,n是使用数字,而不是名字
# 向虚拟服务器中添加RIP
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.100 -w 1 -m
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.200 -w 2 -m
# 查看配置
[root@lvs1 ~]# ipvsadm -Ln
# 验证
[root@client1 ~]# for i in {1..6}
> do
> curl http://192.168.88.5
> done
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1
# 删除配置。(如果配置有错,用以下命令删除重配置)
[root@lvs1 ~]# ipvsadm -D -t 192.168.88.5:80
# 修改调度模式为加权轮询
[root@lvs1 ~]# ipvsadm -E -t 192.168.88.5:80 -s wrr
# 验证配置
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.5; done
Welcome from web2
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web2
Welcome from web1
LVS:DR模式:
配置步骤:
1,配置网络参数
2.配置yum
3在web服务器上配置nginx
4.在lvs的物理网卡上配置vip
5.在web服务器的lo网上配置vip
6.在web服务器上配置内核参数
7.在lvs上安装ipvsadm
8,在lvs上编写转发规则
9.在client1上测试访问
×××××× vip一致
在lvs1的eth0上配置vip 192.168.88.15
[root@pubserver cluster]# vim 05-config-lvsvip.yml
---
- name: config lvs vip
hosts: lb
tasks:
- name: add vip
lineinfile: # 确保文件中有某一行内容
path: /etc/sysconfig/network-scripts/ifcfg-eth0
line: IPADDR2=192.168.88.15
notify: restart eth0 # 通知执行handlers中的任务
handlers: # 被通知执行的任务写到这里
- name: restart eth0
shell: nmcli connection down eth0; nmcli connection up eth0
[root@pubserver cluster]# ansible-playbook 05-config-lvsvip.yml
# 在lvs1查看添加的IP地址 如果不行 reboot
[root@lvs1 ~]# ip a s eth0 | grep 88
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.15/24 brd 192.168.88.255 scope global secondary noprefixroute eth0
[root@pubserver cluster]# vim 05-config-lvsvip.yml
---
- name: config lvs vip
hosts: lb
tasks:
- name: add vip
lineinfile: # 确保文件中有某一行内容
path: /etc/sysconfig/network-scripts/ifcfg-eth0
line: IPADDR2=192.168.88.15
notify: restart eth0 # 通知执行handlers中的任务
handlers: # 被通知执行的任务写到这里
- name: restart eth0
shell: nmcli connection down eth0; nmcli connection up eth0
[root@pubserver cluster]# ansible-playbook 05-config-lvsvip.yml
# 在lvs1查看添加的IP地址 如果不行 reboot
[root@lvs1 ~]# ip a s eth0 | grep 88
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.15/24 brd 192.168.88.255 scope global secondary noprefixroute eth0
2台web服务器的lo上配置vip 192.168.88.15。lo:0网卡需要使用network-scripts提供的配置文件进行配置。
[root@pubserver cluster]# vim 06-config-webvip.yml
---
- name: config webservers vip
hosts: webservers
tasks:
- name: install network-scripts # 安装服务
yum:
name: network-scripts
state: present
- name: add lo:0 # 创建lo:0的配置文件
copy:
dest: /etc/sysconfig/network-scripts/ifcfg-lo:0
content: |
DEVICE=lo:0
NAME=lo:0
IPADDR=192.168.88.15
NETMASK=255.255.255.255
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
notify: activate lo:0
handlers:
- name: activate lo:0 # 激活网卡
shell: ifup lo:0
[root@pubserver cluster]# ansible-playbook 06-config-webvip.yml
# 查看结果
[root@web1 ~]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# cat ifcfg-lo:0
DEVICE=lo:0
NAME=lo:0
IPADDR=192.168.88.15
NETMASK=255.255.255.255
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
[root@web1 network-scripts]# ifconfig # 可以查看到lo:0网卡信息
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.88.15 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
临时设置的重启后lo:0会乱码
永久设置
ansible webservers -m lineinfile -a "path=/etc/rc.d/rc.local line='ifup lo:0'"
2台web服务器上配置内核参数,使得它们不响应对192.168.88.15的请求
[root@web1 ~]# sysctl -a | grep arp_ignore
net.ipv4.conf.all.arp_ignore = 1 all 所有
net.ipv4.conf.lo.arp_ignore = 0 lo换回
[root@web1 ~]# sysctl -a | grep arp_announce
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 0
[root@web1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@web1 ~]# sysctl -p
[root@web2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@web2 ~]# sysctl -p
lvs1上配置虚拟服务器
# 创建虚拟服务器
[root@lvs1 ~]# ipvsadm -A -t 192.168.88.15:80 -s wlc
# 向虚拟服务器中加真实服务器
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.100 -w 1 -g
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200 -w 2 -g
# 查看配置
[root@lvs1 ~]# ipvsadm -Ln
# 客户验证
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.15/; done
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web2
Welcome from web1
Welcome from web2
# 创建虚拟服务器
[root@lvs1 ~]# ipvsadm -A -t 192.168.88.15:80 -s wlc
# 向虚拟服务器中加真实服务器
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.100 -w 1 -g
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200 -w 2 -g
# 查看配置
[root@lvs1 ~]# ipvsadm -Ln
# 客户验证
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.15/; done
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web2
Welcome from web1
Welcome from web2
附:出错时,排错步骤:
# 在lvs上可以访问到web服务器
[root@lvs1 ~]# curl http://192.168.88.100/
192.168.99.100
[root@lvs1 ~]# curl http://192.168.88.200/
apache web server2
# 查看vip
[root@lvs1 ~]# ip a s eth0 | grep 88
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.15/24 brd 192.168.88.255 scope global secondary noprefixroute eth0
[root@web1 ~]# ifconfig lo:0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.88.15 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
# 查看内核参数
[root@web1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
# 查看规则
[root@lvs1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.88.15:80 wlc
-> 192.168.88.100:80 Route 1 0 12
-> 192.168.88.200:80 Route 2 0
KeepAlived:高可用集群
工作原理:VRRP(虚拟路由冗余协议)
环境说明:
web1:eth0->192.168.88.100/24
web2:eth0->192.168.88.200/24
配置keepalived
# 在两台web服务器上安装keepalived
[root@pubserver cluster]# vim 07-install-keepalived.yml
---
- name: install keepalived
hosts: webservers
tasks:
- name: install keepalived # 安装keepalived
yum:
name: keepalived
state: present
[root@pubserver cluster]# ansible-playbook 07-install-keepalived.yml
# 修改配置文件
[root@web1 ~]# vim /etc/keepalived/keepalived.conf
12 router_id web1 # 设置本机在集群中的唯一识别符
13 vrrp_iptables # 自动配置iptables放行规则
... ...
20 vrrp_instance VI_1 {
21 state MASTER # 状态,主为MASTER,备为BACKUP
22 interface eth0 # 网卡
23 virtual_router_id 51 # 虚拟路由器地址
24 priority 100 # 优先级 值越大优先级越高
25 advert_int 1 # 发送心跳消息的间隔
26 authentication {
27 auth_type PASS # 认证类型为共享密码
28 auth_pass 1111 # 集群中的机器密码相同,才能成为一个集群
29 }
30 virtual_ipaddress {
31 192.168.88.80/24 # VIP地址
32 }
33 }
# 删除下面所有行
[root@web1 ~]# systemctl start keepalived
# 等几秒服务完全启动后,可以查看到vip
[root@web1 ~]# ip a s eth0 | grep '88'
inet 192.168.88.100/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.80/24 scope global secondary eth0
# 配置web2
[root@web1 ~]# scp /etc/keepalived/keepalived.conf 192.168.88.200:/etc/keepalived/
[root@web2 ~]# vim /etc/keepalived/keepalived.conf
12 router_id web2 # 改id
13 vrrp_iptables
... ...
20 vrrp_instance VI_1 {
21 state BACKUP # 改状态
22 interface eth0
23 virtual_router_id 51
24 priority 80 # 改优先级
25 advert_int 1
26 authentication {
27 auth_type PASS
28 auth_pass 1111
29 }
30 virtual_ipaddress {
31 192.168.88.80/24
32 }
33 }
# 启动服务
[root@web2 ~]# systemctl start keepalived
# 查看地址,eth0不会出现vip
[root@web2 ~]# ip a s | grep '88'
inet 192.168.88.200/24 brd 192.168.88.255 scope global noprefixroute eth0
# 测试,现在访问88.80,看到是web1上的内容
[root@client1 ~]# curl http://192.168.88.80
Welcome from web1
# 模拟web1出现故障
[root@web1 ~]# systemctl stop keepalived.service
# 测试,现在访问88.80,看到是web2上的内容
[root@client1 ~]# curl http://192.168.88.80
Welcome from web2
# 在web2上查看vip,可以查看到vip 192.168.88.80
[root@web2 ~]# ip a s | grep '88'
inet 192.168.88.200/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.80/24 scope global secondary eth0
web1修好后会自动切回web1
监控本机80端口,实现主备切换
配置高可用的web集群时,Keepalived只为服务器提供了VIP,Keepalived不知道服务器上运行了哪些服务,MASTER服务器可以通过跟踪脚本监视本机的80端口,一旦本机80端口失效,则将VIP切换至BACKUP服务器
Keepalived对脚本的要求是,退出码为0表示访问成功;退出码为1表示失败 exit:指定退出
退出码为8进制,显示的是二进制最大255,大于255,只截取低八位。
# 1. 在MASTER上创建监视脚本
[root@web1 ~]# vim /etc/keepalived/check_http.sh
#!/bin/bash
ss -tlnp | grep :80 &> /dev/null && exit 0 || exit 1
[root@web1 ~]# chmod +x /etc/keepalived/check_http.sh
# 2. 修改MASTER配置文件,使用脚本
[root@web1 ~]# vim /etc/keepalived/keepalived.conf
1 ! Configuration File for keepalived
2
3 global_defs {
...略...
18 }
19
20 vrrp_script chk_http_port { # 定义监视脚本 ,自己起名字
21 script "/etc/keepalived/check_http.sh"
22 interval 2 # 脚本每隔2秒运行一次
23 }
24
25 vrrp_instance VI_1 {
26 state MASTER
27 interface eth0
28 virtual_router_id 51
29 priority 100
30 advert_int 1
31 authentication {
32 auth_type PASS
33 auth_pass 1111
34 }
35 virtual_ipaddress {
36 192.168.88.80/24
37 }
38 track_script { # 引用脚本
39 chk_http_port
40 }
41 }
# 3. 重起服务
[root@web1 ~]# systemctl restart keepalived.service
# 4. 测试,关闭web1的nginx后,VIP将会切换至web2
[root@web1 ~]# systemctl stop nginx.service
[root@web1 ~]# ip a s | grep 88
inet 192.168.88.100/24 brd 192.168.88.255 scope global noprefixroute eth0
[root@web2 ~]# ip a s | grep 88
inet 192.168.88.200/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.80/24 scope global secondary eth0
# 5. 当MASTER的nginx修复后,VIP将会切换回至web1
[root@web1 ~]# systemctl start nginx.service
[root@web1 ~]# ip a s | grep 88
inet 192.168.88.100/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.80/24 scope global secondary eth0
[root@web2 ~]# ip a s | grep 88
inet 192.168.88.200/24 brd 192.168.88.255 scope global noprefixroute eth0
HAProxy:实现负载均衡的调度器
用于负载特别大的web站点
工作模式:
- mode http:只适用于web服务
- mode tcp:适用于各种服务
- mode health:仅做健康检查,很少使用
环境准备:
- client1:eth0 -> 192.168.88.10
- HAProxy:eth0 -> 192.168.88.5
- web1:eth0 -> 192.168.88.100
- web2:eth0 -> 192.168.88.200
初始化配置
# 关闭192.168.88.6
[root@lvs2 ~]# shutdown -h now
# 配置192.168.88.5为haproxy服务器
[root@pubserver cluster]# vim 12-config-haproxy.yml
---
- name: config haproxy
hosts: lvs1
tasks:
- name: rm lvs keepalived # 删除软件包
yum:
name: ipvsadm,keepalived
state: absent
- name: rename hostname # 修改主机名
shell: hostnamectl set-hostname haproxy1
- name: install haproxy # 安装软件包
yum:
name: haproxy
state: present
[root@pubserver cluster]# ansible-playbook 12-config-haproxy.yml
# web服务器,不需要配置vip,不需要改内核参数。但是存在对haproxy也没有影响。
配置haproxy
# 修改配置文件
[root@haproxy1 ~]# vim /etc/haproxy/haproxy.cfg
# 配置文件中,global是全局配置;default是缺省配置,如果后续有和default相同的配置,default配置将会被覆盖。
# 配置文件中,frontend描述haproxy怎么和用户交互;backend描述haproxy怎么和后台应用服务器交互。这两个选项,一般不单独使用,而是合并到一起,名为listen。
# 将64行之后全部删除,写入以下内容
64 #---------------------------------------------------------------------
65 listen myweb # 定义虚拟服务器
66 bind 0.0.0.0:80 # 监听在所有可用地址的80端口
67 balance roundrobin # 定义轮询调度算法
# 对web服务器做健康检查,2秒检查一次,如果连续2次检查成功,认为服务器是健康的,如果连续5次检查失败,认为服务器坏了
68 server web1 192.168.88.100:80 check inter 2000 rise 2 fall 5
69 server web2 192.168.88.200:80 check inter 2000 rise 2 fall 5
70
71 listen stats # 定义虚拟服务器
72 bind 0.0.0.0:1080 # 监听在所有可用地址的1080端口
73 stats refresh 30s # 设置监控页面自动刷新时间为30秒
74 stats uri /stats # 定义监控地址是/stats
75 stats auth admin:admin # 监控页面的用户名和密码都是admin
# 启服务
[root@haproxy1 ~]# systemctl start haproxy.service
# 使用firefox访问监控地址 http://192.168.88.5:1080/stats
# 客户端访问测试
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.5/; done
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1
# client1上使用ab访问
[root@client1 ~]# yum install -y httpd-tools
[root@client1 ~]# ab -n1000 -c200 http://192.168.88.5/
负载均衡调度器比较
LVS适用于需要高并发性和稳定性的场景
Nginx适用于静态文件服务和反向代理等应用层负载均衡场景
HAProxy则具备较为丰富的功能和灵活性,适用于多种负载均衡场景。
LVS(Linux Virtual Server)
优点:
- 高性能:LVS使用Linux内核中的IP负载均衡技术,能够实现非常高的并发处理能力。
- 稳定性:LVS经过长时间的实践应用,成熟稳定,被广泛使用。
- 可用性:支持高可用性的配置,可以实现故障自动切换,提供无中断的服务。
- 灵活性:可根据需要采用多种负载均衡算法,如轮询、加权轮询、哈希等。
缺点:
- 配置复杂:相对于其他两个技术,LVS的配置相对较为复杂,需要更深入的了解和配置。
- 功能相对局限:LVS主要是一种传输层负载均衡技术,无法像Nginx和HAProxy那样对应用层协议进行处理。
Nginx
优点:
- 高性能:Nginx采用了基于事件驱动的异步非阻塞架构,能够处理大量并发连接。
- 负载均衡:Nginx具备内置的负载均衡功能,可以根据配置进行请求的转发。
- 丰富的功能:Nginx支持反向代理、静态文件服务、缓存、SSL等,在Web服务器领域有很广泛的应用。
缺点:
- 功能相对较少:相对于LVS和HAProxy,Nginx在负载均衡算法和健康检查等方面的功能相对较少。
- 限制于应用层协议:Nginx只能对HTTP和HTTPS等应用层协议进行处理,无法处理其他协议。
HAProxy
优点:
- 灵活性:HAProxy支持丰富的负载均衡算法和会话保持方式,可以根据需求进行灵活配置。
- 完整的功能:HAProxy支持高可用性配置、健康检查、故障恢复、SSL等功能,在负载均衡领域应用广泛。
- 高性能:HAProxy性能优良,能够处理大量并发连接,并且支持异步IO模型。
缺点:
- 内存占用:相对于Nginx和LVS,HAProxy在处理大量连接时消耗的内存稍高一些。
- 高可用性:HAProxy需要借助额外的工具来实现高可用性,例如Keepalived。