环境:
Centos 7
Docker
Nginx
本次利用宿主机和两个nginx容器来实现负载均衡 宿主机无任何内容 只搭载一台nginx服务器 并由此台服务器将请求转发给两个nginx容器来进行处理 那就让我们的实验开始吧!
一、安装相关环境
1.安装Docker(个人推荐配置阿里云源 )
2.宿主机安装Nginx
下面我比较偷懒 直接wget Nginx rpm包
老规矩 先关防火墙和selinux
[root@cany ~]# systemctl stop firewalld.service[root@cany ~]# setenforce 0[root@cany ~]# yum install docker -y[root@cany ~]# wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm[root@cany ~]# yum install nginx -y
用命令测试是否成功安装 能否开启服务
[root@cany ~]# systemctl start docker.service[root@cany ~]# systemctl start nginx.service Centos 7 启动服务命令与6.x不同哦
测试web界面是否显示?
完全ok啦 没问题!!!
下面我们来删除掉nginx默认html 并创建一个空白index.html
[root@cany ~]# cd /usr/share/nginx/html/[root@cany 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 [root@cany html]# touch index.html
为什么要创建一个空白index.html呢?
答:其实我一开始也是全删的 结果发现访问无效 才想起要让nginx有响应才会触发nginx.conf配置 也就是才会触发我们配置的负载均衡 这是我个人的理解 有错误敬请提出
二、安装Nginx容器
1.pull nginx镜像
[root@cany ~]# 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/nginx5de4b4d551f8: Pull complete d4b36a5e9443: Pull complete 0af1f0713557: Pull complete Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
2.pull完成使用docker images命令查看
[root@cany ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE hub.c.163.com/library/nginx latest 46102226f2fd 9 months ago 109.4 MB
3.Docker run 创建容器
[root@cany ~]# 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.html index.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容器
[root@cany ~]# 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.html index.html root@41b3eec738b5:/usr/share/nginx/html# rm *root@41b3eec738b5:/usr/share/nginx/html# echo hello nginx_web2 Cany > index.htmlroot@41b3eec738b5:/usr/share/nginx/html# exit
exit退出后容器不运行 先启动两个nginx容器再执行启动服务命令
[root@cany ~]# docker start 41b3eec738b541b3eec738b5 [root@cany ~]# docker start 53cda1b9de1e53cda1b9de1e [root@cany ~]# docker exec -d 41b3eec738b5 service nginx start [root@cany ~]# docker exec -d 53cda1b9de1e service nginx start
三、配置主机host文件
宿主机ip 10.2.4.88 对应 www.abc.top
四、配置宿主机Nginx.conf文件
[root@cany ~]# find / -name nginx.conf 查找nginx.conf文件/etc/nginx/nginx.conf
在http段加入以下代码
upstream www.abc.top { server 10.2.4.88:8080 weight=10; server 10.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
[root@cany ~]# systemctl restart nginx.service
查看Nginx容器运行状态
五、测试
那就再刷新多几次?
web1 web2 看出来了吧?
实验成功!!!