このチュートリアルでは、クラスタとアプリのセキュリティ ポスチャーを改善する方法について説明します。ご自身が、信頼性の高いサービス メッシュのモニタリングと管理に役立つ一連のツール Cloud Service Mesh を使用してオンライン ストア用のアプリを管理している組織のプラットフォーム管理者であると想定してください。メッシュとアプリを保護するのは、ご自身の責任です。
構成ミスを防止し、Cloud Service Mesh ポリシーを自動的に検証するには、Policy Controller と Config Sync を使用します。Policy Controller を使用すると、クラスタに対して完全にプログラム可能なポリシーを適用できます。Policy Controller には、メッシュのセキュリティ脆弱性とベスト プラクティスのコンプライアンスを監査するために、Cloud Service Mesh のセキュリティ バンドルと併用できる制約テンプレートのデフォルト ライブラリが付属しています。Config Sync は、中央の Kubernetes 宣言型構成ファイルを使用してクラスタの状態を継続的に調整します。Policy Controller と Config Sync を併用すると、Cloud Service Mesh ポリシーの構成に制約を継続的に適用できます。
次の図は、Cloud Service Mesh、Policy Controller、Config Sync が連携して、このチュートリアルで使用する Ingress ゲートウェイと Online Boutique サンプルアプリを管理し保護する仕組みを示しています。
目標
- Google Kubernetes Engine(GKE)クラスタを作成し、そのクラスタをフリートに登録する。
- Policy Controller、Config Sync、Cloud Service Mesh をクラスタにインストールする。
- 複数のリポジトリを同期するように Config Sync を構成する。
- Config Sync を使用して、構成ファイル、アプリ、Istio リソースをデプロイするためのベスト プラクティスを適用する。
- クラスタ構成ファイル、Online Boutique サンプルアプリ、Config Sync の Ingress ゲートウェイをデプロイする。
- Policy Controller の Cloud Service Mesh ポリシー バンドルを利用して、次のセキュリティのベスト プラクティスを適用する。
- メッシュ内のすべてのワークロードに自動サイドカー インジェクションがあることを確認する。
- メッシュ内のすべてのトラフィックを暗号化する。
- メッシュ内のすべてのワークロードについて、きめ細かなアクセス制御を保証する。
費用
このドキュメントでは、課金対象である次の Google Cloudコンポーネントを使用します。
- GKE.
- GKE Enterprise. The billing for GKE Enterprise includes billing for the Cloud Service Mesh, Config Sync, and Policy Controller.
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
このドキュメントに記載されているタスクの完了後、作成したリソースを削除すると、それ以上の請求は発生しません。詳細については、クリーンアップをご覧ください。
始める前に
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
環境を準備する
このセクションでは、Cloud Service Mesh、Policy Controller、Config Sync をインストールできるよう環境を準備します。
-
In the Google Cloud console, activate Cloud Shell.
Google Cloud CLI の最新バージョンにアップグレードします。
gcloud components update
このチュートリアルで作成したファイルを保存するには、ディレクトリを作成します。
mkdir ~/asm-acm-tutorial-dir
チュートリアルの残りの部分を簡略化するため、次の環境変数を作成します。
PROJECT_ID=PROJECT_ID gcloud config set project $PROJECT_ID CLUSTER=asm-acm-tutorial CLUSTER_ZONE=us-east4-a MEMBERSHIP=asm-acm-tutorial PROJECT_NUMBER=$(gcloud projects describe ${PROJECT_ID} --format='get(projectNumber)')
PROJECT_ID
は、このチュートリアルで使用するプロジェクト ID に置き換えます。Cloud Shell を承認するように求められたら、[承認] をクリックして操作を完了します。
このチュートリアルで必要な API を有効にします。
gcloud
gcloud services enable \ mesh.googleapis.com \ anthos.googleapis.com
Config Connector
このチュートリアルでは、Config Connector リソースを扱います。これらのリソースを使用して、[
gcloud
] タブと同じタスクを完了できます。これらのリソースを利用するには、Config Connector をインストールし、環境に最適な方法でリソースを適用します。次の
Services
マニフェストを使用します。apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1 kind: Service metadata: annotations: cnrm.cloud.google.com/deletion-policy: "abandon" cnrm.cloud.google.com/disable-dependent-services: "false" name: mesh.googleapis.com spec: resourceID: mesh.googleapis.com projectRef: external: PROJECT_ID --- apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1 kind: Service metadata: annotations: cnrm.cloud.google.com/deletion-policy: "abandon" cnrm.cloud.google.com/disable-dependent-services: "false" name: anthos.googleapis.com spec: resourceID: anthos.googleapis.com projectRef: external: PROJECT_ID
このオペレーションは、完了までに 1 分以上かかることがあります。
GKE クラスタを作成します。
gcloud
gcloud container clusters create ${CLUSTER} \ --zone ${CLUSTER_ZONE} \ --machine-type=e2-standard-4 \ --num-nodes 4 \ --workload-pool ${PROJECT_ID}.svc.id.goog \ --labels mesh_id=proj-${PROJECT_NUMBER}
Config Connector
次の
ContainerCluster
マニフェストとContainerNodePool
マニフェストを使用します。apiVersion: container.cnrm.cloud.google.com/v1beta1 kind: ContainerNodePool metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID name: asm-acm-tutorial spec: clusterRef: name: asm-acm-tutorial location: us-east4-a nodeConfig: machineType: e2-standard-4 nodeCount: 4 --- apiVersion: container.cnrm.cloud.google.com/v1beta1 kind: ContainerCluster metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID cnrm.cloud.google.com/remove-default-node-pool: "true" labels: mesh_id: proj-PROJECT_NUMBER name: asm-acm-tutorial spec: location: us-east4-a initialNodeCount: 1 workloadIdentityConfig: workloadPool: PROJECT_ID.svc.id.goog
PROJECT_NUMBER
の部分は、以前に取得したPROJECT_NUMBER
環境変数の値に置き換えます。このオペレーションが完了するまでに 5 分ほどかかります。
GKE クラスタを確実に作成するには、そのステータスを確認します。
gcloud container clusters list \ --zone ${CLUSTER_ZONE} \ --project ${PROJECT_ID}
出力は次のようになります。
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS asm-acm-tutorial us-east4-a 1.23.12-gke.100 35.186.179.30 e2-standard-4 1.23.12-gke.100 3 RUNNING
GKE クラスタに接続します。
gcloud container clusters get-credentials ${CLUSTER} \ --zone ${CLUSTER_ZONE} \ --project ${PROJECT_ID}
クラスタをフリートに登録します。
gcloud
gcloud container fleet memberships register ${MEMBERSHIP} \ --project ${PROJECT_ID} \ --gke-cluster ${CLUSTER_ZONE}/${CLUSTER} \ --enable-workload-identity
出力は次のようになります。
kubeconfig entry generated for asm-acm-tutorial. Waiting for membership to be created...done. Created a new membership [projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial] for the cluster [asm-acm-tutorial] Generating the Connect Agent manifest... Deploying the Connect Agent on cluster [asm-acm-tutorial] in namespace [gke-connect]... Deployed the Connect Agent on cluster [asm-acm-tutorial] in namespace [gke-connect]. Finished registering the cluster [asm-acm-tutorial] with the Fleet.
Config Connector
次の
GKEHubMembership
マニフェストを使用します。apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubMembership metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID name: asm-acm-tutorial spec: location: global authority: issuer: https://container.googleapis.com/v1/projects/PROJECT_ID/locations/us-east4-a/clusters/asm-acm-tutorial endpoint: gkeCluster: resourceRef: name: asm-acm-tutorial
GKE クラスタを確実に登録するには、そのステータスを確認します。
gcloud container fleet memberships list
出力は次のようになります。
NAME EXTERNAL_ID LOCATION asm-acm-tutorial 0e12258c-8831-4d81-b5c0-5e7099a468cc global
root-sync
にはRootSync
リポジトリとして、RepoSyncs
、Constraints
、ClusterRole
、RoleBindings
、およびistio-system
などの一部のシステム名前空間に含まれるリソースなど、クラスタにわたるすべての構成ファイルが含まれています。ingress-gateway
には、最初のRepoSync
として、Ingress ゲートウェイをデプロイして、このチュートリアルを通じてそれを段階的に保護するために必要なすべてのリソースが含まれています。online-boutique
には、2 番目のRepoSync
として、Online Boutique アプリをデプロイし、このチュートリアルを通じてこれらを段階的に保護するために必要なすべてのリソースが含まれています。Config Sync と Policy Controller を管理する
ConfigManagement
演算子を有効にします。gcloud
gcloud beta container fleet config-management enable
Config Connector
次の
GKEHubFeature
マニフェストを使用します。apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeature metadata: name: configmanagement spec: projectRef: external: PROJECT_ID location: global resourceID: configmanagement
フリートで Cloud Service Mesh を有効にします。
gcloud
gcloud container fleet mesh enable
Config Connector
次の
GKEHubFeature
マニフェストを使用します。apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeature metadata: name: servicemesh spec: projectRef: external: PROJECT_ID location: global resourceID: servicemesh
Cloud Service Mesh の自動管理を有効にして、マネージド Cloud Service Mesh の推奨構成を Google が適用できるようにします。
gcloud
gcloud container fleet mesh update \ --management automatic \ --memberships ${MEMBERSHIP}
Config Connector
次の
GKEHubFeatureMembership
マニフェストを使用します。apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeatureMembership metadata: name: servicemesh-membership spec: projectRef: external: PROJECT_ID location: global membershipRef: name: asm-acm-tutorial featureRef: name: servicemesh mesh: management: MANAGEMENT_AUTOMATIC
Config Sync と Policy Controller を有効にします。
gcloud
次のマニフェストを
acm-config.yaml
として~/asm-acm-tutorial-dir
ディレクトリに保存します。applySpecVersion: 1 spec: configSync: enabled: true policyDir: asm-acm-tutorial/root-sync/init secretType: none sourceFormat: unstructured syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples syncBranch: main policyController: enabled: true referentialRulesEnabled: true templateLibraryInstalled: true
Google Cloud CLI の構成フィールドの詳細については、gcloud apply spec フィールドをご覧ください。
ファイルを適用します。
gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
Config Connector
次の
GKEHubFeatureMembership
マニフェストを使用します。apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeatureMembership metadata: name: configmanagement-membership spec: projectRef: external: PROJECT_ID location: global membershipRef: name: asm-acm-tutorial featureRef: name: configmanagement configmanagement: configSync: sourceFormat: unstructured git: policyDir: asm-acm-tutorial/root-sync/init secretType: none syncBranch: main syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples policyController: enabled: true referentialRulesEnabled: true templateLibraryInstalled: true
Policy Controller と Config Sync がクラスタにインストールされています。次に、Config Sync はデフォルトの
RootSync
のすべての構成ファイルをクラスタと同期し始めます。これらの構成ファイルにより、次の主要コンポーネントがインストールされ、構成されます。Online Boutique アプリと Ingress ゲートウェイを構成する
RepoSync
オブジェクトが同期されます。Istio リソースを作成するには
RepoSync
Reconciler に追加の権限が必要であるため、これらの権限を付与するClusterRole
と 2 つのRoleBinding
オブジェクトもクラスタに適用されます。
Policy Controller と Config Sync が正常にインストールされていることを確認するには、ステータスを確認します。
gcloud beta container fleet config-management status
出力は次のようになります。
Name: asm-acm-tutorial Status: SYNCED Last_Synced_Token: 4b3384d Sync_Branch: main Last_Synced_Time: 2022-05-04T21:32:58Z Policy_Controller: INSTALLED
Status
行またはPolicy_Controller
行にPENDING
またはNOT_INSTALLED
が表示されている場合は、数分待ってからgcloud beta container fleet config-management status
を再度実行します。Cloud Service Mesh が正常にインストールされていることを確認するには、そのステータスを記述します。
gcloud container fleet mesh describe
出力は次のようになります。
createTime: '2022-09-13T23:12:56.477042921Z' membershipSpecs: projects/PROJECT_NUMBER/locations/global/memberships/asm-acm-tutorial: mesh: management: MANAGEMENT_AUTOMATIC membershipStates: projects/PROJECT_NUMBER/locations/global/memberships/asm-acm-tutorial: servicemesh: controlPlaneManagement: details: - code: REVISION_READY details: 'Ready: asm-managed' state: ACTIVE dataPlaneManagement: details: - code: OK details: Service is running. state: ACTIVE state: code: OK description: |- Revision(s) ready for use: asm-managed. All Canonical Services have been reconciled successfully. updateTime: '2022-09-14T00:19:10.571552206Z' name: projects/PROJECT_ID/locations/global/features/servicemesh resourceState: state: ACTIVE spec: {} state: state: {} updateTime: '2022-09-14T00:19:14.135113118Z'
state.code: OK
ではなくstate.code: ERROR
が表示された場合は、数分待ってからgcloud container fleet mesh describe
を再度実行します。チュートリアルに進む前に、servicemesh.controlPlaneManagement.details.code
フィールドの値がREVISION_READY
であるかを確認する必要があります。Online Boutique のサンプル アプリケーションと Ingress ゲートウェイをデプロイします。
次のコマンドは、
sed
を使用してacm-config.yaml
マニフェストを更新し、Ingress ゲートウェイとサンプルアプリのデプロイに必要なリソースを Config Sync がデプロイするようにします。sed -i "s,root-sync/init,root-sync/deployments,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
このステップが完了するまでに数分かかることがあります。
RootSync
と 2 つのRepoSyncs
の Config Sync ステータスを表示します。gcloud alpha anthos config sync repo describe
出力は次のようになります。
getting 3 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/deployments@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/ingress-gateway/deployments@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/online-boutique/deployments@main", "status": "SYNCED" } ]
status: SYNCED
ではなくstatus: RECONCILING
が表示された場合は、数分待ってからgcloud alpha anthos config sync repo describe
を再度実行します。1 つのリポジトリの情報のみを表示するには、
--sync-name
フラグと--sync-namespace
フラグを使用します。マネージド リソースの詳細を表示するには、--managed-resources
フラグを追加します。詳細については、複数のクラスタの Config Sync のステータスを表示するをご覧ください。Ingress ゲートウェイのパブリック IP アドレスがプロビジョニングされるまで待ちます。
until kubectl -n asm-ingress get svc asm-ingressgateway -o jsonpath='{.status.loadBalancer}' | grep "ingress"; do : ; done
Ingress ゲートウェイのパブリック IP アドレスを取得します。
EXTERNAL_IP=$(kubectl get svc asm-ingressgateway -n asm-ingress -o jsonpath="{.status.loadBalancer.ingress[*].ip}")
ブラウザから IP アドレスにアクセスし、Online Boutique アプリが正常にデプロイされたことを確認します。
echo http://${EXTERNAL_IP}
サイドカー プロキシ インジェクションを適用するには、制約を適用します。
次のコマンドでは、
sed
を使用してacm-config.yaml
マニフェストを更新し、Config Sync が関連リソースのデプロイを開始するようにします。sed -i "s,root-sync/deployments,root-sync/enforce-sidecar-injection,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
上記のコマンドは次のリソースをデプロイします。
メッシュ内の任意の
Namespace
に対し、特定の Cloud Service Mesh サイドカー プロキシ インジェクション ラベルを含むように要求するK8sRequiredLabels
Constraint
。メッシュ内の
Pod
が Istio プロキシ サイドカーの挿入をバイパスすることを禁止するAsmSidecarInjection
Constraint
。
RootSync
の Config Sync ステータスを表示します。gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system
出力は次のようになります。
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-sidecar-injection@main", "status": "SYNCED" } ]
status: SYNCED
ではなくstatus: RECONCILING
が表示された場合は、数分待ってからgcloud alpha anthos config sync repo describe
を再度実行します。Constraints
が作成されたかどうかを確認します。kubectl get constraints
Policy Controller がこれらの制約を評価するまでに数分かかる場合があります。
TOTAL-VIOLATIONS
列に値が表示されない場合は、もう一度kubectl get constraints
を実行します。出力は次のようになります。
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS podsidecarinjectionannotation.constraints.gatekeeper.sh/pod-sidecar-injection-annotation deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label deny 0
Namespaces
とPods
は適切に設定されているため、Constraints
には0
TOTAL-VIOLATIONS
があります。これらの
Constraints
を有効にするには、label
とannotation
のどちらも指定せずにクラスタにNamespace
を作成します。kubectl create namespace test
出力は次のようなエラーになります。
Error from server (Forbidden): admission webhook "validation.gatekeeper.sh" denied the request: [namespace-sidecar-injection-label] you must provide labels: {"istio-injection"}
トラフィックの暗号化を適用するには、制約を適用します。
次のコマンドでは、
sed
を使用してacm-config.yaml
マニフェストを更新し、Config Sync が関連リソースのデプロイを開始するようにします。sed -i "s,root-sync/enforce-sidecar-injection,root-sync/enforce-strict-mtls,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
上記のコマンドは次のリソースをデプロイします。
メッシュレベルの mTLS
PeerAuthentication
をistio-system
Namespace に適用するAsmPeerAuthnMeshStrictMtls
Constraint
。gatekeeper-system
Namespace の参照制約Config
。この参照制約により、AsmPeerAuthnMeshStrictMtls
Constraint
は、定義内の別のオブジェクトを参照できます(たとえば、istio-system
Namespace
内のPeerAuthentication
を検索します)。Istio
DestinationRules
内のすべてのホストとホストのサブセットに対する TLS の無効化を禁止するDestinationRuleTLSEnabled
Constraint
。すべての
PeerAuthentications
がSTRICT
mTLS を上書きできないように強制適用するAsmPeerAuthnStrictMtls
Constraint
。
RootSync
の Config Sync ステータスを表示します。gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system
出力は次のようになります。
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-strict-mtls@main", "status": "SYNCED" } ]
status: SYNCED
ではなくstatus: RECONCILING
が表示された場合は、数分待ってからgcloud alpha anthos config sync repo describe
を再度実行します。次のコマンドを実行して、
PeerAuthentication
違反の詳細情報を取得します。kubectl get asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls -ojsonpath='{.status.violations}' | jq
出力は次のようになります。
[ { "enforcementAction": "deny", "group": "constraints.gatekeeper.sh", "kind": "AsmPeerAuthnMeshStrictMtls", "message": "Root namespace <istio-system> does not have a strict mTLS PeerAuthentication", "name": "mesh-level-strict-mtls", "version": "v1beta1" } ]
PeerAuthentication
をistio-system
にデプロイして、問題を修正します。メッシュ内のすべてのサービスが平文トラフィックを受け入れるのを防ぐには、mTLS モードをSTRICT
に設定してメッシュ全体のPeerAuthentication
ポリシーを設定します。ポリシーをデプロイすると、コントロール プレーンが TLS 証明書を自動的にプロビジョニングし、ワークロードの相互認証を可能にします。次のコマンドでは、
sed
を使用してacm-config.yaml
マニフェストを更新し、Config Sync が関連リソースのデプロイを開始するようにします。sed -i "s,root-sync/enforce-strict-mtls,root-sync/fix-strict-mtls,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
上記のコマンドは、
istio-system
Namespace に次のSTRICT
mTLSPeerAuthentication
をデプロイします。これにより、メッシュ全体に mTLSSTRICT
が適用されます。RootSync
の Config Sync ステータスを表示します。gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system
出力は次のようになります。
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/fix-strict-mtls@main", "status": "SYNCED" } ]
status: SYNCED
ではなくstatus: RECONCILING
が表示された場合は、数分待ってからgcloud alpha anthos config sync repo describe
を再度実行します。Constraints
が作成されたかどうかを確認します。kubectl get constraints
Policy Controller がこれらの
Constraints
の評価を開始するまで、数分かかることがあります。各行のTOTAL-VIOLATIONS
列の下に値が表示されるまで、少し待ってからこのkubectl get constraints
コマンドを再度実行します。出力は次のようになります。
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS destinationruletlsenabled.constraints.gatekeeper.sh/destination-rule-tls-enabled deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnstrictmtls.constraints.gatekeeper.sh/peerauthentication-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmsidecarinjection.constraints.gatekeeper.sh/pod-sidecar-injection-annotation deny 0
きめ細かなアクセス制御を適用するには、制約を適用します。
次のコマンドでは、
sed
を使用してacm-config.yaml
マニフェストを更新し、Config Sync が関連リソースのデプロイを開始するようにします。sed -i "s,root-sync/fix-strict-mtls,root-sync/enforce-authorization-policies,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
上記のコマンドは次のリソースをデプロイします。
メッシュレベルのデフォルトの deny
AuthorizationPolicy
をistio-system
Namespace で適用するAsmAuthzPolicyDefaultDeny
Constraint
。任意の
AuthorizationPolicies
がきめ細かなソース プリンシパル(「*」以外)を定義しているAsmAuthzPolicyEnforceSourcePrincipals
Constraint
。エンドユーザーからトラフィックを受信し、そのトラフィックを Online Boutique のfrontend
アプリにリダイレクトするための例外は、asm-ingress
Namespace 内の Ingress ゲートウェイのみです。
RootSync
の Config Sync ステータスを表示します。gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system
出力は次のようになります。
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-authorization-policies@main", "status": "SYNCED" } ]
status: SYNCED
ではなくstatus: RECONCILING
が表示された場合は、数分待ってからgcloud alpha anthos config sync repo describe
を再度実行します。次のコマンドを実行して、関連する違反の詳細情報を取得します。
kubectl get asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/default-deny-authorization-policies -ojsonpath='{.status.violations}' | jq
出力は次のようになります。
[ { "enforcementAction": "deny", "group": "constraints.gatekeeper.sh", "kind": "AsmAuthzPolicyDefaultDeny", "message": "Root namespace <istio-system> does not have a default deny AuthorizationPolicy", "name": "default-deny-authorization-policies", "version": "v1beta1" } ]
AuthorizationPolicy
をistio-system
Namespace にデプロイして、問題を修正します。次のコマンドでは、
sed
を使用してacm-config.yaml
マニフェストを更新し、Config Sync が関連リソースのデプロイを開始するようにします。sed -i "s,root-sync/enforce-authorization-policies,root-sync/fix-default-deny-authorization-policy,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
上記のコマンドは、
istio-system
Namespace に次の deny-allAuthorizationPolicy
をデプロイします。RootSync
の Config Sync ステータスを表示します。gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system
出力は次のようになります。
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/fix-default-deny-authorization-policy@main", "status": "SYNCED" } ]
status: SYNCED
ではなくstatus: RECONCILING
が表示された場合は、数分待ってからgcloud alpha anthos config sync repo describe
を再度実行します。Constraints
が作成されたかどうかを確認します。kubectl get constraints
Policy Controller がこれらの
Constraints
の評価を開始するまで、数分かかることがあります。各行のTOTAL-VIOLATIONS
列の下に値が表示されるまで、少し待ってからこのkubectl get constraints
コマンドを再度実行します。出力は次のようになります。
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmsidecarinjection.constraints.gatekeeper.sh/pod-sidecar-injection-annotation deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/default-deny-authorization-policies deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS destinationruletlsenabled.constraints.gatekeeper.sh/destination-rule-tls-enabled deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnstrictmtls.constraints.gatekeeper.sh/peerauthentication-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmauthzpolicyenforcesourceprincipals.constraints.gatekeeper.sh/authz-source-principals-not-all deny 0
ブラウザから Online Boutique アプリにアクセスします。
echo http://${EXTERNAL_IP}
エラー
RBAC: access denied
が表示されます。これは、デフォルトの denyAuthorizationPolicy
がメッシュ全体に適用されていることを示します。この問題を修正するには、
asm-ingress
とonlineboutique
の Namespace に、よりきめ細かなAuthorizationPolicies
をデプロイします。次のコマンドでは、
sed
を使用してacm-config.yaml
マニフェストを更新し、Config Sync が関連リソースのデプロイを開始するようにします。sed -i "s,root-sync/fix-default-deny-authorization-policy,root-sync/deploy-authorization-policies,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
上記のコマンドは次のリソースをデプロイします。
asm-ingress
Namespace 内のAuthorizationPolicy
:onlineboutique
Namespace 内のアプリごとのAuthorizationPolicy
。cartservice
アプリの例を次に示します。AuthorizationPolicies
でprincipal
と評価されるアプリごとに一意の ID を指定するためのasm-ingress
Namespace とonlineboutique
Namespace のアプリごとのServiceAccount
。cartservice
アプリの例を次に示します。
RootSync
と 2 つのRepoSyncs
の Config Sync ステータスを表示します。gcloud alpha anthos config sync repo describe
出力は次のようになります。
getting 3 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/deploy-authorization-policies@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/ingress-gateway/authorization-policies@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/online-boutique/authorization-policies@main", "status": "SYNCED" } ]
status: SYNCED
ではなくstatus: RECONCILING
が表示された場合は、数分待ってからgcloud alpha anthos config sync repo describe
を再度実行します。1 つのリポジトリの情報のみを表示するには、
--sync-name
フラグと--sync-namespace
フラグを使用します。マネージド リソースの詳細を表示するには、--managed-resources
フラグを追加します。詳細については、複数のクラスタの Config Sync のステータスを表示するをご覧ください。ブラウザから Online Boutique アプリに再度アクセスします。
echo http://${EXTERNAL_IP}
数分待ってから、ウェブサイトが期待どおりに動作することを再度確認します。
Google Cloud コンソールで、[GKE Enterprise Security] ページに移動します。
[ポリシーの概要] には、サービスのアクセス制御(
AuthorizationPolicies
)や mTLS などのアプリケーション セキュリティのステータスが表示されます。[ポリシーの監査] をクリックして、クラスタと両方の名前空間(
asm-ingress
とonlineboutique
)のワークロード ポリシーのステータスを表示します。サービスのアクセス制御カードと mTLS ステータス カードには、概要が記載されています。
[ワークロード] リストに、各ワークロードのサービスのアクセス制御と mTLS ステータスが表示されます。
GKE クラスタの設定
このセクションでは、GKE クラスタを作成してフリートに登録します。フリートとは、複数のクラスタと他のリソースを論理的に編成するためのGoogle Cloud コンセプトです。フリートを使用することによって、マルチクラスタ機能の使用と管理、複数のシステム間での一貫したポリシーの適用が可能になります。
このセクションで作成するクラスタは、Cloud Service Mesh、Policy Controller、Config Sync をインストールするクラスタです。また、Online Boutique サンプルアプリをデプロイするクラスタでもあります。
クラスタを設定する手順は次のとおりです。
リポジトリを確認する
次のインストール セクションでは、マニフェスト acm-config.yaml
ファイルを適用します。このマニフェストは、サンプル リポジトリの asm-acm-tutorial
フォルダから同期するようにクラスタを構成します。このフォルダには、チュートリアルの残りの部分を実行するために必要なすべての構成ファイルが格納されています。
このチュートリアルを簡略化するため、sed
コマンドを使用して acm-config.yaml
を更新します。acm-config.yaml
ファイルを使用して、Config Sync はこのチュートリアルの各手順に必要なマニフェストをデプロイします。1 つのファイルを更新すると、ファイルを繰り返し操作して git
コマンドを繰り返し実行することなく、クラスタ、メッシュ、アプリケーションを保護するコンセプトとフローに注力できます。
Config Sync の複数のリポジトリを同期する機能を利用するには、次のリソースを使用します。
Policy Controller、Config Sync、マネージド Cloud Service Mesh をインストールする
クラスタを作成して登録したら、Config Sync、Policy Controller、Cloud Service Mesh をクラスタにインストールし、デフォルトの RootSync
の構成ファイルから同期するようにクラスタを構成できます。
Ingress ゲートウェイとサンプル アプリケーションをデプロイする
このセクションでは、Online Boutique サンプル アプリケーションと Ingress ゲートウェイをデプロイして、Ingress トラフィックを管理します。
ポリシーを適用してメッシュを保護する
次のセクションでは、Policy Controller を活用し、制約を作成して Cloud Service Mesh ポリシー バンドルのポリシーを適用します。
サイドカー プロキシのインジェクションを適用する
このセクションでは、ポリシーを適用して、メッシュ内のすべてのワークロードで自動サイドカー インジェクションが有効になっていることを確認します。
トラフィックの暗号化を適用する
このセクションでは、ポリシーを適用して、メッシュ内のすべてのトラフィックが暗号化されるようにします。
詳細なアクセス制御を適用する
このセクションでは、ポリシーを適用して、メッシュ内のすべてのワークロードがきめ細かなアクセス制御を行えるようにします。
GKE Enterprise のセキュリティ機能のステータスを表示する
認証ポリシーや認可ポリシーなどの GKE Enterprise セキュリティ機能のステータスは、 Google Cloud コンソールで確認できます。
これで、Policy Controller と Config Sync を使用してクラスタとメッシュを保護できました。
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。
プロジェクトを削除する
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
リソースを個別に削除する
個々のリソースを削除するには:
クラスタをフリートから登録解除します。
gcloud
gcloud container fleet memberships unregister ${CLUSTER} \ --project=${PROJECT_ID} \ --gke-cluster=${CLUSTER_ZONE}/${CLUSTER}
出力は次のようになります。
kubeconfig entry generated for asm-acm-tutorial. Waiting for membership to be deleted...done. Deleting membership CR in the cluster...done. Deleting namespace [gke-connect] in the cluster...done.
Config Connector
kubectl delete -f ~/asm-acm-tutorial-dir/fleet-membership.yaml
クラスタを削除します。
gcloud
gcloud container clusters delete ${CLUSTER} \ --zone ${CLUSTER_ZONE}
プロンプトが表示されたら、
y
キーを押します。このコマンドが完了するまでに 5 分ほどかかります。出力は次のようになります。
Deleting cluster asm-acm-tutorial...done. Deleted [https://container.googleapis.com/v1/projects/PROJECT_ID/zones/us-east4-a/clusters/asm-acm-tutorial].
Config Connector
kubectl delete -f ~/asm-acm-tutorial-dir/container-cluster.yaml
作成したファイルを削除します。
rm -r ~/asm-acm-tutorial-dir
次のステップ
- Cloud Service Mesh セキュリティのベスト プラクティスを確認する。
- Cloud Service Mesh ポリシー バンドルの詳細を確認する。
- Policy Controller の制約テンプレート ライブラリを確認する。
- Google Cloud に関するリファレンス アーキテクチャ、図、ベスト プラクティスを確認する。Cloud アーキテクチャ センターをご覧ください。