目录
本文通过将一个构建好的springboot的hello-world项目镜像,通过yaml部署的方式将其部署到K8s上。通过回顾部署的具体实现作为springboot项目K8s容器化部署的开始,后续考虑补充多个服务部署过程中出现的一些问题,加深对K8s的理解。
本文默认已经制作好了springboot项目的镜像,可以参见博主之前的一篇博客,介绍如何制作该镜像。
对于制作好的镜像,可以通过docker load -i 导入的方式导入或者从harbor上拉取,本文由于之前的镜像在harbor上,故直接拉取镜像。
- [root@localhost k8s-env]# docker pull 192.168.79.131:8443/test/springboot-helloworld:latest
- latest: Pulling from test/springboot-helloworld
- 7448db3b31eb: Pull complete
- c36604fa7939: Pull complete
- 29e8ef0e3340: Pull complete
- a0c934d2565d: Pull complete
- a360a17c9cab: Pull complete
- cfcc996af805: Pull complete
- 2cf014724202: Pull complete
- 4bc402a00dfe: Pull complete
- 44a7449e65ee: Pull complete
- 25a52df076b4: Pull complete
- Digest: sha256:15a44f192e1ebe7ae69b2f792ff8876d6184099aeb3eaaa75039091f378d57af
- 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

执行如下指令
kubectl create deployment springboot-helloworld --image=springboot-helloworld:latest --dry-run -o yaml > springboot-helloworld.yaml
- [root@localhost k8s-env]# kubectl create deployment springboot-helloworld --image=springboot-helloworld:latest --dry-run -o yaml > springboot-helloworld.yaml
- W0911 22:21:58.731993 14903 helpers.go:557] --dry-run is deprecated and can be replaced with --dry-run=client.
- [root@localhost k8s-env]# ll
- total 4
- -rw-r--r-- 1 root root 487 Sep 11 22:21 springboot-helloworld.yaml
查看生成的yaml文件
- [root@localhost k8s-env]# cat springboot-helloworld.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- creationTimestamp: null
- labels:
- app: springboot-helloworld
- name: springboot-helloworld
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: springboot-helloworld
- strategy: {}
- template:
- metadata:
- creationTimestamp: null
- labels:
- app: springboot-helloworld
- spec:
- containers:
- - image: springboot-helloworld:latest
- name: springboot-helloworld
- resources: {}
- status: {}

- [root@localhost k8s-env]# kubectl apply -f springboot-helloworld.yaml
- deployment.apps/springboot-helloworld created
- [root@localhost k8s-env]# kubectl get pod -A
- NAMESPACE NAME READY STATUS RESTARTS AGE
- default springboot-helloworld-b8cd9f79c-47sxr 1/1 Running 0 26s
-
-
- [root@localhost k8s-env]# kubectl get deployment
- NAME READY UP-TO-DATE AVAILABLE AGE
- springboot-helloworld 1/1 1 1 60s
-
执行如下指令暴露服务端口
kubectl expose deployment springboot-helloworld --port=8089 --type=NodePort
- [root@localhost k8s-env]# kubectl expose deployment springboot-helloworld --port=8089 --type=NodePort
- service/springboot-helloworld exposed
- [root@localhost k8s-env]# kubectl get svc
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- kubernetes ClusterIP 10.96.0.1
443/TCP 11h - springboot-helloworld NodePort 10.100.70.103
8089:30210/TCP 19s
访问服务地址:http://192.168.79.139:30210/hello
部署成功。