假设你已经完成Swarm的初始化,那么接下来开始部署BladeX服务
1.首先要创建一个自定义网络,后续所有的服务都指定使用这个网络
docker network create -d overlay \ --subnet=10.10.0.0/16 \ --gateway=10.10.0.254 \ --attachable=true \ ${网络名称}
2. 然后是编写服务DockerCompose文件
2.1 先创建Nacos和Sentinel服务,保存名为(文件名可以自定义):swarm-nacos-sentinel.yml
version: "3.4" services: nacos-server: image: nacos/nacos-server:1.0.0 hostname: nacos-server deploy: endpoint_mode: vip replicas: 1 restart_policy: condition: on-failure networks: - ${网络名称} environment: - MODE=standalone - SPRING_DATASOURCE_PLATFORM=mysql - MYSQL_MASTER_SERVICE_DB_NAME=${主mysql数据库名称} - MYSQL_MASTER_SERVICE_HOST=${主mysql连接} - MYSQL_MASTER_SERVICE_USER=${主mysql连接用户名} - MYSQL_MASTER_SERVICE_PASSWORD=${主mysql连接密码} - MYSQL_SLAVE_SERVICE_HOST=${副mysql连接} - MYSQL_SLAVE_SERVICE_DB_NAME=${副mysql数据库名称} - MYSQL_SLAVE_SERVICE_USER=${副mysql连接用户名} - MYSQL_SLAVE_SERVICE_PASSWORD=${副mysql连接用户名} volumes: - /docker/nacos/standalone-logs/:/home/nacos/logs - /docker/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties ports: - "8848:8848" sentinel: image: bladex/sentinel-dashboard:1.6.0 hostname: sentinel networks: - ${网络名称} deploy: endpoint_mode: vip replicas: 1 restart_policy: condition: on-failure ports: - "8858:8858" networks: ${网络名称}: external: true
2.2 发布nacos和sentinel
docker stack deploy -c warm-nacos-sentinel.yml --with-registry-auth ${服务栈名称}
2.3 编写BladeX服务的DockerCompose文件,保存为:swarm-bladex-apps.yml
version: "3.4" services: blade-gateway: image: ${镜像地址} hostname: blade-gateway networks: - ${网络名称} deploy: endpoint_mode: vip replicas: 3 restart_policy: condition: on-failure command: - --spring.profiles.active=test - --spring.cloud.nacos.config.server-addr=nacos-server:8848 - --spring.cloud.nacos.discovery.server-addr=nacos-server:8848 - --spring.cloud.nacos.config.group=DEAFULT_GROUP - --spring.cloud.sentinel.transport.dashboard=sentinel:8858 - --spring.cloud.inetutils.preferred-networks=10.0. ports: - "88:88" depends_on: - nacos-server - sentinel blade-system: image: ${镜像地址} hostname: blade-system networks: - ${网络名称} deploy: endpoint_mode: vip replicas: 1 restart_policy: condition: on-failure command: - --spring.profiles.active=test - --spring.cloud.nacos.config.server-addr=nacos-server:8848 - --spring.cloud.nacos.discovery.server-addr=nacos-server:8848 - --spring.cloud.nacos.config.group=DEAFULT_GROUP - --spring.cloud.sentinel.transport.dashboard=sentinel:8858 - --spring.cloud.inetutils.preferred-networks=10.0. ports: - "8102:8102" depends_on: - nacos-server - sentinel ......其他服务一样的写法 networks: ${网络名称}: external: true
这里要注意的一个地方是服务启动参数里的
--spring.cloud.inetutils.preferred-networks=10.0.
这里指定服务已我们前面创建的那个网络的ip前缀的网卡向nacos注册,如果不加这个参数默认是使用的是Docker自建的网卡,这样的话服务还是访问不了
2.4 发布BladeX服务
docker stack deploy -c warm-bladex-apps.yml --with-registry-auth ${服务栈名称}
这里的服务栈名称和nacos的保持一致,不一样的话行不行我也没试过
2.5 编写Nginx服务的DockerCompose文件,保存为 swarm-nginx.yml
version: "3.4" services: saber-nginx: image: nginx:stable-alpine-perl hostname: saber-nginx ports: - 80:80 - 443:443 networks: - ${网络名称} deploy: endpoint_mode: vip replicas: 1 restart_policy: condition: on-failure volumes: - /docker/nginx/dist:/usr/share/nginx/html - /docker/nginx/nginx.conf:/etc/nginx/nginx.conf - /docker/nginx/conf.d:/etc/nginx/conf.d/ depends_on: - blade-gateway networks: ${网络名称}: external: true
发布服务前需要把saber编译好的文件放到上边volumes中的位置
/docker/nginx/dist
然后调好nginx的反代设置
replicas: 1 这个参数是节点数量
2.6 发布nginx服务
docker stack deploy -c swarm-nginx.yml --with-registry-auth ${服务栈名称}
3. 分享下我的Nginx配置
nginx.conf: #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 80 default_server; listen [::]:80 default_server; server_name _; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #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 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; #} } include /etc/nginx/conf.d/*.conf; } saber站点配置 saber.conf upstream gateway{ server blade-gateway:88 weight=1; } # Settings for a TLS enabled server. server { listen 443 ssl http2; server_name 你的域名; root /usr/share/nginx/html; ssl_certificate /etc/nginx/conf.d/saber.pem; ssl_certificate_key /etc/nginx/conf.d/saber.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. #include /etc/nginx/default.d/*.conf; location / { } location ~* \.(png|jpeg|jpg|bmp|gif|ico|js|css|scss){ expires 3d; } location ^~/api { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #=======WebSocket配置=======# proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; #=======WebSocket配置=======# proxy_buffering off; rewrite ^/api/(.*)$ /$1 break; proxy_pass http://gateway; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server{ listen 80; server_name 你的域名; rewrite ^(.*)$ https://$host$1 permanent; }
补上部署后的图片:
说明一下,我这边改了服务名......按自己的来就好
扫一扫访问 Blade技术社区 移动端