docker搭建nginx+springboot集群

简介: 1、首先准备两个springboot jar包,一个端口设置为8000,一个设置为8080。2、打包第一个springboot jar包,Dockerfile如下FROM java:8VOLUME /tmpADD spring-boot-docker-0.

1、首先准备两个springboot jar包,一个端口设置为8000,一个设置为8080。

2、打包第一个springboot jar包,Dockerfile如下

FROM java:8
VOLUME /tmp
ADD spring-boot-docker-0.1.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

然后运行docker构建命令

docker build -t xiao/springboot .

3、同理我们构建第二个镜像springboot2


4、构建nginx,Dockerfile如下

FROM ubuntu:latest
MAINTAINER xiao
ENV REFRESHED_AT 2017-03-19
RUN apt-get update
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

其中global.conf设置如下

upstream service_group{
      server sample:8080 max_fails=1 fail_timeout=60s weight=1;
      server sample2:8000 max_fails=1 fail_timeout=60s weight=2;
}


server {
        listen 80;
        server_name localhost;
        location / {  
                proxy_pass http://service_group;  
                proxy_redirect default;  
            }  
              
      
            error_page   500 502 503 504  /50x.html;  
            location = /50x.html {  
                root   html;  
            }  
} 

nginx.conf如下

user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;

events {  }

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
}

注意daemon设置为off省的一启动就关闭容器。

5、构建nginx镜像。

6、使用 docker iamges查看镜像

7、启动两个springboot镜像

docker run -d -h sample -p 8080:8080 --name sample xiao/springboot
docker run -d -h sample2 -p 8000:8000 --name sample2 xiao/springboot2

8、启动nginx镜像

docker run -d -h sample2 -p 9000:80 --link sample:sample --link sample2:sample2  --name nginx1 xiao/nginx nginx

9、使用docker ps查看container状态

10、访问http://localhost:9000/ 就能实现nginx负载均衡随机选择两个服务器进行转发。

 

相关文章
|
18天前
|
应用服务中间件 网络安全 nginx
快速上手!使用Docker和Nginx部署Web服务的完美指南
快速上手!使用Docker和Nginx部署Web服务的完美指南
|
1月前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
148 0
|
2月前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
64 0
|
3天前
|
应用服务中间件 nginx
nginx配置集群轮训策略
nginx配置集群轮训策略
11 0
|
4天前
|
存储 运维 负载均衡
Heartbeat+Nginx实现高可用集群
通过Heartbeat与Nginx的结合,您可以建立一个高可用性的负载均衡集群,确保在服务器故障时仍能提供无中断的服务。这种配置需要仔细的计划和测试,以确保系统在故障情况下能够正确运行。
10 2
|
5天前
|
应用服务中间件 nginx Docker
docker安装nginx
`docker search`找镜像,`pull`下载,后台 `-d` 运行容器,命名 `--name`,映射端口 `-p`。本机测试,确保服务器安全组开放端口,公网通过`http://ip:port`访问。用`docker stop id`停止容器。[查看详情](https://blog.csdn.net/javayoungcoolboy/article/details/134976510)
|
6天前
|
监控 Docker 容器
【Docker 专栏】Docker Swarm 集群的扩展与缩容策略
【5月更文挑战第8天】本文探讨了Docker Swarm集群的扩展与缩容策略。集群扩展可提高性能、增强可用性和适应业务发展,可通过手动或自动方式实现。缩容则需考虑业务需求、资源利用率和节点状态,可手动或按策略执行。关键步骤包括添加/移除节点及任务迁移。注意数据同步、监控评估和测试验证。案例分析和总结强调了灵活管理对保持集群最佳状态的重要性。
【Docker 专栏】Docker Swarm 集群的扩展与缩容策略
|
6天前
|
关系型数据库 Java 数据库
docker部署postgresql数据库和整合springboot连接数据源
docker部署postgresql数据库和整合springboot连接数据源
16 0
|
14天前
|
关系型数据库 MySQL 应用服务中间件
centos7在线安装jdk1.8+tomcat+mysql8+nginx+docker
现在,你已经成功在CentOS 7上安装了JDK 1.8、Tomcat、MySQL 8、Nginx和Docker。你可以根据需要配置和使用这些服务。请注意,安装和配置这些服务的详细设置取决于你的具体需求。
53 2
|
15天前
|
Java Docker 微服务