##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的日誌輸出
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>
專案部署參考 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