如何将我的 Nginx 实例指向另一个 pod 的 ClusterIP 服务?

Har*_*mer 0 kubernetes

我正在尝试配置我的 Kubernetes 应用程序,以便我的前端应用程序可以与我的后端通信,该后端在另一个部署上运行并通过 ClusterIP 服务公开。

目前,前端应用程序通过 Nginx 提供一些静态内容。该服务器的配置位于已安装的配置内。我已经获得了/向用户提供静态内容的路由,并且我想在服务器块中配置另一个路由以指向我的后端,但/api我不确定如何将其定向到我的 ClusterIP 服务其他部署。

完整的前端部署文件如下所示:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  nginx.conf: |

    ## To make changes to the configuration
    ## You use the kubectl rollout restart nginx command.

    events {}
    http {

      include /etc/nginx/mime.types;
      include /etc/nginx/conf.d/*.conf;
      include /etc/nginx/extra-conf.d/*.conf;

      server {

        listen 80;

        location / {
          root /usr/share/nginx/html;
          index index.html index.htm;
          try_files $uri $uri/ /index.html =404;
        }

        #location /api {
        ##
        ## Send traffic to my API, running on another Kubernetes deployment, here...
        ## }

        }
      }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: mydockerusername/ts-react
        imagePullPolicy: Always 
        ports:
          - containerPort: 80
        volumeMounts:
          - name: nginx-conf
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
            readOnly: true
      volumes:
        - name: nginx-conf
          configMap:
            name: nginx-conf
Run Code Online (Sandbox Code Playgroud)

我的后端 API 通过端口 1234 上的 ClusterIP 服务公开,如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: typeorm
spec:
  selector:
    matchLabels:
      app: typeorm # Find and manage all the apps with this label
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: typeorm # Create apps with this label
    spec:
      containers:
        - image: mydockerusername/typeorm
          imagePullPolicy: Always
          name: typeorm
          ports:
            - containerPort: 1234
          env:
            - name: ENV
              value: "production"
          envFrom:
            - secretRef:
                name: typeorm-config
--- 
apiVersion: v1
kind: Service
metadata:
  name: typeorm 
  labels:
    app: typeorm
spec:
  type: ClusterIP
  ports:
  - port: 1234
    targetPort: 1234
  selector:
    app: typeorm
Run Code Online (Sandbox Code Playgroud)

Pul*_*ick 5

您无法通过此处的 nginx 配置文件公开 ClusterIP 服务,因为 ClusterIP 服务仅在 kubernetes 内部可用。您需要一个 nginx 入口控制器和入口组件来向外界公开您的 ClusterIP 服务。

您可以使用入口组件将 ClusterIP 服务公开到 /api 路径。您的入口清单文件将如下所示。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: demo-ingress
spec:
  rules:
  - host: foo.bar.com #your server address here
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: typeorm
            port:
              number: 1234
Run Code Online (Sandbox Code Playgroud)

即使您可以只使用一个入口组件来公开您的前端和后端。但为此,您需要另一个指向该前端部署的服务。然后您的清单文件如下所示:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: simple-fanout-example
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: frontend-service
            port:
              number: 80
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: backend-service
            port:
              number: 1234


Run Code Online (Sandbox Code Playgroud)