基于consul实现nginx的动态upstream-国外便宜VPS

基于consul实现nginx的动态upstream-国外便宜VPS

consul下载地址 https://releases.hashicorp.com/consul/1.0.6/consul_1.0.6_linux_amd64.zip

nginx-upsync-module下载地址 https://github.com/weibocom/nginx-upsync-module/archive/nginx-upsync-1.8.x.zip

nginx下载地址

2. 简单Demo 2.1 安装并配置Consul

1

2

3

4

5

6

7

8

9

 

# 解压包

unzip consul_1.0.6_linux_amd64.zip

# 移动consul到格式位置

mv consul/root/services/account_consul/

# 启动consul

/root/services/account_consul/consul agent-server-bootstrap-expect=1-data-dir=/root/data/consul/-node=accounting01-bind=172.31.132.207-config-dir=/root/data/consul_config/-client0.0.0.0-ui

# 添加upstreams配置

curl-XPUT http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8000

curl-XPUT http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8001

 

访问:8500/ui/

基于consul实现nginx的动态upstream-国外便宜VPS

上图我们可以看出,我们已经通过http请求存储了upstreams配置信息, 接下来我们需要让Nginx从consul自动更新upstreams配置;

2.2 重新编译安装Nginx,添加upsync模块 2.2.1 install

1

2

3

4

5

6

7

8

9

 

# 解压

tar-zxvf nginx-1.8.0.tar.gz

unzip nginx-upsync-1.8.x.zip

# 编译安装

cd nginx-1.8.0

./configure   –prefix=/usr/local/nginx     –with-pcre  –with-http_stub_status_module –with-http_ssl_module –with-http_gzip_static_module –with-http_realip_module –with-http_sub_module  –add-module=/root/packages/nginx-upsync-module-nginx-upsync-1.8.x

make&&make install

# 检查Nginx是否按照成功

/usr/local/nginx/sbin/nginx

 

2.2.2 config

1

2

3

4

5

6

7

8

9

10

11

12

13

 

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

# 修改用户

(PS:由于我是root环境启动的服务,Nginx的用户不是root的话,某些文件无法访问,例如:静态文件)

user  root;

# 添加额外配置文件路径

(PS:非必须,但是我习惯这么做)

http{

    …

    include/usr/local/nginx/conf/sites-enabled/*;

    server{

    ….

    }

}

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

 

# 添加Nginx的upstream配置

(accouting_server)  conf cd sites-enabled

(accouting_server)  sites-enabled pwd

/usr/local/nginx/conf/sites-enabled

(accouting_server)  sites-enabled ll

total8.0K

-rw-r–r– 1 root root 915 Feb 24 18:13 accounting

-rw-r–r– 1 root root 213 Feb 24 18:13 consul

(accouting_server)  sites-enabledcataccounting

upstreamaccounting{

    server127.0.0.1:11111;

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。