1動的な負荷分散とは何ですか
伝統的な負荷分散、もしUpstream
パラメータの変更、再ロードするたびに必要nginx.conf
我々が達成するために動的負荷分散を使用できるように拡張性は、あまり高くないので、ファイル、Upstream
可配置化
、动态化
、无需人工重新加载
nginx.conf
。
これは、分散構成の中心に類似しています。
2動的負荷分散の実装
設定の変更はnginxのをリロードする必要があるすべての発見は、nginxのを再起動します。
Consul
+Consul-template
Consul
+OpenResty
リロードすることなく、動的な負荷分散を実現Consul
+upsync
+Nginx
リロードすることなく、動的な負荷分散を実現
3一般的なサーバーの登録と発見フレーム
一般的なサービス発見フレームワークConsul
、Eureka
、ZooKeeper
とEtcd
。
ZooKeeper
それはで始まったプロジェクトのこの種の最古の一つですHadoop
。それは非常に多くの大企業(YouTubeの、イーベイ、ヤフーなど)によって使用される、信頼性の高い、成熟しています。
etcd
それを使用することであるHTTP
プロトコル键/值
分散システムの構成及び機能レベルは、サービス発見システムを構築するために使用することができるされているストレージシステムに。これは、導入が簡単で、インストールして使用、信頼性の高いデータ永続化機能を提供します。また、非常に安全かつ完全な文書です。
4 Consul
クイックスタート
Consul
サービスの登録と発見システムを分散し、オープンソースでありHTTP
API
、5月メイクサービス登録を実装するのは非常に簡単で発見、それは次の機能をサポートしています。
服务注册
:サービスの実装をすることができますHTTP API
かDNS
の方法サービスが登録されていますConsul
。服务发现
:サービスの消費者ができるHTTP API
か、DNS
ファッション、からのConsul
サービスへのアクセスIP
とPORT
。故障检测
次のような支持体TCP
、HTTP
ヘルスチェックおよび他の方法のメカニズム、サービスが故障している場合、それによって自動的に除去されます。K/V存储
:使用K/V存储
使用して、動的流通センター、HTTP
変更と構成の変更を達成するために、長いポーリングトリガーを。多数据中心
:複数のデータセンターのサポート、あなたが登録しても、地方消費のみ、ルームサービス、マルチデータセンターのクラスタをサポートする単一のデータセンター内の単一障害点を回避するためであるデータセンターでサービスを見つけることができます。Raft算法
:Consul
使用するRaft
アルゴリズムクラスタのデータの一貫性を。
Consul
管理できるサービス登録と発見、その後、必要があり、Nginx
同じマシン上に配備Agent
実装するNginx
構成の変更やNginx
再起動機能を。我々は持っているConfd
か、Consul-template
2つのオプションが、しかしConsul-template
であるConsul
、我々はそれを選択し、公式の。使用するHTTP
(使用して、変更と構成の変更を達成するために、長いポーリングトリガーをコマンドの実装を)。言い換えれば、我々は、使用設定テンプレートを達成するために、次に引っ張る生成するために、コンフィギュレーションレンダリングテンプレートを実際の構成を。Consul
watch
Consul-template
Consul
Nginx
5 Nginx
+ Consul
+ UpSync
原則
ConsulServer
ロードバランシングはストア構成に使用されています
ConsulWeb
視覚的なプロファイルの変更インタフェースとして
Nginx
合格するUpSync
読書間隔ConsulServer
以上のプロファイルを、プロファイルはまた、原因のサービスの停止にならないように、ローカルにキャッシュされますセンターの設定は使用できません。
6 Consul
ビルドに環境
1.ダウンロード
consul_0.7.5_linux_amd64.zip
wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip
2.エキス
consul_0.7.5_linux_amd64.zip
unzip consul_0.7.5_linux_amd64.zip
解凍エラーの場合
-bash: unzip: 未找到命令
ソリューション
yum -y install unzip
3.実行し
./consul
、インストールが成功した上で、次のメッセージが表示されますが
./consul
[root@weaver-1 ~]# ./consul
usage: consul [--version] [--help] <command> [<args>]
Available commands are:
agent Runs a Consul agent
configtest Validate config file
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
snapshot Saves, restores and inspects snapshots of Consul server state
version Prints the Consul version
watch Watch for changes in Consul
[root@weaver-1 ~]#
4. [スタート]
consul
./consul agent -dev -ui -node=consul-dev -client=192.168.153.11
5.一時的にファイアウォールを無効にします
systemctl stop firewalld
6.ブラウザアクセス:http://192.168.153.11:8500/
7.郵便配達登録HTTPサービスの
http://192.168.153.11:8500/v1/catalog/register
{
"Datacenter":"dc1",
"Node":"tomcat",
"Address":"192.168.153.11",
"Service":{
"Id":"192.168.153.11:8001",
"Service":"test",
"tags":[
"dev"
],
"Port":8001
}
}
{
"Datacenter":"dc1",
"Node":"tomcat",
"Address":"192.168.153.11",
"Service":{
"Id":"192.168.153.11:8002",
"Service":"test",
"tags":[
"dev"
],
"Port":8002
}
}
Datacenter
:指定したデータセンターAddress
:IPサービスを指定しますService.Id
:ユニークなIDを指定しますService.Service
:指定したサービスのグループ化Service.tags
:指定サービスタグ(このようなテスト環境として、事前の環境)Service.Port
:サービスポートを指定します。
7.発見のHttpサービス
http://192.168.153.11:8500//v1/catalog/service/item_jd_tomcat
7 nginx-upsync-module
注:クリアする前に
Nginx
環境を再インストールします。
7.1 nginx-upsync-module
はじめに
Upsync
新浪微博は、オープンソースに基づいてNginx
動的に構成された3つのモジュールを達成するために。Nginx-Upsync-Module
関数は、プルするConsul
の後端server
リスト、および動的に更新Nginx
情報をルーティングします。このモジュールは、サードパーティのモジュールに依存しません。使用サービス、各それぞれのプロセス独立プルする構成を、そのルーティングを更新します。Consul
Nginx
DB
Consul
KV
Nginx Work
upstream
7.2 nginx-upsync-module
インストール
ファイルのダウンロード
cd /usr/local/
1.ダウンロード
Nginx
wget http://nginx.org/download/nginx-1.9.10.tar.gz
役割:リバースプロキシを達成するために、ロード・ロード・ライブラリー
2.ダウンロード
consul
wget https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip
役割:動的な負荷分散は、登録を達成するように構成されています
3.ダウンロード
nginx-upsync-module
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip
役割:nginx
最新への動的アクセスupstream
情報
解凍インストーラ
unzip master.zip
unzip consul_0.7.1_linux_amd64.zip
解凍エラーの場合
-bash: unzip: 未找到命令
ソリューション
yum -y install unzip
インストール Nginx
減圧
Nginx
tar -zxvf nginx-1.9.10.tar.gz
コンフィギュレーション
Nginx
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
コンパイル
Nginx
cd nginx-1.9.10
./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
コンパイルが与えられています
./configure: error: SSL modules require the OpenSSL library.
ソリューション
yum -y install openssl openssl-devel
Upstream
動的構成
## 动态去consul 获取注册的真实反向代理地址
upstream test {
server 127.0.0.1:11111;
upsync 192.168.153.11: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 {
listen 80;
server_name localhost;
location / {
proxy_pass http://test;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upsync
命令の指定からconsul
パス上流のプルサーバの設定;upsync_timeout
以下からの設定consul
上流のサーバ設定のタイムアウトプル。upsync_interval
以下からの設定consul
上流のサーバの設定間隔プル。upsync_type
指定のconsul
コンフィギュレーション・サーバ;strong_dependency
配置されたnginx
必須の依存関係の設定サーバは、設定されている場合かどうか、起動時にon
、プル構成が失敗したときにnginx
、起動にも失敗しました。upsync_dump_path
指定されたconsul
場合でも、そうすることを、引っ張っ位置に永続上流サーバーconsul
のサーバーに問題があり、ローカルバックアップがあります。
注意:交換してください
consul
。レジストリ住所を
作ります
upsync_dump_path
mkdir /usr/local/nginx/conf/servers/
upsync_dump_path
指定されたconsul
場合でも、そうすることを、引っ張っ位置に永続上流サーバーconsul
のサーバーに問題があり、ローカルバックアップがあります。
スタート
consul
一時的にファイアウォールを無効にします
systemctl stop firewalld
/usr/local/consul agent -dev -ui -node=consul-dev -client=192.168.153.11
両方の開始
Tomcat
サービスを
/root/tomcat-1/bin/startup.sh
/root/tomcat-2/bin/startup.sh
スタート
Nginx
/usr/local/nginx/sbin/nginx
追加
nginx
Upstream
サービス
- 使用し
postmen
た送信put
要求を
http://192.168.153.11:8500/v1/kv/upstreams/test/192.168.153.11:8001
http://192.168.153.11:8500/v1/kv/upstreams/test/192.168.153.11:8002
テスト:
負荷分散情報パラメータを変更します。
{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}
{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}
試験重量は力に見出すことができます。