目录
1.新建SpringBoot项目
a.结构如下
?b.文件内容
2.编写打包和部署脚本
a. 文件目录如下
b.文件内容
3.执行构建和运行
a.执行build脚本
b.执行deploy脚本
4.查看执行效果
a.查看管理页面
b.查看启动日志
c.访问服务测试
可能碰到的问题
1.新建SpringBoot项目
idea 新创建一个springBoot的项目。
a.结构如下
?b.文件内容
pom文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-docker-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-docker-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>pasq</docker.image.prefix>
<!-- docker harbor地址 -->
<docker.repostory>192.168.1.253:8081</docker.repostory>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
接口:
package com.example.demo.docker.test.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class IndexController {
@GetMapping("/test/abc")
public String test(String name) {
log.info("hello.....{}", name);
return name + ":hello";
}
}
上边算是建完项目了。
2.编写打包和部署脚本
a. 文件目录如下
b.文件内容
dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD project.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
build.sh
测试构建脚本,简单模拟打包完后的jar执行docker build。
mvn clean package 这个指令可以加到前头凑成一个完整的,我这个只是实验下。
(还有就是如果是公司级别使用,肯定都要有自己的?私有仓库-harbor,所以理论上也是要push的harbor上的,我单机测试,懒得启动那么多服务了。)
#!/bin/bash
cp ../target/demo-docker-test-0.0.1-SNAPSHOT.jar project.jar
docker build -t demo-docker-test:0.0.1 .
rm -rf project.jar
deploy.sh
#!/bin/bash
kubectl create -f deploy.yaml
deploy.yaml
apiVersion: v1
kind: Service
metadata:
name: demo-docker-test
namespace: default
labels:
app: demo-docker-test
spec:
type: NodePort
ports:
- port: 9999
nodePort: 30090 #service对外开放端口
selector:
app: demo-docker-test
---
apiVersion: apps/v1
kind: Deployment #对象类型
metadata:
name: demo-docker-test #名称
labels:
app: demo-docker-test #标注
spec:
replicas: 3 #运行容器的副本数,修改这里可以快速修改分布式节点数量
selector:
matchLabels:
app: demo-docker-test
template:
metadata:
labels:
app: demo-docker-test
spec:
containers: #docker容器的配置
- name: demo-docker-test
image: demo-docker-test:0.0.1 # pull镜像的地址 ip:prot/dir/images:tag
imagePullPolicy: IfNotPresent #pull镜像时机,
ports:
- containerPort: 9999 #容器对外开放端口
3.执行构建和运行
a.执行build脚本
ruishens-MacBook-Pro:deploy ruishen$ sh build.sh
[+] Building 31.0s (7/7) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:8-jdk-alpine 1.8s
=> [internal] load build context 1.2s
=> => transferring context: 21.45MB 1.2s
=> [1/2] FROM docker.io/library/openjdk:8-jdk-alpine@sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3 28.5s
=> => resolve docker.io/library/openjdk:8-jdk-alpine@sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3 0.0s
=> => sha256:c2274a1a0e2786ee9101b08f76111f9ab8019e368dce1e325d3c284a0ca33397 70.73MB / 70.73MB 25.5s
=> => sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3 1.64kB / 1.64kB 0.0s
=> => sha256:44b3cea369c947527e266275cee85c71a81f20fc5076f6ebb5a13f19015dce71 947B / 947B 0.0s
=> => sha256:a3562aa0b991a80cfe8172847c8be6dbf6e46340b759c2b782f8b8be45342717 3.40kB / 3.40kB 0.0s
=> => sha256:e7c96db7181be991f19a9fb6975cdbbd73c65f4a2681348e63a141a2192a5f10 2.76MB / 2.76MB 1.7s
=> => sha256:f910a506b6cb1dbec766725d70356f695ae2bf2bea6224dbe8c7c6ad4f3664a2 238B / 238B 6.2s
=> => extracting sha256:e7c96db7181be991f19a9fb6975cdbbd73c65f4a2681348e63a141a2192a5f10 0.3s
=> => extracting sha256:f910a506b6cb1dbec766725d70356f695ae2bf2bea6224dbe8c7c6ad4f3664a2 0.0s
=> => extracting sha256:c2274a1a0e2786ee9101b08f76111f9ab8019e368dce1e325d3c284a0ca33397 2.7s
=> [2/2] ADD project.jar app.jar 0.2s
=> exporting to image 0.2s
=> => exporting layers 0.2s
=> => writing image sha256:79b617e7d7d1547a70cd8fcf591eeaf23c9bd1ad6cdc4d576b00b094da63bad2 0.0s
=> => naming to docker.io/library/demo-docker-test:0.0.1 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
ruishens-MacBook-Pro:deploy ruishen$
执行完成,检查下。 下边就有了 demo-dokcer-test的镜像
ruishens-MacBook-Pro:deploy ruishen$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo-docker-test 0.0.1 79b617e7d7d1 About a minute ago 126MB
alpine/git latest 0deb7380d708 8 days ago 27.4MB
docker/desktop-kubernetes kubernetes-v1.21.5-cni-v0.8.5-critools-v1.17.0-debian 967a1c03eb00 5 weeks ago 290MB
k8s.gcr.io/kube-apiserver v1.21.5 7b2ac941d4c3 6 weeks ago 126MB
k8s.gcr.io/kube-proxy v1.21.5 e08abd2be730 6 weeks ago 104MB
k8s.gcr.io/kube-scheduler v1.21.5 8e60ea3644d6 6 weeks ago 50.8MB
k8s.gcr.io/kube-controller-manager v1.21.5 184ef4d127b4 6 weeks ago 120MB
docker/desktop-vpnkit-controller v2.0 8c2c38aa676e 5 months ago 21MB
docker/desktop-storage-provisioner v2.0 99f89471f470 6 months ago 41.9MB
k8s.gcr.io/pause 3.4.1 0f8457a4c2ec 9 months ago 683kB
k8s.gcr.io/coredns/coredns v1.8.0 296a6d5035e2 12 months ago 42.5MB
k8s.gcr.io/etcd 3.4.13-0 0369cf4303ff 14 months ago 253MB
kubernetesui/dashboard v2.0.0-beta1 4640949a39e6 2 years ago 64.6MB
kubernetesui/metrics-scraper v1.0.0 44390ebe2b73 2 years ago 36.8MB
mirrorgooglecontainers/kubernetes-dashboard-amd64 v1.10.1 f9aed6605b81 2 years ago 122MB
ruishens-MacBook-Pro:deploy ruishen$
b.执行deploy脚本
ruishens-MacBook-Pro:deploy ruishen$ sh deploy.sh
service/demo-docker-test created
deployment.apps/demo-docker-test created
4.查看执行效果
a.查看管理页面
这个时候已经看到对应的k8s中的pots,且有3台服务
b.查看启动日志
ruishens-MacBook-Pro:k8s-docker-desktop-for-mac ruishen$ kubectl get pods
NAME READY STATUS RESTARTS AGE
demo-docker-test-7cf5bf9c78-5t8gd 1/1 Running 0 3m57s
demo-docker-test-7cf5bf9c78-kw49t 1/1 Running 0 3m57s
demo-docker-test-7cf5bf9c78-n45ck 1/1 Running 0 3m57s
ruishens-MacBook-Pro:k8s-docker-desktop-for-mac ruishen$ kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep kubernetes-dashboard-admin | awk '{print $1}')
ruishens-MacBook-Pro:k8s-docker-desktop-for-mac ruishen$ kubectl logs -f demo-docker-test-7cf5bf9c78-n45ck
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
2021-10-28 14:36:33.019 INFO 1 --- [ main] c.e.d.d.test.DemoDockerTestApplication : Starting DemoDockerTestApplication v0.0.1-SNAPSHOT using Java 1.8.0_212 on demo-docker-test-7cf5bf9c78-n45ck with PID 1 (/app.jar started by root in /)
2021-10-28 14:36:33.026 INFO 1 --- [ main] c.e.d.d.test.DemoDockerTestApplication : No active profile set, falling back to default profiles: default
2021-10-28 14:36:36.493 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
2021-10-28 14:36:36.526 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-28 14:36:36.526 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-28 14:36:36.714 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-28 14:36:36.715 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3506 ms
2021-10-28 14:36:39.417 INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2021-10-28 14:36:39.616 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path ''
2021-10-28 14:36:39.645 INFO 1 --- [ main] c.e.d.d.test.DemoDockerTestApplication : Started DemoDockerTestApplication in 8.16 seconds (JVM running for 9.48)
c.访问服务测试
我重启后,简单测试下查看多个pods下的日志。
pods1.
ruishens-MacBook-Pro:k8s-docker-desktop-for-mac ruishen$ kubectl logs -f demo-docker-test-5bf6f6f44f-fwfq4
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
2021-10-28 14:47:02.383 INFO 1 --- [ main] c.e.d.d.test.DemoDockerTestApplication : Starting DemoDockerTestApplication v0.0.1-SNAPSHOT using Java 1.8.0_212 on demo-docker-test-5bf6f6f44f-fwfq4 with PID 1 (/app.jar started by root in /)
2021-10-28 14:47:02.388 INFO 1 --- [ main] c.e.d.d.test.DemoDockerTestApplication : No active profile set, falling back to default profiles: default
2021-10-28 14:47:06.113 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
2021-10-28 14:47:06.169 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-28 14:47:06.180 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-28 14:47:06.644 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-28 14:47:06.652 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4181 ms
2021-10-28 14:47:08.629 INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2021-10-28 14:47:08.854 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path ''
2021-10-28 14:47:08.903 INFO 1 --- [ main] c.e.d.d.test.DemoDockerTestApplication : Started DemoDockerTestApplication in 7.399 seconds (JVM running for 8.805)
2021-10-28 14:48:59.262 INFO 1 --- [nio-9999-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-28 14:48:59.263 INFO 1 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-28 14:48:59.264 INFO 1 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2021-10-28 14:48:59.320 INFO 1 --- [nio-9999-exec-1] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:48:59.571 INFO 1 --- [nio-9999-exec-2] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:48:59.898 INFO 1 --- [nio-9999-exec-3] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:00.210 INFO 1 --- [nio-9999-exec-4] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:06.043 INFO 1 --- [nio-9999-exec-5] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:06.458 INFO 1 --- [nio-9999-exec-6] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:06.791 INFO 1 --- [nio-9999-exec-7] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:16.625 INFO 1 --- [nio-9999-exec-8] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:28.023 INFO 1 --- [nio-9999-exec-9] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:28.396 INFO 1 --- [io-9999-exec-10] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:28.615 INFO 1 --- [nio-9999-exec-1] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:28.950 INFO 1 --- [nio-9999-exec-2] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:29.433 INFO 1 --- [nio-9999-exec-3] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:29.550 INFO 1 --- [nio-9999-exec-4] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:29.635 INFO 1 --- [nio-9999-exec-5] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:29.719 INFO 1 --- [nio-9999-exec-6] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:29.809 INFO 1 --- [nio-9999-exec-7] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:29.891 INFO 1 --- [nio-9999-exec-8] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:29.975 INFO 1 --- [nio-9999-exec-9] c.e.d.d.test.controller.IndexController : hello.....123
2021-10-28 14:49:36.202 INFO 1 --- [io-9999-exec-10] c.e.d.d.test.controller.IndexController : hello.....234
2021-10-28 14:49:37.779 INFO 1 --- [nio-9999-exec-1] c.e.d.d.test.controller.IndexController : hello.....2345
2021-10-28 14:49:39.756 INFO 1 --- [nio-9999-exec-2] c.e.d.d.test.controller.IndexController : hello.....23456
2021-10-28 14:49:41.776 INFO 1 --- [nio-9999-exec-3] c.e.d.d.test.controller.IndexController : hello.....234567
2021-10-28 14:49:57.030 INFO 1 --- [nio-9999-exec-4] c.e.d.d.test.controller.IndexController : hello.....234567
2021-10-28 14:50:15.481 INFO 1 --- [nio-9999-exec-5] c.e.d.d.test.controller.IndexController : hello.....123
pods2
pods3
参考学习:K8S部署SpringBoot应用_都超的博客-CSDN博客_k8s部署springboot项目
可能碰到的问题
部署太慢了,修改下镜像地址:
ruishens-MacBook-Pro:deploy ruishen$ sh build.sh
[+] Building 107.5s (5/6)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 185B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:8-jdk-alpine 1.4s
=> [internal] load build context 0.8s
=> => transferring context: 21.45MB 0.8s
=> CANCELED [1/2] FROM docker.io/library/openjdk:8-jdk-alpine@sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3 106.0s
=> => resolve docker.io/library/openjdk:8-jdk-alpine@sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3 0.0s
=> => sha256:a3562aa0b991a80cfe8172847c8be6dbf6e46340b759c2b782f8b8be45342717 3.40kB / 3.40kB 0.0s
=> => sha256:e7c96db7181be991f19a9fb6975cdbbd73c65f4a2681348e63a141a2192a5f10 2.76MB / 2.76MB 38.2s
=> => sha256:f910a506b6cb1dbec766725d70356f695ae2bf2bea6224dbe8c7c6ad4f3664a2 238B / 238B 2.4s
=> => sha256:c2274a1a0e2786ee9101b08f76111f9ab8019e368dce1e325d3c284a0ca33397 1.05MB / 70.73MB 106.0s
=> => sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3 1.64kB / 1.64kB 0.0s
=> => sha256:44b3cea369c947527e266275cee85c71a81f20fc5076f6ebb5a13f19015dce71 947B / 947B 0.0s
=> => extracting sha256:e7c96db7181be991f19a9fb6975cdbbd73c65f4a2681348e63a141a2192a5f10 0.2s
=> => extracting sha256:f910a506b6cb1dbec766725d70356f695ae2bf2bea6224dbe8c7c6ad4f3664a2 0.0s
context canceled
修改地址:进入到docker的设置页面,修改registry-mirros属性,然后 apply && restart
|