我想構建一個訪問 kubernetes 作業節點的 python 腳本,列出網路介面并捕獲所選介面的流量(以 tcpdump 為例),然后將 pcap 檔案存盤在主節點上的某個位置。
我想知道是否可以在沒有 ssh 的情況下從我的主節點訪問作業節點?(也許直接呼叫 k8s-apiserver ?)如果 ssh 是訪問作業節點的唯一方法,我如何在不輸入作業人員密碼的情況下建立連接(用于身份驗證)。
或者也許還有另一種方法可以做到這一點?
uj5u.com熱心網友回復:
當不能選擇 SSH 時,連接節點的一種方法是啟動一些特權容器,該容器將訪問您的節點檔案系統,禁用 pid 命名空間隔離。
假設我有一些節點“infra1”
$> kubectl get nodes
infra1 Ready infra 728d v1.21.6
我可以使用:
$ kubectl debug node/infra1 -it --image=busybox
Creating debugging pod node-debugger-infra1-s46g6 with container debugger on node infra1.
If you don't see a command prompt, try pressing enter.
/ #
/ # chroot /host
root@infra1:/#
root@infra1:/# crictl ps | head -2
CONTAINER IMAGE CREATED STATE
NAME ATTEMPT POD ID
a36863efe2a2e 3fb5cabb64693 4 minutes ago Running
作為/host一個“卷”,與該除錯容器共享我的主機檔案系統。使用 chroot,您現在可以從節點運行時作業。
$ k get pods
NAME READY STATUS RESTARTS AGE
node-debugger-infra1-g5nwg 0/1 Error 0 71s
node-debugger-infra1-s46g6 1/1 Running 0 55s
在實踐中,這是通過創建一個 Pod 來完成的,如下所示:
apiVersion: v1
kind: Pod
metadata:
annotations:
kubernetes.io/psp: hostaccess
name: node-debugger-infra1-s46g6
namespace: default
spec:
containers:
- image: busybox
...
volumeMounts:
- mountPath: /host
name: host-root
...
dnsPolicy: ClusterFirst
enableServiceLinks: true
hostIPC: true
hostNetwork: true
hostPID: true
nodeName: infra1
nodeSelector:
kubernetes.io/os: linux
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Never
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- operator: Exists
volumes:
- hostPath:
path: /
type: ""
name: host-root
...
回答您關于 caliXXX 介面的后續問題。這些是特定于 calico SDN 的,盡管同樣的評論可能適用于其他實作:沒有簡單的方法可以從這些中決議 pod IP。
然而,我們可以檢查 pod 配置,找出它們使用的介面:
# crictl pods
...
b693b9ff6487c 3 hours ago Ready controller-6b78bff7d9-5b7vr metallb-system 2 (default)
# crictl inspectp b693b9ff6487c | jq '.info.cniResult'
{
"Interfaces": {
"cali488d4aeb0e6": {
"IPConfigs": null,
"Mac": "",
"Sandbox": ""
},
"eth0": {
"IPConfigs": [
{
"IP": "10.233.105.55",
"Gateway": ""
}
],
"Mac": "",
"Sandbox": ""
},
"lo": {
"IPConfigs": [
{
"IP": "127.0.0.1",
"Gateway": ""
},
{
"IP": "::1",
"Gateway": ""
}
],
"Mac": "00:00:00:00:00:00",
"Sandbox": "/var/run/netns/cni-55322514-e37a-2913-022a-9f7488df8ca5"
}
},
"DNS": [
{},
{}
],
"Routes": null
}
然后,決議給定 IP 的介面名稱,我們可以這樣:
# MATCH_IP="10\.233\.105\.55"
# crictl pods | awk '/Ready/{print $1}' | while read pod
do
crictl inspectp $pod | grep $MATCH_IP >/dev/null 2>&1 || continue
echo found pod $pod
crictl inspectp $pod \
| jq '.info.cniResult.Interfaces | with_entries(select(.key|match("cali"))) | to_entries[] | .key'
break
done
found pod b693b9ff6487c
"cali488d4aeb0e6"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/483207.html
標籤:Python 码头工人 Kubernetes kubernetes-apiserver
