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:
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 {br/>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 {br/>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
配置内容如下
$mem = new Memcache;
$mem->connect("172.16.1.9", 11211) or die("Could not connect");
$version = $mem->getVersion();
echo "Server's version: ".$version."
\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)
\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