在kubernetes上创建入口服务,用于将springboot [后端]连接到前端[Angular ]

icnyk63a  于 2023-01-08  发布在  Kubernetes
关注(0)|答案(1)|浏览(125)

我正在尝试创建两个入口服务,一个将前端暴露给互联网,第二个将后端连接到前端。这是在Minikube和我有ingress addon启用。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: angular-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: angular.test
    - http:
        paths:
          - path: /
            pathType: Prefix  
            backend:
              service:
                name: angular-service
                port:
                  number: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: spring-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: springboot.test
    - http:
        paths:
          - path: /exact/form-config/applicant/field?section=Additional%20Details&page-index=1&enabled-on=
            pathType: Exact
            backend:
              service:
                name: angular-service
                port:
                  number: 8080

我想使用的名称后端主机网址在我的Angular 应用程序的springboot,我希望他们连接不使用IP,因为IP是动态的。这是正确的?

4dbbbstv

4dbbbstv1#

假设您的Angular应用程序有这样一个服务:

apiVersion: v1
kind: Service
metadata:
  name: your-angular-app
  namespace: dev
spec:
  selector:
    app: your-angular-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      name: http

和Sping Boot 应用程序的服务,位于相同的名称空间中,如下所示:

apiVersion: v1
kind: Service
metadata:
  name: your-spring-app
  namespace: dev
spec:
  selector:
    app: your-spring-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      name: http

确保您的部署(或您用于在群集中实际创建应用示例的任何内容)具有匹配的标签。部署示例:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-spring-app
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: your-spring-app # <- this must match the spec.selector.app in the service

假设您的Spring应用提供所有带有/api前缀的API端点,您可以使用如下的Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: your-ingress
  namespace: dev
spec:
  rules:
    - host: your-site.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: your-angular-app
                port:
                 name: http
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: your-spring-app
                port:
                 name: http

在云环境中,您很可能需要在Ingress上添加额外的注解,如Ingress类,但是这些信息可以在云提供商的文档中找到。

相关问题