啟用 kubelet 唯讀通訊埠

如果工作負載或監控工具仍依賴 kubelet 唯讀通訊埠,您可以在 Google Distributed Cloud 管理員和使用者叢集的節點上啟用該通訊埠。由於通訊埠預設為停用,您必須部署可修改 kubelet 參數的特殊權限 DaemonSet,才能啟用通訊埠。唯讀通訊埠不需要驗證或授權,因此建議改用安全的 kubelet 通訊埠 10250。

修課條件

執行下列修補指令碼前,請確認 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 上提供相同的端點,建議您遷移至該安全通訊埠。