搭建Keepalived+LNMP架構web動態博客 實現高可用與負載均衡

環境準備:php

192.168.193.80  node1html

192.168.193.81 node2node

關閉防火牆mysql

[root@node1 ~]# systemctl stop firewalld        #兩臺都關閉
[root@node1 ~]# setenforce 0
setenforce: SELinux is disabled

host文件nginx

[root@node1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.193.80 node1
192.168.193.81 node2

keepalived爲主備模式sql

master爲node1節點數據庫

backup爲node2節點vim

 

yum源centos

[epel]
name=epel
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
[centos]
name=centos base
enabled=1
gpgcheck=0
baseurl=http://mirrors.163.com/centos/7/os/x86_64/

 

master節點配置bash

下載keeplived

[root@node1 ~]# yum -y install keepalived
[root@node1 ~]# vim /etc/keepalived/keepalived.conf
5,7 s/^/#/g    #5-7行加註釋
9,16 s/^/#/g    #9-16
36,157 s/^/#/g  #註釋掉

 

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100            #優先級
    advert_int 1         #一秒檢查一次  一秒給baskup發送一個報文
    authentication {
        auth_type PASS      #這個是密碼認證,和51相輔相成,證實在一個網段裏
        auth_pass 1111      #密碼
    }
    virtual_ipaddress {
        192.168.193.252      #這個就vip   能夠有一個也能夠有多個
    }
}
[root@node1 ~]# scp /etc/keepalived/keepalived.conf 192.168.193.81:/etc/keepalived/keepalived.conf  #把配置文件傳到node2節點

配置backup端

[root@node2 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.193.252
    }
}
[root@node1 ~]# systemctl start keepalived          #啓動
[root@node2 ~]# systemctl start keepalived
[root@node1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:84:7a:50 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.80/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.193.252/32 scope global ens33       #這個就是虛擬IP
       valid_lft forever preferred_lft forever
    inet6 fe80::9a7d:fb7d:d10f:e5e8/64 scope link 
       valid_lft forever preferred_lft forever
[root@node2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:98:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.81/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::80ce:27dd:68f9:5adc/64 scope link 
       valid_lft forever preferred_lft forever

實驗中止node1節點IP是否發生漂移

[root@node1 ~]# systemctl stop  keepalived
[root@node2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:98:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.81/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.193.252/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::80ce:27dd:68f9:5adc/64 scope link 
       valid_lft forever preferred_lft forever

發生漂移,這樣就成功了,利用了VRRP協議

配置LVS

node1配置

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.193.252
    }
}

virtual_server 192.168.193.252 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.193.80 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.193.81 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

node2配置

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.193.252
    }
}

virtual_server 192.168.193.252 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.193.80 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.193.81 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@node1 ~]# yum -y install 
[root@node1 ~]# bash  1.sh  start
啓動成功
[root@node2 ~]# bash  1.sh  start
啓動成功

 

#!/bin/bash
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 ens33:0 192.168.193.252/32 broadcast 192.168.193.252 up
        if [ $? -eq 0 ];then
                route add -host 192.168.193.252 dev ens33:0
        fi
        echo "啓動成功"
;;
stop)
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        ifconfig ens33:0 down
        route del -host 192.168.193.252
        echo "刪除成功"
;;
*)
        echo "usage start|stop"
;;
esac
[root@node1 ~]# systemctl restart   keepalived   
[root@node2 ~]# systemctl restart   keepalived   
[root@node1 ~]# yum -y install ipvsadm 

[root@node1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.193.252:80 rr -> 192.168.193.80:80 Route 1 0 0 -> 192.168.193.81:80 Route 1 0 0

這樣Keeplived就繼承了LVS的功能  高可用與負載均衡

 繼續搭建nginx

如下操做2臺節點一樣操做

[root@node1 ~]# yum install nginx php php-fpm mariadb-server  php-mysql  -y

[root@node2 ~]# yum install nginx php php-fpm mariadb-server  php-mysql  -y
 38     server {
 39         listen       80 default_server;
 40         listen       [::]:80 default_server;
 41         server_name  _;
 42         root         /var/www/html;   #修改網站訪問目錄
 43         index      index.php  index。html;     #修改動態靜態



 47 
 48         location / {              #複製
 49         }
 50  
 51         location ~ ^/.*\.php$ {             #添加
 52         fastcgi_pass 127.0.0.1:9000; 
 53         include fastcgi.conf;
 54         }
 55         

檢測配置文件是否有錯

[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 ~]# systemctl restart mariadb     #啓動數據庫
[root@node1 ~]# cd /var/www/html/   #進入網站根目錄
[root@node1 html]# rz          #上傳動態博客壓縮包
rz waiting to receive.
 zmodem trl+C ȡ

  100%    4548 KB 4548 KB/s 00:00:01       0 Errorszip...

[root@node1 html]# ls
wordpress-3.3.1-zh_CN.zip

[root@node1 html]# unzip wordpress-3.3.1-zh_CN.zip

[root@node1 html]# mv wordpress/*  . 

[root@node1 html]# cp wp-config-sample.php wp-config.php

 

[root@node1 html]#  vim wp-config.php
 18 define('DB_NAME', 'king');
 19 
 20 /** MySQL 數據庫用戶名 */
 21 define('DB_USER', 'king');
 22 
 23 /** MySQL 數據庫密碼 */
 24 define('DB_PASSWORD', '123');
[root@node1 html]# mysql -uroot
MariaDB [(none)]> create database king;
MariaDB [(none)]> grant all on *.* to king@'localhost' identified by '123';
MariaDB [(none)]> exit
Bye
[root@node1 ~]# systemctl restart nginx        #兩臺一樣操做
[root@node1 ~]# systemctl restart php-fpm
[root@node1 ~]# systemctl restart keepalived
[root@node1 ~]# ipvsadm -Ln                 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.193.252:80 rr
  -> 192.168.193.80:80            Route   1      0          0         
  -> 192.168.193.81:80            Route   1      0          0      

[root@node2 ~]# ipvsadm -Ln                 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.193.252:80 rr
  -> 192.168.193.80:80            Route   1      0          0         
  -> 192.168.193.81:80            Route   1      0          0   

如今訪問虛擬IP試驗

 

相關文章
相關標籤/搜索