Note:
Nginx version must be greater than or equal to 1.9,linux I am using version 7.0, remember to turn off the firewall
Start text:
Here only to mention the two more error-prone configuration, Consul,upsync,pcre Library, SSL library, zlib library configuration online A lot of not detailed (I was configured in the/usr/local)
configuration of Nginx:
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
If an error occurs, SSL modules require the OpenSSL library
Solutions
Yum-y Install the OpenSSL openssl-devel, and then re-execute the above code
configuration of the keepalived:
1. Download keepalived
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
2. Unzip the installation:
TAR-ZXVF keepalived-1.2.18.tar.gz-c/usr/local/
3. Download the plugin OpenSSL
Yum install-y OpenSSL openssl-devel (need to install a package)
4. Start compiling keepalived
CD keepalived-1.2.18/&&/configure--prefix=/usr/local/keepalived
5. Make && make install
Execute./configure--prefix=/usr/local/keepalived times wrong: configure:error:Popt Libraries is required
The cause of this error occurs:
Development package for Popt not installed
Workaround:
Yum Install Popt-devel
Install the POPT development package. Perform the above steps again.
keepalived installed as a Linux system service:
Install Keepalived as a Linux system service because you do not use the default installation path for keepalived (the default path:/usr/local), you need to do some modifications after the installation is complete:
First create the folder and copy the keepalived configuration file:
Mkdir/etc/keepalived
cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
Then copy the keepalived script file:
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/
Installation to this step may result in an error file already exists, so execute the following code:
cd/usr/sbin/
Rm-f keepalived
cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
You can set the boot start: Chkconfig keepalived on, and finally create a folder to hold the backup, mkdir/usr/local/nginx/conf/servers/, to this installation is complete.
Nginx.config configuration file:
Intercept the key two parts:
Upstream Test {
#这个不用管, fixed
Server 127.0.0.1:11111;
#连接ConculServer, dynamically acquire upstream configuration load balancing information
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;
}
}
Keepalived.config configuration file:
! Configuration File for Keepalived
Vrrp_script Chk_nginx {
Script "/etc/keepalived/nginx_check.sh" #运行脚本
Interval 2 #检测时间间隔
weight-20 #如果条件成立的话, then weight-20
}
# define virtual routes, aa_1 as identifiers for virtual routes, define their own names
Vrrp_instance aa_1 {
# # # #MASTER The meaning of the Lord BACKUP from
State MASTER #来决定主从
Interface Ens33 # BIND virtual IP network interface, fill in according to your own machine
VIRTUAL_ROUTER_ID 111 # The ID number of the virtual route, the two node settings must be the same (as defined by yourself) when the master is ready to connect Nginx
Mcast_src_ip 192.168.44.128 #填写虚拟机ip
Priority 100 # node precedence, mainly higher than from node priority
Nopreempt # High priority setting Nopreempt resolving the problem of re-preemption after exception recovery
Advert_int 1 # Multicast information send interval, two node settings must be the same, default 1s
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Chk_nginx #执行 nginx Monitoring Services
}
# # # Virtual IP address configuration specification first three identical, the last one is not the same
virtual_ipaddress {
192.168.44.110 # virtual IP
}
}
After configuring the virtual machine to view the IP address, the following two instructions are configured successfully
Final deployment script:
#!/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
Authorization script:
chmod 777 nginx_check.sh
Configuration complete, start testing
Start Keepalived:service keepalived Start
Start Consul (start in local directory):./consul agent-dev-ui-node=consul-dev-client= (virtual machine IP address)
Conculweb IP address (to be the same as the virtual machine's IP address): http://192.168.44.128:8500
Postman in the URL (must use Put,postman to write, send the following link can be found in the Key/value in Consulweb):
I opened two Tomcat, one port number is 8081, the other is 8082.
http://192.168.44.128:8500/v1/kv/upstreams/test/......:8081
http://192.168.44.128:8500/v1/kv/upstreams/test/......:8082
After installing the keepalived, I just start the LVS VIP virtual IP address can start Nginx
Note: The URL "Test" is consistent with the configuration file, 192.168.44.128 is the IP address of my virtual machine, the code is my IP address, but the following must be followed by two Tomcat port number.
At this point, the end. The wrong place wants you to correct me.
Dynamic load balancing for Consul+nginx+upsync+linux+keepalived+lvs