两个Pod之间的istio路由

时间:2018-08-30 13:19:12

标签: kubernetes istio

试图在kubernetes上进入istio,但是似乎我缺少一些基本知识,或者我正在做一些事情。我对kubernetes很有经验,但是istio及其虚拟服务使我有些困惑。

我创建了2个部署(helloworld-v1 / helloworld-v2)。两者都有相同的映像,唯一不同的是环境变量-输出版本:“ v1”或版本:“ v2”。我使用的是我编写的一个testcontainer,它基本上返回我进入应用程序的标头。名为“ helloworld”的kubernetes服务可以同时实现。

我创建了虚拟服务和Destinationrule

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: helloworld
spec:
  hosts:
  - helloworld
http:
  - route:
     - destination:
       host: helloworld
       subset: v1
     weight: 90
     - destination:
       host: helloworld
       subset: v2
     weight: 10
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: helloworld
spec:
  host: helloworld
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

根据文档未提及任何网关,应使用内部“网状”网关。 Sidecar容器已成功连接:

kubectl -n demo get all
NAME                                 READY     STATUS    RESTARTS   AGE
pod/curl-6657486bc6-w9x7d            2/2       Running   0          3h
pod/helloworld-v1-d4dbb89bd-mjw64    2/2       Running   0          6h
pod/helloworld-v2-6c86dfd5b6-ggkfk   2/2       Running   0          6h

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/helloworld   ClusterIP   10.43.184.153   <none>        80/TCP     6h

NAME                            DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/curl            1         1         1            1           3h
deployment.apps/helloworld-v1   1         1         1            1           6h
deployment.apps/helloworld-v2   1         1         1            1           6h

NAME                                       DESIRED   CURRENT   READY     AGE
replicaset.apps/curl-6657486bc6            1         1         1         3h
replicaset.apps/helloworld-v1-d4dbb89bd    1         1         1         6h
replicaset.apps/helloworld-v2-6c86dfd5b6   1         1         1         6h

当我从“外部”(istio-ingressgateway)访问应用程序时,一切工作都很好,v2被调用一次,v1被调用9次:

curl --silent -H 'host: helloworld' http://localhost
{"host":"helloworld","user-agent":"curl/7.47.0","accept":"*/*","x-forwarded-for":"10.42.0.0","x-forwarded-proto":"http","x-envoy-internal":"true","x-request-id":"a6a2d903-360f-91a0-b96e-6458d9b00c28","x-envoy-decorator-operation":"helloworld:80/*","x-b3-traceid":"e36ef1ba2229177e","x-b3-spanid":"e36ef1ba2229177e","x-b3-sampled":"1","x-istio-attributes":"Cj0KF2Rlc3RpbmF0aW9uLnNlcnZpY2UudWlkEiISIGlzdGlvOi8vZGVtby9zZXJ2aWNlcy9oZWxsb3dvcmxkCj8KGGRlc3RpbmF0aW9uLnNlcnZpY2UuaG9zdBIjEiFoZWxsb3dvcmxkLmRlbW8uc3ZjLmNsdXN0ZXIubG9jYWwKJwodZGVzdGluYXRpb24uc2VydmljZS5uYW1lc3BhY2USBhIEZGVtbwooChhkZXN0aW5hdGlvbi5zZXJ2aWNlLm5hbWUSDBIKaGVsbG93b3JsZAo6ChNkZXN0aW5hdGlvbi5zZXJ2aWNlEiMSIWhlbGxvd29ybGQuZGVtby5zdmMuY2x1c3Rlci5sb2NhbApPCgpzb3VyY2UudWlkEkESP2t1YmVybmV0ZXM6Ly9pc3Rpby1pbmdyZXNzZ2F0ZXdheS01Y2NiODc3NmRjLXRyeDhsLmlzdGlvLXN5c3RlbQ==","content-length":"0","version":"v1"}
"version": "v1",
"version": "v1",
"version": "v2",
"version": "v1",
"version": "v1",
"version": "v1",
"version": "v1",
"version": "v1",
"version": "v1",

但是,一旦我从吊舱内卷曲(在本例中为byrnedo / alpine-curl),对服务的感觉就会变得混乱:

curl --silent -H 'host: helloworld' http://helloworld.demo.svc.cluster.local
{"host":"helloworld","user-agent":"curl/7.61.0","accept":"*/*","version":"v1"}
"version":"v2"
"version":"v2"
"version":"v1"
"version":"v1"
"version":"v2"
"version":"v2"
"version":"v1"
"version":"v2“
"version":"v1"

不仅我错过了所有istio属性(在服务到服务通信中我了解了这些属性,因为据我所知,它们是在请求首次通过网关进入网格时设置的),但对我而言,余额似乎是默认值kubernetes服务的余额为50:50。

在服务间通信中要实现相同的1:9平衡,我该怎么做?我是否必须创建第二个“内部”网关来代替使用fqdn服务?我错过了定义吗?从Pod内调用服务fqdn是否应该尊重虚拟服务路由?

使用的istio版本是1.0.1,使用的kubernetes版本是v1.11.1。

更新 根据建议部署了sleep-pod(这次不依赖于演示名称空间的自动注入),而是按照sleep示例中的说明手动进行了

kubectl -n demo get deployment sleep -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS          IMAGES                                     SELECTOR
sleep     1         1         1            1           2m        
sleep,istio-proxy   tutum/curl,docker.io/istio/proxyv2:1.0.1   app=sleep

还将Virtualservice更改为0/100,以便乍一看是否有效。不幸的是,这并没有太大变化:

export SLEEP_POD=$(kubectl get -n demo pod -l app=sleep -o jsonpath={.items..metadata.name})
kubectl -n demo exec -it $SLEEP_POD -c sleep curl http://helloworld
{"user- agent":"curl/7.35.0","host":"helloworld","accept":"*/*","version":"v2"}
kubectl -n demo exec -it $SLEEP_POD -c sleep curl http://helloworld
{"user-agent":"curl/7.35.0","host":"helloworld","accept":"*/*","version":"v1"}
kubectl -n demo exec -it $SLEEP_POD -c sleep curl http://helloworld
{"user-agent":"curl/7.35.0","host":"helloworld","accept":"*/*","version":"v2"}
kubectl -n demo exec -it $SLEEP_POD -c sleep curl http://helloworld
{"user-agent":"curl/7.35.0","host":"helloworld","accept":"*/*","version":"v1"}
kubectl -n demo exec -it $SLEEP_POD -c sleep curl http://helloworld
{"user-agent":"curl/7.35.0","host":"helloworld","accept":"*/*","version":"v2"

2 个答案:

答案 0 :(得分:1)

找到了解决方案,先决条件之一(我忘了)是正确的路由需要命名端口:@see https://istio.io/docs/setup/kubernetes/spec-requirements/

错误:

spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 3000

右:

spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 3000

使用名称http后,所有内容都像魅力一样

答案 1 :(得分:0)

路由规则是在客户端评估的,因此您需要确保运行curl的Pod附加有Istio sidecar。如果它只是直接调用该服务,它就不能评估您设置的90-10规则,而只会进入默认的kube循环路由。

Istio sleep sample是一个很好的测试客户端吊舱。