
数据管理,那么这波玩个好玩的,实战演练【nginx负载均衡-轮询】在常规场景中,我们经常遇到的一个问题就是使用nginx进行反向代理和负载均衡,那么我们这波直接在docker上来进行操作,启动多个容器来模拟负载均衡场景,废话不多说,直接上手操作
- # 创建web1文件夹 以及页面
- mkdir /root/web1
- echo "This is website 1 Page 11111111111111111" > /root/web1/index.html
-
- # 创建web2文件夹 以及页面
- mkdir /root/web2
- echo "This is website 2 Page 22222222222222222" > /root/web2/index.html
- 复制代码
- 复制代码

- # 创建nginx1容器挂载 web1页面
- docker run -dit --name nginx1 -v /root/web1/index.html:/usr/share/nginx/html/index.html -p 8091:80 nginx
- # 创建nginx2容器挂载 web2页面
- docker run -dit --name nginx2 -v /root/web2/index.html:/usr/share/nginx/html/index.html -p 8092:80 nginx
- 复制代码

配置文件中的 172.31.36.113 为宿主机的内网IP地址,请按实际情况修改
配置文件中的两个server ip:port 分别代表当前正在运行的docker容器,待会会将流量分发到tomcat_alb组中,组中的2个server分别处理流量
- user nginx;
- worker_processes 1;
-
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
-
-
-
-
- events {
- worker_connections 1024;
- }
-
-
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
-
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
-
- access_log /var/log/nginx/access.log main;
-
- sendfile on;
- #tcp_nopush on;
-
- keepalive_timeout 65;
-
- #gzip on;
-
-
- include /etc/nginx/conf.d/*.conf;
- # 定义Tomcat服务的负载均衡
- upstream tomcat_alb {
-
- server 172.31.36.113:8091;
-
- server 172.31.36.113:8092;
-
- }
- server {
- listen 80;
- server_name 172.31.36.113;#宿主机的内网IP
-
- #charset koi8-r;
- #access_log /var/log/nginx/host.access.log main;
- # 这个是调用Tomcat服务的负载均衡
- location / {
- proxy_pass http://tomcat_alb;
- }
- }
- }
- 复制代码
- # -v 标记挂载本地的 /root/nginx.conf 文件
- docker run -dit --name nginx-alb -v /root/nginx.conf:/etc/nginx/nginx.conf -p 80:80 nginx
- 复制代码

这里使用一个shell的for循环语句来进行curl测试,看不同的请求得到什么样的返回值
- # 循环测试
- for i in `seq 1 6`;do curl 172.31.36.113;done
- 复制代码

通过上面5个步骤的实验,可以很方面的得到实验结果,那么也可以采取打包容器的方式来制作一个nginx的负载均衡容器镜像
首先将1.3的nginx配置文件复制下来,命名为 nginx.conf
- user nginx;
- worker_processes 1;
-
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
-
-
-
-
- events {
- worker_connections 1024;
- }
-
-
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
-
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
-
- access_log /var/log/nginx/access.log main;
-
- sendfile on;
- #tcp_nopush on;
-
- keepalive_timeout 65;
-
- #gzip on;
-
-
- include /etc/nginx/conf.d/*.conf;
- # 定义Tomcat服务的负载均衡
- upstream tomcat_alb {
-
- server 172.31.36.113:8091;
-
- server 172.31.36.113:8092;
-
- }
- server {
- listen 80;
- server_name 172.31.36.113;#宿主机的内网IP
-
- #charset koi8-r;
- #access_log /var/log/nginx/host.access.log main;
- # 这个是调用Tomcat服务的负载均衡
- location / {
- proxy_pass http://tomcat_alb;
- }
- }
- }
- 复制代码
- docker run -dit --name nginx-alb -v /root/nginx.conf:/etc/nginx/nginx.conf -p 80:80 nginx
- 复制代码
- [root@base ~]# docker run -dit --name nginx-alb -v /root/nginx.conf:/etc/nginx/nginx.conf -p 80:80 nginx
- 6d1e6eff0388267cef089c7db7a6822b9c6ab8ceb4baab4d1338dc6bd3388e08
- [root@base ~]# docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 6d1e6eff0388 nginx "/docker-entrypoin..." 4 seconds ago Exited (1) 2 seconds ago nginx-alb
- 7dda8b117154 registry "/entrypoint.sh /e..." 4 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp registry
- 53b462f31348 ssh-server "/usr/sbin/sshd -D" 24 hours ago Exited (0) 22 hours ago sshd
- 88958f0f4fc8 7e989e1e46e7 "/bin/bash" 2 weeks ago Exited (137) 2 weeks ago centos7
- [root@base ~]# docker export 6d1e6eff0388 > nginx_alb,tar
- [root@base ~]# ls -l|grep nginx_alb
- -rw-r--r--. 1 root root 144022528 Aug 25 21:44 nginx_alb,tar
-
- 复制代码

现在您已经成功打包了该容器
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

这些资料,对于想进阶【自动化测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
