keepalived雙主模式實現nginx高可用及LNAMMP架構php
1、利用keepalived實現nginx調度器高可用;html
2、構建LNAMMP架構:前端
1) Nginx既是前端調度器,又是緩存服務器;linux
2) 將php的session緩存於memcached中;nginx
3) 在Apache和php上部署Discuz論壇程序;web
4) 使用https鏈接,即便用戶使用的是http協議也能夠以https協議進行訪問;數據庫
-------------------------------------------------------------------------------------apache
1、vim
實驗規劃:後端
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
! 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
!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地址,以下
2、
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;
}
}
瀏覽器測試