将Spring Boot发布到K8S(一)

11 篇文章 0 订阅
10 篇文章 0 订阅

将Spring Boot程序打包成Docker镜像

利用maven将程序打包成war或者jar

  1. 打包成jar

    进入pom.xml文件目录,运行命令mvn clean install -Dmaven.test.skip=true执行打包。

  2. 打包成war
    1. 修改pom.xml文件
      <groupId>com.example</groupId>
      <artifactId>ROOT</artifactId>
      <packaging>jar</packaging>
      

      jar修改为war

      <groupId>com.example</groupId>
      <artifactId>ROOT</artifactId>
      <packaging>war</packaging>
      

      加入依赖

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>
      </dependency>
      

      去除包冲突(与Tomcat中自带的日志包会发生冲突)

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      

      如果使用第三方的jar包, 需要将第三方的包所在目录作为资源文件目录(不然在打包后,不会自动加入该jar)

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
              <webResources>
                  <resource>
                      <directory>${project.basedir}/libs</directory>
                      <targetPath>WEB-INF/lib/</targetPath>
                      <includes>
                          <include>**/*.jar</include>
                      </includes>
                  </resource>
              </webResources>
          </configuration>
      </plugin>
      

    注意:

    • 在除去包冲突时,需要使用mvn dependency:tree命令去查看包依赖,去除重复的包
    1. 修改程序入口class文件

      继承SpringBootServletInitializer,同时重写configure方法

      @ServletComponentScan
      @SpringBootApplication(scanBasePackages={"com.example.data_lake_demo","com.liuyis.jsonfilter"})
      public class DataLakeDemoApplication extends SpringBootServletInitializer {
      
          public static void main(String[] args) {
              SpringApplication.run(DataLakeDemoApplication.class, args);
          }
      
          @Override
          protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      
              return builder.sources(DataLakeDemoApplication.class);
          }
      }
      
    2. 运行maven命令mvn clean install -Dmaven.test.skip=true执行打包

利用DockerFile将jar或者war包打成Docker镜像

  1. 将jar打成Docker镜像

    DockerFile文件:

    # 拉取JDK8的系统镜像
    From java:8
    # 设置时区
    ENV TZ=Asia/Shanghai
    # 时区写入系统文件
    RUN ln -snf /usr/share/zoneinfo/$TZ  /etc/localtime && echo $TZ > /etc/timezone
    
    VOLUME /tmp
    # 加入打包好的jar文件(xxxxx改为自己文件名)
    ADD ./target/xxxxxx.jar /
    
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/xxxxxx.jar"]
    
    

    运行命令docker build -t 自定义镜像名称 .(注意后面有个 . 不要漏掉)

    如果需要将该镜像加入我本地的harbor仓库(需要自己搭建),则镜像名称需要为:

    仓库地址/名称:v版本号

    如:192.168.123.45:8000/library/data_lake:v1.0

  2. 将war打成Docker镜像

    DockerFile文件:

    # 拉取tomcat镜像
    FROM tomcat:latest
    # 设置时区
    ENV TZ=Asia/Shanghai
    # 时区写入系统文件
    RUN ln -snf /usr/share/zoneinfo/$TZ  /etc/localtime && echo $TZ > /etc/timezone
    # 移除Tomcat目录下的ROOT目录
    RUN rm -rf /usr/local/tomcat/webapps/ROOT
    # 将war包移入Tomcat的webapps目录下
    COPY ./target/ROOT.war /usr/local/tomcat/webapps/
    # 暴露的端口
    EXPOSE 8080
    

    运行命令docker build -t 自定义镜像名称 .(注意后面有个 . 不要漏掉)

    如果需要将该镜像加入我本地的harbor仓库(需要自己搭建),则镜像名称需要为:

    仓库地址/名称:v版本号

    如:192.168.123.45:8000/librarydata_lake:v1.0

    注意:

    • 需要写入中国时区,默认的时区是零时区,比北京时间早8小时
    • 移除Tomcat的ROOT目录(同时要保证war的名称是ROOT)的目的,是为了保证接口的访问起始是从域名的根目录开始(ROOT下为根目录,如果是其他名称的文件夹,则需要在接口前加上文件夹的名称)
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值