Ucarp和nginx提供内网vip

ucarp我们来实战一下完成ucarp+nginx做内网vip,模拟F5的vip的方法

ucarp的安装参考之前的文章,环境如下:

  • ucarp1:192.168.19.1
  • ucarp2:192.168.19.2
  • vip:172.18.19.10

在172.18.19.1和172.18.19.2上编译Nginx 1.16.1

1    ./configure --prefix=/export/servers/nginx1161 --with-stream --with-stream_ssl_module
2	make
3	make install

重点是/export/servers/nginx1161/conf/nginx.conf

 1cat /export/servers/nginx1161/conf/nginx.conf
 2
 3  user  nobody;
 4  worker_processes  auto;
 5
 6  events {
 7    use epoll;
 8    worker_connections  65535;
 9  }
10
11  stream {
12  log_format proxy '$remote_addr [$time_local] '
13               '$protocol $status $bytes_sent $bytes_received '
14               '$session_time "$upstream_addr" '
15               '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
16  }
17  
18  access_log logs/tcp-access.log proxy ;
19  open_log_file_cache off;
20
21  upstream stream_backend01 {
22    hash $remote_addr consistent;
23    #server 172.18.31.2:80 weight=5;
24    server 172.18.31.2:80 max_fails=2 fail_timeout=30s;
25    #server 172.18.31.2:80 max_conns=3;
26  }
27
28  server {
29        listen 172.18.19.10:80; 
30        proxy_timeout 20s;
31        proxy_pass stream_backend01;
32  }
33
34}   

注意: 1、打出了tcp-access.log 2、根据源IP做hash,强制分配到后面的同一台服务器上,保证一致性 3、后端的server可以有权重,最大连接,以及失效检测(30s内无法连通2次,就摘掉这个服务器)

同时调整vip-up.sh

1cat /usr/local/bin/vip-up.sh 
2
3#!/bin/sh
4/sbin/ip addr add ${2}/24 dev ${1}
5/sbin/ip neigh flush dev ${1}
6/export/servers/nginx/sbin/nginx
7/export/servers/nginx/sbin/nginx -s reload

谁获得了主ip 172.18.19.10,谁就会启动nginx,并且强制刷一下配置

注意,一开始的时候,由于172.18.19.2没有获得主ip 172.18.19.10,所以上面是不会自动起nginx进程的!!!

测试一下:

1在172.18.19.1上面
2kill -usr2 ucarp的进程pid
3
4看172.18.19.2上面,nginx已经自动启动了
5然后访问
6curl http://172.18.19.10/

ok


LXC更新到Docker之后的IP部分修改
CentOS7安装ZFS
comments powered by Disqus