skaffold dev操作java项目

213 阅读7分钟

title: skaffold dev操作java项目 date: 2023-03-07T12:36:08+08:00 lastmode: 2023-03-07T12:36:08+08:00 tags:

  • k8s CATEGORIES:
  • k8s

获取java源代码:

 git clone git@github.com:GoogleContainerTools/skaffold.git
 cd skaffold/examples/buildpacks-java

需要修改skaffold.yaml

 apiVersion: skaffold/v4beta2
 kind: Config
 build:
   local:
     push: false
   artifacts:
   - image: skaffold-buildpacks
     buildpacks:
       builder: "10.7.20.12:5000/paketo-buildpacks/builder:base"
       # env:
       # - GOOGLE_RUNTIME_VERSION=8
 # profiles:
 # - name: gcb
 #   build:
 #     googleCloudBuild: {}
 

修改pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
 ​
   <groupId>org.skaffold</groupId>
   <artifactId>hello-spring-boot</artifactId>
   <version>0.1.0</version>
   <description>Spring Boot</description>
 ​
   <properties>
     <java.version>1.8</java.version>
   </properties>
 ​
   <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.7.4</version>
   </parent>
  <repositories>
     <repository>
         <id>nexus-163</id>
         <name>Nexus 163</name>
         <url>http://mirrors.163.com/maven/repository/maven-public/</url>
         <layout>default</layout>
         <snapshots>
             <enabled>false</enabled>
         </snapshots>
         <releases>
             <enabled>true</enabled>
         </releases>
     </repository>
 </repositories>
 <pluginRepositories>
     <pluginRepository>
         <id>nexus-163</id>
         <name>Nexus 163</name>
         <url>http://mirrors.163.com/maven/repository/maven-public/</url>
         <snapshots>
             <enabled>false</enabled>
         </snapshots>
         <releases>
             <enabled>true</enabled>
         </releases>
     </pluginRepository>
 </pluginRepositories>
        <!--配置阿里云maven私有仓库(即配阿里私服)-->
   
   <dependencyManagement>
     <dependencies>
       <!-- SnakeYAML 1.32 addresses CVE-2022-25857 and CVE-2022-38752 -->
       <dependency>
         <groupId>org.yaml</groupId>
         <artifactId>snakeyaml</artifactId>
         <version>1.32</version>
       </dependency>
     </dependencies>
   </dependencyManagement>
 ​
   <dependencies>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
   </dependencies>
 ​
   <build>
     <finalName>hello</finalName>
     <plugins>
       <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
       </plugin>
     </plugins>
   </build>
 </project>
 ​

首先本地需要有minikube部署好,准备部署了

 ⬢ [Systemd]HTTPS_PROXY=http://172.30.144.1:10809 skaffold dev
 Generating tags...
  - skaffold-buildpacks -> skaffold-buildpacks:v2.1.0-57-g05a239970-dirty
 Checking cache...
  - skaffold-buildpacks: Not found. Building
 Starting build...
 Building [skaffold-buildpacks]...
 Target platforms: [linux/amd64]
 base: Pulling from paketo-buildpacks/builder
 Digest: sha256:d6586597015aa00003c69d4c2cdbb5d14f806ffa7eac8ef02961eb33c6d57b98
 Status: Image is up to date for 10.7.20.12:5000/paketo-buildpacks/builder:base
 base-cnb: Pulling from paketobuildpacks/run
 Digest: sha256:dec5146cd16049064246308ee633ab451ccb8c8e7e0ec595727ea12d3719a006
 Status: Image is up to date for paketobuildpacks/run:base-cnb
 0.16.0: Pulling from buildpacksio/lifecycle
 Digest: sha256:f75a04887fced3ae0504a37edb2c0d29d366511cd9ede34dbb90c5282b106e79
 Status: Image is up to date for buildpacksio/lifecycle:0.16.0
 ===> ANALYZING
 [analyzer] Restoring data for SBOM from previous image
 ===> DETECTING
 [detector] 10 of 26 buildpacks participating
 [detector] paketo-buildpacks/ca-certificates   3.6.0
 [detector] paketo-buildpacks/bellsoft-liberica 9.11.0
 [detector] paketo-buildpacks/syft              1.25.0
 [detector] paketo-buildpacks/maven             6.13.0
 [detector] paketo-buildpacks/executable-jar    6.6.1
 [detector] paketo-buildpacks/apache-tomcat     7.12.0
 [detector] paketo-buildpacks/apache-tomee      1.6.1
 [detector] paketo-buildpacks/liberty           3.4.1
 [detector] paketo-buildpacks/dist-zip          5.5.1
 [detector] paketo-buildpacks/spring-boot       5.23.0
 ===> RESTORING
 [restorer] Restoring metadata for "paketo-buildpacks/ca-certificates:helper" from app image
 [restorer] Restoring metadata for "paketo-buildpacks/bellsoft-liberica:java-security-properties" from app image
 [restorer] Restoring metadata for "paketo-buildpacks/bellsoft-liberica:jre" from app image
 [restorer] Restoring metadata for "paketo-buildpacks/bellsoft-liberica:helper" from app image
 [restorer] Restoring metadata for "paketo-buildpacks/bellsoft-liberica:jdk" from cache
 [restorer] Restoring metadata for "paketo-buildpacks/syft:syft" from cache
 [restorer] Restoring metadata for "paketo-buildpacks/maven:application" from cache
 [restorer] Restoring metadata for "paketo-buildpacks/maven:cache" from cache
 [restorer] Restoring metadata for "paketo-buildpacks/maven:maven" from cache
 [restorer] Restoring metadata for "paketo-buildpacks/spring-boot:spring-cloud-bindings" from app image
 [restorer] Restoring metadata for "paketo-buildpacks/spring-boot:web-application-type" from app image
 [restorer] Restoring metadata for "paketo-buildpacks/spring-boot:helper" from app image
 [restorer] Restoring data for "paketo-buildpacks/bellsoft-liberica:jdk" from cache
 [restorer] Restoring data for "paketo-buildpacks/syft:syft" from cache
 [restorer] Restoring data for "paketo-buildpacks/maven:application" from cache
 [restorer] Restoring data for "paketo-buildpacks/maven:cache" from cache
 [restorer] Restoring data for "paketo-buildpacks/maven:maven" from cache
 [restorer] Restoring data for SBOM from cache
 ===> BUILDING
 [builder] 
 [builder] Paketo Buildpack for CA Certificates 3.6.0
 [builder]   https://github.com/paketo-buildpacks/ca-certificates
 [builder]   Launch Helper: Reusing cached layer
 [builder] 
 [builder] Paketo Buildpack for BellSoft Liberica 9.11.0
 [builder]   https://github.com/paketo-buildpacks/bellsoft-liberica
 [builder]   Build Configuration:
 [builder]     $BP_JVM_JLINK_ARGS           --no-man-pages --no-header-files --strip-debug --compress=1  configure custom link arguments (--output must be omitted)
 [builder]     $BP_JVM_JLINK_ENABLED        false                                                        enables running jlink tool to generate custom JRE
 [builder]     $BP_JVM_TYPE                 JRE                                                          the JVM type - JDK or JRE
 [builder]     $BP_JVM_VERSION              11                                                           the Java version
 [builder]   Launch Configuration:
 [builder]     $BPL_DEBUG_ENABLED           false                                                        enables Java remote debugging support
 [builder]     $BPL_DEBUG_PORT              8000                                                         configure the remote debugging port
 [builder]     $BPL_DEBUG_SUSPEND           false                                                        configure whether to suspend execution until a debugger has attached
 [builder]     $BPL_HEAP_DUMP_PATH                                                                       write heap dumps on error to this path
 [builder]     $BPL_JAVA_NMT_ENABLED        true                                                         enables Java Native Memory Tracking (NMT)
 [builder]     $BPL_JAVA_NMT_LEVEL          summary                                                      configure level of NMT, summary or detail
 [builder]     $BPL_JFR_ARGS                                                                             configure custom Java Flight Recording (JFR) arguments
 [builder]     $BPL_JFR_ENABLED             false                                                        enables Java Flight Recording (JFR)
 [builder]     $BPL_JMX_ENABLED             false                                                        enables Java Management Extensions (JMX)
 [builder]     $BPL_JMX_PORT                5000                                                         configure the JMX port
 [builder]     $BPL_JVM_HEAD_ROOM           0                                                            the headroom in memory calculation
 [builder]     $BPL_JVM_LOADED_CLASS_COUNT  35% of classes                                               the number of loaded classes in memory calculation
 [builder]     $BPL_JVM_THREAD_COUNT        250                                                          the number of threads in memory calculation
 [builder]     $JAVA_TOOL_OPTIONS                                                                        the JVM launch flags
 [builder]     Using buildpack default Java version 11
 [builder]   BellSoft Liberica JDK 11.0.18: Reusing cached layer
 [builder]   BellSoft Liberica JRE 11.0.18: Reusing cached layer
 [builder]   Launch Helper: Reusing cached layer
 [builder]   Java Security Properties: Reusing cached layer
 [builder] 
 [builder] Paketo Buildpack for Syft 1.25.0
 [builder]   https://github.com/paketo-buildpacks/syft
 [builder] 
 [builder] Paketo Buildpack for Maven 6.13.0
 [builder]   https://github.com/paketo-buildpacks/maven
 [builder]   Build Configuration:
 [builder]     $BP_EXCLUDE_FILES                                                                 colon separated list of glob patterns, matched source files are removed
 [builder]     $BP_INCLUDE_FILES                                                                 colon separated list of glob patterns, matched source files are included
 [builder]     $BP_JAVA_INSTALL_NODE      false                                                  whether to install Yarn/Node binaries based on the presence of a package.json or yarn.lock file
 [builder]     $BP_MAVEN_BUILD_ARGUMENTS  -Dmaven.test.skip=true --no-transfer-progress package  the arguments to pass to Maven
 [builder]     $BP_MAVEN_BUILT_ARTIFACT   target/*.[ejw]ar                                       the built application artifact explicitly.  Supersedes $BP_MAVEN_BUILT_MODULE
 [builder]     $BP_MAVEN_BUILT_MODULE                                                            the module to find application artifact in
 [builder]     $BP_MAVEN_DAEMON_ENABLED   false                                                  use maven daemon
 [builder]     $BP_MAVEN_POM_FILE         pom.xml                                                the location of the main pom.xml file, relative to the application root
 [builder]     $BP_MAVEN_SETTINGS_PATH                                                           the path to a Maven settings file
 [builder]     $BP_MAVEN_VERSION          3                                                      the Maven version
 [builder]     $BP_NODE_PROJECT_PATH                                                             configure a project subdirectory to look for `package.json` and `yarn.lock` files
 [builder]   Apache Maven 3.9.0: Reusing cached layer
 [builder]     Creating cache directory /home/cnb/.m2
 [builder]   Compiled Application: Contributing to layer
 [builder]     Executing mvn --batch-mode -Dmaven.test.skip=true --no-transfer-progress package
 [builder]       [INFO] Scanning for projects...
 [builder]       [INFO] 
 [builder]       [INFO] -------------------< org.skaffold:hello-spring-boot >-------------------
 [builder]       [INFO] Building hello-spring-boot 0.1.0
 [builder]       [INFO]   from pom.xml
 [builder]       [INFO] --------------------------------[ jar ]---------------------------------
 [builder]       [INFO] 
 [builder]       [INFO] --- resources:3.2.0:resources (default-resources) @ hello-spring-boot ---
 [builder]       [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [builder]       [INFO] Using 'UTF-8' encoding to copy filtered properties files.
 [builder]       [INFO] skip non existing resourceDirectory /workspace/src/main/resources
 [builder]       [INFO] skip non existing resourceDirectory /workspace/src/main/resources
 [builder]       [INFO] 
 [builder]       [INFO] --- compiler:3.10.1:compile (default-compile) @ hello-spring-boot ---
 [builder]       [INFO] Changes detected - recompiling the module!
 [builder]       [INFO] Compiling 2 source files to /workspace/target/classes
 [builder]       [INFO] 
 [builder]       [INFO] --- resources:3.2.0:testResources (default-testResources) @ hello-spring-boot ---
 [builder]       [INFO] Not copying test resources
 [builder]       [INFO] 
 [builder]       [INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ hello-spring-boot ---
 [builder]       [INFO] Not compiling test sources
 [builder]       [INFO] 
 [builder]       [INFO] --- surefire:2.22.2:test (default-test) @ hello-spring-boot ---
 [builder]       [INFO] Tests are skipped.
 [builder]       [INFO] 
 [builder]       [INFO] --- jar:3.2.2:jar (default-jar) @ hello-spring-boot ---
 [builder]       [INFO] Building jar: /workspace/target/hello.jar
 [builder]       [INFO] 
 [builder]       [INFO] --- spring-boot:2.7.4:repackage (repackage) @ hello-spring-boot ---
 [builder]       [INFO] Replacing main artifact with repackaged archive
 [builder]       [INFO] ------------------------------------------------------------------------
 [builder]       [INFO] BUILD SUCCESS
 [builder]       [INFO] ------------------------------------------------------------------------
 [builder]       [INFO] Total time:  3.681 s
 [builder]       [INFO] Finished at: 2023-03-07T04:18:14Z
 [builder]       [INFO] ------------------------------------------------------------------------
 [builder]       
 [builder]   Removing source code
 [builder]   Restoring application artifact
 [builder] 
 [builder] Paketo Buildpack for Executable JAR 6.6.1
 [builder]   https://github.com/paketo-buildpacks/executable-jar
 [builder]   Class Path: Contributing to layer
 [builder]     Writing env/CLASSPATH.delim
 [builder]     Writing env/CLASSPATH.prepend
 [builder]   Process types:
 [builder]     executable-jar: java org.springframework.boot.loader.JarLauncher (direct)
 [builder]     task:           java org.springframework.boot.loader.JarLauncher (direct)
 [builder]     web:            java org.springframework.boot.loader.JarLauncher (direct)
 [builder] 
 [builder] Paketo Buildpack for Spring Boot 5.23.0
 [builder]   https://github.com/paketo-buildpacks/spring-boot
 [builder]   Build Configuration:
 [builder]     $BP_SPRING_CLOUD_BINDINGS_DISABLED   false  whether to contribute Spring Boot cloud bindings support
 [builder]   Launch Configuration:
 [builder]     $BPL_SPRING_CLOUD_BINDINGS_DISABLED  false  whether to auto-configure Spring Boot environment properties from bindings
 [builder]     $BPL_SPRING_CLOUD_BINDINGS_ENABLED   true   Deprecated - whether to auto-configure Spring Boot environment properties from bindings
 [builder]   Creating slices from layers index
 [builder]     dependencies (16.7 MB)
 [builder]     spring-boot-loader (281.8 KB)
 [builder]     snapshot-dependencies (0.0 B)
 [builder]     application (29.3 KB)
 [builder]   Launch Helper: Reusing cached layer
 [builder]   Spring Cloud Bindings 1.11.0: Reusing cached layer
 [builder]   Web Application Type: Reusing cached layer
 [builder]   4 application slices
 [builder]   Image labels:
 [builder]     org.opencontainers.image.title
 [builder]     org.opencontainers.image.version
 [builder]     org.springframework.boot.version
 ===> EXPORTING
 [exporter] Reusing layer 'paketo-buildpacks/ca-certificates:helper'
 [exporter] Reusing layer 'paketo-buildpacks/bellsoft-liberica:helper'
 [exporter] Reusing layer 'paketo-buildpacks/bellsoft-liberica:java-security-properties'
 [exporter] Reusing layer 'paketo-buildpacks/bellsoft-liberica:jre'
 [exporter] Reusing layer 'paketo-buildpacks/executable-jar:classpath'
 [exporter] Reusing layer 'paketo-buildpacks/spring-boot:helper'
 [exporter] Reusing layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings'
 [exporter] Reusing layer 'paketo-buildpacks/spring-boot:web-application-type'
 [exporter] Reusing layer 'buildpacksio/lifecycle:launch.sbom'
 [exporter] Reusing 5/5 app layer(s)
 [exporter] Reusing layer 'buildpacksio/lifecycle:launcher'
 [exporter] Reusing layer 'buildpacksio/lifecycle:config'
 [exporter] Reusing layer 'buildpacksio/lifecycle:process-types'
 [exporter] Adding label 'io.buildpacks.lifecycle.metadata'
 [exporter] Adding label 'io.buildpacks.build.metadata'
 [exporter] Adding label 'io.buildpacks.project.metadata'
 [exporter] Adding label 'org.opencontainers.image.title'
 [exporter] Adding label 'org.opencontainers.image.version'
 [exporter] Adding label 'org.springframework.boot.version'
 [exporter] Setting default process type 'web'
 [exporter] Saving skaffold-buildpacks:latest...
 [exporter] *** Images (3da633f394b3):
 [exporter]       skaffold-buildpacks:latest
 [exporter] Adding cache layer 'paketo-buildpacks/bellsoft-liberica:jdk'
 [exporter] Adding cache layer 'paketo-buildpacks/syft:syft'
 [exporter] Adding cache layer 'paketo-buildpacks/maven:application'
 [exporter] Adding cache layer 'paketo-buildpacks/maven:cache'
 [exporter] Adding cache layer 'paketo-buildpacks/maven:maven'
 [exporter] Reusing cache layer 'buildpacksio/lifecycle:cache.sbom'
 Build [skaffold-buildpacks] succeeded
 Tags used in deployment:
  - skaffold-buildpacks -> skaffold-buildpacks:3da633f394b3d8bf756e5fae0d7ce89186843ed1eff57ca572f2544c47f3ff5f
 Starting deploy...
  - service/web created
  - deployment.apps/web created
 Waiting for deployments to stabilize...
  - kubernetes-dashboard:deployment/web: unable to determine current service state of pod "web-7c6dcfdf46-m7x5q"
     - kubernetes-dashboard:pod/web-7c6dcfdf46-m7x5q: unable to determine current service state of pod "web-7c6dcfdf46-m7x5q"
  - kubernetes-dashboard:deployment/web is ready.
 Deployments stabilized in 20.855 seconds
 Listing files to watch...
  - skaffold-buildpacks
 Press Ctrl+C to exit
 Watching for changes...
 [web] Setting Active Processor Count to 6
 [web] Calculating JVM memory based on 6769968K available memory
 [web] For more information on this calculation, see https://paketo.io/docs/reference/java-reference/#memory-calculator
 [web] Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx6175211K -XX:MaxMetaspaceSize=82756K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 6769968K, Thread Count: 250, Loaded Class Count: 12197, Headroom: 0%)
 [web] Enabling Java Native Memory Tracking
 [web] Adding 124 container CA certificates to JVM truststore
 [web] Spring Cloud Bindings Enabled
 [web] Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -XX:+ExitOnOutOfMemoryError -XX:ActiveProcessorCount=6 -XX:MaxDirectMemorySize=10M -Xmx6175211K -XX:MaxMetaspaceSize=82756K -XX:ReservedCodeCacheSize=240M -Xss1M -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics -Dorg.springframework.cloud.bindings.boot.enable=true
 [web] 
 [web]   .   ____          _            __ _ _
 [web]  /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
 [web] ( ( )___ | '_ | '_| | '_ / _` | \ \ \ \
 [web]  \/  ___)| |_)| | | | | || (_| |  ) ) ) )
 [web]   '  |____| .__|_| |_|_| |___, | / / / /
 [web]  =========|_|==============|___/=/_/_/_/
 [web]  :: Spring Boot ::                (v2.7.4)
 [web] 
 [web] 2023-03-07 04:19:31.119  INFO 1 --- [           main] hello.Application                        : Starting Application v0.1.0 using Java 11.0.18 on web-7c6dcfdf46-m7x5q with PID 1 (/workspace/BOOT-INF/classes started by cnb in /workspace)
 [web] 2023-03-07 04:19:31.133  INFO 1 --- [           main] hello.Application                        : No active profile set, falling back to 1 default profile: "default"
 [web] 2023-03-07 04:19:33.755  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
 [web] 2023-03-07 04:19:33.785  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
 [web] 2023-03-07 04:19:33.786  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
 [web] 2023-03-07 04:19:34.060  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
 [web] 2023-03-07 04:19:34.060  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2741 ms
 [web] 2023-03-07 04:19:34.928  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
 [web] 2023-03-07 04:19:34.942  INFO 1 --- [           main] hello.Application                        : Started Application in 5.026 seconds (JVM running for 10.487)
 [web] 2023-03-07 04:20:42.742  INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
 [web] 2023-03-07 04:20:42.742  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
 [web] 2023-03-07 04:20:42.743  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
 ​

访问8080,试试

 xfhuangskaffold  🍣 main 📝 ×2🛤️  ×2🐢 ×5via 🏎💨 v1.19.3 as 👩•💻 
 ⬢ [Systemd] 🌈 curl -L http://localhost:8080                                                                            
 curl: (7) Failed to connect to localhost port 8080 after 0 ms: Couldn't connect to server
 (⎈|minikube:kubernetes-dashboard)
 xfhuangskaffold  🍣 main 📝 ×2🛤️  ×2🐢 ×5v

查看编译的镜像多大:

 docker image ls|grep skaffold-buildpacks

image-20230307124444117

run一个poeye

image-20230307133043577

参考文档:

github.com/paketo-buil…

skaffold.dev/docs/builde…