/etc/haproxy/haproxy.cfg //haproxy 的配置檔案
haproxy預設是使用5000端口
[root@localhost ~]# lsof -i:80 //檢視一下80端口有沒有占用
HAProxy-反向代理
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg

在 backend wangdada 下
添加你的real server
注意!:
frontend main *:預設是5000 ,我該成了80端口。
[root@localhost ~]# systemctl start haproxy
[root@localhost ~]# lsof -i:80
在/etc/haproxy/haproxy.cfg添加此段,可以檢視haproxy狀态
listen status
bind 0.0.0.0:8000
option httplog
stats refresh 30s
stats uri /status
stats realm Haproxy_Manager_Page
stats auth admin:admin
stats hide-version
測試:10.18.42.157:8000/status 在輸入你設定的密碼
[root@localhost ~]# systemctl restart haproxy
[root@localhost ~]# lsof -i:8000
haproxy-代理mysql
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg //添加listem msql段
[root@localhost ~]# lsof -i:3306
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# systemctl status haproxy
然後再你的 上遊伺服器(真實mysql)的mysql裡面添加:
mysql> grant all on *.* to root@'10.18.42.%' identified by '[email protected]';
mysql> flush privileges;
mysql> select * from mysql.user\G;
随便找一台機子通路10.18.42.157,實質式通路的是10.18.42.74
[root@www ~]# mysql -uroot -p'[email protected]' -h 10.18.42.157
haproxy的高可用
haproxy+keepalived
做兩個haproxy,将做好的一個配置檔案cp一份,記得backup配置檔案。這樣就有兩個一模一樣的haproxy,在這兩個有haproxy的伺服器上都安裝上keepalived分别進行如下設定:
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id wang //多個keepalived這個名字之間不能一樣
}
vrrp_script chk_haproxy { //設定一個vrrp_scrpt,名字随意
script "lsof -i:80 | grep haproxy || exit 1" //判斷80端口有無haproxy,沒有傳回1
interval 2 //兩秒判斷每一次
fail 1 //允許失敗1次
vrrp_instance web {
state BACKUP //自己根據情況選擇master和backup
interface ens33
virtual_router_id 43
priority 250
nopreempt 設定為不搶占 注:這個配置隻能設定在backup主機上,而且這個主機優先級要比另外一台高
advert_int 1
authentication {
auth_type PASS
auth_pass 1234444
}
track_script {
chk_haproxy //調用上面的腳本
virtual_ipaddress {
10.18.42.188
相比以往增加的内容是青色背景部分,增加了一個腳本語句,和調用這個腳本的語句。如果檢測到80端口沒有haproxy,keepalived會讓讓出VIP,VIP就會飄到另一個keepalived伺服器上。就實作了高可用性。