總覽
本文說明如何在 Google Distributed Cloud 的每個節點中部署具備權限的 DaemonSet,藉此修改 kubelet 參數,啟用唯讀埠。在 1.16 以上版本中,kubelet 唯讀埠預設為停用。
修課條件
請先確認 Google Distributed Cloud 運作正常,再執行下列修補程式指令碼。您可以使用這項解決方案,修補 1.16 以上版本的管理員叢集和使用者叢集。
建立 DaemonSet 檔案
在目前目錄中建立並儲存名為 patch.yaml 的 DaemonSet 檔案, 其中應包含以下內容:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: onprem-node-patcher
namespace: kube-system
spec:
selector:
matchLabels:
name: onprem-node-patcher
updateStrategy:
type: RollingUpdate
template:
metadata:
labels:
name: onprem-node-patcher
spec:
tolerations:
- operator: Exists
volumes:
- name: host
hostPath:
path: /
hostPID: true
initContainers:
- name: read-only-patcher
image: "ubuntu"
env:
- name: KUBELET_READONLY_PORT
value: "10255"
# Number of 1G hugepages. Update the value as desired.
command:
- /bin/bash
- -c
- |
set -xeuo pipefail
configfile="/host/var/lib/kubelet/config.yaml"
kubeletservice="/host/etc/systemd/system/kubelet.service"
# $1: The read-only port for the kubelet to serve on with no
# authentication/authorization (set to 0 to disable)
function set-readonly-port-in-config() {
[[ "$#" -eq 1 ]] || return
local readonlyport; readonlyport="$1"
local actual; actual="$(grep readOnlyPort "${configfile}")"
if [[ "${actual}" == "" ]]; then
echo "readOnlyPort: ${readonlyport}" >> "${configfile}"
else
sed -E -i 's/readOnlyPort: [0-9]+/readOnlyPort: '"${readonlyport}"'/g' ${configfile}
fi
echo "Successfully append readOnlyPort: ${readonlyport} to ${configfile}"
}
sed -E -i 's/--read-only-port=[0-9]+/--read-only-port='"${KUBELET_READONLY_PORT}"'/g' ${kubeletservice}
[[ -f ${configfile} ]] && set-readonly-port-in-config "${KUBELET_READONLY_PORT}"
echo "Restarting kubelet..."
chroot /host nsenter -a -t1 -- systemctl daemon-reload
chroot /host nsenter -a -t1 -- systemctl restart kubelet.service
echo "Success!"
volumeMounts:
- name: host
mountPath: /host
resources:
requests:
memory: 5Mi
cpu: 5m
securityContext:
privileged: true
containers:
- image: gcr.io/google-containers/pause:3.2
name: pause
# Ensures that the pods will only run on the nodes having the correct
# label.
nodeSelector:
"kubernetes.io/os": "linux"
更新唯讀通訊埠號碼
如要變更通訊埠號碼,請手動編輯 DaemonSet YAML 中的環境變數
KUBELET_READONLY_PORT。預設唯讀通訊埠為
10255,請勿選擇10250,因為這會與預先定義的安全通訊埠衝突。
修補管理員叢集
kubectl apply -f patch.yaml \
--kubeconfig ADMIN_CLUSTER_KUBECONFIG
修補使用者叢集
kubectl apply -f patch.yaml \
--kubeconfig USER_CLUSTER_KUBECONFIG
還原
如要停用唯讀埠,請在 DaemonSet YAML 中手動編輯環境變數
KUBELET_READONLY_PORT。儲存變更後,系統會重新執行 DaemonSet,並據此修改 kubelet。
注意事項
這個修補程式的生命週期與已安裝的第三方應用程式相同。您可以隨時執行這項作業,做為第 2 天的作業。但重新建立叢集後,這項設定可能不會保留。如要永久變更,請在 Google Distributed Cloud 初始化後動作中,部署這個 DaemonSet 做為步驟。
執行一次後,系統應會修改並重新載入 kubelet 設定檔。您可以安全地執行
kubectl delete -f patch.yaml,清除 DaemonSet 資源。在 Windows 上執行的 Google Distributed Cloud 不支援此修補程式。
Kubernetes 不會對這個不安全的連接埠
10255執行任何驗證或授權檢查。啟用這項功能會導致 kubelet 資料不受保護,並暴露給未經授權的使用者。kubelet 會在更安全的已驗證通訊埠10250上提供相同的端點,建議您遷移至該安全通訊埠。