Kubernates之ingress方式部署springboot

    部署一个简单的springboot服务,有个restful接口,如下:

List-1

mjduan@mjduan-host:/opt/software/tool/virtualbox-vagrant/master$ curl http://192.168.33.10:30002/person/1
{"id":1,"name":"name=1","address":"address"}
mjduan@mjduan-host:/opt/software/tool/virtualbox-vagrant/master$ 

1、NodePort方式暴露端口

    我们将这个springboot部署到k8s上,制作镜像就不描述了,k8s的yaml文件如下,之后执行"kubectl  apply  -f  k8s-demo.yaml",就可以了。

List-1.1  k8s-demo.yaml

apiVersion: v1
kind: Service
metadata:
  name: k8s-learn-service
  namespace: default
  labels:
    app: k8s-learn-service
spec:
  type: NodePort
  ports:
  - port: 8080
    nodePort: 30002
  selector:
    app: k8s-learn-service

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-learn-deployment
  labels:
    app: k8s-learn-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8s-learn-service
  template:
    metadata:
      labels:
        app: k8s-learn-service
    spec:
      containers:
      - name: k8s-learn-container1
        image: k8s-learn:1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        volumeMounts: 
        - mountPath : /opt/applog
          name: my-volume
      volumes:
      - name: my-volume
        emptyDir: {}

2、ClusterIP方式,用Ingress暴露端口

    NodePort方式,是直接暴露端口,这种方式不是很好。ClusterIP方式,集群外是访问不了的,我们用Ingress方式来暴露端口,如下:

List-2.1 k8s-demo.yaml

apiVersion: v1
kind: Service
metadata:
  name: k8s-learn-service
  namespace: default
  labels:
    app: k8s-learn-service
spec:
  type: ClusterIP
  ports:
  - port: 8080
  selector:
    app: k8s-learn-service

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-learn-deployment
  labels:
    app: k8s-learn-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8s-learn-service
  template:
    metadata:
      labels:
        app: k8s-learn-service
    spec:
      containers:
      - name: k8s-learn-container1
        image: k8s-learn:1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        volumeMounts: 
        - mountPath : /opt/applog
          name: my-volume
      volumes:
      - name: my-volume
        emptyDir: {}

    之后执行"kubectl  apply  -f k8s-demo.yaml",之后查看该服务:

List-2.2

[root@master springboot]# kubectl get services
NAME                               TYPE           CLUSTER-IP       EXTERNAL-IP                                 PORT(S)                          AGE
...
k8s-learn-service                  ClusterIP      10.107.55.103    <none>                                      8080/TCP                         3m
...

    之后创建用于ingress,如下

List-2.3  springboot-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: apps-springboot
spec:
  rules:
  - host: springboot-ui
    http:
      paths:
      - path: /
        backend:
          serviceName: k8s-learn-service
          servicePort: 8080

    执行"kubectl  apply  -f  springboot-ingress.yaml ",查看ingress状态:

List-2.4

[root@master springboot]# kubectl get ingress
NAME               HOSTS           ADDRESS   PORTS     AGE
apps-springboot    springboot-ui             80        17m

    之后在你本地/etc/hosts中加入"k8s的任一节点IP  springboot-ui",在浏览器中访问"http://springboot-ui/person/12"

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券