【注意】最后更新于 November 5, 2018,文中内容可能已过时,请谨慎使用。
1.istio sidecar代理默认只处理集群内部的目标
先做一个测试,首先将default namespace开启sidecar代理的自动注入:
1
|
kubectl label namespace default istio-injection=enabled
|
部署sleep示例应用:
1
|
kubectl apply -f samples/sleep/sleep.yaml
|
sleep应用启动后,进入到容器内部,使用curl访问http://httpbin.org/uuid,出现404:
1
2
3
4
5
6
7
8
|
kubectl get pod
NAME READY STATUS RESTARTS AGE
sleep-7549f66447-47sls 2/2 Running 0 2m24s
kubectl exec sleep-7549f66447-47sls -c sleep -it sh
# curl -s -w "%{http_code}\n" http://httpbin.org/uuid
404
|
而在node节点上是可以访问http://httpbin.org/uuid的。
1
2
3
4
5
|
curl -s -w "%{http_code}\n" http://httpbin.org/uuid
{
"uuid": "1bd6212c-2008-40a3-976e-66d51f465f60"
}
200
|
也就是默认情况istio ServiceMesh内的Pod无法访问Kubernetes集群外部的服务。这是因为iptables将Pod的所有外出流量都透明的转发给了sidecar代理,而sidecar代理默认只处理集群内部的目标。
2.将集群外部的服务暴露给集群内的Pod
将集群外部的服务暴露给集群内的Pod,可以采用两种方式:
- 方式一:需要在Istio中定义ServiceEntry配置外部服务可被访问;
- 方式二:配置跳过Istio,让容器直接访问集群外部服务
使用方式一,外出流量仍然会经过sidecar proxy,这样可以对集群外部的服务进行流量管理,也就是可以说对于集群外部的服务也可以使用享受istio的功能。
2.1 定义ServiceEntry配置外部服务可被访问
创建一个 ServiceEntry 对象,放行外部服务httpbin.org的访问:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
cat <<EOF | kubectl apply -f -
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: httpbin-ext
spec:
hosts:
- httpbin.org
ports:
- number: 80
name: http
protocol: HTTP
resolution: DNS
location: MESH_EXTERNAL
EOF
|
1
2
3
|
ubectl get ServiceEntry
NAME AGE
httpbin-ext 12s
|
此时再进入到sleep容器内部访问httpbin.org,已经可以访问:
1
2
3
4
5
6
|
kubectl exec sleep-7549f66447-47sls -c sleep -it sh
# curl -s -w "%{http_code}\n" http://httpbin.org/uuid
{
"uuid": "65af6e8b-340f-4098-b04e-61f7723dd080"
}
200
|
httpbin.org同时也支持https访问的,接下来在sleep容器内部尝试使用https协议访问:
1
2
|
curl -s -w "%{http_code}\n" https://httpbin.org/uuid
000
|
无法在集群内部以https访问集群外部的服务,这是因为对于暴露HTTPS的服务,除了需要ServiceEntry外,还需要VirtualService:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
cat <<EOF | kubectl apply -f -
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: httpbin
spec:
hosts:
- httpbin.org
ports:
- number: 443
name: https
protocol: HTTPS
resolution: DNS
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- httpbin.org
tls:
- match:
- port: 443
sni_hosts:
- httpbin.org
route:
- destination:
host: httpbin.org
port:
number: 443
weight: 100
EOF
|
此时再次进入到sleep容器内部可以正常访问https://httpbin.org/uuid了。
2.2 直接访问集群外部服务
如果想要跳过istio直接访问外部服务,需要配置envoy sidecar不再劫持到指定ip范围向外部服务的请求。
可以通过修改ConfigMap istio-sidecar-injector
中的global.proxy.includeIPRanges
,将集群内部服务的ip地址范围给它如10.244.0.1/24
。
将对后续部署的服务起作用。
1
|
kubectl edit cm istio-sidecar-injector -n istio-system
|
参考