• K8s部署SpringBoot项目简单例子


    目录

    前言

    前提条件

    正文

    1. 获取镜像

    2. 空运行测试生成部署yaml文件

    3. 修改yaml文件,增加镜像拉取策略

    4. 以yaml文件的方式部署springboot项目

    5. 查看部署pod的状态

    6. 暴露服务端口

    7.通过浏览器访问服务


    前言

    本文通过将一个构建好的springboot的hello-world项目镜像,通过yaml部署的方式将其部署到K8s上。通过回顾部署的具体实现作为springboot项目K8s容器化部署的开始,后续考虑补充多个服务部署过程中出现的一些问题,加深对K8s的理解。

    前提条件

    本文默认已经制作好了springboot项目的镜像,可以参见博主之前的一篇博客,介绍如何制作该镜像。

    Docker启动SpringBoot简单例子

    正文

    1. 获取镜像

    对于制作好的镜像,可以通过docker load -i 导入的方式导入或者从harbor上拉取,本文由于之前的镜像在harbor上,故直接拉取镜像。

    1. [root@localhost k8s-env]# docker pull 192.168.79.131:8443/test/springboot-helloworld:latest
    2. latest: Pulling from test/springboot-helloworld
    3. 7448db3b31eb: Pull complete
    4. c36604fa7939: Pull complete
    5. 29e8ef0e3340: Pull complete
    6. a0c934d2565d: Pull complete
    7. a360a17c9cab: Pull complete
    8. cfcc996af805: Pull complete
    9. 2cf014724202: Pull complete
    10. 4bc402a00dfe: Pull complete
    11. 44a7449e65ee: Pull complete
    12. 25a52df076b4: Pull complete
    13. Digest: sha256:15a44f192e1ebe7ae69b2f792ff8876d6184099aeb3eaaa75039091f378d57af
    14. Status: Downloaded newer image for 192.168.79.131:8443/test/springboot-helloworld:latest

    查看拉取的镜像

    为方便管理,重新发个简单的tag

    docker tag 192.168.79.131:8443/test/springboot-helloworld:latest springboot-helloworld:latest

    2. 空运行测试生成部署yaml文件

    执行如下指令

    kubectl create deployment springboot-helloworld --image=springboot-helloworld:latest --dry-run -o yaml > springboot-helloworld.yaml
    1. [root@localhost k8s-env]# kubectl create deployment springboot-helloworld --image=springboot-helloworld:latest --dry-run -o yaml > springboot-helloworld.yaml
    2. W0911 22:21:58.731993 14903 helpers.go:557] --dry-run is deprecated and can be replaced with --dry-run=client.
    3. [root@localhost k8s-env]# ll
    4. total 4
    5. -rw-r--r-- 1 root root 487 Sep 11 22:21 springboot-helloworld.yaml

     查看生成的yaml文件

    1. [root@localhost k8s-env]# cat springboot-helloworld.yaml
    2. apiVersion: apps/v1
    3. kind: Deployment
    4. metadata:
    5. creationTimestamp: null
    6. labels:
    7. app: springboot-helloworld
    8. name: springboot-helloworld
    9. spec:
    10. replicas: 1
    11. selector:
    12. matchLabels:
    13. app: springboot-helloworld
    14. strategy: {}
    15. template:
    16. metadata:
    17. creationTimestamp: null
    18. labels:
    19. app: springboot-helloworld
    20. spec:
    21. containers:
    22. - image: springboot-helloworld:latest
    23. name: springboot-helloworld
    24. resources: {}
    25. status: {}

    3. 修改yaml文件,增加镜像拉取策略

    4. 以yaml文件的方式部署springboot项目

    1. [root@localhost k8s-env]# kubectl apply -f springboot-helloworld.yaml
    2. deployment.apps/springboot-helloworld created

    5. 查看部署pod的状态

    1. [root@localhost k8s-env]# kubectl get pod -A
    2. NAMESPACE NAME READY STATUS RESTARTS AGE
    3. default springboot-helloworld-b8cd9f79c-47sxr 1/1 Running 0 26s
    4. [root@localhost k8s-env]# kubectl get deployment
    5. NAME READY UP-TO-DATE AVAILABLE AGE
    6. springboot-helloworld 1/1 1 1 60s

    6. 暴露服务端口

    执行如下指令暴露服务端口

    kubectl expose deployment springboot-helloworld --port=8089 --type=NodePort
    1. [root@localhost k8s-env]# kubectl expose deployment springboot-helloworld --port=8089 --type=NodePort
    2. service/springboot-helloworld exposed
    3. [root@localhost k8s-env]# kubectl get svc
    4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    5. kubernetes ClusterIP 10.96.0.1 443/TCP 11h
    6. springboot-helloworld NodePort 10.100.70.103 8089:30210/TCP 19s

    7.通过浏览器访问服务

    访问服务地址:http://192.168.79.139:30210/hello

    部署成功。

  • 相关阅读:
    MySQL--大数据量的分页优化方案
    数据库Mysql经典面试题之SQL语句
    全面了解v-if和v-show的区别
    【LeetCode回溯算法#06】复原IP地址详解(练习如何处理边界条件,判断IP合法性)
    springboot外委员工后台管理系统毕业设计源码101157
    若依框架代码生成详解
    【技术积累】算法中的动态规划【二】
    C#中的for和foreach的探究与学习
    单视频播放量超20万的公开课配套教材,猫书来了~
    【MATLAB源码-第48期】基于matlab的16QAM信号盲解调仿真。
  • 原文地址:https://blog.csdn.net/akenseren/article/details/126810680