@@ -103,15 +103,23 @@ clusterip ClusterIP 10.0.170.92 <none> 80/TCP 51s
103
103
104
104
从相同集群中的一个 pod 访问这个 ` ClusterIP ` :
105
105
106
- ``` console
106
+ ``` shell
107
107
kubectl run busybox -it --image=busybox --restart=Never --rm
108
108
```
109
109
输出结果与以下结果类似:
110
110
```
111
111
Waiting for pod default/busybox to be running, status is Pending, pod ready: false
112
112
If you don't see a command prompt, try pressing enter.
113
+ ```
114
+
115
+ 然后你可以在 Pod 内运行命令:
113
116
114
- # ip addr
117
+ ``` shell
118
+ # 在终端内使用"kubectl run"执行
119
+
120
+ ip addr
121
+ ```
122
+ ```
115
123
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
116
124
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
117
125
inet 127.0.0.1/8 scope host lo
@@ -124,8 +132,15 @@ If you don't see a command prompt, try pressing enter.
124
132
valid_lft forever preferred_lft forever
125
133
inet6 fe80::188a:84ff:feb0:26a5/64 scope link
126
134
valid_lft forever preferred_lft forever
135
+ ```
136
+
137
+ 然后使用 ` wget ` 去请求本地 Web 服务器
138
+ ``` shell
139
+ # 用名为 "clusterip" 的服务的 IPv4 地址替换 "10.0.170.92"
127
140
128
- # wget -qO - 10.0.170.92
141
+ wget -qO - 10.0.170.92
142
+ ```
143
+ ```
129
144
CLIENT VALUES:
130
145
client_address=10.244.3.8
131
146
command=GET
@@ -178,17 +193,19 @@ client_address=10.240.0.3
178
193
179
194
用图表示:
180
195
181
- ```
182
- client
183
- \ ^
184
- \ \
185
- v \
186
- node 1 <--- node 2
187
- | ^ SNAT
188
- | | --->
189
- v |
190
- endpoint
191
- ```
196
+ {{< mermaid >}}
197
+ graph LR;
198
+ client(client)-->node2[ 节点 2] ;
199
+ node2-->client;
200
+ node2-. SNAT .->node1[ 节点 1] ;
201
+ node1-. SNAT .->node2;
202
+ node1-->endpoint(端点);
203
+
204
+ classDef plain fill:#ddd,stroke:#fff,stroke-width:4px,color:#000 ;
205
+ classDef k8s fill:#326ce5,stroke:#fff,stroke-width:4px,color:#fff;
206
+ class node1,node2,endpoint k8s;
207
+ class client plain;
208
+ {{</ mermaid >}}
192
209
193
210
194
211
为了防止这种情况发生,Kubernetes 提供了一个特性来保留客户端的源 IP 地址[ (点击此处查看可用特性)] ( /zh/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip ) 。设置 ` service.spec.externalTrafficPolicy ` 的值为 ` Local ` ,请求就只会被代理到本地 endpoints 而不会被转发到其它节点。这样就保留了最初的源 IP 地址。如果没有本地 endpoints,发送到这个节点的数据包将会被丢弃。这样在应用到数据包的任何包处理规则下,你都能依赖这个正确的 source-ip 使数据包通过并到达 endpoint。
@@ -229,17 +246,18 @@ client_address=104.132.1.79
229
246
230
247
用图表示:
231
248
232
- ```
233
- client
234
- ^ / \
235
- / / \
236
- / v X
237
- node 1 node 2
238
- ^ |
239
- | |
240
- | v
241
- endpoint
242
- ```
249
+ {{< mermaid >}}
250
+ graph TD;
251
+ client --> node1[ 节点 1] ;
252
+ client(client) --x node2[ 节点 2] ;
253
+ node1 --> endpoint(端点);
254
+ endpoint --> node1;
255
+
256
+ classDef plain fill:#ddd,stroke:#fff,stroke-width:4px,color:#000 ;
257
+ classDef k8s fill:#326ce5,stroke:#fff,stroke-width:4px,color:#fff;
258
+ class node1,node2,endpoint k8s;
259
+ class client plain;
260
+ {{</ mermaid >}}
243
261
244
262
245
263
@@ -285,17 +303,7 @@ client_address=10.240.0.5
285
303
286
304
用图表示:
287
305
288
- ```
289
- client
290
- |
291
- lb VIP
292
- / ^
293
- v /
294
- health check ---> node 1 node 2 <--- health check
295
- 200 <--- ^ | ---> 500
296
- | V
297
- endpoint
298
- ```
306
+ ![ Source IP with externalTrafficPolicy] ( /images/docs/sourceip-externaltrafficpolicy.svg )
299
307
300
308
301
309
你可以设置 annotation 来进行测试:
@@ -367,7 +375,7 @@ __跨平台支持__
367
375
2 . 使用一个包转发器,因此从客户端发送到负载均衡器 VIP 的请求在拥有客户端源 IP 地址的节点终止,而不被中间代理。
368
376
369
377
370
- 第一类负载均衡器必须使用一种它和后端之间约定的协议来和真实的客户端 IP 通信,例如 HTTP [ X-FORWARDED-FOR] ( https://en.wikipedia.org/wiki/X-Forwarded-For ) 头,或者 [ proxy 协议] ( http ://www.haproxy.org/download/1.5 /doc/proxy-protocol.txt) 。
378
+ 第一类负载均衡器必须使用一种它和后端之间约定的协议来和真实的客户端 IP 通信,例如 HTTP [ X-FORWARDED-FOR] ( https://en.wikipedia.org/wiki/X-Forwarded-For ) 头,或者 [ proxy 协议] ( https ://www.haproxy.org/download/1.8 /doc/proxy-protocol.txt) 。
371
379
第二类负载均衡器可以通过简单的在保存于 Service 的 ` service.spec.healthCheckNodePort ` 字段上创建一个 HTTP 健康检查点来使用上面描述的特性。
372
380
373
381
@@ -394,6 +402,4 @@ $ kubectl delete deployment source-ip-app
394
402
## {{% heading "whatsnext" %}}
395
403
396
404
397
- * 学习更多关于 [ 通过 services 连接应用] ( /zh/docs/concepts/services-networking/connect-applications-service/ )
398
- * 学习更多关于 [ 负载均衡] ( /zh/docs/user-guide/load-balancer )
399
-
405
+ * 进一步学习 [ 通过 services 连接应用] ( /zh/docs/concepts/services-networking/connect-applications-service/ )
0 commit comments