初創(chuàng)型公司-持續(xù)部署系列(三)部署nginx和consul

nginx的作用是讓后端多個(gè)服務(wù)器來同時(shí)分擔(dān)用戶的流量,從而減少單個(gè)服務(wù)器的壓力。在自動(dòng)化持續(xù)交付的時(shí)候,我們不能采用手工得方式來進(jìn)行灰度發(fā)布,即在nginx服務(wù)器上關(guān)閉發(fā)布服務(wù)器,平滑重啟nginx。這時(shí)引用了consul,通過consul來動(dòng)態(tài)的管理nginx后端服務(wù)器,不需要平滑重啟nginx也能在nginx上關(guān)閉后端服務(wù)器。

安裝依賴

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

下載文件

cd /opt/software
wget https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip
wget http://nginx.org/download/nginx-1.11.5.tar.gz
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

unzip consul_0.7.1_linux_amd64.zip
unzip master.zip 
tar zxf nginx-1.11.5.tar.gz

配置nginx

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx

編譯安裝nginx

./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
make && make install

nginx配置文件

# /usr/local/nginx/conf/nginx.conf
#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 192.168.77.129:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        upsync_dump_path /usr/local/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;
        }
    }
}

創(chuàng)建upsync_dump_path

mkdir /usr/local/nginx/conf/servers/

安裝consul

mkdir /usr/local/consul
mv consul /usr/local/consul/

啟動(dòng)consul

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

添加后端服務(wù)

curl -X PUT http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.140:80
curl -X PUT http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.142:80

關(guān)閉后端服務(wù)

curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}' http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.140:80

開啟后端服務(wù)

curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}' http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.142:80

啟動(dòng)nginx

nginx

查看consul web信息

Paste_Image.png
Paste_Image.png

查看通過consul添加的后端服務(wù)

cat /usr/local/nginx/conf/servers/servers_test.conf 
server 192.168.77.142:80 weight=2 max_fails=2 fail_timeout=10s;
server 192.168.77.140:80 weight=2 max_fails=2 fail_timeout=10s;

訪問nginx,測(cè)試下是否代理到后端服務(wù)

Paste_Image.png

real地址就是后端服務(wù)器的地址。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • 在互聯(lián)網(wǎng)應(yīng)用領(lǐng)域,服務(wù)的動(dòng)態(tài)性需求十分常見,這就對(duì)服務(wù)的自動(dòng)發(fā)現(xiàn)和可動(dòng)態(tài)擴(kuò)展提出了很高的要求。 微服務(wù)系統(tǒng)動(dòng)輒上萬...
    Liberalman閱讀 8,288評(píng)論 23 80
  • 第一章 Nginx簡(jiǎn)介 Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Ngi...
    JokerW閱讀 33,018評(píng)論 24 1,002
  • Page 1:nginx 服務(wù)器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,673評(píng)論 1 41
  • 愛在左,情在右,在生命的兩旁,隨時(shí)撒種,隨時(shí)開花,將這一徑長途點(diǎn)綴得花香彌漫,使得穿花拂葉的行人,踏著荊棘,不覺痛...
    微光陌沫閱讀 95評(píng)論 0 0

友情鏈接更多精彩內(nèi)容