keepalived雙主模式實現nginx高可用及LNAMMP架構
keepalived雙主模式實現nginx高可用及LNAMMP架構
一、利用keepalived實現nginx調度器高可用;
二、構建LNAMMP架構:
1) Nginx既是前端調度器,又是緩存服務器;
2) 將php的session緩存於memcached中;
3) 在Apache和php上部署Discuz論壇程序;
4) 使用https連接,即使用戶使用的是http協議也可以以https協議進行訪問;
-------------------------------------------------------------------------------------
一、
實驗規劃:
director1:vip(172.16.1.8),dip(172.16.1.100)
director2:vip(172.16.1.9),dip(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狀態
測試一下
關閉一個後端RS的httpd服務
重新打開那個httpd服務
客戶端查看,由於是輪詢模式,所以兩個後端RS主機交替訪問,分別查看兩個虛擬ip地址,如下
二、
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
打開瀏覽器查看
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查看
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;
}
}
瀏覽器測試
本文出自 “12657170” 博客,請務必保留此出處http://12667170.blog.51cto.com/12657170/1979764
keepalived雙主模式實現nginx高可用及LNAMMP架構