このチュートリアルでは、クラスタとアプリのセキュリティ ポスチャーを改善する方法について説明します。ご自身が、信頼性の高いサービス メッシュのモニタリングと管理に役立つ一連のツール 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.comConfig 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}. \ --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.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 RUNNINGGKE クラスタに接続します。
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-tutorialGKE クラスタを確実に登録するには、そのステータスを確認します。
gcloud container fleet memberships list出力は次のようになります。
NAME EXTERNAL_ID LOCATION asm-acm-tutorial 0e12258c-8831-4d81-b5c0-5e7099a468cc globalroot-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 enableConfig 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 enableConfig Connector
次の
GKEHubFeatureマニフェストを使用します。apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeature metadata: name: servicemesh spec: projectRef: external: PROJECT_ID location: global resourceID: servicemeshCloud 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_AUTOMATICConfig 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: trueGoogle Cloud CLI の構成フィールドの詳細については、gcloud apply spec フィールドをご覧ください。
ファイルを適用します。
gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yamlConfig 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: truePolicy Controller と Config Sync がクラスタにインストールされています。次に、Config Sync はデフォルトの
RootSyncのすべての構成ファイルをクラスタと同期し始めます。これらの構成ファイルにより、次の主要コンポーネントがインストールされ、構成されます。Online Boutique アプリと Ingress ゲートウェイを構成する
RepoSyncオブジェクトが同期されます。Istio リソースを作成するために
RepoSyncReconciler に追加の権限が必要であるため、これらの権限を付与する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: INSTALLEDStatus行または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 : ; doneIngress ゲートウェイのパブリック 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 サイドカー プロキシ インジェクション ラベルを含むように要求するK8sRequiredLabelsConstraint。メッシュ内の
Podが Istio プロキシ サイドカーの挿入をバイパスすることを禁止するAsmSidecarInjectionConstraint。
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 constraintsPolicy 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 0NamespacesとPodsは適切に設定されているため、Constraintsには0TOTAL-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-systemNamespace に適用するAsmPeerAuthnMeshStrictMtlsConstraint。gatekeeper-systemNamespace の参照制約Config。この参照制約により、AsmPeerAuthnMeshStrictMtlsConstraintは、定義内の別のオブジェクトを参照できます(たとえば、istio-systemNamespace内のPeerAuthenticationを検索します)。Istio の
DestinationRules内のすべてのホストとホストのサブセットに対する TLS の無効化を禁止するDestinationRuleTLSEnabledConstraint。すべての
PeerAuthenticationsがSTRICTmTLS を上書きできないようにするAsmPeerAuthnStrictMtlsConstraint。
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-systemNamespace に次のSTRICTmTLSPeerAuthenticationをデプロイします。これにより、メッシュ全体に 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 constraintsPolicy 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-systemNamespace で適用するAsmAuthzPolicyDefaultDenyConstraint。きめ細かなソース プリンシパル(「*」以外)を定義することをすべての
AuthorizationPoliciesに強制するAsmAuthzPolicyEnforceSourcePrincipalsConstraint。このルールへの例外は、asm-ingressNamespace 内の Ingress ゲートウェイだけです(エンドユーザーからトラフィックを受信して Online Boutique のfrontendアプリにリダイレクトするため)。
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-systemNamespace にデプロイして、問題を修正します。次のコマンドでは、
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-systemNamespace に次の 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 constraintsPolicy 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-ingressNamespace 内のAuthorizationPolicy:onlineboutiqueNamespace 内のアプリごとのAuthorizationPolicy。cartserviceアプリの例を次に示します。AuthorizationPoliciesでprincipalとして評価されるアプリごとに一意の ID を指定するためのasm-ingressNamespace とonlineboutiqueNamespace 内のアプリごとの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 アーキテクチャ センターをご覧ください。