1-- nginxのサーバー] [ハイパフォーマンス - 8 HTTP動的負荷分散

1動的な負荷分散とは何ですか

伝統的な負荷分散、もしUpstreamパラメータの変更、再ロードするたびに必要nginx.conf我々が達成するために動的負荷分散を使用できるように拡張性は、あまり高くないので、ファイル、Upstream 可配置化动态化无需人工重新加载 nginx.conf

これは、分散構成の中心に類似しています。

2動的負荷分散の実装

設定の変更はnginxのをリロードする必要があるすべての発見は、nginxのを再起動します。

  1. Consul + Consul-template
  2. Consul+ OpenRestyリロードすることなく、動的な負荷分散を実現
  3. Consul+ upsync+ Nginxリロードすることなく、動的な負荷分散を実現

3一般的なサーバーの登録と発見フレーム

一般的なサービス発見フレームワークConsulEurekaZooKeeperEtcd

ZooKeeperそれはで始まったプロジェクトのこの種の最古の一つですHadoopそれは非常に多くの大企業(YouTubeの、イーベイ、ヤフーなど)によって使用される、信頼性の高い、成熟しています。

etcdそれを使用することであるHTTPプロトコル键/值分散システムの構成及び機能レベルは、サービス発見システムを構築するために使用することができるされているストレージシステムに。これは、導入が簡単で、インストールして使用、信頼性の高いデータ永続化機能を提供します。また、非常に安全かつ完全な文書です。

4 Consulクイックスタート

Consulサービスの登録と発見システムを分散し、オープンソースでありHTTP API、5月メイクサービス登録を実装するのは非常に簡単で発見、それは次の機能をサポートしています。

  • 服务注册:サービスの実装をすることができますHTTP APIDNSの方法サービスが登録されていますConsul
  • 服务发现:サービスの消費者ができるHTTP APIか、DNSファッション、からのConsulサービスへのアクセスIPPORT
  • 故障检测次のような支持体TCPHTTPヘルスチェックおよび他の方法のメカニズム、サービスが故障している場合、それによって自動的に除去されます。
  • K/V存储:使用K/V存储使用して、動的流通センター、HTTP変更と構成の変更を達成するために、長いポーリングトリガーを。
  • 多数据中心:複数のデータセンターのサポート、あなたが登録しても、地方消費のみ、ルームサービス、マルチデータセンターのクラスタをサポートする単一のデータセンター内の単一障害点を回避するためであるデータセンターでサービスを見つけることができます。
  • Raft算法Consul使用するRaftアルゴリズムクラスタのデータの一貫性を。

Consul管理できるサービス登録と発見、その後、必要があり、Nginx同じマシン上に配備Agent実装するNginx構成の変更やNginx再起動機能を。我々は持っているConfdか、Consul-template2つのオプションが、しかしConsul-templateであるConsul、我々はそれを選択し、公式の。使用するHTTP(使用して、変更と構成の変更を達成するために、長いポーリングトリガーをコマンドの実装を)。言い換えれば、我々は、使用設定テンプレートを達成するために、次に引っ張る生成するために、コンフィギュレーションレンダリングテンプレートを実際の構成を。ConsulwatchConsul-templateConsulNginx

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情報をルーティングします。このモジュールは、サードパーティのモジュールに依存しません。使用サービス、各それぞれのプロセス独立プルする構成を、そのルーティングを更新します。ConsulNginxDBConsulKVNginx Workupstream

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サービス

  1. 使用し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}

ここに画像を挿入説明
ここに画像を挿入説明

試験重量は力に見出すことができます。

675元記事公開 ウォンの賞賛214 ビューに14万+を

おすすめ

転載: blog.csdn.net/weixin_42112635/article/details/104951988