测试如下教程时,请确认kubernates环境正常,可参考该文件搭建kubernates环境Kubernetes微服务入门教程:环境安装_jacarri的博客-CSDN博客。
访问https://start.spring.io/创建一个最简单的 spring boot应用,如下图所示
添加用于测试java代码
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String index() {
return "Hello World\t" + new Date();
}
}
添加用于制作镜像的文件:src/main/docker/Dockerfile,文件内容如下:
FROM openjdk:8-jdk-oracle
ADD spring-boot-demo-0.0.1-SNAPSHOT.jar /app.jar
ENV TZ=Asia/Shanghai
ENTRYPOINT [ "java", "-jar", "/app.jar" ]
在pom.xml 添加用于制作镜像的 docker-maven-plugin 插件
1.8
${java.version}
${java.version}
UTF-8
jacarrichan
...
com.spotify
docker-maven-plugin
1.2.2
package
build
${docker.image.prefix}/${project.artifactId}
${project.basedir}/src/main/docker
${project.version}
latest
/
${project.build.directory}
${project.build.finalName}.jar
...
添加用于声明Kubernates服务的yml文件,内容如下:
apiVersion: v1
kind: Service
metadata:
name: spring-boot-demo
labels:
app: spring-boot-demo
spec:
type: NodePort
ports:
- name: http
port: 8080
nodePort: 30000 # 此处定义POD的端口8080映射为Node的30000
selector:
app: spring-boot-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-boot-demo
spec:
replicas: 1
selector:
matchLabels:
app: spring-boot-demo
template:
metadata:
labels:
app: spring-boot-demo
spec:
containers:
- name: spring-boot-demo
image: jacarrichan/spring-boot-demo:0.0.1-SNAPSHOT
ports:
- name: http
containerPort: 8080 # 此处定义POD暴露的端口
PS C:\Users\jacarrichan\temp\spring-boot-demo> mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.jacarrichan.demo:spring-boot-demo >----------------
[INFO] Building spring-boot-demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ spring-boot-demo ---
[INFO] Deleting C:\Users\jacarrichan\temp\spring-boot-demo\target
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ spring-boot-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Users\jacarrichan\temp\spring-boot-demo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ spring-boot-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\jacarrichan\temp\spring-boot-demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ spring-boot-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\jacarrichan\temp\spring-boot-demo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-boot-demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.2.2:jar (default-jar) @ spring-boot-demo ---
[INFO] Building jar: C:\Users\jacarrichan\temp\spring-boot-demo\target\spring-boot-demo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.6.6:repackage (repackage) @ spring-boot-demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- docker-maven-plugin:1.2.2:build (default) @ spring-boot-demo ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying C:\Users\jacarrichan\temp\spring-boot-demo\target\spring-boot-demo-0.0.1-SNAPSHOT.jar -> C:\Users\jacarrichan\temp\spring-boot-demo\target\docker\spring-boot-demo-0.0.1-SNAPSHOT.jar
[INFO] Copying C:\Users\jacarrichan\temp\spring-boot-demo\src\main\docker\Dockerfile -> C:\Users\jacarrichan\temp\spring-boot-demo\target\docker\Dockerfile
[INFO] Building image jacarrichan/spring-boot-demo
Step 1/4 : FROM openjdk:8-jdk-oracle
---> 5783fe09fd20
Step 2/4 : ADD spring-boot-demo-0.0.1-SNAPSHOT.jar /app.jar
---> 4c07a1a20273
Step 3/4 : ENV TZ=Asia/Shanghai
---> Running in cc7f05568a73
Removing intermediate container cc7f05568a73
---> dac566480459
Step 4/4 : ENTRYPOINT [ "java", "-jar", "/app.jar" ]
---> Running in a9a9088ac338
Removing intermediate container a9a9088ac338
---> 98cf1242fba2
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 98cf1242fba2
Successfully tagged jacarrichan/spring-boot-demo:latest
[INFO] Built jacarrichan/spring-boot-demo
[INFO] Tagging jacarrichan/spring-boot-demo with 0.0.1-SNAPSHOT
[INFO] Tagging jacarrichan/spring-boot-demo with latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.186 s
[INFO] Finished at: 2022-04-10T19:42:30+08:00
[INFO] ------------------------------------------------------------------------
PS C:\Users\jacarrichan\temp\spring-boot-demo>
在Docker Desktop中查看制作的镜像
PS C:\Users\jacarrichan\temp\spring-boot-demo> kubectl create -f .\k8s.yaml
service/spring-boot-demo created
deployment.apps/spring-boot-demo created
PS C:\Users\jacarrichan\temp\spring-boot-demo>
PS C:\Users\jacarrichan\temp\spring-boot-demo> kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 9d
spring-boot-demo NodePort 10.107.197.80 8080:30000/TCP 2m12s
从中可以看到spring-boot-demo 这个Service的 8080端口被映射为了30000端口。
在访问30000端口,测试链路状态信息:
PS C:\Users\jacarrichan\temp\spring-boot-demo> kubectl get pods
NAME READY STATUS RESTARTS AGE
spring-boot-demo-d8bdc7896-7d4l4 1/1 Running 0 7m39s
PS C:\Users\jacarrichan\temp\spring-boot-demo> kubectl describe pod spring-boot-demo-d8bdc7896-7d4l4
Name: spring-boot-demo-d8bdc7896-7d4l4
Namespace: default
Priority: 0
Node: docker-desktop/192.168.65.4
Start Time: Sun, 10 Apr 2022 19:46:15 +0800
Labels: app=spring-boot-demo
pod-template-hash=d8bdc7896
Annotations:
Status: Running
IP: 10.1.1.188
IPs:
IP: 10.1.1.188
Controlled By: ReplicaSet/spring-boot-demo-d8bdc7896
Containers:
spring-boot-demo:
Container ID: docker://e05adb4204782ab6851e86eb6eee97e2fc9027cf4bcac405acf3d3c0bcf0d821
Image: jacarrichan/spring-boot-demo:0.0.1-SNAPSHOT
Image ID: docker://sha256:98cf1242fba27986e06b26549d42417af93bf6a313d3490ef6a844507d0546f4
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Sun, 10 Apr 2022 19:46:16 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-mgdks (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-mgdks:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional:
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 7m42s default-scheduler Successfully assigned default/spring-boot-demo-d8bdc7896-7d4l4 to docker-desktop
Normal Pulled 7m42s kubelet Container image "jacarrichan/spring-boot-demo:0.0.1-SNAPSHOT" already present on machine
Normal Created 7m42s kubelet Created container spring-boot-demo
Normal Started 7m42s kubelet Started container spring-boot-demo
本教程中涉及到的代码可从此处获取:
https://github.com/jacarrichan/spring-boot-kubernetes-demo