能夠針對HTTP請求添加cookie,進行路由後端服務器
可平衡負載至後端服務器,並支持持久鏈接
支持基於cookie進行調度
支持全部主服務器故障切換至備用服務器
支持專用端口實現監控服務
支持不影響現有鏈接狀況下中止接受新鏈接請求
能夠在雙向添加,修改或刪除HTTP報文首部
支持基於pattern實現鏈接請求的訪問控制
經過特定的URI爲受權用戶提供詳細的狀態信息php
基於vrrp協議完成地址流動
爲vip地址所在的節點生成ipvs規則(在配置文件中預先定義)
爲ipvs集羣的各RS作健康狀態檢測
基於腳本調用接口經過執行腳本完成腳本中定義的功能,進而影響集羣事務,以此 支持nginx、haproxy等服務html
OS:centos7.6x86_64
keepalived:keepalived-1.3.5-6.el7.x86_64
haproxy:haproxy-1.5.18-8.el7.x86_64
Apache:httpd-2.4.6-88.el7.centos.x86_64
php-fpm:php-fpm-5.4.16-46.el7.x86_64
mysql:mariadb-server-1:5.5.60-1.el7_5.x86_64
wordpross:wordpress-4.9.4-zh_CN.tar.gzmysql
Haproxy+Keepalived主從高可用,keepalived爲Haproxy主從提供高可用保證,主調度器若掛掉,從調度器能無縫接管,haproxy爲後端web提供檢測功能,實現WEB站點負載均衡+高可用性nginx
IP:192.168.8.143
IP:192.168.8.144web
[root@centos7 ~]#yum -y install haproxy [root@centos7 ~]#cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak [root@centos7 ~]#vim /etc/haproxy/haproxy.cfg.bak #在配置文件末尾添加要監聽的web服務器的地址 ...... listen web_http mode http bind 192.168.2.100:80 server web1 192.168.8.146:80 check inter 3000 fall 3 rise 5 server web2 192.168.8.145:80 check inter 3000 fall 3 rise 5 #在調度器上打開內核路由轉發功能,和地址監聽功能 [root@centos7 ~]#vim /etc/sysctl.d/99-sysctl.conf #編輯配置文件,添加以下兩行 net.ipv4.ip_forward = 1 net.ipv4.ip_nonlocal_bind = 1 #使配置文件生效 [root@centos7 ~]#sysctl -p #啓動haproxy服務,而且查看地址是否監聽 [root@centos7 ~]#systemctl start haproxy.service [root@centos7 ~]#systemctl enable haproxy.service #查看是否已監聽VIP地址 [root@centos7 ~]#ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 192.168.8.100:80 *:*
keepalived master IP:192.168.8.143sql
[root@centos7 ~]#yum -y install keepalived [root@centos7 ~]#yum -y install ipvsadm [root@centos7 ~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@centos7 ~]#vim /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id lvs1 vrrp_skip_check_adv_addr vrrp_strict vrrp_iptables #關閉防火牆,不然當VIP轉移時會自動打開防火牆,攔截全部的鏈接 vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 70 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.8.100 dev eth0 label eth0:0 } } virtual_server 192.168.8.100 80 { delay_loop 3 lb_algo wrr lb_kind DR protocol TCP sorry_server 192.168.8.149 80 real_server 192.168.8.145 80 { weight 2 TCP_CHECK { connect_timeout 5 nb_get_retry 3 delay_befor_retry 3 connect_port 80 } } real_server 192.168.8.146 80 { weight 2 TCP_CHECK { connect_timeout 5 nb_get_retry 3 delay_befor_retry 3 connect_port 80 } } } #啓動keepalived服務 [root@centos7 ~]#systemctl start keepalived.service [root@centos7 ~]#systemctl enable keepalived.service
keepalived slave IP:192.168.8.144(大部分配置文件同樣,只需改動少量配置)數據庫
[root@centos7 ~]#yum -y install keepalived [root@centos7 ~]#yum -y install ipvsadm [root@centos7 ~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@centos7 ~]#vim /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id lvs1 vrrp_skip_check_adv_addr vrrp_strict vrrp_iptables vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP #改成從模式 interface eth0 virtual_router_id 51 priority 70 #改動優先級爲70,要比主服務器優先級小 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.8.100 dev eth0 label eth0:0 } } virtual_server 192.168.8.100 80 { delay_loop 3 lb_algo wrr lb_kind DR protocol TCP sorry_server 192.168.8.149 80 real_server 192.168.8.145 80 { weight 2 TCP_CHECK { connect_timeout 5 nb_get_retry 3 delay_befor_retry 3 connect_port 80 } } real_server 192.168.8.146 80 { weight 2 TCP_CHECK { connect_timeout 5 nb_get_retry 3 delay_befor_retry 3 connect_port 80 } } } #啓動keepalived服務 [root@centos7 ~]#isystemctl start keepalived.service [root@centos7 ~]#isystemctl enable keepalived.service
nfs server IP:192.168.8.149vim
[root@centos7 ~]#yum -y install nfs-utils [root@centos7 ~]#tar xf wordpress-4.9.4-zh_CN.tar.gz [root@centos7 ~]#mkdir /wordpross #建立掛載目錄 [root@centos7 ~]#mv wordpress/* /wordpross/ #移動配置文件到建立的目錄中 ...... define( 'DB_NAME', 'wordpross' ); #數據庫名稱 /** MySQL database username */ define( 'DB_USER', 'user1' ); #管理帳號用戶名 /** MySQL database password */ define( 'DB_PASSWORD', 'centos' ); #管理帳號密碼 /** MySQL hostname */ define( 'DB_HOST', 'www.test.com' ); #數據庫IP或域名 ...... [root@centos7 ~]#vim /etc/exports /wordpross 192.168.8.0/24(rw,no_root_squash) #啓動nfs服務 [root@centos7 ~]#systemctl start nfs.service [root@centos7 ~]#systemctl enable nfs.service
mysql master IP:192.168.8.150後端
[root@centos7 ~]#yum -y install mariadb-server [root@centos7 ~]#vim /etc/my.cnf #編輯配置文件,添加以下幾行 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock server_id=1 skip_name_resolve innodb_file_per_table log_bin #啓動mariadb服務 systemctl start mariadb systemctl enable mariadb #進入數據庫查看二進制文件的位置 MariaDB [(none)]> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mariadb-bin.000001 | 245 | #添加複製帳號 MariaDB [(none)]> grant replication slave on *.* to test@'192.168.8.134' identified by 'centos';
mysql slave IP:192.168.8.134centos
[root@centos7 ~]#yum -y install mariadb-server [root@centos7 ~]#vim /etc/my.cnf #編輯配置文件,添加以下幾行 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock server_id=2 read_only skip_name_resolve innodb_file_per_table #啓動數據庫服務 [root@centos7 ~]#systemctl start mariadb [root@centos7 ~]#systemctl enable mariadb #進入數據庫,添加同步帳號 MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='192.168.8.150', -> MASTER_USER='test', -> MASTER_PASSWORD='centos', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='mariadb-bin.000001', -> MASTER_LOG_POS=245; #開始進行同步 MariaDB [(none)]> start slave; #查看同步信息 MariaDB [(none)]> show slave status\G; Slave_IO_Running: Yes #同步線程以開啓 Slave_SQL_Running: Yes 在masyql主服務器上添加wordpross數據庫 MariaDB [(none)]> create database wordpross; MariaDB [(none)]> grant all on wordpross.* to user1@'192.168.8.%' identified by 'centos';
web1 IP:192.168.8.145
web2 IP:192.168.8.146
[root@centos7 ~]# yum -y install httpd [root@centos7 ~]# yum -y install php-fpm [root@centos7 ~]# yum -y install nfs-utils #啓動httpb服務 [root@centos7 ~]# systemctl start httpd [root@centos7 ~]# systemctl enable httpd [root@centos7 ~]# vim /etc/php-fpm.d/www.conf #註釋掉這一行 ;listen.allowed_clients = 127.0.0.1 #啓動php-fpm服務 [root@centos7 ~]#systemctl restart php-fpm [root@centos7 ~]#systemctl enable php-fpm #查看9000端口是否監聽 [root@centos7 ~]#iss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 127.0.0.1:9000 *:* #掛載nfs設備 [root@centos7 ~]#ivim /etc/fstab 192.168.8.149:/wordpross /var/www/html nfs defaults 0 0 #使配置文件生效 [root@centos7 ~]#mount -a #查看掛載是否成功 [root@centos7 ~]#df 192.168.8.149:/wordpross 151967360 1462528 150504832 1% /var/www/html #在迴環網卡上綁定VIP地址,並關閉網卡的廣播功能,須要腳原本實現 [root@centos7 ~]#vim lvs_dr_rs.sh #!/bin/bash vip=192.168.8.100 #寫入VIP的地址 mask='255.255.255.255' dev=lo:1 rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null service httpd start &> /dev/null && echo "The httpd Server is Ready!" case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev echo "The RS Server is Ready!" ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac #執行腳本並查看是否綁定成功 [root@centos7 ~]#bash lvs_dr_rs.sh start [root@centos7 ~]#ifconfig lo:1: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.8.100 netmask 255.255.255.255 loop txqueuelen 1000 (Local Loopback) #編輯host文件,將解析指向mysql服務器的地址 [root@centos7 ~]#vim /etc/hosts 192.168.8.150 www.test.com
client IP :192.168.8.132
MariaDB [(none)]> use wordpross; MariaDB [wordpross]> select * from wp_users\G; *************************** 1. row *************************** ID: 1 user_login: admin user_pass: $P$BV98wIJbUmTzsprC6v7Dea1VGKUFIU/ user_nicename: admin user_email: 1058498224@qq.com user_url: user_registered: 2019-06-09 06:36:37 user_activation_key: user_status: 0 display_name: admin