##Spring-boot 的helloworld 项目
package com.example.demo; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import lombok.extern.slf4j.Slf4j; @RestController @RequestMapping("/test") @Slf4j public class HelloWorldController { // 假设 DatePatten 类中定义了如下常量 // public static final DateTimeFormatter NORM_DATETIME_MS_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); @GetMapping("/test1") // 推荐使用 @GetMapping 而不是 @RequestMapping("/test1") public String test1() { // 假设 LocalDateTimeUtil 有一个静态方法 format 来格式化 LocalDateTime // String formattedDateTime = LocalDateTimeUtil.format(LocalDateTime.now(), DatePatten.NORM_DATETIME_MS_FORMATTER); // 如果没有 LocalDateTimeUtil,您可以直接使用 DateTimeFormatter DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); String formattedDateTime = LocalDateTime.now().format(formatter); log.info("执行时间:{}", formattedDateTime); // 使用 log 而不是 System.out.println return "升级之后的项目"; } }
编写Dockerfile
[root@node1 kubectl]# more Dockerfile #penJDK 17作为基础镜像 #FROM openjdk:17-jre-slim #FROM openjdk:11-jre-slim FROM eclipse-temurin:17-jre # 设置工作目录为/app WORKDIR /app # 将当前目录中的jar包复制到容器的/app目录下 COPY hello-world-0.0.1-SNAPSHOT.jar /app/app.jar # 暴露应用程序端口(这里假设你的Spring Boot应用程序在8080端口上运行) EXPOSE 8080 # 在容器启动时运行jar包 CMD ["java", "-jar", "app.jar"]
构建镜像 镜像名称 my-spring-boot-app 版本 v2
[root@node1 kubectl]# docker build -t my-spring-boot-app:v2 . [+] Building 0.4s (2/2) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 505B 0.0s => CANCELED [internal] load metadata for docker.io/library/eclipse-temurin:17-jre 0.4s ERROR: failed to solve: Canceled: context canceled [root@node1 kubectl]# docker build -t hello-world-app:v2 . [+] Building 17.3s (8/8) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 505B 0.0s => [internal] load metadata for docker.io/library/eclipse-temurin:17-jre 16.6s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/3] FROM docker.io/library/eclipse-temurin:17-jre@sha256:15b50cf95210242511c17e1ec8281fd691ad7d34f163c250a21298943d65de87 0.0s => [internal] load build context 0.2s => => transferring context: 21.47MB 0.2s => CACHED [2/3] WORKDIR /app 0.0s => [3/3] COPY hello-world-0.0.1-SNAPSHOT.jar /app/app.jar 0.1s => exporting to image 0.2s => => exporting layers 0.1s => => writing image sha256:1e8dbdc55dddcc23adfae071a4c8ede5a5f76a5388a822024eb64b9a015c3648 0.0s => => naming to docker.io/library/hello-world-app:v2
我这是上传到阿里云的镜像仓库 登录阿里云镜像仓库,并上传文件到仓库 记得替换自己的镜像ID tag
https://cr.console.aliyun.com/repository/cn-hangzhou/kubectlte/hello-world-app/details
$ docker login --username=15539211956 registry.cn-hangzhou.aliyuncs.com $ docker tag 1e8dbdc55ddd registry.cn-hangzhou.aliyuncs.com/kubectlte/hello-world-app:v2 $ docker push registry.cn-hangzhou.aliyuncs.com/kubectlte/hello-world-app:v2
上传成功后,可以在仓库查看
然后编辑 helloworld的yaml
[root@node1 kubectl]# more helloworld.yaml apiVersion: apps/v1 kind: Deployment metadata: name: k8s-demo spec: selector: matchLabels: app: k8s-demo replicas: 2 #2个副本 template: metadata: labels: app: k8s-demo spec: containers: - name: k8s-demo image: registry.cn-hangzhou.aliyuncs.com/kubectlte/hello-world-app:v1 # 记得改 ports: - containerPort: 8080 --- # 创建Pod的Service service的80端口指向pod的8080 apiVersion: v1 kind: Service metadata: name: k8s-demo spec: type: NodePort ports: - port: 7003 targetPort: 8080 selector: app: k8s-demo ---
创建pod、service
[root@node1 kubectl]# kubectl create -f helloworld.yaml 查看pod [root@node1 kubectl]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES k8s-demo-6646c6b47b-2rdvp 1/1 Running 0 3h8m 10.244.2.25 node2 <none> <none> k8s-demo-6646c6b47b-mb599 1/1 Running 0 3h8m 10.244.1.18 node1 <none> <none> 查看service [root@node1 kubectl]# kubectl get service -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR k8s-demo NodePort 10.101.189.212 <none> 7003:30073/TCP 3h8m app=k8s-demo
查看 deploy
[root@node1 kubectl]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
k8s-demo 2/2 2 2 3h10m k8s-demo registry.cn-hangzhou.aliyuncs.com/kubectlte/hello-world-app:v1 app=k8s-demo
查看node [root@node1 kubectl]# kubectl get node -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME master Ready control-plane,master 6d2h v1.23.0 10.0.4.17 <none> CentOS Linux 7 (Core) 3.10.0-1160.99.1.el7.x86_64 docker://25.0.5 node1 Ready <none> 6d2h v1.23.0 10.0.12.2 <none> CentOS Linux 7 (Core) 3.10.0-1160.108.1.el7.x86_64 docker://25.0.3 node2 Ready <none> 6d2h v1.23.0 10.0.16.3 <none> CentOS Linux 7 (Core) 3.10.0-1160.108.1.el7.x86_64 docker://25.0.3 访问的话 可以通过 nodeip + port 如 curl 10.0.12.2:30073/test/test1 进行项目访问 curl 10.0.16.3:30073/test/test1 进行项目访问
需要开通节点的外网的端口 不然无法访问
kubectl logs -f pod名称 就可以查看pod的日志输出
标签:kubectl,k8s,demo,app,boot,Spring,node1,docker,K8S From: https://www.cnblogs.com/JIKes/p/18172522