【问题标题】:How to expose redis to outside with istio sidecar?如何使用 istio sidecar 将 redis 暴露在外部?
【发布时间】:2020-01-24 00:37:13
【问题描述】:

我正在使用带有 k8s 1.15.0、istio 1.4.3 的 redis,它在网络内部运行良好。

但是,当我尝试使用 istio 网关和 sidecar 将其暴露给外部网络时,它失败了。

然后我移除了 istio sidecar,刚刚在 k8s 中启动了 redis 服务器,它工作了。

搜索后,我将 DestinationRule 添加到配置中,但没有帮助。

那么它有什么问题呢?感谢您的任何提示!

这是我的redis.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: docker.io/redis:5.0.5-alpine
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 16379
          protocol: TCP
          name: redis-port
        volumeMounts:
        - name: redis-data
          mountPath: /data
        - name: redis-conf
          mountPath: /etc/redis
        command:
          - "redis-server"
        args:
          - "/etc/redis/redis.conf"
          - "--protected-mode"
          - "no"
      volumes:
        - name: redis-conf
          configMap:
            name: redis-conf
            items:
              - key: redis.conf
                path: redis.conf
        - name: redis-data
          nfs:
            path: /data/redis
            server: 172.16.8.34

---
apiVersion: v1
kind: Service
metadata:
  name: redis-svc
  labels:
    app: redis-svc
spec:
  type: ClusterIP
  ports:
  - name: redis-port
    port: 16379
    protocol: TCP
  selector:
    app: redis

---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: redis-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: tcp
      protocol: TCP
    hosts:
    - "redis.basic.svc.cluster.local"

---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: redis-svc
spec:
  host: redis-svc

---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: redis-vs
spec:
  hosts:
  - "redis.basic.svc.cluster.local"
  gateways:
  - redis-gateway
  tcp:
  - route:
    - destination:
        host: redis-svc.basic.svc.cluster.local
        port:
          number: 16379

更新:

这就是我的要求

[root]# redis-cli -h redis.basic.svc.cluster.local -p 80
redis.basic.svc.cluster.local:80> get Test
Error: Protocol error, got "H" as reply type byte

【问题讨论】:

  • 您是否启用了 mTLS?
  • 你是如何提出请求的?
  • @suren 更新到问题

标签: kubernetes redis istio


【解决方案1】:

在使用 istio 暴露 TCP 应用程序的情况下,几乎没有什么需要改变的。

  1. hosts: 必须是 "*",因为 TCP 协议仅适用于 IP:PORT。 L4 中没有标题。

  2. 需要有TCP 端口match 你的VirtualService 匹配GateWay。我建议以独特的方式命名它并匹配Deployment端口名称。

  3. 我建议避免使用端口80,因为它已在默认入口配置中使用,并且可能导致端口冲突,因此我将其更改为11337

所以你的GateWay 应该是这样的:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: redis-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 11337
      name: redis-port
      protocol: TCP
    hosts:
    - "*"

你的VirtualService 是这样的:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: redis-vs
spec:
  hosts:
  - "*"
  gateways:
  - redis-gateway
  tcp:
  - match:
    - port: 11337
    route:
      - destination:
          host: redis-svc
          port:
            number: 16379

请注意,为了清楚起见,我删除了命名空间。

然后使用以下命令将我们的自定义端口添加到默认入口网关:

kubectl edit svc istio-ingressgateway -n istio-system

并添加以下其他端口定义:

- name: redis-port
  nodePort: 31402
  port: 11337
  protocol: TCP
  targetPort: 16379

要访问暴露的应用程序,请使用我们的 istio 网关外部 IP 和端口 刚刚设置好了。

要获取您的网关外部 IP,您可以使用:

export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
redis-cli -h $INGRESS_HOST -p 11337

如果您的istio-ingressgateway 没有分配外部IP,请使用您的节点IP 地址和端口31402 之一。

希望这会有所帮助。

【讨论】:

  • 我已经编辑了我的答案。将端口更改为默认 LB 端口范围 80-15443。
【解决方案2】:

感谢suren的回答。

但我认为 redis.basic.svc.cluster.local 位于 DNS 主机之外以与 VirtualService 匹配,而 VirtualService.host 是具有完整命名空间路径的服务 redis-svc 的路由。

也许不是因为这个原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多