Desativar porta somente leitura do Kubelet

Visão geral

Neste documento, mostramos como implantar um daemonset com privilégios em cada nó do GKE no VMware para modificar os parâmetros do kubelet e desativar as portas somente leitura.

Pré-requisitos

Verifique se o GKE no VMware está íntegro antes de executar o script de patch a seguir. Essa solução pode ser usada para aplicar patch a clusters de administrador e de usuário. Ele deve funcionar em todas as versões do GKE no VMware.

Salve o seguinte YAML do Daemonset no arquivo local (por exemplo, patch.yaml)

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: "0"
        # 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: 0/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"

Aplicar patch no cluster de administrador

   kubectl apply -f patch.yaml \
    --kubeconfig ADMIN_CLUSTER_KUBECONFIG

Aplicar patch no cluster de usuário

   kubectl apply -f patch.yaml \
    --kubeconfig USER_CLUSTER_KUBECONFIG

Restaurar

  • Para reativar a porta somente leitura, edite manualmente a variável de ambiente KUBELET_READONLY_PORT no YAML do Daemonset.

  • Depois de salvar as alterações, o daemonset será executado novamente para modificar o kubelet de acordo.

Advertências

  • Esse patch tem o mesmo ciclo de vida dos apps de terceiros instalados. É possível executá-lo a qualquer momento como uma operação de segundo dia. No entanto, talvez ele não persista depois que você recriar o cluster. Para tornar essa mudança persistente, implante este daemonset como uma etapa na ação de pós-inicialização do GKE no VMware.

  • Depois de executado uma vez, o arquivo de configuração do Kubelet deve ser modificado e recarregado. É possível executar kubectl delete -f patch.yaml com segurança para limpar recursos do daemonset.

  • No momento, o Anthos em execução no Windows não é compatível com esse patch.

  • As métricas a seguir são perdidas para versões de cluster 1.13 e anteriores: