【问题标题】:How to use nginx ingress to route traffic based on port如何使用 nginx 入口根据端口路由流量
【发布时间】:2020-12-23 12:24:25
【问题描述】:

我目前正致力于在 kubernetes 集群上部署 ELK 堆栈,我成功地在 minikube 上使用 ClusterIP 服务和 nginx-ingress 将入站 http 流量路由到 kibana(5601 端口),需要输入关于如何路由流量的信息基于入站端口而不是路径?

使用下面的 Ingress 对象声明,我能够成功连接到我的 kibana 部署,但是如何访问暴露在不同端口(9200、5044、9600)上的其他工具堆栈?

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: ingress-service
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: kibana-service
          servicePort: 5601

CUrl'ing minikube ip 在默认 80 端口返回有效响应

# curl http://<minikube-ip>/api/status
{"name":"kibana",....}

注意:我不想使用 NodePort,但想知道 nodeport 是否是我们实现上述目标的唯一方法?

【问题讨论】:

  • 您可以尝试使用multiple paths。我会在一段时间内准备答案并附上描述。

标签: kubernetes-ingress minikube


【解决方案1】:

因为您已经启用了 minikubeminikube ingress addon

$ minikube addons list | grep ingress
| ingress                     | minikube | enabled ✅   |
| ingress-dns                 | minikube | enabled ✅   |

提醒一下:

targetPort: 是容器接受流量的端口(应用程序在 pod 内运行的端口)。

port: 是抽象的Service port,可以是其他pod用来访问Service的任何端口。

请记住,如果您的容器不会监听targetPort 中指定的端口,您将无法连接到 pod。 还要记住防火墙配置以允许流量。

例如我用过这个yamls:

apiVersion: v1
kind: Service
metadata:
  name: service-one
spec:
  selector:
    key: application-1
  ports:
    - port: 81
      targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-1
spec:
  replicas: 1
  selector:
    matchLabels:
      key: application-1
  template:
    metadata:
      labels:
        key: application-1
    spec:
      containers:
      - name: hello1
        image: gcr.io/google-samples/hello-app:1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: service-two
spec:
  selector:
    key: application-2
  ports:
    - port: 82
      targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-2
spec:
  replicas: 1
  selector:
    matchLabels:
      key: application-2
  template:
    metadata:
      labels:
        key: application-2
    spec:
      containers:
      - name: hello2
        image: gcr.io/google-samples/hello-app:2.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - http:
      paths:
      - path: /hello
        backend:
          serviceName: service-one
          servicePort: 81
      - path: /hello2
        backend:
          serviceName: service-two
          servicePort: 82

service/service-one created
deployment.apps/deployment-1 created
service/service-two created
deployment.apps/deployment-2 created
Warning: networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.networking.k8s.io/ingress created

警告:networking.k8s.io/v1beta1 Ingress 在 v1.19+ 中已弃用,在 v1.22+ 中不可用;使用networking.k8s.io/v1 Ingress

请记住,根据上述警告,Minikube 很快就会更改 apiVersion

下面这个配置的输出:

$ curl http://172.17.0.3/hello
Hello, world!
Version: 1.0.0
Hostname: deployment-1-77ddb77d56-2l4cp
minikube-ubuntu18:~$ curl http://172.17.0.3/hello2
Hello, world!
Version: 2.0.0
Hostname: deployment-2-fb984955c-5dvbx

你可以使用:

      paths:
      - path: /elasticsearch
        backend:
          serviceName: elasticsearch-service
          servicePort: 100
      - path: /anotherservice
        backend:
          serviceName: another-service
          servicePort: 101

服务的样子:

  name: elasticsearch-service
  ...
  ports:
    - port: 100
      targetPort: 9200
  ---
  name: another-service
  ...
  ports:
    - port: 101
      targetPort: 5044

但是,如果您需要更高级的path 配置,您也可以使用rewrite。您也可以使用default backend 重定向到特定服务。

有关访问 Minikube 的更多信息,您可以在 Minikube documentation 找到。

这是您正在寻找的东西还是不同的东西?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2021-08-11
    • 2022-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多