K8S 创建Spring-boot项目并进行项目启动与访问

 

##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



如果使用 imagePullSecrets 需要添加 regucred

kubectl create secret docker-registry regcred \
--docker-server=registry.cn-hangzhou.aliyuncs.com \
--docker-username=15539211946 \
--docker-password=Uutest123 \
--docker-email=15539211946@163.com

如果密码更改 可以通过这进行调整

[root@master ~]# kubectl delete secret regcred
secret "regcred" deleted
[root@master ~]# 
[root@master ~]# kubectl create secret docker-registry regcred \
> --docker-server=registry.cn-hangzhou.aliyuncs.com \
> --docker-username=15539211946 \
> --docker-password=Un1we123 \
> --docker-email=15539211946@163.com
secret/regcred created
[root@master ~]# 

 

 

 

上传成功后,可以在仓库查看

 然后编辑 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的日志输出

 

 

 K8S 根据镜像升级   (我本地有两个镜像 一个是V1 版本 一个是V2 版本 现在 我从V1版本升级到V2版本)

kubectl set image deployment web java=nginx
`#kubectl set image+ 资源类型+名字(web)+ (java=nginx)原镜像名=新镜像名`
#kubectl edit service/web #如果配置文件有改动,直接修改server
http://192.168.106.103:30909/ #能够访问到最新的项目

#kubectl set image deployment/k8s-demo k8s-demo=registry.cn-hangzhou.aliyuncs.com/kubectlte/hello-world-app:v2

  #deployment.apps/k8s-demo image updated

 在刷新访问浏览器就变成了 升级之后的项目了

 

 

根据历史版本进行回滚 

[root@node1 kubectl]#  kubectl rollout history deployment k8s-demo
deployment.apps/k8s-demo 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

 查看回滚版本的信息 

[root@node1 ~]# kubectl rollout history deployment/k8s-demo --revision=3
deployment.apps/k8s-demo with revision #3
Pod Template:
  Labels:       app=k8s-demo
        pod-template-hash=6646c6b47b
  Containers:
   k8s-demo:
    Image:      registry.cn-hangzhou.aliyuncs.com/kubectlte/hello-world-app:v1
    Port:       8080/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

[root@node1 ~]# 

 

 根据回滚的版本进行回滚

[root@node1 ~]# kubectl rollout undo deployment/k8s-demo --to-revision=1
deployment.apps/k8s-demo rolled back
[root@node1 ~]# kubectl rollout undo deployment/k8s-demo --to-revision=2
deployment.apps/k8s-demo rolled back

 

副本扩容

[root@node1 kubectl]# kubectl scale deployment k8s-demo --replicas=5
deployment.apps/k8s-demo scaled
[root@node1 kubectl]# kubectl get pods 
NAME                               READY   STATUS             RESTARTS   AGE
k8s-demo-6646c6b47b-cjkfm          1/1     Running            0          9m3s
k8s-demo-6646c6b47b-gh5jm          1/1     Running            0          9m4s
k8s-demo-6646c6b47b-qx66t          1/1     Running            0          4s
k8s-demo-6646c6b47b-t256n          1/1     Running            0          4s
k8s-demo-6646c6b47b-xlt4z          1/1     Running            0          4s
nginx-deployment-9456bbbf9-p7mrj   1/1     Running            0          2d
nginx-deployment-9456bbbf9-z8pjj   1/1     Running            0          2d
nginx-deployment-9456bbbf9-zj4ww   1/1     Running            0          2d
nginx-po                           1/1     Running            0          6d17h
web-d4554f5ff-5xn4z                1/1     Running            0          6d17h
web-d4554f5ff-jjdsw                0/1     ImagePullBackOff   0          6d17h
web-d4554f5ff-mdwvn                1/1     Running            0          6d17h

 

kubectl  scale  deployment  k8s-demo --replicas=2

 

 

进行升级

[root@node1 kubectl]# kubectl set image -n=deployment-test deployment/deployment-nginx-test nginx=nginx:latest
deployment.apps/deployment-nginx-test image updated

查看容器升级情况

[root@node2 ~]#  kubectl get pods -n=deployment-test --watch
NAME                                     READY   STATUS    RESTARTS   AGE
deployment-nginx-test-6c965ff977-rf4dv   1/1     Running   0          5m2s
deployment-nginx-test-6c965ff977-zrfqf   1/1     Running   0          5m2s








deployment-nginx-test-d966cd4f8-lrl8f    0/1     Pending   0          0s
deployment-nginx-test-d966cd4f8-lrl8f    0/1     Pending   0          0s
deployment-nginx-test-d966cd4f8-lrl8f    0/1     ContainerCreating   0          0s
deployment-nginx-test-d966cd4f8-lrl8f    1/1     Running             0          1s
deployment-nginx-test-6c965ff977-zrfqf   1/1     Terminating         0          5m12s
deployment-nginx-test-d966cd4f8-xfxcm    0/1     Pending             0          0s
deployment-nginx-test-d966cd4f8-xfxcm    0/1     Pending             0          0s
deployment-nginx-test-d966cd4f8-xfxcm    0/1     ContainerCreating   0          0s
deployment-nginx-test-6c965ff977-zrfqf   0/1     Terminating         0          5m13s
deployment-nginx-test-6c965ff977-zrfqf   0/1     Terminating         0          5m13s
deployment-nginx-test-6c965ff977-zrfqf   0/1     Terminating         0          5m13s
deployment-nginx-test-d966cd4f8-xfxcm    1/1     Running             0          1s
deployment-nginx-test-6c965ff977-rf4dv   1/1     Terminating         0          5m13s
deployment-nginx-test-6c965ff977-rf4dv   0/1     Terminating         0          5m14s
deployment-nginx-test-6c965ff977-rf4dv   0/1     Terminating         0          5m14s
deployment-nginx-test-6c965ff977-rf4dv   0/1     Terminating         0          5m14s

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

项目部署参考 https://blog.csdn.net/CLanMua/article/details/136189976

idear打包 jar项目参考 https://blog.csdn.net/qq_38306801/article/details/113178551

K8S单机部署参考 https://blog.csdn.net/chj_1224365967/article/details/136365077

K8S集群搭建 参考 https://zhuanlan.zhihu.com/p/627310856

K8S 日常使用参考 https://blog.csdn.net/chj_1224365967/article/details/117779493

 

K8S 名词详解参考 

有状态-无状态 https://blog.csdn.net/IT_ZRS/article/details/134040807
Label selector https://blog.csdn.net/IT_ZRS/article/details/134059035

 

K8S 中文文档社区参考

http://docs.kubernetes.org.cn/317.html

 

posted @ 2024-05-04 17:53  不会游泳的鱼丶  阅读(454)  评论(0编辑  收藏  举报