怎样用Docker部署SpringBoot应用请参考上篇文章,本文假设已经部署了两个SpringBoot应用:
访问:http://192.168.43.151:8080/user/test 输出“测试1”
访问:http://192.168.43.151:8081/user/test 输出“测试2”
下面说一下怎么安装Nginx,以及实现两个应用的负载,本文采用简单轮询。
1、安装Nginx
1)安装镜像
查找镜像
docker search Nginx
拉取镜像
docker pull Nginx
说明:拉取的最新镜像。ps:之前不知道怎么误删了/var/lib/Nginx下的tmp目录,导致无法拉取,重建后好了。
2)根据镜像,启动构建容器
docker run -d -p 80:80 --name Nginx_upstream Nginx
upstream myLoad { server 192.168.43.151:8080; server 192.168.43.151:8081; } server { listen 80; server_name localhost; #charset koi8-r; access_log /var/log/Nginx/test_proxy.access.log main; resolver 8.8.8.8; location / { proxy_pass http://myLoad; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/Nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.PHP$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.PHP$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.PHP; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with Nginx's one # #location ~ /\.ht { # deny all; #} }
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/upstream_test.conf; }
3、将配置文件放到容器中
docker cp /usr/local/mystore/dockerfile/Nginx/conf/Nginx.conf d3e3f77036d0:/etc/Nginx/Nginx.conf docker cp /usr/local/mystore/dockerfile/Nginx/conf/conf.d/upstream_test.conf d3e3f77036d0:/etc/Nginx/conf.d/upstream_test.conf
【格式】docker cp 宿主机目录 容器ID:容器目录
重新启动容器
docker restart Nginx_upstream
4、测试
浏览器输入:http://192.168.43.151/user/test 不停刷新,发现轮询输出 “测试1”、“测试1”,测试 成功!
备注
如果将配置文件cp到容器中,重启容器失败,可以重新cp一个正确的覆盖或者直接重新run一个容器。
如果不想执行cp命令复制文件,可以考虑容器卷的方式。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。