keepalived+nginx+apach搭建
keepalived
keepalived的工作原理是VRRP(虚拟路由冗余协议),保证当前主机的下一跳路由出现故障时由另外一台路由器来代理出现故障的路由器进行工作,而默认工作的路由以及备用路由都是根据优先级来确定主|从,这里简单搭建一下实现基本功能。
配置框架:这里使用了四台centos7.3,大概拓扑图这样,keepalived双主模型+两个高可用nginx 代理两个Apache 实现负载均衡;
需要四台服务器,配置之前首先每台服务器需要配置好防火墙并且selinux要关闭。
##临时关闭selinux~]# setenforce 0
两个Apache:每台服务器需要安装httpd,每个服务器一个网卡;
图片服务器centos 1:192.168.150.149 (需要单独安装一个lrzsz来导入图片使用)文档服务器centos 3:192.168.150.152
两个nginx :每台服务器需要安装nginx 以及keepalived,每个服务器两个网卡一个连接内网一个连接外网;
centos 2:内网:192.168.150.154外网:172.16.251.68centos 4:内网:192.168.150.155外网:172.16.251.162
----------------------------------------------------------------------------------------------------------------------
centos 1 httpd配置
~]#cd /var/www/html(拖一个图片文件到这个目录)##查看目录下内容文件已经存在~l]# lsup.png##启动服务~]# systemctl start httpd##查看80端口已经启用~]# ss -tanlState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 128 *:22 *:*LISTEN 0 100 127.0.0.1:25 *:*LISTEN 0 128 :::80 :::*LISTEN 0 128 :::22 :::*LISTEN 0 100 ::1:25 :::*
----------------------------------------------------------------------------------------------------------------------
centos 3 httpd配置
##这里使用默认目录以及默认文件。vim /var/www/html/index.html<h1>centos7.3</h1>##启动服务~]# systemctl start httpd##查看80端口已经启用~]# ss -tanlState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 128 *:22 *:*LISTEN 0 100 127.0.0.1:25 *:*ISTEN 0 128 :::80 :::*LISTEN 0 128 :::22 :::*LISTEN 0 100 ::1:25 :::*
----------------------------------------------------------------------------------------------------------------------
centos 2:nginx配置
##文件是自己创建的,没有修改原配置文件,但是创建的文件必须在conf.d目录下以.conf结尾,因为在nginx.conf文件中有设定;vim /etc/nginx/conf.d/ap.conf##给服务器配置缓存功能;三级目录,定义缓存关键字以及大小,磁盘上用于缓存数据的上限;proxy_cache_path /data/web levels=1:1:1 keys_zone=pxycache:20m max_size=1g;##配置一个server;server {##监听在本机所有IP 的80端口;listen 80;##给反代的web服务器配置的域名;server_name www.myue.com;##设置网站根目录;root /var/www/html;##指明要调用的缓存;proxy_cache pxycache;##设置缓存的key;proxy_cache_key $request_uri;##定义状态码203、302、304缓存多久;proxy_cache_valid 200 302 304 1h;##定义除了以上的状态码另外的状态缓存多久;proxy_cache_valid any 1m;##文档访问IP ;location / {proxy_pass http://192.168.150.149:80;}##图片访问IP,用正则匹配。因为我只配置了一个图片所以只匹配.png的图片,可以根据自己需要进行配置;location ~* \.png$ {proxy_pass http://192.168.150.152:80;}}##手动创建缓存目录;~]#mkdir /data/web -pv##测试配置文件语法;~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful##注意keepalived重启不稳定,如果需要重启需要stop 然后在start,双主模式下只能启动不能停止;~]# systemctl stop keepalived~]# systemctl start keepalived
----------------------------------------------------------------------------------------------------------------------
centos 2:keepalived配置
~]# vim /etc/keepalived/keepalived.conf##默认起始行! Configuration File for keepalived##全局配置;global_defs {##故障出现是发送的邮箱地址,这里设置为本主机;notification_email {root@localhost}##表示发送邮件的源地址;notification_email_from keepalived@localhost##发送有限的smtp服务器地址;smtp_server 127.0.0.1##超时时长smtp_connect_timeout 30##组广播地址vrrp_mcast_group4 224.1.100.100}##keepalived从服务器定义;vrrp_instance VI_1 {##定义为从,可选项MASTER|BACKUP;state BACKUP##使用的网卡;interface ens33##定义组ID;virtual_router_id 51##定义优先级,从服务器的优先级要比主服务器低;priority 96##多久在组里广播一下自己的心跳信息,默认单位为秒s;advert_int 1##通信时认证;authentication {##使用简单字符串方式,不能超过8个字符,超过部分不予理会;auth_type PASS##主从两个服务器的字符串要设定为相同的;auth_pass YN9jd93d}##检测网卡状态;track_interface {ens33}##给keepalived配置的虚拟IP;virtual_ipaddress {172.16.100.100/16}}vrrp_instance VI_2 {##主服务器,因为是双主模式,所以一个服务器可以即为主又为从,可在网站PV量较大的时候两个服务器同时工作提高效率;state MASTERinterface ens33##组ID和上面配置不能相同,但是和需要相同组内的ID要保持一致;virtual_router_id 41##优先级定义100,本组内的从服务器优先级要低于这个数字;priority 100advert_int 1authentication {auth_type PASS##这里要和本组内的字符串保持一致;auth_pass YN98d93d}track_interface {ens33}virtual_ipaddress {172.16.100.101/16}}##启动nginx 以及keepalived;~]# systemctl start nginx~]# systemctl stop keepalived~]# systemctl start keepalived
----------------------------------------------------------------------------------------------------------------------
centos 4 nginx配置与 centos 2 完全相同 这里省略
----------------------------------------------------------------------------------------------------------------------
centos 4:keepalived配置
##大致配置相同,只有几处地方不同,上文已经指出,并且下面已经用红颜色标注,这里不过多介绍;! Configuration File for keepalivedglobal_defs {notification_email {root@localhost}notification_email_from keepalived@localhostsmtp_server 127.0.0.1smtp_connect_timeout 30vrrp_mcast_group4 224.1.100.100}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass YN9jd93d}track_interface {ens33}virtual_ipaddress {172.16.100.100/16}}vrrp_instance VI_2 {state BACKUPinterface ens33virtual_router_id 41priority 96advert_int 1authentication {auth_type PASSauth_pass YN98d93d}track_interface {ens33}virtual_ipaddress {172.16.100.101/16}}##启动nginx 以及keepalived~]# systemctl start nginx~]# systemctl stop keepalived~]# systemctl start keepalived
----------------------------------------------------------------------------------------------------------------------
测试配置结果
##抓包组IP试一下看到两个服务器都为主并且都每隔1s在组内发送一下自己的心跳~]# tcpdump -i ens33 -nn host 224.1.100.100tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes10:17:27.566889 IP 172.16.251.68 > 224.1.100.100: VRRPv2, Advertisement, vrid 41, prio 100, authtype simple, intvl 1s, length 2010:17:27.951391 IP 172.27.35.8 > 224.1.100.100: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 2010:17:28.567182 IP 172.16.251.68 > 224.1.100.100: VRRPv2, Advertisement, vrid 41, prio 100, authtype simple, intvl 1s, length 2010:17:28.952235 IP 172.27.35.8 > 224.1.100.100: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20^C13 packets captured13 packets received by filter0 packets dropped by kernel
因为并没有中间路由转发所以测试的时候需要在自己的Windows主机解析一下域名
C:\Windows\System32\drivers\etc\HOSTS ##文件最下面添加两行解析
##把两个IP解析为相同域名,实现简单的轮询效果, 保存退出
172.16.100.100 www.myue.com
172.16.100.101 www.myue.com
访问域名已经可以访问,访问图片内容;
访问文档内容;
##停止一个nginx 或者停止一个keepalived同样可以访问
~]# systemctl stop nginx
~]# systemctl stop keepalived
##并且缓存目录中已经有内容,并且三级目录。
~]# ls /data/web/9/d/7/6666cd76f96956469e7be39d750cc7d9
/data/web/9/d/7/6666cd76f96956469e7be39d750cc7d
----------------------------------------------------------------------------------------------------------------------
实验结束