consul 使用

# 服務端安裝:

 

## consul  安裝


#### 依賴關係:

```

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

```


#### 安裝consul:

```bash

yum install consul -y

```

#### 啓動命令(簡單啓動):

```bash

consul agent -server -bootstrap -data-dir /tmp/consul

```


#### 查看信息:

```

consul members

```


#### 查看當前agent狀態:

```

consul info

```


#### 安裝consul web UI:

```bash

unzip consul_0.6.0_web_ui.zip

mkdir /tmp/ui

mv index.html static/ /tmp/ui/

```


#### 啓動命令:

``` bash

consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=web1 -bind=0.0.0.0 -config-dir=/etc/consul.d -client 0.0.0.0 -ui-dir=/tmp/ui/


consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=one1 -bind=0.0.0.0 -config-dir=/etc/consul.d -client 0.0.0.0 -ui-dir=/tmp/ui/



consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -client 0.0.0.0 -bind=192.168.100.123 -ui-dir=/tmp/ui/ -node=server01 -dc=upstream


作者:cooper_zh

鏈接:http://www.jianshu.com/p/35b03c82f9fd

來源:簡書

著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

```


#### 命令行添加 機器:

```bash

curl  -X PUT http://10.19.50.111:8500/v1/kv/upstreams/test/10.19.168.73:8000

```

#### 命令行查詢註冊的服務:

```

curl http://localhost:8500/v1/catalog/service/web


 curl -s http://localhost:8500/v1/catalog/service/consul


 curl -s http://localhost:8500/v1/catalog/node/one1

```

#### 命令行查看鍵值對信息:

```bash

curl http://localhost:8500/v1/kv/?recurse

```


**** 



# 客戶端安裝:


## nginx +  upsync 安裝:


####  下載並解壓nginx-upsync:

```bash

wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

unzip master.zip

```


#### 創建upsync需要的目錄:

```BASH

mkdir -p /data/nginx/conf/servers/

chmod 777 -R /data/nginx/conf/servers/


```


#### 安裝nginx  (最後一個選項很重要):

```bash

wget http://nginx.org/download/nginx-1.11.5.tar.gz

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

mkdir -p /var/tmp/nginx/client/

mkdir -p /data/nginx


tar -zxvf nginx-1.11.5.tar.gzs

./configure --prefix=/data/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-uwsgi-temp-path=/var/tmp/nginx/uwsgi  \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre  \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

--add-module=../nginx-upsync-module-master

```


#### nginx 配置文件:

```bash


user  nobody;

worker_processes  1;


#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;


#pid        logs/nginx.pid;



events {

    worker_connections  1024;

}



http {

    include       mime.types;

    default_type  application/octet-stream;


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"'

               '$upstream_addr $upstream_status $upstream_response_time $request_time';


    access_log  logs/access.log  main;


    sendfile        on;

    #tcp_nopush     on;


    #keepalive_timeout  0;

    keepalive_timeout  65;

    

    #gzip  on;

    #

    #vhost_traffic_status_zone;

    #proxy_cache_path /tmp/cache_backend keys_zone=cache_backend:10m;


    upstream test {

        server 127.0.0.1:11111;

        upsync 10.19.50.111:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

        upsync_dump_path /data/nginx/conf/servers/servers_test.conf;

        #server 192.168.77.140:8080 backup;

    }


    upstream bar {

        server 192.168.77.139:80 weight=1 fail_timeout=10 max_fails=3;

    }



    server {

        listen 80;


        location = / {

        proxy_pass http://test;

          proxy_set_header  Host  $host;

          proxy_set_header  X-Real-IP  $remote_addr;

          proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

          add_header    real $upstream_addr;

        }


        location ~ /Content|Scripts/ {

          proxy_pass http://test;

          proxy_set_header  Host  $host;

          proxy_set_header  X-Real-IP  $remote_addr;

          proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

        } 

        

        location = /bar {

            proxy_pass http://bar;

        }   

        

        location = /upstream_show {

            upstream_show;

        }   

        

        location = /upstream_status {

            stub_status on;

            access_log off;

        }   

    }   

}   



```


***** 










































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章