在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 前言 经过《Maven一键部署Springboot到Docker仓库,为自动化做准备》, 2 一键部署Springboot 2.1 准备yaml文件 当准备好镜像文件后,要部署到 apiVersion: apps/v1 kind: Deployment metadata: name: pkslow-springboot-deployment spec: selector: matchLabels: app: springboot replicas: 2 template: metadata: labels: app: springboot spec: containers: - name: springboot image: pkslow/springboot-mongo:0.0.6 ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: labels: app: springboot name: pkslow-springboot-service spec: ports: - port: 8080 name: springboot-service protocol: TCP targetPort: 8080 nodePort: 30080 selector: app: springboot type: NodePort
2.2 通过kubectl命令部署 当 $ kubectl create -f pksow-springboot.yaml deployment.apps/pkslow-springboot-deployment created service/pkslow-springboot-service created 看控制台日志则表明成功创建了 访问 通过命令行检查一下: $ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE pkslow-springboot-deployment 2/2 2 2 8m2s $ kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10m pkslow-springboot-service NodePort 10.102.218.119 <none> 8080:30080/TCP 8m7s $ kubectl get pod NAME READY STATUS RESTARTS AGE pkslow-springboot-deployment-68dffc6795-874tp 1/1 Running 0 8m15s pkslow-springboot-deployment-68dffc6795-89xww 1/1 Running 0 8m15s 至此,我们已经成功把 2.3 试试杀死一个pod?
我们试着删掉一个 $ kubectl delete pod pkslow-springboot-deployment-68dffc6795-89xww pod "pkslow-springboot-deployment-68dffc6795-89xww" deleted $ kubectl get pod NAME READY STATUS RESTARTS AGE pkslow-springboot-deployment-68dffc6795-874tp 1/1 Running 0 13m pkslow-springboot-deployment-68dffc6795-gpw67 1/1 Running 0 46s 可以发现,删除了其它一个 2.4 试试杀死一个容器? 我们再来探索一下如果杀死一个容器实例,会有什么反应。 $ docker ps $ docker rm -f 57869688a226 57869688a226 $ docker ps 经实验,杀死一个容器后,也会自动为我们重新生成一个容器实例。而 2.5 快速扩容Pod 用户请求突增,服务要撑不住了,这时需要增加 $ kubectl apply -f pksow-springboot.yaml 查看 3 一键部署Nginx 如果大家没有 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: nginx replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.19.0 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: labels: app: nginx name: nginx-service spec: ports: - port: 80 name: nginx-service1 protocol: TCP targetPort: 80 nodePort: 30000 - port: 81 name: nginx-service2 protocol: TCP targetPort: 80 nodePort: 30001 selector: app: nginx type: NodePort 执行部署命令: $ kubectl apply -f nginx-deployment-scale.yaml deployment.apps/nginx-deployment created service/nginx-service created 查看 访问服务:http://localhost:30000/ 或 http://localhost:30001 。因为我们设置了两个。 4 总结 本文以两个实例带大家感性地认识了 到此这篇关于用Kubernetes部署Springboot或Nginx的文章就介绍到这了,更多相关Kubernetes部署Springboot或Nginx内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论