keepalived+Nginx实现分布式VIP部署

Nginx安装

yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

nginx安装

[root@localhost src]# tar zxvf nginx-1.10.2.tar.gz
省略安装内容...

[root@localhost src]# cd nginx-1.10.2

[root@localhost nginx-1.10.2]# ./configure && make && make install
省略安装内容...

3、启动Nginx

先找一下nginx安装到什么位置上了

 

进入nginx目录并启动

 

nginx的基本操作

keepalived+Nginx实现分布式VIP部署_第1张图片

 

启动

[root@localhost ~]# /usr/local/nginx/sbin/nginx

停止/重启

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)

命令帮助

[root@localhost ~]# /usr/local/nginx/sbin/nginx -h

验证配置文件

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t

配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

 

keepalived+Nginx实现分布式VIP部署_第2张图片

 

 

负载均衡监听地址:

http://10.88.202.19:8888/

http://10.88.202.20:8888/

http://10.88.202.21:8888/

 

tomcat服务地址:

http://10.88.202.19:8880/Demo/index.html

http://10.88.202.20:8880/Demo/index.html

http://10.88.202.21:8880/Demo/index.html

总入口:

http://10.88.202.19:8888【监听端口】/Demo

windows电脑,本地改成映射的ip域名:

 keepalived+Nginx实现分布式VIP部署_第3张图片

总入口:

http://wcq20:8888/Demo

nginx配置限制同一个ip的访问频率

1、在nginx.conf里的http{}里加上如下代码:

limit_conn_zone $binary_remote_addr zone=perip:10m;

limit_conn_zone $server_name zone=perserver:10m;

2、在需要限制并发数和下载带宽的网站配置server{}里加上如下代码:

limit_conn perip 2;

limit_conn perserver 20;

limit_rate 100k;

补充说明下参数:

$binary_remote_addr是限制同一客户端ip地址;

$server_name是限制同一server最大并发数;

limit_conn为限制并发连接数;

limit_rate为限制下载速度;

 

 

keepalived安装

修改linux主机名

https://jingyan.baidu.com/article/574c52192a1d8d6c8d9dc1ce.html

  1. 以根用户登录,或者登录后切换到根用户,然后在提示符下输入hostname命令,可以看出当前系统的主机名为localhost.localdomain。

keepalived+Nginx实现分布式VIP部署_第4张图片

 

2、更改/etc/sysconfig下的network文件,在提示符下输入vi /etc/sysconfig/network,然后将HOSTNAME后面的值改为想要设置的主机名。

keepalived+Nginx实现分布式VIP部署_第5张图片keepalived+Nginx实现分布式VIP部署_第6张图片keepalived+Nginx实现分布式VIP部署_第7张图片

 

 

3、更改/etc下的hosts文件,在提示符下输入vi /etc/hosts,然后将localhost.localdomain改为想要设置的主机名。keepalived+Nginx实现分布式VIP部署_第8张图片keepalived+Nginx实现分布式VIP部署_第9张图片

 

 

4、在提示符下输入reboot命令,重新启动服务器。

keepalived+Nginx实现分布式VIP部署_第10张图片

5、重启完成后用hostname命令查询系统主机名,可以看出系统主机名已经变更为mycomputer。

keepalived+Nginx实现分布式VIP部署_第11张图片

 

keepalived安装:

https://www.cnblogs.com/skying555/p/8024503.html

安装依赖

yum -y install gcc gcc+ gcc-c++

yum install popt-devel openssl openssl-devel libssl-dev libnl-devel popt-devel

安装内核

yum -y install kernel kernel-devel

keepalived+Nginx实现分布式VIP部署_第12张图片

当前kernel代码建立连接 ln -s /usr/src/kerners/2.6....../ /usr/src/linux
安装keepalived

wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

tar -zxvf keepalived-1.2.2.tar.gz

cd keepalived-1.2.2

./configure

keepalived+Nginx实现分布式VIP部署_第13张图片

make

keepalived+Nginx实现分布式VIP部署_第14张图片

make install

keepalived+Nginx实现分布式VIP部署_第15张图片

拷贝相应的文件

cp /usr/local/sbin/keepalived /usr/sbin/ 

cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ 

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

cp -r /usr/local/etc/keepalived/ /etc/

配置keeplivednginx主机

进入keepalived目录,上传脚本

#!/bin/sh
ng_thread=$(ps -ef|grep nginx |grep process|wc -l)
Tom_thread=$(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|awk '{print $2}')
echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')],进程ID为:&Tom_thread"
if [  $ng_thread -eq 0 ];then
    echo "Nginx已经停止,启动Nginx,Starting nginx!"
    /usr/local/nginx/sbin/nginx
    
elif  [ ! $Tom_thread ];then
    echo "8880的Tomcat已经停止,启动!"
    sh /opt/tomcat8880/bin/startup.sh
fi
 

 

 

cd /etc/keepalived

上传服务器后,文件需要获取执行权限

chmod +x /etc/keepalived/nginx_check.sh

两个服务器都执行以下操作:

vi /etc/keepalived/keepalived.conf

keepalived+Nginx实现分布式VIP部署_第16张图片

Tips

state   参数值:主的是MASTER、备用的是BACKUP

priority 参数值: MASTER > BACKUP

virtual_router_id 参数值要一样

测试测试:

两台测试机134\135均启动keepalivednginx

service keepalived restart

service keepalived nginx

验证nginx启动正常:

ip a 验证虚拟IP是否成功

keepalived+Nginx实现分布式VIP部署_第17张图片

访问 masterhttp://10.88.202.19:8888/

访问 backup: http://10.88.202.20:8888/

查看keepalived的日志信息:

134\135均打开日志信息方便查看keepalived动态:

tail -f /var/log/messages

浏览器打开虚拟ip访问:http://192.168.232.16:8080/ ,此时显示IP10.88.202.19

 

http://10.88.202.88:8888/

keepalived+Nginx实现分布式VIP部署_第18张图片

http://10.88.202.88:8888/Demo

keepalived+Nginx实现分布式VIP部署_第19张图片

常用操作命令

vim /etc/keepalived/keepalived.conf

service keepalived restart

 

vim /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/sbin/nginx -s reload [stop start]

其他

Linux测试Nginx

elinks 10.88.202.20:8888  --dump

keepalived+Nginx实现分布式VIP部署_第20张图片

测试主从keepalived

keepalived+Nginx实现分布式VIP部署_第21张图片

 

你可能感兴趣的:(系统部署相关)