keepalived雙主模式實現nginx高可用及LNAMMP架構

keepalived雙主模式實現nginx高可用及LNAMMP架構


一、利用keepalived實現nginx調度器高可用;

二、構建LNAMMP架構:

1) Nginx既是前端調度器,又是緩存服務器;

2) 將php的session緩存於memcached中;

3) 在Apache和php上部署Discuz論壇程序;

4) 使用https連接,即使用戶使用的是http協議也可以以https協議進行訪問;

-------------------------------------------------------------------------------------

一、


實驗規劃:

director1: ip(172.16.1.8),虛擬ip(172.16.1.100)

director2: ip(172.16.1.9),虛擬ip(172.16.1.200)

RS1:    rip(172.16.1.3)

RS2:    rip(172.16.1.6)


1.首先關閉所有節點上iptables和selinux,同時進行時間同步。


2.在兩個後端RS上分別添加一個網頁

echo "www1.zrs.com" > /var/www/html/index.html

echo "www2.zrs.com" > /var/www/html/index.html


3.兩個director配置 

安裝keepalived

 yum -y install keepalived


4.安裝nginx

 此次用EPEL源的安裝包,也可以編譯安裝

  ~]# cd /etc/yum.repos.d/

  ~]# vim nginx.repo

  [nginx]

  name=nginx repo

  baseurl=http://nginx.org/packages/centos/7/$basearch/

  gpgcheck=0

  enabled=1

  ~]# yum install -y nginx


5.在nginx.conf配置文件中的http段內添加upstream內容,將後端兩臺RS加入到該upstream中

upstream webservers {

    server 172.16.1.3;

    server 172.16.1.6;

}

server {

    listen 80;

    location / {

        proxy_pass http://webservers;

        proxy_set_header X-Real-IP $remote_addr;

    }

}


6.配置keepalived的主配置文件,實現對nginx的雙主模式的高可用:

keepalived的配置文件1:

! Configuration File for keepalived
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from  keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id drct1
   vrrp_mcast_group4 224.200.100.18
}
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 81
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass zrs66zrs
    }
    virtual_ipaddress {
        172.16.1.100/32 brd 172.16.1.100  dev eno16777736 label eno16777736:0
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eno16777736
    virtual_router_id 80
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass zrs88zrs
    }
    virtual_ipaddress {
        172.16.1.200/32 brd  172.16.1.200 dev eno16777736 label eno16777736:1
    }
}


keepalived的配置文件2:

!Configuration File for keepalived
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id drct1
   vrrp_mcast_group4 224.200.100.18
}
vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 81
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass zrs66zrs
    }
    virtual_ipaddress {
        172.16.1.200/32 brd 172.16.1.200 dev  eno16777736 label eno16777736:0
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eno16777736
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass zrs88zrs
    }
    virtual_ipaddress {
        172.16.1.100/32 brd 172.16.1.100 dev  eno16777736 label eno16777736:1
    }
}




7.開啓核心轉發功能

echo 1 > /proc/sys/net/ipv4/ip_forward


查看keepalived狀態

c3ed3a4a25d8acccae25049a84c0d7d0.png

a929f05bf4a0cd2921d740b6e2bd3482.png

測試一下

f32512fcd1689d00569d1629569b9fd2.png

關閉一個後端RS的httpd服務

5804c2c7050f6449899024c65ea9085c.png

重新打開那個httpd服務

62977c9d158ec7f8fe04d36e441d2950.png

客戶端查看,由於是輪詢模式,所以兩個後端RS主機交替訪問,分別查看兩個虛擬ip地址,如下

76a842516604d375ff9df916c0a98d56.png

b9e643330f695e0da34a77d7d05c5743.png

55440fdfc6659cbb1bb6bc2a83d8198b.png

d07f1de7b29cf61261d5ca8f18f8ba73.png


二、


LNAMMP架構:Linux+Nginx+Apache+MySQL+Memcached+PHP 


1.在兩個後端RS上創建數據庫

MariaDB [(none)]>  create database dzdb;

MariaDB [(none)]> grant all on dzdb.*TO 'dzuser'@'172.16.%.%'IDENTIFIED BY'123456'; 

MariaDB [(none)]> FLUSH PRIVILEGES;


在兩個後端RS上導入Discuz程序包,並解壓,將解壓出來的upload文件包移動到指定目錄,並賦予必要的權限

cp -R ./upload /var/www/html

cd /var/www/html

chown apache:apache -R ./upload

cd upload/

chmod -R 777 config

chmod -R 777 data

chmod -R 777 uc_client

chmod -R 777 uc_server


打開瀏覽器查看

96ff40fbd037067a42b7b2c370af4f90.png

a19484574395d99115c51126f3bfa145.png


2.進行緩存設置,因爲Nginx既是前端調度器,又是緩存服務器,所以選取其中一個調度器172.16.1.9作爲這次的緩存服務器


在172.16.1.9上安裝並開啓服務

yum install -y memcached

systemctl start memcached


在後端兩個RS上安裝php和其連接memcache必要的擴展程序

yum install -y php php-pecl-memcache


修改/etc/php.ini該配置文件中的[Session]段中的緩存路徑爲如下,

session.save_handler = memcache

session.save_handler = "tcp://172.16.1.9:11211"

重載httpd

systemctl reload httpd

配置一個測試頁面,以測試緩存設置是否正常

[root@zj03 upload]# cd /var/www/html

[root@zj03 html]# vim sessstore.php


配置內容如下

<?php

$mem = new Memcache;

$mem->connect("172.16.1.9", 11211)  or die("Could not connect");


$version = $mem->getVersion();

echo "Server's version: ".$version."<br/>\n";


$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");

echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";


$get_result = $mem->get('hellokey');

echo "$get_result is from memcached server.";         

?>


打開瀏覽器訪問虛擬ip查看

92e8441e7910da8e3f07d2b316113ae7.png

ba0327fceddf713876202c4f114c710e.png

3.設置https協議訪問

後端RS配置虛擬主機及密鑰,安裝https必要的程序包

yum install -y mod_ssl


前端nginx服務器上配置rewrite功能,在server模塊中的location中添加如下

rewrite ^(.*)$ https://$host$1 permanent;


添加server配置段

server {

   listen       443 ssl;

   server_name  www1.zrs.com;

   ssl_certificate     /etc/nginx/ssl/nginx.crt;

   ssl_certificate_key /etc/nginx/ssl/nginx.key;

   ssl_session_cache   shared:SSL:1m;

   ssl_session_timeout  5m;

   ssl_ciphers  HIGH:!aNULL:!MD5;

   ssl_prefer_server_ciphers  on;

   location / {

       root   html;

       index index.php index.html index.htm;

   }

}


瀏覽器測試

ed09c2e40f77d25cc50a834cb92819e3.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章