溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么搭建LVS+Keepalived+MySQL

發(fā)布時(shí)間:2021-11-18 17:06:02 來源:億速云 閱讀:186 作者:iii 欄目:MySQL數(shù)據(jù)庫

本篇內(nèi)容主要講解“怎么搭建LVS+Keepalived+MySQL”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么搭建LVS+Keepalived+MySQL”吧!

LVS負(fù)責(zé)負(fù)載均衡轉(zhuǎn)發(fā)請(qǐng)求,keepalived負(fù)責(zé)檢查L(zhǎng)VS的realserver狀態(tài),及時(shí)remove失效節(jié)點(diǎn)、add復(fù)活節(jié)點(diǎn)。
本實(shí)驗(yàn)主要功能測(cè)試,所以只準(zhǔn)備了兩臺(tái)MySQL服務(wù)器,僅為簡(jiǎn)單說明問題。
vm1 10.0.0.11        master   server_id 11
vm2 10.0.0.12        slave         server_id 12

Director:
vm3  10.0.0.14  

VIP: 10.0.0.20

1 在Directory server vm3 上安裝ipvsadm和keepalived
yum install ipvsadm
安裝keepalived
yum install libnfnetlink*
tar -zxvf keepalived-1.2.7.tar.gz
./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 /etc/keepalived

2 vm1 vm2上,執(zhí)行以下操作.
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore  
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce  
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore  
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce  

ifconfig lo:0 10.0.0.20 netmask 255.255.255.255

3 在Directory server vm3上編輯/etc/keepalived/keepalived.conf文件,內(nèi)容如下:

點(diǎn)擊(此處)折疊或打開

  1. global_defs {

  2. router_id HaMySQL_1

  3. }

  4. vrrp_sync_group VGM {

  5. group {

  6. VI_MYSQL

  7. }

  8. }

  9. vrrp_instance VI_MYSQL {

  10. state MASTER

  11. interface eth0

  12. virtual_router_id 100

  13. priority 100

  14. advert_int 1

  15. authentication {

  16. auth_type PASS

  17. auth_pass 1111

  18. }

  19. virtual_ipaddress {

  20. 10.0.0.20

  21. }

  22. }


  23. virtual_server 10.0.0.20 3306 {

  24. delay_loop 6

  25. lb_algo rr

  26. lb_kind DR

  27. protocol TCP

  28. nat_mask 255.255.255.0

  29. persistence_timeout 10


  30. real_server 10.0.0.11 3306 {

  31. weight 3

  32. TCP_CHECK {

  33. connect_timeout 3

  34. nb_get_retry 3

  35. delay_before_retry 3

  36. connect_port 3306

  37. }

  38. }


  39. real_server 10.0.0.12 3306 {

  40. weight 3

  41. TCP_CHECK {

  42. connect_timeout 3

  43. nb_get_retry 3

  44. delay_before_retry 3

  45. connect_port 3306

  46. }

  47. }


  48. }


4 啟動(dòng)MySQL和keepalived。
vm1:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3306/my.cnf &

vm2:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3306/my.cnf

vm3:
/etc/init.d/keepalived start

5 驗(yàn)證
vm3:
[root@vm3 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.20:3306 rr persistent 2
  -> 10.0.0.11:3306               Route   3      1          0
  -> 10.0.0.12:3306               Route   3      1          0

可以看到負(fù)載均衡已啟動(dòng),vm2 vm3都已加進(jìn)來了。
從客戶端連接VIP 10.0.0.20:3306
C:\mysql-5.7.11-winx64\mysql-5.7.11-winx64\bin>mysql.exe -uroot -pmysql -h20.0.0.20 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 90
Server version: 5.6.27-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@server_id;exit;
+-------------+
| @@server_id |
+-------------+
|          11 |
+-------------+
1 row in set (0.00 sec)

從server_id可以看出客戶端連接的是vm1。

關(guān)掉vm1上的MySQL服務(wù)。
[root@vm3 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.20:3306 rr persistent 2
  -> 10.0.0.12:3306               Route   3      1          0


LVS規(guī)則已經(jīng)更新,remove了vm1,再次從客戶端連接。
C:\mysql-5.7.11-winx64\mysql-5.7.11-winx64\bin>mysql.exe -uroot -pmysql -h20.0.0.20 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.6.27-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@server_id;exit;
+-------------+
| @@server_id |
+-------------+
|          12 |
+-------------+
1 row in set (0.00 sec)

重新啟動(dòng)vm1上的MySQL服務(wù)。
[root@vm3 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.20:3306 rr persistent 2
  -> 10.0.0.11:3306               Route   3      1          0
  -> 10.0.0.12:3306               Route   3      1          1

LVS規(guī)則已自動(dòng)刷新,vm1重新被加入。

到此,相信大家對(duì)“怎么搭建LVS+Keepalived+MySQL”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)
推薦閱讀:
  1. DNS搭建
  2. gitlab搭建

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI