Ubuntu18 上使用 docker 的 nginx 容器模拟负载均衡

安装Docker

创建sh文件,也可以一条一条复制运行 vim install-docker.sh

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce

sudo systemctl enable docker
sudo systemctl start docker

运行安装
file
查看docker
file
换成国内源
vim /etc/docker/daemon.json 没有则新建此文件

{
    "registry-mirrors": [
        "https://registry.docker-cn.com"
    ]
}

保存,然后重启docker server
service docker restart

拉取nginx镜像&运行容器实例

  1. 获取nginx镜像 docker pull nginx
  2. 然后使用 docker images可以查看获取的镜像
  3. 在本地创建docker/nginx1/index.html文件和docker/nginx2/index.html文件,用于挂载项目文件
  4. 在index.html文件输入内容 "hello,this is nginx1." 和 "hello,this is nginx2".
  5. 创建运行nginx容器
    file
    1.模拟转发请求的nginx
    docker run  -d --name nginx -p 8081:80 nginx
    2.模拟第一台服务器的nginx
    docker run  -d --name nginx1 -p 8082:80 -v /root/docker/nginx1:/usr/share/nginx/html nginx
    3.模拟第二台服务器的nginx
    docker run  -d --name nginx2 -p 8083:80 -v /root/docker/nginx2:/usr/share/nginx/html nginx

-d:守护进程,在后台运行
-v:挂载文件,挂载刚刚创建的docker/nginx1目录
-p:表示本地映射容器内的端口 (本地8081映射容器内的80)
--name :取名称

在浏览器输入127.0.0.1:8081、127.0.0.1:8082、127.0.0.1:8083可以访问容器内的index.html
file
filefile
查看容器状态
docker ps -a
file
进入容器nginx,安装vim
docker exec -it nginx /bin/bash
sudo apt-get update
sudo apt-gei install vim
file
到这里环境就装完了,接下来是nginx的配置.

修改nginx的配置

  1. 编辑vim /etc/nginx/nginx.conf 文件,注意这里的ip是宿主机的ip

    upstream webapp { 
      server 192.168.1.187:8082; 
      server 192.168.1.187:8083; 
    }

    file

  2. 编辑vim /etc/nginx/conf.d/default.conf文件,在location / {}引入刚刚写的webapp.

    proxy_pass http://webapp;

    file

  3. 重启nginx service nginx restart

测试

file

upstream依照轮询(默认)方式进行负载,每一个请求按时间顺序逐一分配到不同的后端服务器。假设后端服务器down掉。能自己主动剔除。尽管这样的方式简便、成本低廉。但可靠性低和负载分配不均衡。

upstream有很多分配策略:如weight、ip_hash等。

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 2

update不了,没法编辑文件了,怎么办?

[root@s docker]# docker exec -it nginx /bin/bash
root@159ef707449a:/# apt-get update
Get:1 http://security-cdn.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Ign:1 http://security-cdn.debian.org/debian-security stretch/updates InRelease
Ign:2 http://cdn-fastly.deb.debian.org/debian stretch InRelease
Get:3 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Ign:3 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease
Get:5 http://cdn-fastly.deb.debian.org/debian stretch Release [118 kB]
Get:4 http://security-cdn.debian.org/debian-security stretch/updates Release [92.7 kB]
Ign:4 http://security-cdn.debian.org/debian-security stretch/updates Release
Ign:5 http://cdn-fastly.deb.debian.org/debian stretch Release
Get:6 http://cdn-fastly.deb.debian.org/debian stretch-updates Release [89.4 kB]
Ign:6 http://cdn-fastly.deb.debian.org/debian stretch-updates Release
Get:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages [207 kB]
Ign:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages
Get:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7084 kB]
Ign:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages
Get:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages [480 kB]
Ign:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages
Get:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages [3359 kB]
Ign:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages
Ign:7 http://security.debian.org/debian-security stretch/updates/main all Packages
Ign:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages
Ign:8 http://security.debian.org/debian-security stretch/updates/main amd64 Packages
Ign:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages
Get:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages [5728 B]
Ign:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages
Ign:7 http://security.debian.org/debian-security stretch/updates/main all Packages
Ign:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages
Ign:8 http://security.debian.org/debian-security stretch/updates/main amd64 Packages
Ign:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages
Get:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [11.1 kB]
Ign:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages
Get:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages [254 kB]
Ign:7 http://security.debian.org/debian-security stretch/updates/main all Packages
Ign:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages
Ign:9 http://deb.debian.org/debian stretch/main amd64 Packages
Ign:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages
Ign:10 http://deb.debian.org/debian stretch/main all Packages
Ign:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages
Get:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages [600 kB]
Ign:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages
Ign:11 http://deb.debian.org/debian stretch-updates/main all Packages
Ign:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages
Ign:7 http://security.debian.org/debian-security stretch/updates/main all Packages
Ign:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages
Ign:12 http://deb.debian.org/debian stretch-updates/main amd64 Packages
Ign:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages
Ign:8 http://security.debian.org/debian-security stretch/updates/main amd64 Packages
Ign:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages
Ign:9 http://deb.debian.org/debian stretch/main amd64 Packages
Ign:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages
Ign:7 http://security.debian.org/debian-security stretch/updates/main all Packages
Ign:7 http://security-cdn.debian.org/debian-security stretch/updates/main all Packages
Ign:10 http://deb.debian.org/debian stretch/main all Packages
Ign:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages
Ign:11 http://deb.debian.org/debian stretch-updates/main all Packages
Ign:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages
Err:8 http://security.debian.org/debian-security stretch/updates/main amd64 Packages
  Error writing to output file - write (28: No space left on device) Error writing to file - write (28: No space left on device)
Ign:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages
Ign:12 http://deb.debian.org/debian stretch-updates/main amd64 Packages
Ign:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages
Get:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [9478 kB]
Ign:9 http://deb.debian.org/debian stretch/main amd64 Packages
Ign:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages
Get:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages [4411 kB]
Ign:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages
Get:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages [6173 B]
Ign:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages
Get:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [12.3 kB]
Ign:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages
Ign:9 http://deb.debian.org/debian stretch/main amd64 Packages
Ign:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages
Ign:10 http://deb.debian.org/debian stretch/main all Packages
Ign:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages
Ign:11 http://deb.debian.org/debian stretch-updates/main all Packages
Ign:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages
Ign:12 http://deb.debian.org/debian stretch-updates/main amd64 Packages
Ign:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages
Err:9 http://deb.debian.org/debian stretch/main amd64 Packages
  Error writing to output file - write (28: No space left on device) Error writing to file - write (28: No space left on device)
Ign:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages
Ign:10 http://cdn-fastly.deb.debian.org/debian stretch/main all Packages
Ign:11 http://deb.debian.org/debian stretch-updates/main all Packages
Ign:11 http://cdn-fastly.deb.debian.org/debian stretch-updates/main all Packages
Err:12 http://deb.debian.org/debian stretch-updates/main amd64 Packages
  Error writing to output file - write (28: No space left on device) Error writing to file - write (28: No space left on device)
Ign:12 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages
Reading package lists... Done
W: The repository 'http://security.debian.org/debian-security stretch/updates Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: The repository 'http://deb.debian.org/debian stretch Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: The repository 'http://deb.debian.org/debian stretch-updates Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: Failed to fetch http://security.debian.org/debian-security/dists/stretch/updates/main/binary-amd64/Packages  Error writing to output file - write (28: No space left on device) Error writing to file - write (28: No space left on device)
E: Failed to fetch http://deb.debian.org/debian/dists/stretch/main/binary-amd64/Packages  Error writing to output file - write (28: No space left on device) Error writing to file - write (28: No space left on device)
E: Failed to fetch http://deb.debian.org/debian/dists/stretch-updates/main/binary-amd64/Packages  Error writing to output file - write (28: No space left on device) Error writing to file - write (28: No space left on device)
E: Some index files failed to download. They have been ignored, or old ones used instead.
root@159ef707449a:/# apt-get install vim -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vim
5年前 评论

@lovecn 换中科大源,重新update。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!