@@ -67,7 +67,7 @@ graph LR;
67
67
client([client])-- local <br> traffic .-> local_ssh[Local SSH <br> SOCKS5 proxy];
68
68
end
69
69
local_ssh[SSH <br>SOCKS5 <br> proxy]-- SSH Tunnel --\>sshd
70
-
70
+
71
71
subgraph remote[Remote server]
72
72
sshd[SSH <br> server]-- local traffic --\>service1;
73
73
end
@@ -110,86 +110,62 @@ Figure 1. SOCKS5 tutorial components
110
110
<!--
111
111
## Using ssh to create a SOCKS5 proxy
112
112
113
- This command starts a SOCKS5 proxy between your client machine and the remote server.
114
- The SOCKS5 proxy lets you connect to your cluster's API server.
113
+ The following command starts a SOCKS5 proxy between your client machine and the remote SOCKS server:
115
114
-->
116
115
## 使用 SSH 创建 SOCKS5 代理
117
116
118
- 此命令在你的客户端计算机和远程服务器之间启动一个 SOCKS5 代理。
119
- SOCKS5 代理允许你连接到集群的 API 服务器。
117
+ 下面的命令在你的客户端计算机和远程 SOCKS 服务器之间启动一个 SOCKS5 代理:
120
118
121
119
``` shell
122
120
# 运行此命令后,SSH 隧道继续在前台运行
123
121
ssh -D 1080 -q -N
[email protected]
124
122
```
125
123
126
124
<!--
125
+ The SOCKS5 proxy lets you connect to your cluster's API server based on the following configuration:
127
126
* `-D 1080`: opens a SOCKS proxy on local port :1080.
128
127
* `-q`: quiet mode. Causes most warning and diagnostic messages to be suppressed.
129
128
* `-N`: Do not execute a remote command. Useful for just forwarding ports.
130
- * `[email protected] `: the remote SSH server where the Kubernetes cluster is running.
129
+ * `[email protected] `: the remote SSH server behind which the Kubernetes cluster
130
+ is running (eg: a bastion host).
131
131
-->
132
132
* ` -D 1080 ` : 在本地端口 1080 上打开一个 SOCKS 代理。
133
133
* ` -q ` : 静音模式。导致大多数警告和诊断消息被抑制。
134
134
* ` -N ` : 不执行远程命令。仅用于转发端口。
135
- * ` [email protected] ` :运行 Kubernetes 集群的远程 SSH 服务器。
135
+ * ` [email protected] ` :运行 Kubernetes 集群的远程 SSH 服务器
(例如:堡垒主机) 。
136
136
137
137
<!--
138
138
## Client configuration
139
139
140
- To explore the Kubernetes API you'll first need to instruct your clients to send their queries through
141
- the SOCKS5 proxy we created earlier.
142
-
143
- For command-line tools, set the `https_proxy` environment variable and pass it to commands that you run.
140
+ To access the Kubernetes API server through the proxy you must instruct `kubectl` to send queries through
141
+ the `SOCKS` proxy we created earlier. Do this by either setting the appropriate environment variable,
142
+ or via the `proxy-url` attribute in the kubeconfig file. Using an environment variable:
144
143
-->
145
144
## 客户端配置
146
145
147
- 要探索 Kubernetes API,你首先需要指示你的客户端通过我们之前创建的 SOCKS5
148
- 代理发送他们的查询。
149
- 对于命令行工具,设置 ` https_proxy ` 环境变量并将其传递给你运行的命令。
146
+ 要通过代理访问 Kubernetes API 服务器,你必须指示 ` kubectl ` 通过我们之前创建的 SOCKS5
147
+ 代理发送查询。
148
+ 这可以通过设置适当的环境变量或通过 kubeconfig 文件中的 ` proxy-url ` 属性来实现。
149
+ 使用环境变量:
150
150
151
151
``` shell
152
- export https_proxy=socks5h ://localhost:1080
152
+ export HTTPS_PROXY=socks5 ://localhost:1080
153
153
```
154
154
155
155
<!--
156
- When you set the `https_proxy` variable, tools such as `curl` route HTTPS traffic through the proxy
157
- you configured. For this to work, the tool must support SOCKS5 proxying.
158
- -->
159
- 当你设置 ` https_proxy ` 变量时,` curl ` 等工具会通过你配置的代理路由 HTTPS 流量。
160
- 为此,该工具必须支持 SOCKS5 代理。
161
-
162
- {{< note >}}
163
- <!--
164
- In the URL https://localhost:6443/api, `localhost` does not refer to your local client computer.
165
- Instead, it refers to the endpoint on the remote server known as `localhost`.
166
- The `curl` tool sends the hostname from the HTTPS URL over SOCKS, and the remote server
167
- resolves that locally (to an address that belongs to its loopback interface).
156
+ To always use this setting on a specific `kubectl` context, specify the `proxy-url` attribute in the relevant
157
+ `cluster` entry within the `~/.kube/config` file. For example:
168
158
-->
169
- 在 URL https://localhost:6443/api 中,` localhost ` 不是指你的本地客户端计算机。
170
- 它指的是远程服务器上称为 ` localhost ` 的端点。
171
- ` curl ` 工具通过 SOCKS 从 HTTPS URL 发送主机名,远程服务器在本地解析(到属于其环回接口的地址)。
172
- {{< /note >}}
173
-
174
- ``` shell
175
- curl -k -v https://localhost:6443/api
176
- ```
177
-
178
- <!--
179
- To use the official Kubernetes client `kubectl` with a proxy, set the `proxy-url` element
180
- for the relevant `cluster` entry within your `~/.kube/config` file. For example:
181
- -->
182
- 要将 Kubernetes 官方客户端 ` kubectl ` 与代理一起使用,
183
- 请在 ` ~/.kube/config ` 文件中为相关的
184
- ` cluster ` 条目设置 ` proxy-url ` 元素。例如:
159
+ 要始终在特定的 ` kubectl ` 上下文中使用此设置,请在 ` ~/.kube/config ` 文件中为相关的
160
+ ` cluster ` 条目设置 ` proxy-url ` 属性。例如:
185
161
186
162
``` yaml
187
163
apiVersion : v1
188
164
clusters :
189
165
- cluster :
190
166
certificate-authority-data : LRMEMMW2 # 简化以便阅读
191
167
# “Kubernetes API”服务器,换言之,kubernetes-remote-server.example 的 IP 地址
192
- server : https://<API_SERVER_IP_ADRESS>:6443
168
+ server : https://<API_SERVER_IP_ADRESS>:6443
193
169
# 上图中的“SSH SOCKS5代理”(内置 DNS 解析)
194
170
proxy-url : socks5://localhost:1080
195
171
name : default
@@ -209,10 +185,11 @@ users:
209
185
` ` `
210
186
211
187
<!--
212
- If the tunnel is operating and you use ` kubectl` with a context that uses this cluster, you can interact with your cluster through that proxy. For example:
188
+ Once you have created the tunnel via the ssh command mentioned earlier, and defined either the environment variable or
189
+ the ` proxy-url` attribute, you can interact with your cluster through that proxy. For example:
213
190
-->
214
- 如果隧道能够正常工作,并且你调用 `kubectl` 时使用此集群的上下文 ,
215
- 则可以通过该代理与你的集群交互 。例如:
191
+ 一旦你通过前面提到的 SSH 命令创建了隧道,并定义了环境变量或 `proxy-url` 属性 ,
192
+ 你就可以通过该代理与你的集群交互 。例如:
216
193
217
194
` ` ` shell
218
195
kubectl get pods
@@ -223,6 +200,39 @@ NAMESPACE NAME READY STATUS RESTA
223
200
kube-system coredns-85cb69466-klwq8 1/1 Running 0 5m46s
224
201
` ` `
225
202
203
+ {{< note >}}
204
+ <!--
205
+ - Before `kubectl` 1.24, most `kubectl` commands worked when using a socks proxy, except `kubectl exec`.
206
+ - ` kubectl` supports both `HTTPS_PROXY` and `https_proxy` environment variables. These are used by other
207
+ programs that support SOCKS, such as `curl`. Therefore in some cases it
208
+ will be better to define the environment variable on the command line :
209
+ -->
210
+ - 在 `kubectl` 1.24 之前,大多数 `kubectl` 命令在使用 socks 代理时都有效,除了 `kubectl exec`。
211
+ - ` kubectl` 支持读取 `HTTPS_PROXY` 和 `https_proxy` 环境变量。 这些被其他支持 SOCKS 的程序使用,例如 `curl`。
212
+ 因此在某些情况下,在命令行上定义环境变量会更好:
213
+ ` ` ` shell
214
+ HTTPS_PROXY=socks5://localhost:1080 kubectl get pods
215
+ ` ` `
216
+ <!--
217
+ - When using `proxy-url`, the proxy is used only for the relevant `kubectl` context,
218
+ whereas the environment variable will affect all contexts.
219
+ -->
220
+ - 使用 `proxy-url` 时,代理仅用于相关的 `kubectl` 上下文,而环境变量将影响所有上下文。
221
+ <!--
222
+ - The k8s API server hostname can be further protected from DNS leakage by using the `socks5h` protocol name
223
+ instead of the more commonly known `socks5` protocol shown above. In this case, `kubectl` will ask the proxy server
224
+ (such as an ssh bastion) to resolve the k8s API server domain name, instead of resolving it on the system running
225
+ ` kubectl` . Note also that with `socks5h`, a k8s API server URL like `https://localhost:6443/api` does not refer
226
+ to your local client computer. Instead, it refers to `localhost` as known on the proxy server (eg the ssh bastion).
227
+ -->
228
+ - 通过使用 `socks5h` 协议名称而不是上面显示的更广为人知的 `socks5` 协议,
229
+ 可以进一步保护 k8s API 服务器主机名免受 DNS 泄漏影响。
230
+ 这种情况下,`kubectl` 将要求代理服务器(例如 SSH 堡垒机)解析 k8s API 服务器域名,
231
+ 而不是在运行 `kubectl` 的系统上进行解析。
232
+ 另外还要注意,使用 `socks5h` 时,像 `https://localhost:6443/api` 这样的 k8s API 服务器 URL 并不是指你的本地客户端计算机。
233
+ 相反,它指向的是代理服务器(例如 SSH 堡垒机)上已知的 `localhost`。
234
+ {{</ note >}}
235
+
226
236
<!--
227
237
# # Clean up
228
238
@@ -244,4 +254,3 @@ Type `unset https_proxy` in a terminal to stop forwarding http traffic through t
244
254
# # 进一步阅读
245
255
246
256
* [OpenSSH 远程登录客户端](https://man.openbsd.org/ssh)
247
-
0 commit comments