webtet 发表于 2018-5-26 10:40:16

Docker部署nginx实现负载均衡

Docker部署nginx实现负载均衡
  实验环境:
Centos 7
Docker
Nginx
  本次利用宿主机和两个nginx容器来实现负载均衡 宿主机无任何内容 只搭载一台nginx服务器 并由此台服务器将请求转发给两个nginx容器来进行处理 那就让我们的实验开始吧!

  一、安装相关环境
1.安装Docker(个人推荐配置阿里云源 相关配置可从我博客翻阅)
2.宿主机安装Nginx
下面我比较偷懒直接wget Nginx rpm包
老规矩 先关防火墙和selinux

# systemctl stop firewalld.service
# setenforce 0
# yum install docker -y
# wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum install nginx -y
  用命令测试是否成功安装 能否开启服务

# systemctl start docker.service
# systemctl start nginx.service   Centos 7 启动服务命令与6.x不同哦
  测试web界面是否显示?

  完全ok啦 没问题!!!
  下面我们来删除掉nginx默认html 并创建一个空白index.html

# cd /usr/share/nginx/html/
# rm *
rm: remove regular file ‘404.html’? y
rm: remove regular file ‘50x.html’? y
rm: remove regular file ‘index.html’? y
rm: remove regular file ‘nginx-logo.png’? y
rm: remove regular file ‘poweredby.png’? y
# touch index.html
  为什么要创建一个空白index.html呢?
答:其实我一开始也是全删的结果发现访问无效 才想起要让nginx有响应才会触发nginx.conf配置也就是才会触发我们配置的负载均衡这是我个人的理解 有错误敬请提出
  二、安装Nginx容器
1.pull nginx镜像

# docker pull hub.c.163.com/library/nginx:latest             下载最新Nginx镜像
Trying to pull repository hub.c.163.com/library/nginx ...
latest: Pulling from hub.c.163.com/library/nginx
5de4b4d551f8: Pull complete
d4b36a5e9443: Pull complete
0af1f0713557: Pull complete
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6

  2.pull完成使用docker images命令查看

# docker images
REPOSITORY                  TAG               IMAGE ID            CREATED             SIZE
hub.c.163.com/library/nginx   latest            46102226f2fd      9 months ago      109.4 MB
  3.Docker run 创建容器

# docker run -p 8080:80 --name nginx_web1 -it hub.c.163.com/library/nginx /bin/bash             容器名:nginx_web1映射容器8080端口到宿主机的80端口上
  进入nginx_web1容器的nginx目录下 创建一个index.html

root@53cda1b9de1e:/# cd /usr/share/nginx/html/
root@53cda1b9de1e:/usr/share/nginx/html# ls
50x.htmlindex.html
root@53cda1b9de1e:/usr/share/nginx/html# rm *
root@53cda1b9de1e:/usr/share/nginx/html# echo hello nginx_web1 Cany > index.html
root@53cda1b9de1e:/usr/share/nginx/html# exit
  下面创建多一个新的nginx容器

# docker run -p 8081:80 --name nginx_web2 -it hub.c.163.com/library/nginx /bin/bash             容器名:nginx_web2映射容器8081端口到宿主机的80端口上
  进入nginx_web2容器的nginx目录下 创建一个index.html

root@41b3eec738b5:/# cd /usr/share/nginx/html/
root@41b3eec738b5:/usr/share/nginx/html# ls
50x.htmlindex.html
root@41b3eec738b5:/usr/share/nginx/html# rm *
root@41b3eec738b5:/usr/share/nginx/html# echo hello nginx_web2 Cany > index.html
root@41b3eec738b5:/usr/share/nginx/html# exit
  exit退出后容器不运行 先启动两个nginx容器再执行启动服务命令

# docker start 41b3eec738b5
41b3eec738b5
# docker start 53cda1b9de1e
53cda1b9de1e
# docker exec -d 41b3eec738b5service nginx start
# docker exec -d 53cda1b9de1eservice nginx start
  三、配置主机host文件
宿主机ip 10.2.4.88对应 www.abc.top

  四、配置宿主机Nginx.conf文件

# find / -name nginx.conf      查找nginx.conf文件
/etc/nginx/nginx.conf
  在http段加入以下代码

upstream www.abc.top {
server10.2.4.88:8080 weight=10;
server10.2.4.88:8081 weight=20;
}
server{
listen 80;
server_name www.abc.top;
location / {
proxy_pass         http://www.abc.top;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP      $remote_addr;
proxy_set_header   X-Forwarded-For$proxy_add_x_forwarded_for;
}
}
  配置如下图

  保存配置并重启宿主机Nginx

# systemctl restart nginx.service
  查看Nginx容器运行状态

  五、测试

  那就再刷新多几次?

  web1 web2 看出来了吧?
实验成功!!!
页: [1]
查看完整版本: Docker部署nginx实现负载均衡