本地springboot项目发布到k8s
Posted boonya
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了本地springboot项目发布到k8s相关的知识,希望对你有一定的参考价值。
编写一个SpringBoot项目
项目里面提供一个简单的API
@RestController
public class ApiController
@GetMapping(value = "/index")
public String index()
String info = "接口访问成功";
System.out.println(info);
return info;
目录结果如下:
Dockerfile镜像制作
Dockerfile内容编辑
FROM adoptopenjdk/openjdk8:alpine-jre
ADD k8s-demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
打成镜像
S F:\\lwjCode\\k8s-demo\\target> docker build -t k8s-demo:0.0.1 .
[+] Building 2.8s (7/7) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 154B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/adoptopenjdk/openjdk8:alpine-jre 2.3s
=> [internal] load build context 0.2s
=> => transferring context: 25.26MB 0.2s
=> CACHED [1/2] FROM docker.io/adoptopenjdk/openjdk8:alpine-jre@sha256:a57843f7e988613cc9bbe15b90573b7bfea3b7adb06d81624e6abac9393de2f3 0.0s
=> [2/2] ADD k8s-demo-0.0.1-SNAPSHOT.jar app.jar 0.1s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:dbaf5b173b66e86259d3cb7683efd6a241ff5b10aa86f8b6c0d5eeb75f410ebb 0.0s
=> => naming to docker.io/library/k8s-demo:0.0.1 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
PS F:\\lwjCode\\k8s-demo\\target>
K8s服务发布
C:\\Users\\boonya>kubectl create deployment k8s-demo --image=k8s-demo:0.0.1 --port=8080
deployment.apps/k8s-demo created
C:\\Users\\boonya>kubectl get pods
NAME READY STATUS RESTARTS AGE
k8s-demo-7c55fc656d-qbgbl 1/1 Running 0 7s
nginx-565785f75c-w4zsk 1/1 Running 1 (8h ago) 2d4h
C:\\Users\\boonya>kubectl logs k8s-demo-7c55fc656d-qbgbl
. ____ _ __ _ _
/\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.6.RELEASE)
2023-03-10 09:31:07.072 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on k8s-demo-7c55fc656d-qbgbl with PID 1 (/app.jar started by root in /)
2023-03-10 09:31:07.076 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2023-03-10 09:31:07.720 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-03-10 09:31:07.729 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-03-10 09:31:07.730 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
2023-03-10 09:31:07.773 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-03-10 09:31:07.773 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 664 ms
2023-03-10 09:31:08.045 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-03-10 09:31:08.164 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-03-10 09:31:08.170 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.358 seconds (JVM running for 1.622)
C:\\Users\\boonya>kubectl expose deployment k8s-demo --type=NodePort
service/k8s-demo exposed
C:\\Users\\boonya>kubectl service k8s-demo
Error: unknown command "service" for "kubectl"
Run 'kubectl --help' for usage.
C:\\Users\\boonya>kubectl get pods
NAME READY STATUS RESTARTS AGE
k8s-demo-7c55fc656d-qbgbl 1/1 Running 0 3m28s
nginx-565785f75c-w4zsk 1/1 Running 1 (8h ago) 2d4h
C:\\Users\\boonya>kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
k8s-demo-7c55fc656d-qbgbl 1/1 Running 0 5m51s 10.1.2.132 docker-desktop <none> <none>
nginx-565785f75c-w4zsk 1/1 Running 1 (8h ago) 2d4h 10.1.2.126 docker-desktop <none> <none>
C:\\Users\\boonya>kubectl port-forward k8s-demo 8080:8080
Error from server (NotFound): pods "k8s-demo" not found
C:\\Users\\boonya>kubectl port-forward k8s-demo-7c55fc656d-qbgbl 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
Handling connection for 8080
Handling connection for 8080
C:\\Users\\boonya>kubectl logs k8s-demo-7c55fc656d-qbgbl
. ____ _ __ _ _
/\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.6.RELEASE)
2023-03-10 09:31:07.072 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on k8s-demo-7c55fc656d-qbgbl with PID 1 (/app.jar started by root in /)
2023-03-10 09:31:07.076 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2023-03-10 09:31:07.720 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-03-10 09:31:07.729 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-03-10 09:31:07.730 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
2023-03-10 09:31:07.773 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-03-10 09:31:07.773 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 664 ms
2023-03-10 09:31:08.045 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-03-10 09:31:08.164 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-03-10 09:31:08.170 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.358 seconds (JVM running for 1.622)
2023-03-10 09:39:14.946 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-03-10 09:39:14.947 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-03-10 09:39:14.963 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 16 ms
接口访问成功
接口访问成功
接口访问成功
接口访问成功
C:\\Users\\boonya>
浏览器访问服务:
上面是通过进行port-forward
kubectl port-forward: Kubernetes Port Forwarding Guide | phoenixNAP KB
端口暴露访问的,语法如下:
kubectl port-forward [resource-type]/[resource-name] [local-port]:[resource-port]
直接发布出来的服务是一个容器内IP地址,不可访问,下一步深入探究如何不通过port-forward进行服务访问。
以上是关于本地springboot项目发布到k8s的主要内容,如果未能解决你的问题,请参考以下文章
springboot项目部署到linux系统无法连接本地数据库
springboot文件上传到本地电脑,项目目录,保存到数据库