概要
このドキュメントでは、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 が適宜変更されます。
注意点
このパッチのライフサイクルは、インストールしたサードパーティ アプリと同じです。Day 2 運用として、いつでも実行できます。ただし、クラスタを再作成しても保持されない場合があります。この変更を永続化するには、この DaemonSet を Google Distributed Cloud の初期化後アクションのステップとしてデプロイします。
1 度実行すると、Kubelet 構成ファイルを変更して、再読み込みする必要があります。
kubectl delete -f patch.yamlを安全に実行して DaemonSet リソースをクリーンアップできます。Windows で実行されている Google Distributed Cloud では、このパッチはサポートされていません。
Kubernetes は、この安全でないポート
10255では認証や認可のチェックを実行しません。これを有効にすると、kubelet データが保護されず、権限のないユーザーに公開される可能性があります。kubelet は、より安全な認証済みポート10250で同じエンドポイントにサービスを提供します。その安全なポートへの移行を検討してください。