keepalive实验
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalive实验相关的知识,希望对你有一定的参考价值。
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 { } notification_email_from [email protected] 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 { } notification_email_from [email protected] 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
配置一个测试页面,以测试缓存设置是否正常
[[email protected] upload]# cd /var/www/html
[[email protected] 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;
}
}
浏览器测试
以上是关于keepalive实验的主要内容,如果未能解决你的问题,请参考以下文章