docker搭建nginx+springboot集群

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负载均衡随机选择两个服务器进行转发。

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值