【问题标题】:How to open custom port in Kubernetes如何在 Kubernetes 中打开自定义端口
【发布时间】:2020-11-28 08:52:43
【问题描述】:

我在集群上部署了 rabbit mq,目前在 15672 端口上运行良好:http://test.website.com/

但需要打开一些其他端口(25672、15672、15674)。我在 yaml 中这样定义:

apiVersion: v1
kind: Service
metadata:
  name: rabbitmq
spec:
  selector:
    name: rabbitmq
  ports:
   - port: 80
     name: http
     targetPort: 15672
     protocol: TCP
   - port: 443
     name: https
     targetPort: 15672
     protocol: TCP

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rabbitmq
spec:
  selector:
    matchLabels:
      app: rabbitmq
  strategy:
    type: RollingUpdate
  template:
    metadata:
      name: rabbitmq
    spec:
      containers:
        - name: rabbitmq
          image: rabbitmq:latest
          ports:
            - containerPort: 15672
              name: http
              protocol: TCP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: rabbitmq
spec:
  hosts:
  -  “test.website.com”
  gateways:
  - gateway
  http:
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        port:
          number: 80
        host: rabbitmq      

如何在 yaml 文件中设置打开其他端口?

【问题讨论】:

  • 不确定白名单是什么意思?您尝试过服务吗?
  • 澄清:你不能 telnet test.website.com 5672 运行 pod 吗?
  • 与您的网络或安全团队核实任何出站防火墙
  • 嗨@Rico 我刚刚更新了描述..
  • 嗨@AlexYu 我刚刚更新了描述..

标签: kubernetes deployment


【解决方案1】:

假设 Istio Gateway 正在为 TCP 网络连接提供服务,您也许可以将一个 Gateway 配置组合用于两个外部端口。

这是一个例子:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: port1
      protocol: TCP
    hosts:
    - example.myhost.com
  - port:
      number: 443
      name: port2
      protocol: TCP
    hosts:
    - example.myhost.com

hosts 字段在这里标识了一个目标地址列表,该列表必须通过此Gateway 公开。

为了对嵌套 Pod 进行适当的网络路由,请指定 VirtualService 以及端口的匹配集:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: rabbitmq-virtual-service
spec:
  hosts:
  - example.myhost.com 
  gateways:
  - gateway
  tcp:
  - match:
    - port: 80
    route:
    - destination:
        host: app.example.svc.cluster.local
        port:
          number: 15672
  - match:
    - port: 443
    route:
    - destination:
        host: app.example.svc.cluster.local
        port:
          number: 15674

VirtualService 以上定义了将来自 test.website.com 的 80 和 443 端口的网络流量分别路由到 rabbitmq 服务端口 15672、15674 的规则。

您可以根据需要调整这些文件以打开其他一些端口。

看看:virtualservice-for-a-service-which-exposes-multiple-ports

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 2016-10-06
    • 2017-10-11
    • 1970-01-01
    • 2019-08-07
    • 2016-10-20
    • 2019-12-06
    • 2011-03-25
    • 1970-01-01
    相关资源
    最近更新 更多