标签:configure type org val stc tom open 硬盘 nginx
注:
Nginx版本必须大于等于1.9,linux我使用的是7.0版本,记得关闭防火墙
开始正文:
这里只提一下两个比较会出错的配置,consul,upsync,PCRE库,SSL库,ZLIB库的配置网上很多不一一详解(我是在/usr/local中配置的)
cd /usr/local/
wget http://nginx.org/download/nginx-1.9.10.tar.gz
tar -zxvf nginx-1.9.10.tar.gz
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
cd nginx-1.9.10
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=../nginx-upsync-module-master
此时如果报错SSL modules require the OpenSSL library
解决办法
yum -y install openssl openssl-devel,然后重新执行以上代码
1.下载keepalived
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
2.解压安装:
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
3.下载插件openssl
yum install -y openssl openssl-devel(需要安装一个软件包)
4.开始编译keepalived
cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
5. make && make install
执行./configure --prefix=/usr/local/keepalived时报错:configure: error: Popt libraries is required
出现此错误的原因:
未安装popt的开发包
解决方法:
yum install popt-devel
安装好popt的开发包。重新执行以上步骤即可。
将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作:
首先创建文件夹,将keepalived配置文件进行复制:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
然后复制keepalived脚本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
安装到这一步可能会报错文件已经存在,所以执行下面代码:
cd /usr/sbin/
rm -f keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
可以设置开机启动:chkconfig keepalived on,最后再创建一个存放备份的文件夹,mkdir /usr/local/nginx/conf/servers/,到此安装完毕。
截取关键两部分:
upstream test {
#这个不用管,固定的
server 127.0.0.1:11111;
#连接ConculServer,动态获取upstream配置负载均衡信息
upsync 192.168.44.128:8500/v1/kv/upstreams/test upsync_timeout=6m
upsync_interval=500ms upsync_type=consul strong_dependency=off;
#动态获取ConculServer相关负载均衡信息持久化到硬盘
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
}
server {
listen 80;
server_name localhost;
location = / {
proxy_pass http://test;
index index.html index.htm;
proxy_connect_timeout 1;
proxy_send_timeout 1;
proxy_read_timeout 1;
}
}
! Configuration File for keepalived
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" #运行脚本
interval 2 #检测时间间隔
weight -20 #如果条件成立的话,则权重 -20
}
# 定义虚拟路由,AA_1 为虚拟路由的标示符,自己定义名称
vrrp_instance AA_1 {
###MASTER 主的意思 BACKUP 从
state MASTER #来决定主从
interface ens33 # 绑定虚拟 IP 的网络接口,根据自己的机器填写
virtual_router_id 111 # 虚拟路由的 ID 号, 有主备连个nginx时,两个节点设置必须一样(自己定义的)
mcast_src_ip 192.168.44.128 #填写虚拟机ip
priority 100 # 节点优先级,主要比从节点优先级高
nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx #执行 Nginx 监控的服务
}
### 虚拟IP地址配置规范 前三个一致,最后一个不一样
virtual_ipaddress {
192.168.44.110 # 虚拟ip
}
}
配置完毕后在虚拟机中查看ip地址,有如下两个说明配置成功
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
chmod 777 nginx_check.sh
启动keepalived:service keepalived start
启动consul(local目录下启动):./consul agent -dev -ui -node=consul-dev -client=(虚拟机ip地址)
ConculWeb的ip地址(要和虚拟机的ip地址相同):http://192.168.44.128:8500
Postman中的URL(必须用put,Postman的使用不写了,发送下面链接便可以在consulweb中的key/value中查看到):
我开了两个tomcat,一个端口号为8081,另一个为8082
http://192.168.44.128:8500/v1/kv/upstreams/test/......:8081
http://192.168.44.128:8500/v1/kv/upstreams/test/......:8082
安装好keepalived后我只要启动Lvs的vip虚拟的ip地址便可以启动nginx
注:URL中的“test”是和配置文件中的一致的,192.168.44.128是我的虚拟机的ip地址,打码的是我的ip地址,但后面必须跟两个tomcat的端口号。
Consul+Nginx+Upsync+Linux+Keepalived+Lvs的动态负载均衡
标签:configure type org val stc tom open 硬盘 nginx
原文地址:https://www.cnblogs.com/lzh110/p/9452463.html