日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

Nginx容器动态流量管理方案-nginx-upsync-module+nginx_upstream_check_module初体验

發布時間:2024/2/28 Nginx 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx容器动态流量管理方案-nginx-upsync-module+nginx_upstream_check_module初体验 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

緣起
最近一直在研究日志收集系統的框架,之前在線游戲的數據發送都是由游戲服務器發送的,我來之前一直是rsync傳輸,也還算穩定。但現在上了單機游戲,只能由手機客戶端直接發送,dau比較高,最近很火的<<貪吃蛇>>在海外上線,一個星期dau已經達到千萬級別。初步方案,lvs做4層負載均衡,下掛nginx做7層轉發,數據直接入kafaka。一直不太喜歡通過reload修改nginx upstream,剛好看到微博的upsync覺得很有價值,于是拿來研究了一下。

感興趣的童鞋可以看下這篇文章http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=404151075&idx=1&sn=5f3b8c007981a2d048766f808f8c8c98&scene=2&srcid=0223XScbJrOv7noogVX6T60Q&from=timeline&isappinstalled=0#wechat_redirect(Upsync:微博開源基于Nginx容器動態流量管理方案 )

consul
upsync的upstream可以從consul(https://www.consul.io/intro/getting-started/install.html)或etcd中獲取,本文使用的是consul這個用golang寫的服務注冊與發現框架,據我所知consul在國內也用的非常廣,比如微博,今日頭條等。所以需要先安裝consul,consul的getting-started文檔用vagrant(https://www.vagrantup.com/docs/getting-started/)搭建集群的,vagrant是用ruby開發的用來構建虛擬開發環境的工具,有點類似docker,不過vagrant是依賴與虛擬機的,比如virtualbox

下面是我ubuntu主機安裝操作:?
1.安裝virtualbox和vagrant

sudo apt-get install virtualbox wget https://releases.hashicorp.com/vagrant/1.8.6/vagrant_1.8.6_x86_64.deb sudo dpkg -i vagrant_1.8.6_x86_64.deb



2.下載vagrant鏡像?
由于hashicorp下載太慢,我是在百度云(https://pan.baidu.com/s/1pLzVhnP)上找的一個ubuntu_trusty_64.box鏡像。

vagrant box add ubuntu ~/vagrant_start/ubuntu_trusty_64.box



把ubuntu_trusty_64.box加載到vagrant中,并命名為ubuntu

3.創建Vagrantfile?
在當前目錄下創建Vagrantfile,類似于dockerfile

# -*- mode: ruby -*- # vi: set ft=ruby : $script = <<SCRIPTecho Installing dependencies... sudo apt-get update sudo apt-get install -y unzip curlecho Fetching Consul... cd /tmp/ curl https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip -o consul.zipecho Installing Consul... unzip consul.zip sudo chmod +x consul sudo mv consul /usr/bin/consulsudo mkdir /etc/consul.d sudo chmod a+w /etc/consul.dSCRIPT# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2"Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|config.vm.box = "ubuntu"config.vm.provision "shell", inline: $scriptconfig.vm.define "n1" do |n1|n1.vm.hostname = "n1"n1.vm.network "private_network", ip: "172.20.20.10"endconfig.vm.define "n2" do |n2|n2.vm.hostname = "n2"n2.vm.network "private_network", ip: "172.20.20.11"end end



4.啟動虛擬機并登陸到n1

vagrant up vagrant ssh n1



vagrent up將會讀取當前目錄的Vagrantfile,創建兩個虛擬機n1,n2,并安裝consul到/usr/bin/consul目錄。

5.啟動consul

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



n1將會作為leader節點,并開啟ui界面,可通過http://172.20.20.10:8500訪問consul管理界面?
6.把upstream存到consul

curl -X PUT http://172.20.20.10:8500/v1/kv/upstreams/test/172.20.20.11:8080



將會把upsteam 172.20.20.11:8080存到consul的kv store中。?
到這里consul就結束了

nginx-upsync-module+nginx_upstream_check_module
upsync(https://github.com/weibocom/nginx-upsync-module)是微博開源的,以及在微博廣泛使用了,下面將安裝上面這兩個nginx擴展

1.下載對應軟件包?
如果在宿主機,最好在Vagrantfile目錄下載,Vagrant會共享當前目錄到虛擬機的/vagrant?
目錄,如果在虛擬機則可以在/vagrant目錄下載。

需要注意版本,具體看upsync Readme,由于我使用時upsync master支持nginx1.9+,所以我使用了nginx1.9.2

vagrant ssh n2 wget 'http://nginx.org/download/nginx-1.9.2.tar.gz' git clone https://github.com/weibocom/nginx-upsync-module git clone https://github.com/xiaokai-wang/nginx_upstream_check_module



nginx_upstream_check_module需要根據nginx不同版本給nginx打patch,具體看Readme,upstream_check是tengine(http://tengine.taobao.org/documentation_cn.html)中的模塊,主要作用用于upstream的健康檢查。當遇到不可用的upstream時,nginx雖然會自動使用下一個upstream,但下一次還是會重試該upstream,導致無效請求增多。upstream_check檢測到不可用會剔除該upsteam,該特性還是很有用的。

2.編譯安裝nginx

sudo apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential wget 'http://nginx.org/download/nginx-1.9.2.tar.gz' git clone https://github.com/weibocom/nginx-upsync-module git clone https://github.com/xiaokai-wang/nginx_upstream_check_module patch -p0 < /vagrant/nginx_upstream_check_module/check_1.9.2+.patch./configure --add-module=/vagrant/nginx_upstream_check_module ?--add-module=/vagrant/nginx-upsync-module? make && make install



3.配置nginx?
nginx會默認安裝到/usr/local/nginx目錄下

cd /usr/local/nginx mkdir /usr/local/nginx/conf/conf.d mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_backuseradd -M nginx mkdir /var/log/nginx mkdir /usr/local/nginx/conf/servers



將之前的nginx.conf備份,用下面內容替換?

vim /usr/local/nginx/conf/nginx.confuser ?nginx; worker_processes ?5;error_log ?/var/log/nginx/error.log warn; pid ? ? ? ?/var/run/nginx.pid;events {worker_connections ?1024; }http {include ? ? ? /usr/local/nginx/conf/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"';access_log ?/var/log/nginx/access.log ?main;sendfile ? ? ? ?on;#tcp_nopush ? ? on;keepalive_timeout ?65;#gzip ?on;include /usr/local/nginx/conf/conf.d/*.conf; }



創建check.conf?

vim /usr/local/nginx/conf/conf.d/check.confupstream test {# fake server otherwise ngx_http_upstream will report error when startupserver 127.0.0.1:11111;# all backend server will pull from consul when startup and will delete fake serverupsync 172.20.20.10: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;check interval=1000 rise=2 fall=2 timeout=3000 type=http default_down=false;check_http_send "HEAD / HTTP/1.0\r\n\r\n";check_http_expect_alive http_2xx http_3xx;}upstream bar {server 172.20.20.11:8080 weight=1 fail_timeout=10 max_fails=3;}server {listen 80;location = / {proxy_pass http://test;}location = /upstream_show {upstream_show;}location = /upstream_status {check_status;access_log off;}}



4.啟動nginx

我們在上面介紹consul的最好增加一個172.20.20.11:8080 upstream, 在n2上我們開一個python的http server

/usr/local/nginx/sbin/nginx python -m SimpleHTTPServer 8080



大功告成,看下成果

請將下面的172.20.20.13地址換成你們的172.20.20.11,我開了很多個虛擬機^^^^

首頁就是用python開的simplehttpserver?


upstream列表?


upstream狀態?

---------------------?
作者:yueguanghaidao?
來源:CSDN?
原文:https://blog.csdn.net/yueguanghaidao/article/details/52801043?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

總結

以上是生活随笔為你收集整理的Nginx容器动态流量管理方案-nginx-upsync-module+nginx_upstream_check_module初体验的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。