同一istio-proxy部署的不同端口上的多个网关无法正常工作

时间:2019-02-11 13:31:31

标签: kubernetes istio envoyproxy

1。安装Istio(1.0.2)

kubectl apply -f https://raw.githubusercontent.com/knative/serving/master/third_party/istio-1.0.2/istio-crds.yaml
kubectl apply -f https://raw.githubusercontent.com/knative/serving/master/third_party/istio-1.0.2/istio.yaml

2。创建两个Deployment及其对应的Service

# Create Deployments
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-a
spec:
  selector:
    matchLabels:
      app: a
  replicas: 3
  template:
    metadata:
      labels:
        app: a
    spec:
      containers:
        - name: print
          image: gcr.io/invertible-lens-220304/print
          env:
          - name: TEXT_TO_PRINT
            value: "AAAAAAAAAAAAAAAAAAAA"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-b
spec:
  selector:
    matchLabels:
      app: b
  replicas: 3
  template:
    metadata:
      labels:
        app: b
    spec:
      containers:
        - name: print
          image: gcr.io/invertible-lens-220304/print
          env:
          - name: TEXT_TO_PRINT
            value: "BBBBBBBBBBBBBBBBBBBB"
EOF

# Create Services
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
  name: service-a
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: a
---
apiVersion: v1
kind: Service
metadata:
  name: service-b
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: b
EOF

3。创建两个Gateway及其对应的NodePorts

# Create gateways
cat <<EOF | kubectl apply -f -
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: gateway-a
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 3001
        name: http
        protocol: HTTP
      hosts:
        - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: gateway-b
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 3002
        name: http
        protocol: HTTP
      hosts:
        - "*"
EOF

# Add ports to the istio-ingressgateway service
kubectl patch service istio-ingressgateway -n istio-system --type json --patch "$(cat <<EOF
  [{
    "op" : "add" ,
    "path" : "/spec/ports/-" ,
    "value" : {
      "name" : "node-port-1",
      "nodePort" : 30001,
      "port": 3001,
      "protocol": "TCP",
      "targetPort": 3001
    }
  }, {
    "op" : "add" ,
    "path" : "/spec/ports/-" ,
    "value" : {
      "name" : "node-port-2",
      "nodePort" : 30002,
      "port": 3002,
      "protocol": "TCP",
      "targetPort": 3002
    }
  }]
EOF)"

4。为每个VirtualService

创建Gateway
cat <<EOF | kubectl apply -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: virtualservice-a
  namespace: istio-system
spec:
  gateways:
    - gateway-a
  hosts:
    - example.com
  http:
    - match:
        - authority:
            exact: example.com
      route:
        - destination:
            host: service-a.default.svc.cluster.local
            port:
              number: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: virtualservice-b
  namespace: istio-system
spec:
  gateways:
    - gateway-b
  hosts:
    - example.com
  http:
    - match:
        - authority:
            exact: example.com
      route:
        - destination:
            host: service-b.default.svc.cluster.local
            port:
              number: 80
EOF

5。测试

NODE_IP=$(kubectl get nodes -o jsonpath="{.items[0].status.addresses[1].address}")

curl $NODE_IP:30001 -H "Host: example.com"
# Result: AAAAAAAAAAAAAAAAAAAA
curl $NODE_IP:30002 -H "Host: example.com"
# Result: AAAAAAAAAAAAAAAAAAAA

kubectl delete virtualservice virtualservice-a -n istio-system

curl $NODE_IP:30001 -H "Host: example.com"
# Result: BBBBBBBBBBBBBBBBBBBB
curl $NODE_IP:30002 -H "Host: example.com"
# Result: BBBBBBBBBBBBBBBBBBBB

由于30001绑定到gateway-a指向virtualservice-a的{​​{1}},并且service-a绑定到30002具有{{ 1}}指向gateway-b,我希望结果是:

virtualservice-b

发生了什么事,如何使它正常工作?

P.S。

  1. 我知道有两个service-b部署是可能的,但仅一个部署我就能达到相同的结果吗?
  2. 另一种可能性是只使用一个curl $NODE_IP:30001 -H "Host: example.com" # Result: AAAAAAAAAAAAAAAAAAAA curl $NODE_IP:30002 -H "Host: example.com" # Result: BBBBBBBBBBBBBBBBBBBB 和一个istio-ingressgateway,然后在VirtualService中设置Gateway,但这仍然不是我想要的。
  3. 是的,我希望主机名相同。网关不是彼此隔离吗?

我要在同一个port (docker.io/istio/proxyv2:1.0.2)部署的不同端口上具有两个网关,并分别具有它们自己的虚拟服务,而不互相影响。有可能吗?

0 个答案:

没有答案