Docker經過linux的namespace實現資源隔離、cgroups實現資源控制,經過寫時複製機制(copy-on-write)實現了高效的文件操做,在實際開發中可用於提供一次性的環境、微服務架構的搭建、統一環境的部署。javascript
雖然Docker已是風靡全球的容器技術了,統一環境避免環境配置問題上是Docker的主要吸引點之一,但使用時詳細仍是會遇到很多問題的,好比我的搭建時曾思考過這些問題:css
在實際搭建部署中也找到了與上相對應的答案:html
當探索到自我提問的答案時,便肯定了各組件的主要職責:java
搭建環境流程:node
安裝JDKlinux
安裝Mavennginx
安裝gitgit
安裝Jenkins(該步驟以前的可參考Jenkins安裝並部署Java項目完整流程) 若有權限問題可將/etc/sysconfig/jenkins文件JENKINS_USER
修改成root或手動賦權github
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
複製代碼
使用DockerCompose可省去容器增多時需屢次執行docker run的麻煩
1. SpringBoot項目Dockerfile
FROM java:8
MAINTAINER Wilson
# 統一容器與服務器時間
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#複製target/docker-spring-boot.jar到容器app目錄下
COPY ./target/docker-spring-boot.jar app/docker-spring-boot.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app/docker-spring-boot.jar"]
# docker build -t docker-spring-boot/latest .
複製代碼
2. 配置docker-compose.yml
version: '3.7'
services:
app:
restart: always
build: ./
hostname: docker-spring-boot
container_name: docker-spring-boot
image: docker-spring-boot/latest
# 端口不對外開放
# ports:
# - 8080:8080
volumes:
- ./volumes/app:/app
nginx:
depends_on:
- app
container_name: docker-nginx
hostname: docker-nginx
image: nginx:1.17.6
environment:
TZ: Asia/Shanghai
restart: always
expose:
- 80
ports:
- 80:80
links:
- app
volumes:
- ./volumes/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./volumes/nginx/conf.d:/etc/nginx/conf.d
- ./volumes/nginx/logs:/var/log/nginx
複製代碼
3. Nginx
./volumes/nginx/nginx.conf
user nginx;
worker_processes 2; #設置值和CPU核心數一致
error_log /etc/nginx/error.log crit; #日誌位置和日誌級別
pid /etc/nginx/nginx.pid;
events
{
use epoll;
worker_connections 65535;
}
http{
include 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" "$http_cookie"';
access_log /var/log/nginx/access.log main;
#charset utf8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#server虛擬主機的配置
include /etc/nginx/conf.d/*.conf;
}
複製代碼
./volumes/nginx/conf.d目錄下的default.conf
upstream application {
server docker-spring-boot:8080;
}
server{
listen 80;#監聽端口
server_name localhost;#域名
access_log /var/log/nginx/nginx-spring-boot.log;
location / {
proxy_pass http://application;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
複製代碼
Jenkins腳本(若是Nginx配置更改較多也可添加Nginx容器重啓指令):
cd /var/lib/jenkins/workspace/docker-spring-boot/spring-boot-nginx-docker-demo
mvn clean package
if [ -e "./volumes/app/docker-spring-boot.jar" ]
then rm -f ./volumes/app/docker-spring-boot.jar \
&& cp ./target/docker-spring-boot.jar ./volumes/app/docker-spring-boot.jar \
&& docker restart docker-spring-boot \
&& echo "update restart success"
else mkdir volumes/app -p \
&& cp ./target/docker-spring-boot.jar ./volumes/app/docker-spring-boot.jar \
&& docker-compose -p docker-spring-boot up -d \
&& echo "first start"
fi
複製代碼
docker-compose up指令能夠進行鏡像的安裝,因此也省去了只用docker指令時須要提早準備好鏡像相關指令的麻煩。
docker ps
如需將SpringBoot經過容器集羣搭建,只需進行如下更改:
docker-compose.yml添加SpringBoot項目冗餘,更改冗餘容器名,區分日誌掛載路徑,冗餘項目更改容器名
version: '3.7'
services:
app:
restart: always
build: ./
hostname: docker-spring-boot
container_name: docker-spring-boot
image: docker-spring-boot/latest
volumes:
- ./volumes/app/docker-spring-boot.jar:/app/docker-spring-boot.jar
- ./volumes/app/logs:/app/logs
app-bak:
restart: always
build: ./
hostname: docker-spring-boot
container_name: docker-spring-boot-bak
image: docker-spring-boot/latest
volumes:
- ./volumes/app/docker-spring-boot.jar:/app/docker-spring-boot.jar
- ./volumes/app/logs-bak:/app/logs
nginx:
depends_on:
- app
container_name: docker-nginx
hostname: docker-nginx
image: nginx:1.17.6
environment:
TZ: Asia/Shanghai
restart: always
expose:
- 80
ports:
- 80:80
links:
- app
- app-bak
volumes:
- ./volumes/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./volumes/nginx/conf.d:/etc/nginx/conf.d
- ./volumes/nginx/logs:/var/log/nginx
複製代碼
nginx更改default.conf的upstream,添加冗餘容器配置
upstream application {
server docker-spring-boot:8080 fail_timeout=2s max_fails=2 weight=1;
server docker-spring-boot-bak:8080 fail_timeout=2s max_fails=2 weight=1;
}
server{
listen 80;#監聽端口
server_name localhost;#域名
access_log /var/log/nginx/nginx-spring-boot.log;
location / {
proxy_pass http://application;
proxy_connect_timeout 2s;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
複製代碼
Jenkins添加冗餘容器重啓腳本
BUILD_ID=DONTKILLME
cd /var/lib/jenkins/workspace/docker-spring-boot/spring-boot-nginx-docker-demo
mvn clean package
if [ -e "./volumes/app/docker-spring-boot.jar" ]
then rm -f ./volumes/app/docker-spring-boot.jar \
&& cp ./target/docker-spring-boot.jar ./volumes/app/docker-spring-boot.jar \
&& docker-compose -p docker-spring-boot up -d \
&& docker restart docker-spring-boot \
&& docker restart docker-spring-boot-bak \
&& docker restart docker-nginx \
&& echo "update restart success"
else mkdir volumes/app -p \
&& cp ./target/docker-spring-boot.jar ./volumes/app/docker-spring-boot.jar \
&& docker-compose -p docker-spring-boot up -d \
&& echo "first start"
fi
複製代碼
測試集羣效果:
docker stop docker-spring-boot
,仍可經過url/api經過Nginx訪問能夠看出容器配置集羣的如下優勢: