一掖棉、總體架構(gòu)
-
雙主負(fù)載均衡服務(wù)器
- 架構(gòu)方式:nginx + keepalived實(shí)現(xiàn)nginx雙主高可用的負(fù)載均衡
- 主從調(diào)度:兩臺(tái) nginx + keepalived 負(fù)載均衡服務(wù)器互為主從幔亥,由keepalived配置的具體調(diào)度算法調(diào)度一臺(tái)nginx服務(wù)器來(lái)承載本次請(qǐng)求分發(fā)的能力
-
DNS輪詢
- DNS輪詢請(qǐng)求負(fù)載均衡服務(wù)器针肥,負(fù)載均衡服務(wù)器通過(guò)keepalived調(diào)度算法選擇一臺(tái)負(fù)載均衡服務(wù)器來(lái)分發(fā)請(qǐng)求
-
Web服務(wù)器集群
- 兩臺(tái)及以上Nginx Web服務(wù)器架設(shè)慰枕,來(lái)響應(yīng)負(fù)載均衡服務(wù)器分發(fā)的請(qǐng)求
-
主從讀寫分離數(shù)據(jù)庫(kù)集群
- 讀數(shù)據(jù)庫(kù):一臺(tái)主MySql數(shù)據(jù)庫(kù)處理寫操作的業(yè)務(wù)
- 寫數(shù)據(jù)庫(kù):一臺(tái)以上從MySql服務(wù)器處理讀操作的業(yè)務(wù)
-
總體架構(gòu)圖
二、雙主負(fù)載均衡服務(wù)器搭建
-
服務(wù)器準(zhǔn)備
-
負(fù)載均衡服務(wù)器1
操作系統(tǒng):Centos6.7 x32
真實(shí)ip:192.168.137.2
虛擬ip:192.168.137.100
keepalived MASTER主ip:本機(jī)ip,即192.168.137.100
keepalived BACKUP從ip:負(fù)載均衡服務(wù)器2 的虛擬ip掘猿,即 192.168.137.200
-
負(fù)載均衡服務(wù)器2
操作系統(tǒng):Centos6.7 x32
真實(shí)ip:192.168.137.3
虛擬ip:192.168.137.200
keepalived MASTER主ip:本機(jī)ip礁遵,即192.168.137.200
keepalived BACKUP從ip:負(fù)載均衡服務(wù)器1 的虛擬ip政勃,即 192.168.137.100
-
高可用說(shuō)明
192.168.137.2和 192.168.137.3 兩臺(tái)負(fù)載均衡分別負(fù)責(zé)兩個(gè)虛擬ip的請(qǐng)求
192.168.137.2負(fù)責(zé)192.168.137.100
192.168.137.3負(fù)責(zé)192.168.137.200
當(dāng)192.168.137.2的nginx掛掉了奸远,它負(fù)責(zé)的192.168.137.100的請(qǐng)求由keepalived 調(diào)度給從服務(wù)器即192.168.137.3上的nginx響應(yīng)
-
-
在兩臺(tái)負(fù)載均衡服務(wù)器上nginx安裝配置步驟
-
安裝依賴
#在安裝nginx前丸冕,需要確保系統(tǒng)安裝了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負(fù)載均衡
vi /usr/local/nginx/conf/nginx.conf
#配置負(fù)載均衡分發(fā)的web服務(wù)器集群池 upstream web1 { #Nginx負(fù)載算法:輪詢、ip_hash、weight莹弊、fair(第三方)响迂、url_hash(第三方) ip_hash;#根據(jù) ip_hash 負(fù)載調(diào)度算法命中 web服務(wù)器集群池中 其中一臺(tái) server server 192.168.137.4:80; server 192.168.137.5:80; } #虛擬主機(jī) 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; #反向代理負(fù)載均衡服務(wù)器池 web1 proxy_pass [http://web1;](http://web1%3B/) } }
-
注:nginx五種負(fù)載算法
#輪詢:nginx默認(rèn),按請(qǐng)求順序分配后端服務(wù)器然遏,自動(dòng)剔除down掉的服務(wù)器,自動(dòng)加入存活服務(wù)器 upstream web1 { server 192.168.137.4:80; server 192.168.137.5:80; } #Weight:設(shè)置權(quán)重秧倾,用于后端服務(wù)器性能不均的情況,訪問(wèn)比率約等于權(quán)重之比 upstream web1 { server 192.168.137.4:80 wight=1; server 192.168.137.5:80 wight=5; } #ip_hash:解決了session問(wèn)題,每個(gè)請(qǐng)求按訪問(wèn)IP的hash結(jié)果分配胃榕,這樣每個(gè)訪客可以固定一個(gè)后端服務(wù)器。 upstream web1 { ip_hash; server 192.168.137.4:80; server 192.168.137.5:80; } #fair:按后端服務(wù)器的響應(yīng)時(shí)間來(lái)分配請(qǐng)求,響應(yīng)時(shí)間短的優(yōu)先分配蹲嚣。 upstream web1 { server 192.168.137.4:80; server 192.168.137.5:80; fair; } #url_hash:按訪問(wèn)URL的hash結(jié)果來(lái)分配請(qǐng)求,使每個(gè)URL定向到同一個(gè)后端服務(wù)器,后端服務(wù)器為緩存時(shí)比較適用言询。另外夫啊,在upstream中加入hash語(yǔ)句后,server語(yǔ)句不能寫入weight等其他參數(shù)。 upstream web1 { server 192.168.137.4:80; server 192.168.137.5:80; hash $request_uri; hash_method crc32; }
-
-
在兩臺(tái)負(fù)載均衡服務(wù)器上Keeplived安裝步驟(兩臺(tái)操作一毛一樣)
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
-
負(fù)載均衡服務(wù)器1上keepalived配置
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { #router 一個(gè)實(shí)例的標(biāo)識(shí) router_id LVS_DEVEL } vrrp_script chk_nginx { script "killall -0 nginx" interval 1 weight 2 } vrrp_instance VI_10 { #主/從服務(wù)器:MASTER / BACKUP跷叉,只有這兩個(gè)值梆砸,必須大寫 #負(fù)載均衡服務(wù)器1的主服務(wù)器 = 負(fù)載均衡服務(wù)器1的從服務(wù)器(192.168.137.100) state MASTER interface eth1 virtual_router_id 131 #priority: 優(yōu)先級(jí),值越大優(yōu)先級(jí)越高,MASTER 設(shè)置比 BACKUP大 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } #設(shè)置處理的虛擬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 { #負(fù)載均衡服務(wù)器1的從服務(wù)器 = 負(fù)載均衡服務(wù)器2的主服務(wù)器(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" }
-
負(fù)載均衡服務(wù)器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 { #負(fù)載均衡服務(wù)器2的從服務(wù)器 = 負(fù)載均衡服務(wù)器1的主服務(wù)器(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 { #負(fù)載均衡服務(wù)器2的主服務(wù)器 = 負(fù)載均衡服務(wù)器1的從服務(wù)器(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" }
-
啟動(dòng)兩臺(tái)負(fù)載均衡服務(wù)器
Service keepalived start /usr/local/nginx/sbin/nginx
-
通過(guò)ip a 查看生效的虛擬ip
三、DNS輪詢配置
通過(guò)設(shè)置域名解析多條不同線路的A記錄即可敦冬,或者用第三方DNS服務(wù)器
如例中根域名:test007.com
記錄類型 | 主機(jī)記錄 | 解析線路 | 記錄值 |
---|---|---|---|
A | test | 中國(guó)聯(lián)通 | 192.168.137.100 |
A | test | 中國(guó)移動(dòng) | 192.168.137.200 |
四、Web服務(wù)器集群搭建
-
服務(wù)器準(zhǔn)備
-
web服務(wù)器1
操作系統(tǒng):Centos6.7 x32
真實(shí)ip:192.168.137.4
-
web服務(wù)器2
操作系統(tǒng):Centos6.7 x32
真實(shí)ip:192.168.137.5
-
-
安裝nginx web服務(wù)器
- 同負(fù)載均衡nginx安裝
-
配置nginx web服務(wù)器
vi /usr/local/nginx/conf/nginx.conf
server { listen 80; server_name localhsst; location /{ root /var/www; index index.html index.htm; } }
-
防火墻對(duì)80端口開(kāi)放綠燈
/sbin/iptables -I INPUT -p tcp --dport 80-j ACCEPT /etc/rc.d/init.d/iptables save #測(cè)試操作可以直接讓防火墻放個(gè)帶薪假 service iptables stop
五践险、數(shù)據(jù)庫(kù)主從讀寫分離架構(gòu)設(shè)計(jì)
-
服務(wù)器準(zhǔn)備
-
主MySql服務(wù)器master
操作系統(tǒng):Centos6.7 x32
真實(shí)ip:192.168.137.4
-
從MySql服務(wù)器slave
操作系統(tǒng):Centos6.7 x32
真實(shí)ip:192.168.137.5
-
-
安裝并啟動(dòng)MySql服務(wù)器
yum install mysql-server service mysqld start
-
主MySql服務(wù)器master配置
- 修改配置
vi /etc/my.cnf
#指定server id server-id = 1 #開(kāi)啟binlog日志 log-bin #允許日志同步 sync-binlog=1
- 重啟生效配置
service mysqld restart
- 授權(quán)同步用戶
mysql -u root -p
#設(shè)置授權(quán)賬戶 slave1 給從服務(wù)器做同步使用 mysql>grant replication slave,super,reload on *.* to slave1@192.168.137.5 identified by '123456'; #查看File(binlog日志文件)和position(當(dāng)前位置偏移)的值 mysql>show master status/G;
-
從MySql服務(wù)器slave配置
- 修改配置
vi /etc/my.cnf
#指定server id server-id = 2 #開(kāi)啟binlog日志 log-bin #允許日志同步 sync-binlog=1 #設(shè)置只讀 read_only = 1
- 重啟生效配置
service mysqld restart
- 與主MySql服務(wù)器Master的同步配置
mysql -u root -p
Mysql>Change master to >master_host='192.168.137.4', #主MySql的ip >master_user='slave1', #master授權(quán)用戶 >master_password='123456', #maser授權(quán)用戶密碼 >master_log_file='mysqld-bin.000002', #master binlog文件名 >master_log_pos=106; #master position偏移量 mysql>start slave; #開(kāi)啟slave Mysql>show slave status/G; #看slave狀態(tài) #下面這兩個(gè)進(jìn)程同時(shí)為Yes證明slave工作正常 Slave_IO_Runing: Yes Slave_SQL_Runing: Yes
此時(shí)mysql主從讀寫分離已經(jīng)配置完成瓦胎,在主數(shù)據(jù)庫(kù)上創(chuàng)建數(shù)據(jù)庫(kù)柬祠,創(chuàng)建表插入數(shù)據(jù)等寫入操作等都會(huì)自動(dòng)同步到從數(shù)據(jù)庫(kù)旧蛾。
由markdown編輯器 typora 發(fā)送