搭建高可用web和數據庫集群
本次設計使用keepalived與lvs搭建一個調度器集群,實現lvs調度器的高可用,當lvs調度器的master宕機後,keepalived會自動切換到backup上。
調度後面的web集群,實現web集群的負載均衡,前方調度器會根據lvs算法把服務發給web集群中的主機去響應。
在web集群後面搭建數據庫集群,數據庫設置主主結構,並添加keepalived來實現高可用。
具體拓撲圖如下:
2、環境準備
機器名稱 IP配置 服務角色 備註
LVS1-master 192.168.4.1
(VIP1:192.168.4.100) 調度器的主服務器 配置keepalived
(VIP1:192.168.4.100) 調度器的從服務器 配置keepalived
Web1 192.168.4.3 後端web服務器 開啟lnm的web
Web2 192.168.4.4 後端web服務器 開啟lnm的web
db1 192.168.4.5
(VIP2:192.168.4.200) 後端mysql服務器 配置keepalived
db2 192.168.4.6
(VIP2:192.168.4.200) 後端mysql服務器 配置keepalived
3、配置調度服務器
3.1裝包
[root@lvs1 ~]# yum -y install ipvsadm.x86_64
3.2修改keepalived配置文件(主調度器)
1)全局段——設置發送郵件
global_defs { notification_email { root@localhost } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr # vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } 2)定義主從和vip vrrp_instance VI_1 { state MASTER //主調度器 interface eth0 virtual_router_id 51 //虛擬路由ip priority 150 //優先級 advert_int 1 authentication { auth_type PASS auth_pass 1111 //設置密碼 } virtual_ipaddress { 192.168.4.100 //配置vip } } 3)定義調度算法 virtual_server 192.168.4.100 80 { delay_loop 6 lb_algo rr //算法為輪詢 lb_kind DR //LVS模式為DR persistence_timeout 50 //50s內相同的客戶機來訪問給相同的web服務器 protocol TCP real_server 192.168.4.3 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.4.4 80 { //有幾個web服務器寫幾段 weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } 3.3配置從服務器 從服務器與主服務器的配置文件相似,所以直接scp一份過去,把從服務器上的配置文件中的MASTER改為BACKUP並把優先級調小。
[root@lvs1 ~]# scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
[root@lvs2 ~]# vim /etc/keepalived/keepalived.conf
……
state BACKUP
……
priority 100
……
:wq
4、配置lnp服務器(web)
4.1安裝源碼包nginx
[root@web1 lnmp_soft]# tar -xf nginx-1.12.2.tar.gz
[root@web1 lnmp_soft]# cd nginx-1.12.2/
[root@web1 nginx-1.12.2]# useradd -s /sbin/nologin nginx
[root@web1 nginx-1.12.2]# yum -y install gcc gcc-c++ pcre-devel
[root@web1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx //如果報錯,則把需要的包安裝上
[root@web1 nginx-1.12.2]# yum -y install zlib-devel
[root@web1 nginx-1.12.2]# make & make install
[root@web1 sbin]# ln -s /usr/local/nginx/sbin/nginx /sbin/
4.2修改nginx配置文件
root@web1 sbin]# vim /usr/local/nginx/conf/nginx.conf
1)修改最大鏈接數,增大並發
events {
worker_connections 65535;
}
2)網站根路徑
location / {
root html;
index index.php index.html index.htm;
}
3)做動靜分離
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
4)啟動服務:[root@web1 sbin]# nginx -s reload
4.3改ulimit參數支持更大並發
[root@web1 sbin]# ulimit -Hn 100000 //臨時修改
[root@web1 sbin]# ulimit -Sn 100000
[root@web1 sbin]# vim /etc/security/limits.conf //永久修改
* soft nofile 100000
* hard nofile 100000 //最後添加
4.4 安裝php來支持動態解析
[root@web1 sbin]# yum -y install php php-mysql.x86_64
[root@web1 lnmp_soft]# yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm
[root@web1 lnmp_soft]# systemctl restart php-fpm.service
[root@web1 lnmp_soft]# systemctl enable php-fpm.service
4.5 配置vip在lo:0網卡上(本機回環地址)
[root@web1 lnmp_soft]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# cp ifcfg-lo{,:0}
[root@web1 network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.100
NETMASK=255.255.255.255
NETWORK=192.168.4.100
BROADCAST=192.168.4.100
ONBOOT=yes
NAME=lo:0
:wq
[root@web1 network-scripts]# ifup lo:0 //啟動網卡
[root@web1 network-scripts]# ip addr show lo //查看
4.6 web2上同樣配置(同上)
5、測試一下前面的配置是否正確
1)在web1和web2上寫一個不同的頁面
2)在lvs1&lvs2上啟動keepalived服務
[root@lvs1 ~]# systemctl start keepalived.service
[root@lvs1 ~]# ipvsadm -Ln //看規則中是否有web1&web2
[root@room9pc01 ~]# firefox http://192.168.4.100
3)把其中一臺web掛掉,看看規則中是否會有變化,且是否有郵件
6、配置db1&db2
6.1安裝配置mysql
[root@db1 ~]# tar -xf mysql-5.7.17.tar
[root@db1 ~]# yum -y install mysql-community-*
[root@db1 ~]# systemctl start mysqld //啟服務
[root@db2 ~]# grep -i password /var/log/mysqld.log //找初始登錄秘密
[root@db2 ~]# mysql -hlocalhost -uroot -p‘h:rqUP7PHKqi‘
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=6;
mysql> alter user user() identified by "123456";
[root@db1 ~]# vim /etc/my.cnf
……
validate_password_policy=0
validate_password_length=6
log-bin=db2
server_id=5
binlog_format="mixed"
relay-log=relay-bin
relay-log-index=slave-realy-bin.index
auto-increment-increment=2
auto-increment-offset=2
……
註意:mysql1和mysql只有server-id不同和auto-increment-offset不同,其他必須相同。
部分配置項解釋如下:
binlog_format= mixed:指定mysql的binlog日誌的格式,mixed是混合模式。
relay-log:開啟中繼日誌功能
relay-log-index:中繼日誌清單
auto-increment-increment= 2:表示自增長字段每次遞增的量,其默認值是1。它的值應設為整個結構中服務器的總數,本案例用到兩臺服務器,所以值設為2。
auto-increment-offset= 2:用來設定數據庫中自動增長的起點(即初始值),因為這兩能服務器都設定了一次自動增長值2,所以它們的起點必須得不同,這樣才能避免兩臺服務器數據同步時出現主鍵沖突
[root@db2 ~]# systemctl restart mysqld
[root@db1 mysql]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repluser@‘%‘ identified by "123456";
mysql> grant all on *.* to admin@‘%‘ identified by "123456";
6.2 db1&db2互為主從
mysql> change master to master_host="192.168.4.5",master_user="repluser",master_password="123456",master_log_file="db1.000001",master_log_pos=154;
mysql> start slave;
mysql> show slave status\G;
mysql> change master to master_host="192.168.4.6",master_user="repluser",master_password="123456",master_log_file="db2.000002",master_log_pos=154;
mysql> start slave;
mysql> show slave status\G;
6.3 db1&db2上部署keepalived
1)安包
[root@db1 network-scripts]# yum -y install keepalived.x86_64
2)修改配置文件
[root@db1 ~]# vim /etc/keepalived/keepalived.conf
全局段——發郵件
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id mysql-1
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
定義主服務器
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 53
priority 150
nopreempt //不搶占優先權
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.200
}
}
定義算法和real_server
virtual_server 192.168.4.200 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.4.5 3306 {
weight 1
notify_down /etc/keepalived/bin/mysql.sh
``` //如果mysql宕機了,執行腳本中的內容。
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306 //監控健康狀態端口。
}
}
}
3)把主服務器上的配置文件scp一份到從庫上,只需修改MASTER/優先級和real server的ip即可。並把nopreempt這一行去掉,這個一般只配置在優先級大的上面。
4)啟服務
[root@db2 ~]# systemctl restart keepalived.service
5)看vip是否到這臺主機了。
[root@db1 ~]# ip a s eth0
6.4 db1&db2編寫調度keepalived的腳本
因為keepalived沒有與lvs聯用,所以不會像前面的調度器上調度web一樣,當一臺web掛掉,keepalived會把其從lvs的規則中自動刪除,在這一個db集群中,只有db1或者db2上的keepalived程序掛掉時,vip才會從master上跳到backup上。Mysqld掛掉則不會自動跳,這樣啟不到監控調度的作用,所以我們可以編寫個腳本來調度。
當監控到db1上3306端口掛掉了,則關閉本機的keepalived服務器。
[root@db1 bin]# mkdir /etc/keepalived/bin
[root@db1 bin]# vim mysql.sh
#!/bin/bash
pkill keepalived
/sbin/ifdown eth0 && /sbin/ifup eth0
:wq
6.5 驗證調度keepalived的腳本
在db1上把mysqld服務stop掉,看vip是否跑到db2這臺主機上了。
當數據庫重新起來之後,要把keepalived服務手動啟起來。
搭建高可用web和數據庫集群