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負載均衡隨機選擇兩個服務器進行轉發。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM