version: 0.1
env:
- name: ALLOW_LIST_PATH
value: $(volumeMounts:testvolume)
这里会将volume名为testvolume的卷下载到本地,放在临时目录下,然后ALLOW_LIST_PATH这个value的值,就是零时目录的路径,可以在代码中,使用
ioutil.ReadDir(ioutil.ReadDir(os.Getenv("ALLOW_LIST_PATH")))
这样的方式获取到文件夹内容,但是如果在代码中直接使用绝对路径,则会由于环境的不通,导致无法在本地使用到此卷。使用中发现,使用emptydir,下载volume都会失败,具体原因不详
volumeMounts:
- mountPath: /root/test/data
name: testvolume
volumes:
- name: testvolume
Downloading volume 'testvolume' from '/root/test/data' ...
Downloading from '/root/test/data' ...
Download failed with error: Response status code does not indicate success: 503 (Service Unavailable).
Retrying ...
Download failed with error: Response status code does not indicate success: 503 (Service Unavailable).
Retrying ...
Download failed with error: Response status code does not indicate success: 503 (Service Unavailable).
Retrying ...
Download failed with error: Response status code does not indicate success: 503 (Service Unavailable).
使用cbs就可以正常下载:
Downloading volume 'testvolume' from '/root/test/data' ...
Downloading from '/root/test/data' ...
Download completed.
ALLOW_LIST_PATH: '/var/folders/zp/y2z9789x32j77dk9gtf6cr6c0000gn/T/ed53a6a3'
ed53a6a3 ls /var/folders/zp/y2z9789x32j77dk9gtf6cr6c0000gn/T/ed53a6a3
lost+found test.txt
ed53a6a3 cat /var/folders/zp/y2z9789x32j77dk9gtf6cr6c0000gn/T/ed53a6a3/test.txt
this is a test
Service 'stats-queue' is available on 127.1.1.1:5672.
Service 'frontend' is available on 127.1.1.2:80.
Service 'todos-db' is available on 127.1.1.3:27017.
Service 'details' is available on 127.1.1.4:9081.
Service 'kibana-kibana' is available on 127.1.1.5:5601.
Service 'reviews' is available on 127.1.1.6:9081.
Service 'stats-cache' is available on 127.1.1.7:6379.
Service 'ratings' is available on 127.1.1.8:9081.
Service 'database-api' is available on 127.1.1.9:80.
Service 'stats-api' is available on 127.1.1.10:80.
➜ test git:(master) netstat -nr
Routing tables
Internet:
Destination Gateway Flags Netif Expire
default utun2 USc utun2
default 10.94.100.1 UGScI en0
10.94.100/24 link#6 UCS en0 !
10.94.100.1/32 link#6 UCS en0 !
10.94.100.1 0:0:c:9f:f0:c8 UHLWIir en0 1192
10.94.100.107/32 link#6 UCS en0 !
127 127.0.0.1 UCS lo0
127.0.0.1 127.0.0.1 UH lo0
127.1.1.1 127.1.1.1 UH lo0
127.1.1.2 127.1.1.2 UH lo0
127.1.1.3 127.1.1.3 UH lo0
127.1.1.4 127.1.1.4 UH lo0
127.1.1.5 127.1.1.5 UH lo0
127.1.1.6 127.1.1.6 UH lo0
127.1.1.7 127.1.1.7 UH lo0
127.1.1.8 127.1.1.8 UH lo0
127.1.1.9 127.1.1.9 UH lo0
127.1.1.10 127.1.1.10 UH lo0
127.1.1.87 127.0.0.1 UHW3I lo0 4
可以ping通127.1.1.1
➜ ~ ping -c 4 127.1.1.1
PING 127.1.1.67 (127.1.1.1): 56 data bytes
64 bytes from 127.1.1.1: icmp_seq=0 ttl=64 time=0.071 ms
64 bytes from 127.1.1.1: icmp_seq=1 ttl=64 time=0.132 ms
64 bytes from 127.1.1.1: icmp_seq=2 ttl=64 time=0.111 ms
64 bytes from 127.1.1.1: icmp_seq=3 ttl=64 time=0.092 ms
--- 127.1.1.67 ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.071/0.102/0.132/0.023 ms
也可以直接使用 127.1.1.3:27017,访问到此服务
➜ ~ telnet 127.1.1.3 27017
Trying 127.1.1.3...
Connected to todos-db.
Escape character is '^]'.
可以直接在程序中使用 getenv的方式获取到环境变量,不仅收集pod内部环境变量,本地环境变量也会收集,同时会将 service服务映射到本地ip和端口写入环境变量
RATINGS_SERVICE_PORT_TCP: '9081',
KIBANA_KIBANA_PORT_5601_TCP_PORT: '5601',
STATS_QUEUE_PORT_5672_TCP_ADDR: 'stats-queue',
STATS_CACHE_PORT_6379_TCP_PROTO: 'TCP',
http_proxy: 'http://127.0.0.1:12639',
USER: 'naison',
STATS_API_SERVICE_PORT: '80',
TODOS_DB_PORT_27017_TCP_PORT: '27017',
TODOS_DB_PORT: 'TCP://todos-db:27017',
FRONTEND_PORT_80_TCP_PROTO: 'TCP',
REVIEWS_SERVICE_PORT: '9081',
npm_config_globalconfig: '/usr/local/etc/npmrc',
DETAILS_SERVICE_PORT: '9081',
npm_config_prefer_online: '',
可选所有流量和特定流量
在特定流量下,有两种方式访问到本地服务链接
自动加header,使用subdomain,也就是使用生成的ingress 测试中创建出的ingress:http://naison-e4be.114.117.220.131.nip.io/ 在本地访问不到,
但在pod内部可以访问到,猜想和tke集群有关
手动加header,使用service,自己加上header即可 测试中,使用,可以被正确的路由到本地, header:
-H "kubernetes-route-as:naison-e4be"
使用命令可以访问到本地服务:
curl 172.20.0.79:80/hello -H "kubernetes-route-as:naison-e4be"
其中172.20.0.79为stats-api-envoy-routing-deploy-6c4d44fd7-hjsfx的ip,也就是网关的ip,相关配置文件:tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "bridge-to-kubernetes.resource",
"type": "bridge-to-kubernetes.resource",
"resource": "stats-api",
"resourceType": "service",
"ports": [
3001
],
"targetCluster": "cls-f94m9m3s",
"targetNamespace": "todo-app",
"useKubernetesServiceEnvironmentVariables": false,
"isolateAs": "naison-e4be"
}
]
}
通过查看对应的envoy配置信息:
kubectl exec -it pods/stats-api-envoy-routing-deploy-6c4d44fd7-hjsfx -- cat /etc/envoy/envoy.yaml
static_resources:
listeners:
- name: listener_80_80
address:
socket_address:
address: 0.0.0.0
port_value: 80
listener_filters:
- name: envoy.filters.listener.http_inspector
filter_chains:
- filter_chain_match:
application_protocols:
- http/1.0
- http/1.1
- h2c
filters:
- name: envoy.http_connection_manager
typed_config:
'@type': type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
codec_type: auto
stat_prefix: listener_80_80
route_config:
name: listener_80_80_route
virtual_hosts:
- name: listener_80_80_route_default
domains:
- '*'
routes:
- match:
headers:
- name: kubernetes-route-as
exact_match: naison-e4be
prefix: /
route:
cluster: service_debug_withHeader_kubernetes-route-as_naison-e4be_80_80
- match:
prefix: /
route:
cluster: service_original_clone_80_80
http_filters:
- name: envoy.filters.http.router
clusters:
- name: service_original_clone_80_80
connect_timeout: 1.00s
type: strict_dns
load_assignment:
cluster_name: service_original_clone_80_80
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: stats-api-cloned-routing-svc.todo-app
port_value: 80
- name: service_debug_withHeader_kubernetes-route-as_naison-e4be_80_80
connect_timeout: 1.00s
type: static
load_assignment:
cluster_name: service_debug_withHeader_kubernetes-route-as_naison-e4be_80_80
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 172.20.0.237
port_value: 80
admin:
access_log_path: /tmp/admin_access.log
可以看出envoy的配置,发现这里是个网关,可以按照流量规则,定向流量
bridge to kubernetes可以做到
缺点:
可以做到
缺点:
➜ ~ kubectl get pods ratings-658dcb9dd-rxqmk -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ratings-658dcb9dd-rxqmk 1/1 Running 1 3d8h 172.20.0.51 172.30.0.7 <none> <none>
➜ ~ ping -c 4 172.20.0.51
PING 172.20.0.51 (172.20.0.51): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
--- 172.20.0.51 ping statistics ---
4 packets transmitted, 0 packets received, 100.0% packet loss
➜ ~ telnet 172.20.0.51 9080
Trying 172.20.0.51...
Connected to 172.20.0.51.
Escape character is '^]'.