如何在docker基础上使用sh脚本实现项目部署

文章目录

  • Linux目录架构演示
  • docker-compose脚本
    • docker-compose-app.yml
    • docker-compose-service.yml
    • docker-compose-env.yml
    • 启动具体某个微服务
  • 具体微服务jar包对应目录的配置脚本(重点)
    • Dockerfile
    • xk-api.sh(启动微服务项目,常用)
  • xk-publish.sh(更新启动所有微服务)

  • 前提须知:要安装docker和docker-compose才行,具体参考可跳转至如何安装和使用docker和如何安装和使用docker-compose

Linux目录架构演示

  • 以下每个文件夹对应一个微服务项目,以api文件夹为例演示
  • 以下.yml文件docker-compose的对应配置文件, 主要目的是在不需要重新编译jar包,不需要重新打包jar的情况下 重启服务而用(不需要更新新版本情况下,机子崩了,项目都停了,要重新启动项目)
  • 以下.sh文件是脚本部署文件,主要用于代码更新后新打包的jar包 去发布部署上线(代码更新了新功能,发布新版本,要置换掉原来jar包后才使用该脚本)
    如何在docker基础上使用sh脚本实现项目部署_第1张图片
  • 演示api微服务项目的对应的文件夹
    如何在docker基础上使用sh脚本实现项目部署_第2张图片

docker-compose脚本

  • 以下的app.ymlservice.yml性质是一样的,只是因为演示的项目构造的原因分成2个文件而已(参考其中一个就行),用于启动微服务项目
  • 以下的env.yml用于启动一些中间件配置服务的启动(比如nginx那些)
  • 让现有的镜像变成容器跑起来

docker-compose-app.yml

version: '3'
services:
  xk-auth:
    image: xk/xk-auth:1.0-SNAPSHOT
    container_name: xk-auth
    ports:
      - 6689:6689
    volumes:
      - /data/logs/xk-auth:/logs/xk-practice-auth
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
  xk-act:
    image: xk/xk-act:1.0-SNAPSHOT
    container_name: xk-act
    ports:
      - 6687:6687
    volumes:
      - /data/logs/xk-act:/logs/xk-practice-act
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-ach:
    image: xk/xk-ach:1.0-SNAPSHOT
    container_name: xk-ach
    ports:
      - 6683:6683
    volumes:
      - /data/logs/xk-ach:/logs/xk-practice-ach
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-oact:
    image: xk/xk-oact:1.0-SNAPSHOT
    container_name: xk-oact
    ports:
      - 6682:6682
    volumes:
      - /data/logs/xk-oact:/logs/xk-practice-oact
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-rpt:
    image: xk/xk-rpt:1.0-SNAPSHOT
    container_name: xk-rpt
    ports:
      - 6688:6688
    volumes:
      - /data/logs/xk-rpt:/logs/xk-practice-rpt
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-search:
    image: xk/xk-search:1.0-SNAPSHOT
    container_name: xk-search
    ports:
      - 6686:6686
    volumes:
      - /data/logs/xk-search:/logs/xk-practice-search
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-sys:
    image: xk/xk-sys:1.0-SNAPSHOT
    container_name: xk-sys
    ports:
      - 6684:6684
    volumes:
      - /data/logs/xk-sys:/logs/xk-practice-sys
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-canal:
    image: xk/xk-canal:1.0-SNAPSHOT
    container_name: xk-canal
    ports:
      - 7788:7788
    volumes:
      - /data/logs/xk-canal:/logs/xk-practice-canal-customer
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es

docker-compose-service.yml

version: '3'
services:
  xk-admin:
    image: xk/xk-admin:1.0-SNAPSHOT
    container_name: xk-admin
    ports:
      - 7616:7616
    volumes:
      - /data/logs/xk-admin:/logs/xk-practice-admin-application
      - /data/resource:/data/resource
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-portal:
    image: xk/xk-portal:1.0-SNAPSHOT
    container_name: xk-portal
    ports:
      - 7618:7618
    volumes:
      - /data/logs/xk-portal:/logs/xk-practice-portal-application
      - /data/xk/portal:/data/xk/portal
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-api:
    image: xk/xk-api:1.0-SNAPSHOT
    container_name: xk-api
    ports:
      - 7778:7778
    volumes:
      - /data/logs/xk-api:/logs/xk-practice-api
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
#      - redis:redis
      - elasticsearch:es
  xk-gateway:
    image: xk/xk-gateway:1.0-SNAPSHOT
    container_name: xk-gateway
    ports:
      - 7777:7777
    volumes:
      - /data/logs/xk-gateway:/logs/xk-practice-gateway
    environment:
      - 'TZ="Asia/Shanghai"'
    external_links:
      - nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务

docker-compose-env.yml

version: '3'
services:
  nginx:
    image: nginx
    container_name: nginx
    volumes:
      - /data/nginx/conf:/etc/nginx #配置文件挂载
      - /data/nginx/html:/usr/share/nginx/html #静态资源根目录挂载
      - /data/nginx/log:/var/log/nginx #日志文件挂载
    ports:
      - 80:80
      - 8889:8889
  elasticsearch:
    image: elasticsearch:6.8.0
    container_name: elasticsearch
    environment:
      - "cluster.name=elasticsearch" #设置集群名称为elasticsearch
      - "discovery.type=single-node" #以单一节点模式启动
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小
    volumes:
      - /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
      - /data/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
    ports:
      - 9200:9200
  kibana:
    image: kibana:6.8.0
    container_name: kibana
    links:
      - elasticsearch:es #可以用es这个域名访问elasticsearch服务
    depends_on:
      - elasticsearch #kibana在elasticsearch启动之后再启动
    environment:
      - "elasticsearch.hosts=http://es:9200" #设置访问elasticsearch的地址
    ports:
      - 5601:5601
  logstash:
    image: logstash:6.8.0
    container_name: logstash
    volumes:
      - /data/log-stash/logstash-springboot.conf:/usr/share/logstash/pipeline/logstash.conf 
    depends_on:
      - elasticsearch #kibana在elasticsearch启动之后再启动
    links:
      - elasticsearch:es #可以用es这个域名访问elasticsearch服务
    ports:
      - 4560:4560
  nacos-registry:
    image: nacos/nacos-server:1.3.0
    container_name: nacos-registry
    volumes:
      - /data/nacos-registry:/home/nacos/logs
    environment:
      - "MODE=standalone"
    env_file:
      - /data/nacos-registry/env/nacos-standlone-mysql.env
    ports:
      - 8848:8848
#  redis:
#    image: redis:5.0.8
#    container_name: redis
#    command: redis-server --requirepass rediss --appendonly yes
#    volumes:
#      - /data/redis/data:/data #数据文件挂载
#      - /data/redis/etc/redis.conf:/usr/local/etc/redis/redis.conf
#      - /data/redis/logs:/logs
#    ports:
#      - 6379:6379

启动具体某个微服务

  • docker-compose的yml配置文件的当前目录下输入以下命令 即可指定启动配置文件中某个项目
  • 这里是启动docker-compose-service.yml配置文件里 配置的api微服务
docker-compose -f docker-compose-service.yml up -d xk-api

具体微服务jar包对应目录的配置脚本(重点)

  • 以下演示微服务为api
  • 将自己微服务项目的jar包,放在当前目录下即可
    如何在docker基础上使用sh脚本实现项目部署_第3张图片

Dockerfile

FROM java:openjdk-8-jdk-alpine
ADD *.jar /home/api.jar

ARG JAVA_OPTS="-Xmx1344M -Xms1344M -Xmn448M -XX:MaxMetaspaceSize=192M -XX:MetaspaceSize=192M"
ARG JVM_SERVER_FIXED_ARGS="-server \
-XX:+DisableExplicitGC \
-XX:+UseParNewGC \
-XX:+UseConcMarkSweepGC \
-XX:+UseCMSInitiatingOccupancyOnly \
-XX:CMSInitiatingOccupancyFraction=70 \
-XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses \
-XX:+CMSClassUnloadingEnabled \
-XX:+ParallelRefProcEnabled \
-XX:+CMSScavengeBeforeRemark \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:+PrintGCDetails \
-XX:+PrintGCTimeStamps \
-XX:+PrintHeapAtGC \
-XX:+PrintGCApplicationStoppedTime \
-XX:+PrintGCDateStamps \
-XX:ErrorFile=/var/log/hs_err_pid%p.log \
-XX:HeapDumpPath=/var/log -Xloggc:/var/log/gc%t.log"
#ARG JAVA_AGENT="-javaagent:/usr/skywalking/agent/skywalking-agent.jar -Dskywalking.agent.service_name=bonade-hrm-gateway -Dskywalking.plugin.mysql.trace_sql_parameters=true -Dskywalking.collector.backend_service=127.0.0.1:11800"
ENV TZ=Asia/Shanghai
ENV JAVA_OPTS=${JAVA_OPTS}
ENV JAVA_AGENT=${JAVA_AGENT}
ENV JVM_SERVER_FIXED_ARGS=${JVM_SERVER_FIXED_ARGS}
ENTRYPOINT exec java ${JAVA_OPTS} ${JVM_SERVER_FIXED_ARGS} ${JAVA_AGENT} -jar /home/api.jar

xk-api.sh(启动微服务项目,常用)

  • 输入命令到当前目录下, 直接输入./xk-api.sh 即可直接将当前目录下的jar包变成docker方式启动
  • 使jar包变成镜像,然后将镜像变成容器跑起来

#!/usr/bin/env bash
app_name='xk-api'
docker build -t xk/${app_name}:1.0-SNAPSHOT .
docker stop ${app_name}
echo '----stop container----'
docker rm ${app_name}
echo '----rm container----'
docker rmi `docker images | grep none | awk '{print $3}'`
echo '----rm none images----'
docker run -p 7778:7778 --name ${app_name} \
--link nacos-registry:nacos-registry \
--link elasticsearch:es \
--link logstash:logstash \
--network xk_default \
-e TZ="Asia/Shanghai" \
-v /etc/localtime:/etc/localtime \
-v /data/logs/${app_name}:/logs/xk-practice-api \
-v /data/act_file/inventory:/data/act_file/inventory \
-v /data/act_file/plan:/data/act_file/plan \
-d xk/${app_name}:1.0-SNAPSHOT
echo '----start container----'

xk-publish.sh(更新启动所有微服务)

  • 这里演示的是启动所有微服务项目跑起来,除了adminportal的微服务项目

#!/usr/bin/env sh
for file in ./*
do
  if [ -d $file ]
   then
     if [ $file == './admin' ] ||[ $file == './portal' ]
      then
         echo $file '当前服务需要在auth 服务启动后再启动,请手动执行'
      else
        cd $file
        sh *.sh
        cd ../
     fi
  fi
done

你可能感兴趣的:(后端,运维开发)