LB_之HAProxy 反向 Proxy;代理mysql;高可用

來源:互聯網
上載者:User

標籤:HAProxy   haproxy代理mysql   haproxy反向 Proxy   haproxy高可用   

/etc/haproxy/haproxy.cfg                          //haproxy 的設定檔

haproxy預設是使用5000連接埠

[[email protected] ~]# lsof -i:80       //查看一下80連接埠有沒有佔用

HAProxy-反向 Proxy

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg

在 backend wangdada 下

添加你的real server

注意!:

frontend  main *:預設是5000 ,我該成了80連接埠。

[[email protected] ~]# systemctl start haproxy

[[email protected] ~]# 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       在輸入你設定的密碼

[[email protected] ~]# systemctl restart haproxy

[[email protected] ~]# lsof -i:8000

haproxy-代理mysql

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg           //添加listem msql段

[[email protected] ~]# lsof -i:3306

[[email protected] ~]# systemctl stop mysqld

[[email protected] ~]# systemctl restart haproxy

[[email protected] ~]# systemctl status haproxy

[[email protected] ~]# lsof -i:3306

然後再你的 上遊伺服器(真實mysql)的mysql裡面添加:

mysql> grant all on *.* to [email protected]'10.18.42.%' identified by '[email protected]';

mysql> flush privileges;

mysql> select * from mysql.user\G;

隨便找一台機子訪問10.18.42.157,實質式訪問的是10.18.42.74

[[email protected] ~]# mysql -uroot -p'[email protected]' -h 10.18.42.157

haproxy的高可用

haproxy+keepalived  

做兩個haproxy,將做好的一個設定檔cp一份,記得backup設定檔。這樣就有兩個一模一樣的haproxy,在這兩個有haproxy的伺服器上都安裝上keepalived分別進行如下設定:

[[email protected] ~]# 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伺服器上。就實現了高可用性。

ndb引擎


LB_之HAProxy 反向 Proxy;代理mysql;高可用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.