分布式高可用负载均衡集群架构设计的实战演练
一、总体架构
-
双主负载均衡服务器
- 架构方式:nginx + keepalived实现nginx双主高可用的负载均衡
- 主从调度:两台 nginx + keepalived 负载均衡服务器互为主从,由keepalived配置的具体调度算法调度一台nginx服务器来承载本次请求分发的能力
-
DNS轮询
- DNS轮询请求负载均衡服务器,负载均衡服务器通过keepalived调度算法选择一台负载均衡服务器来分发请求
-
Web服务器集群
- 两台及以上Nginx Web服务器架设,来响应负载均衡服务器分发的请求
-
主从读写分离数据库集群
- 读数据库:一台主MySql数据库处理写操作的业务
- 写数据库:一台以上从MySql服务器处理读操作的业务
-
总体架构图
二、双主负载均衡服务器搭建
-
服务器准备
-
负载均衡服务器1
操作系统:Centos6.7 x32
真实ip:192.168.137.2
虚拟ip:192.168.137.100
keepalived MASTER主ip:本机ip,即192.168.137.100
keepalived BACKUP从ip:负载均衡服务器2 的虚拟ip,即 192.168.137.200
-
负载均衡服务器2
操作系统:Centos6.7 x32
真实ip:192.168.137.3
虚拟ip:192.168.137.200
keepalived MASTER主ip:本机ip,即192.168.137.200
keepalived BACKUP从ip:负载均衡服务器1 的虚拟ip,即 192.168.137.100
-
高可用说明
192.168.137.2和 192.168.137.3 两台负载均衡分别负责两个虚拟ip的请求
192.168.137.2负责192.168.137.100
192.168.137.3负责192.168.137.200
当192.168.137.2的nginx挂掉了,它负责的192.168.137.100的请求由keepalived 调度给从服务器即192.168.137.3上的nginx响应
-
-
在两台负载均衡服务器上nginx安装配置步骤
-
安装依赖
#在安装nginx前,需要确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件 yum install gcc-c++ yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
-
下载源码、解压、编译、安装
wget http://nginx.org/download/nginx-1.8.1.tar.gz tar -zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1 #--prefix=/usr/local/nginx 指定安装目录 ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module --with-http_spdy_module \ --with-http_stub_status_module --with-pcre make make install
-
配置 nginx负载均衡
vi /usr/local/nginx/conf/nginx.conf
#配置负载均衡分发的web服务器集群池 upstream web1 { #Nginx负载算法:轮询、ip_hash、weight、fair(第三方)、url_hash(第三方) ip_hash;#根据 ip_hash 负载调度算法命中 web服务器集群池中 其中一台 server server 192.168.137.4:80; server 192.168.137.5:80; } #虚拟主机 server { listen 80; server_name test.test007.com; location / { root /var/www; index index.html index.htm; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; #反向代理负载均衡服务器池 web1 proxy_pass [http://web1;](http://web1%3B/) } }
-
注:nginx五种负载算法
#轮询:nginx默认,按请求顺序分配后端服务器,自动剔除down掉的服务器,自动加入存活服务器 upstream web1 { server 192.168.137.4:80; server 192.168.137.5:80; } #Weight:设置权重,用于后端服务器性能不均的情况,访问比率约等于权重之比 upstream web1 { server 192.168.137.4:80 wight=1; server 192.168.137.5:80 wight=5; } #ip_hash:解决了session问题,每个请求按访问IP的hash结果分配,这样每个访客可以固定一个后端服务器。 upstream web1 { ip_hash; server 192.168.137.4:80; server 192.168.137.5:80; } #fair:按后端服务器的响应时间来分配请求,响应时间短的优先分配。 upstream web1 { server 192.168.137.4:80; server 192.168.137.5:80; fair; } #url_hash:按访问URL的hash结果来分配请求,使每个URL定向到同一个后端服务器,后端服务器为缓存时比较适用。另外,在upstream中加入hash语句后,server语句不能写入weight等其他参数。 upstream web1 { server 192.168.137.4:80; server 192.168.137.5:80; hash $request_uri; hash_method crc32; }
-
-
在两台负载均衡服务器上Keeplived安装步骤(两台操作一毛一样)
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz tar -zxvf keepalived-1.2.18.tar.gz cd keepalived-1.2.18 ./configure --prefix=/usr/local/keepalived make make install cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ mkdir -p /etc/keepalived
-
负载均衡服务器1上keepalived配置
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { #router 一个实例的标识 router_id LVS_DEVEL } vrrp_script chk_nginx { script "killall -0 nginx" interval 1 weight 2 } vrrp_instance VI_10 { #主/从服务器:MASTER / BACKUP,只有这两个值,必须大写 #负载均衡服务器1的主服务器 = 负载均衡服务器1的从服务器(192.168.137.100) state MASTER interface eth1 virtual_router_id 131 #priority: 优先级,值越大优先级越高,MASTER 设置比 BACKUP大 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } #设置处理的虚拟ip virtual_ipaddress { 192.168.137.100 } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } vrrp_instance VI_20 { #负载均衡服务器1的从服务器 = 负载均衡服务器2的主服务器(192.168.137.200) state BACKUP interface eth1 virtual_router_id 132 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.137.200 } track_script { chk_nginx } notify_master "/etc/keepalived/notify1.sh master" notify_backup "/etc/keepalived/notify1.sh backup" notify_fault "/etc/keepalived/notify1.sh fault" }
-
负载均衡服务器2上keepalived配置
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_nginx { script "killall -0 nginx" interval 1 weight 2 } vrrp_instance VI_10 { #负载均衡服务器2的从服务器 = 负载均衡服务器1的主服务器(192.168.137.100) state BACKUP interface eth1 virtual_router_id 131 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.137.100 } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } vrrp_instance VI_20 { #负载均衡服务器2的主服务器 = 负载均衡服务器1的从服务器(192.168.137.200) state MASTER interface eth1 virtual_router_id 132 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.137.200 } track_script { chk_nginx } notify_master "/etc/keepalived/notify1.sh master" notify_backup "/etc/keepalived/notify1.sh backup" notify_fault "/etc/keepalived/notify1.sh fault" }
-
启动两台负载均衡服务器
Service keepalived start /usr/local/nginx/sbin/nginx
-
通过ip a 查看生效的虚拟ip
负载均衡服务器2
三、DNS轮询配置
通过设置域名解析多条不同线路的A记录即可,或者用第三方DNS服务器
如例中根域名:test007.com
记录类型 | 主机记录 | 解析线路 | 记录值 |
---|---|---|---|
A | test | 中国联通 | 192.168.137.100 |
A | test | 中国移动 | 192.168.137.200 |
四、Web服务器集群搭建
-
服务器准备
-
web服务器1
操作系统:Centos6.7 x32
真实ip:192.168.137.4
-
web服务器2
操作系统:Centos6.7 x32
真实ip:192.168.137.5
-
-
安装nginx web服务器
- 同负载均衡nginx安装
-
配置nginx web服务器
vi /usr/local/nginx/conf/nginx.conf
server { listen 80; server_name localhsst; location /{ root /var/www; index index.html index.htm; } }
-
防火墙对80端口开放绿灯
/sbin/iptables -I INPUT -p tcp --dport 80-j ACCEPT /etc/rc.d/init.d/iptables save #测试操作可以直接让防火墙放个带薪假 service iptables stop
五、数据库主从读写分离架构设计
-
服务器准备
-
主MySql服务器master
操作系统:Centos6.7 x32
真实ip:192.168.137.4
-
从MySql服务器slave
操作系统:Centos6.7 x32
真实ip:192.168.137.5
-
-
安装并启动MySql服务器
yum install mysql-server service mysqld start
-
主MySql服务器master配置
- 修改配置
vi /etc/my.cnf
#指定server id server-id = 1 #开启binlog日志 log-bin #允许日志同步 sync-binlog=1
- 重启生效配置
service mysqld restart
- 授权同步用户
mysql -u root -p
#设置授权账户 slave1 给从服务器做同步使用 mysql>grant replication slave,super,reload on *.* to slave1@192.168.137.5 identified by '123456'; #查看File(binlog日志文件)和position(当前位置偏移)的值 mysql>show master status/G;
-
从MySql服务器slave配置
- 修改配置
vi /etc/my.cnf
#指定server id server-id = 2 #开启binlog日志 log-bin #允许日志同步 sync-binlog=1 #设置只读 read_only = 1
- 重启生效配置
service mysqld restart
- 与主MySql服务器Master的同步配置
mysql -u root -p
Mysql>Change master to >master_host='192.168.137.4', #主MySql的ip >master_user='slave1', #master授权用户 >master_password='123456', #maser授权用户密码 >master_log_file='mysqld-bin.000002', #master binlog文件名 >master_log_pos=106; #master position偏移量 mysql>start slave; #开启slave Mysql>show slave status/G; #看slave状态 #下面这两个进程同时为Yes证明slave工作正常 Slave_IO_Runing: Yes Slave_SQL_Runing: Yes
此时mysql主从读写分离已经配置完成,在主数据库上创建数据库,创建表插入数据等写入操作等都会自动同步到从数据库。
由markdown编辑器 typora 发送