必要な gcloud コンポーネントをインストールしてください。
gcloud components install kubectl alpha
このラボでは Config Sync について学びます。
Config Sync は Anthos Config Management のコンポーネントのひとつであり、GitOps の機能を Kubernetes に提供します。 Config Sync を使うと、Git リポジトリの変更を検知してリポジトリにある最新の Kubernetes のマニフェストを自動で Kubernetes クラスタに反映できます。
このラボでは Config Sync を使って GitHub のサンプルリポジトリにある Kubernetes のマニフェストを GKE クラスタに自動反映するための設定をします。
使用するプロジェクトを設定してください。
gcloud config set project YOUR-PROJECT
利用するサービスの API を有効化します。
gcloud services enable \ anthos.googleapis.com \ container.googleapis.com
Anthos Config Management を有効化します。
gcloud alpha container hub config-management enable
Config Sync を適用する GKE クラスタを作成します。
gcloud container clusters create acm-cluster \ --zone asia-northeast1-a \ --machine-type e2-standard-4 \ --workload-pool $(gcloud config get-value project).svc.id.goog
操作しているユーザーに cluster-admin
のロールを付与してクラスタを管理できるように ClusterRoleBinding を作成します。
kubectl create clusterrolebinding cluster-admin-binding \ --clusterrole cluster-admin \ --user $(gcloud config get-value account)
作成した GKE クラスタを Anthos に登録します。
gcloud beta container hub memberships register acm-cluster \ --gke-cluster asia-northeast1-a/acm-cluster \ --enable-workload-identity
gcloud コマンドを使って作成したクラスタに Config Sync を設定します。
cat <<EOF > config-management.yaml apiVersion: configmanagement.gke.io/v1 kind: ConfigManagement metadata: name: config-management namespace: config-management-system spec: sourceFormat: unstructured git: syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples syncBranch: init secretType: none policyDir: quickstart/multirepo/root EOF gcloud alpha container hub config-management apply \ --membership acm-cluster \ --config config-management.yaml
重要な項目についてそれぞれ次のような意味があります。
spec.git.syncRepo
- 同期する Git リポジトリspec.git.syncBranch
- 同期する Git ブランチspec.git.secretType
- Git リポジトリにアクセスするための認証方法 none
spec.git.policyDir
- 同期するマニフェストが格納されているディレクトリこのラボの設定では https://github.com/GoogleCloudPlatform/anthos-config-management-sample リポジトリの init
ブランチの quickstart/multirepo/root
ディレクトリの構成をクラスタに同期します。
実際に GitHub へアクセスして、どのようなマニフェストがあるか確認してみましょう。 例えば、namespace-gamestore.yamlというファイルでは gamestore
という Namespace が定義されています。
Config Sync によって GitHub 上のマニフェストがクラスタに反映されているか確認します。
まず、Config Sync のステータスを確認します。
gcloud alpha container hub config-management status
Status の列に SYNCED
と表示されていれば同期が完了しています。
次に、実際にクラスタに反映されているかどうかを確認します。 gamestore
という Namespace が存在していることを kubectl
コマンドで確認します。
kubectl get ns
次のように gamestore
Namespace が表示されたら反映が成功しています。
$ kubectl get ns NAME STATUS AGE config-management-monitoring Active 5d2h config-management-system Active 5d2h default Active 5d2h gamestore Active 5d2h gke-connect Active 5d2h kube-node-lease Active 5d2h kube-public Active 5d2h kube-system Active 5d2h monitoring Active 5d2h resource-group-system Active 5d2h
Config Sync で同期されたリソースには専用のラベルが付与されます。 ラベルによる絞り込みで Config Sync に管理されているオブジェクトのみを表示できます。
$ kubectl get clusterroles -l app.kubernetes.io/managed-by=configmanagement.gke.io NAME CREATED AT namespace-reader 2021-06-02T04:36:23Z prometheus-acm 2021-06-02T04:36:32Z prometheus-operator 2021-06-02T04:36:23Z webstore-admin 2021-06-02T04:36:33Z
Config Sync により管理されているオブジェクトは手動で変更できないようになっています。 試しに、gamestore
Namespace を削除してみます。
kubectl delete namespace gamestore
以下のようにエラーが表示されます。
$ kubectl delete namespace gamestore error: You must be logged in to the server (admission webhook "v1.admission-webhook.configsync.gke.io" denied the request: requester is not authorized to delete managed resources)
kubectl
コマンドで gamestore
Namespace が削除されていないことを確認できます。
$ kubectl get ns gamestore NAME STATUS AGE gamestore Active 5d2h
以上でこのラボは終了です。
クリーンアップする場合はプロジェクトごと削除するか、次のコマンドを実行してください。
gcloud container clusters delete acm-cluster --zone asia-northeast1-a gcloud beta container hub memberships delete acm-cluster