使用 SOCKS5 Proxy 來存取 Kubernetes API
功能狀態:
Kubernetes v1.24 [stable]
本頁說明如何使用 SOCKS5 Proxy 來存取遠端 Kubernetes 叢集的 API。當您要存取的叢集未直接在公用網際網路上公開其 API 時,這非常有用。
準備開始
您需要有一個 Kubernetes 叢集,且必須將 kubectl 命令列工具設定為與您的叢集通訊。建議在至少有兩個節點且未充當控制平面主機的叢集上執行本教學課程。如果您還沒有叢集,可以使用 minikube 建立一個,或者您可以使用這些 Kubernetes 練習場之一
您的 Kubernetes 伺服器版本必須是 v1.24 或更新版本。若要檢查版本,請輸入kubectl version
。您需要 SSH 用戶端軟體 (ssh
工具) 以及在遠端伺服器上執行的 SSH 服務。您必須能夠登入遠端伺服器上的 SSH 服務。
任務情境
注意
本範例使用 SSH 通道傳輸流量,其中 SSH 用戶端與伺服器充當 SOCKS Proxy。您可以改用任何其他種類的 SOCKS5 Proxy。圖 1 代表您在本任務中將達成的目標。
- 您有一部用戶端電腦,在接下來的步驟中稱為本機,您將從該電腦建立請求以與 Kubernetes API 通訊。
- Kubernetes 伺服器/API 託管在遠端伺服器上。
- 您將使用 SSH 用戶端與伺服器軟體在本機與遠端伺服器之間建立安全的 SOCKS5 通道。用戶端與 Kubernetes API 之間的 HTTPS 流量將透過 SOCKS5 通道流動,而 SOCKS5 通道本身又透過 SSH 建立通道。
graph LR; subgraph local[本機用戶端機器] client([用戶端])-. 本機
traffic .-> local_ssh[本機 SSH
SOCKS5 Proxy]; end local_ssh[SSH
SOCKS5
Proxy]-- SSH 通道 -->sshd subgraph remote[遠端伺服器] sshd[SSH
伺服器]-- 本機流量 -->service1; end client([用戶端])-. 透過 Proxy 的 Proxy HTTPs 流量
going through the proxy .->service1[Kubernetes API]; classDef plain fill:#ddd,stroke:#fff,stroke-width:4px,color:#000; classDef k8s fill:#326ce5,stroke:#fff,stroke-width:4px,color:#fff; classDef cluster fill:#fff,stroke:#bbb,stroke-width:2px,color:#326ce5; class ingress,service1,service2,pod1,pod2,pod3,pod4 k8s; class client plain; class cluster cluster;
traffic .-> local_ssh[本機 SSH
SOCKS5 Proxy]; end local_ssh[SSH
SOCKS5
Proxy]-- SSH 通道 -->sshd subgraph remote[遠端伺服器] sshd[SSH
伺服器]-- 本機流量 -->service1; end client([用戶端])-. 透過 Proxy 的 Proxy HTTPs 流量
going through the proxy .->service1[Kubernetes API]; classDef plain fill:#ddd,stroke:#fff,stroke-width:4px,color:#000; classDef k8s fill:#326ce5,stroke:#fff,stroke-width:4px,color:#fff; classDef cluster fill:#fff,stroke:#bbb,stroke-width:2px,color:#326ce5; class ingress,service1,service2,pod1,pod2,pod3,pod4 k8s; class client plain; class cluster cluster;
使用 ssh 建立 SOCKS5 Proxy
以下命令會在您的用戶端機器與遠端 SOCKS 伺服器之間啟動 SOCKS5 Proxy
# The SSH tunnel continues running in the foreground after you run this
ssh -D 1080 -q -N username@kubernetes-remote-server.example
SOCKS5 Proxy 可讓您根據以下組態連接到叢集的 API 伺服器
-D 1080
:在本機連接埠 :1080 上開啟 SOCKS Proxy。-q
:靜音模式。導致大多數警告與診斷訊息遭到抑制。-N
:不要執行遠端命令。適用於僅轉發連接埠。username@kubernetes-remote-server.example
:Kubernetes 叢集在其後方執行的遠端 SSH 伺服器 (例如:跳板主機)。
用戶端組態
若要透過 Proxy 存取 Kubernetes API 伺服器,您必須指示 kubectl
透過我們稍早建立的 SOCKS
Proxy 傳送查詢。透過設定適當的環境變數,或透過 kubeconfig 檔案中的 proxy-url
屬性來執行此操作。使用環境變數
export HTTPS_PROXY=socks5://localhost:1080
若要在特定的 kubectl
情境中永遠使用此設定,請在 ~/.kube/config
檔案中相關的 cluster
項目內指定 proxy-url
屬性。例如
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LRMEMMW2 # shortened for readability
server: https://<API_SERVER_IP_ADDRESS>:6443 # the "Kubernetes API" server, in other words the IP address of kubernetes-remote-server.example
proxy-url: socks5://localhost:1080 # the "SSH SOCKS5 proxy" in the diagram above
name: default
contexts:
- context:
cluster: default
user: default
name: default
current-context: default
kind: Config
preferences: {}
users:
- name: default
user:
client-certificate-data: LS0tLS1CR== # shortened for readability
client-key-data: LS0tLS1CRUdJT= # shortened for readability
一旦您透過稍早提及的 ssh 命令建立通道,並定義環境變數或 proxy-url
屬性,您就可以透過該 Proxy 與您的叢集互動。例如
kubectl get pods
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-85cb69466-klwq8 1/1 Running 0 5m46s
注意
- 在
kubectl
1.24 之前,大多數kubectl
命令在使用 socks Proxy 時都能運作,但kubectl exec
除外。 kubectl
同時支援HTTPS_PROXY
與https_proxy
環境變數。其他支援 SOCKS 的程式 (例如curl
) 也會使用這些變數。因此,在某些情況下,最好在命令列上定義環境變數HTTPS_PROXY=socks5://localhost:1080 kubectl get pods
- 使用
proxy-url
時,Proxy 僅用於相關的kubectl
情境,而環境變數將影響所有情境。 - 藉由使用
socks5h
協定名稱而不是上方顯示的更常見的socks5
協定,可以進一步保護 k8s API 伺服器主機名稱免於 DNS 洩漏。在這種情況下,kubectl
將要求 Proxy 伺服器 (例如 SSH 跳板主機) 解析 k8s API 伺服器網域名稱,而不是在本機執行kubectl
的系統上解析它。另請注意,使用socks5h
時,類似https://localhost:6443/api
的 k8s API 伺服器 URL 並非指您的本機用戶端電腦。相反地,它是指 Proxy 伺服器 (例如 SSH 跳板主機) 上已知的localhost
。
清除
在執行 ssh 連接埠轉發程序的終端機上按下 CTRL+C
來停止該程序。
在終端機中輸入 unset https_proxy
以停止透過 Proxy 轉發 http 流量。
延伸閱讀
上次修改時間為 2024年2月13日下午 2:14 PST: 修正 mermaid 語法錯誤 (69706582d4)