1. 程式人生 > >Docker-3:nginx安裝及負載均衡

Docker-3:nginx安裝及負載均衡

一、下載nginx映象:

[[email protected] nginx]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
cfl.io/cfl-account-dubbo-api    latest              afc03cac5e57        2 days ago          670 MB
cfl.io/cfl-account-dubbo-user   latest              10524a6c1f4e        2 days ago          697 MB
docker.io/zookeeper             latest              29d319df9c13        6 days ago          149 MB
docker.io/nginx                 latest              c82521676580        3 weeks ago         109 MB
docker.io/java                  latest              d23bdf5b1b1b        19 months ago       643 MB
[
[email protected]
nginx]#

二、啟動nginx設定檔案掛載:

1、在宿主機器新建資料夾

     /data/docker/nginx/html (靜態資料夾)

     /data/docker/nginx/logs (日誌資料夾)

2、在宿機器新建檔案(進入nginx映象內,拷貝出來一份)

     /data/docker/nginx/nginx.conf

     /data/docker/nginx/conf.d/default.conf

3、修改/data/docker/nginx/nginx.conf

upstream server_account_api {
    server t01:8001 weight=1;
    server t02:8001 weight=1;
} 

4、修改/data/docker/nginx/conf.d/default.conf

location / {
    proxy_pass http://server_account_api;
}

5、啟動

     --name 名稱

     -p 宿主機埠:映象內埠

     --link=需訪問的已啟動映象名稱:nginx.conf配置檔案中的別名

     -v 掛載到宿主機器資料夾或檔案:nginx內被掛載的資料夾或檔案

docker run -d --name nginx -p 80:80 --link=account-api:t01 --link=account-api-2:t02 -v /data/docker/nginx/html:/usr/share/nginx/html -v /data/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf  -v /data/docker/nginx/logs:/var/log/nginx c82521676580	
[[email protected] nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
09784aad6f36        c82521676580        "nginx -g 'daemon ..."   5 hours ago         Up 5 hours          0.0.0.0:80->80/tcp                           nginx
f5faaaf42e36        afc03cac5e57        "sh -c 'java $JAVA..."   5 hours ago         Up 5 hours          0.0.0.0:8002->8001/tcp                       account-api-2
4f5078bf0b9d        afc03cac5e57        "sh -c 'java $JAVA..."   2 days ago          Up 5 hours          0.0.0.0:8001->8001/tcp                       account-api
3ab551787a07        10524a6c1f4e        "sh -c 'java $JAVA..."   2 days ago          Up 5 hours          0.0.0.0:8011->8011/tcp                       account-user
b81da2790da8        29d319df9c13        "/docker-entrypoin..."   2 days ago          Up 5 hours          2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp   zk
[[email protected] nginx]# 

6、在瀏覽器中訪問宿主機器