试图在kubernetes上加入istio,但似乎我缺少一些基本知识,或者我正在做一些事情。我对kubernetes很有经验,但是istio及其虚拟服务使我有些困惑。
我创建了2个部署(helloworld-v1/helloworld-v2)。两者都有相同的镜像,唯一不同的是环境变量-输出版本:“v1”或版本:“v2”。我使用的是我写的一个testcontainer,它基本上返回我进入应用程序的 header 。名为“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",
但是,一旦我在 pods 内进行卷发(在这种情况下,仅是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属性(在服务到服务通信中我了解了这些属性,因为据我了解,它们是在请求首次通过网关进入网格时设置的),而且对我来说,余额似乎是默认的50:50 kubernetes服务的余额。
为了在服务间通信中实现相同的1:9平衡,我该怎么做?我是否必须创建第二个“内部”网关来使用服务fqdn?我错过了定义吗?从Pod内调用服务fqdn是否应该尊重虚拟服务路由?
使用的istio版本是1.0.1,使用的kubernetes版本是v1.11.1。
更新
按照建议的方式部署了sleep-pod(这次不依赖于演示 namespace 的自动注入(inject)),而是按照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"
最佳答案
找到解决方案后,先决条件之一(我忘了)是正确的路由需要命名端口:@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后,所有内容都像一个符咒
关于kubernetes - 两个Pod之间的istio路由,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52098137/