ノート:
- このチュートリアルではOracle Cloudへのアクセスが必要です。無料アカウントにサインアップするには、Oracle Cloud Infrastructure Free Tierの開始を参照してください。
- Oracle Cloud Infrastructure資格証明、テナンシおよびコンパートメントの値の例を使用します。演習を完了する場合は、これらの値をクラウド環境に固有の値に置き換えてください。
Kyvernoを使用したOracle Container Engine for Kubernetes Load Balancer内部のサービスの強制
イントロダクション
Oracle Cloud Infrastructure Container Engine for Kubernetes (OKE)は、完全に管理されたスケーラブルで可用性の高いサービスであり、コンテナ化されたアプリケーションをクラウドにデプロイする際に使用できます。
OKEクラスタで実行されているアプリケーションは、LoadBalancerタイプのサービスを使用して公開できます。サービスごとに、組込みのOKEcloud-controller-managerによってOracle Cloud Infrastructure (OCI) Load Balancerが作成され、OKEワーカー・ノードを含めるようにバックエンドが自動的に構成されます。OCI Load Balancer構成は注釈を使用して制御でき、その不在の場合、cloud-controller-managerは次のデフォルトを想定します:
- 内部ロード・バランサ:
false - シェイプ:
fixed - 帯域幅:
100Mbps - リスナー・プロトコル:
TCP
Kyvernoを使用すると、Kubernetes Mutating Admission Webhooksを利用して、OKEクラスタ内に作成されたLoadBalancerタイプのServicesに追加する予定のデフォルトの注釈セットを使用してポリシーを定義できます。
目的
-
KyvernoをOKEクラスタにデプロイします。
-
Kyvernoを使用して内部ロード・バランサをデフォルトで作成する方法について学習します。
前提条件
-
Oracle Cloudアカウントにサインアップまたはサインインします。
-
既存のOKEクラスタを使用するか、クイック作成フローを使用して作成します。
-
kubectlは、OKEクラスタにアクセスするためにインストールおよび構成されます。
タスク1: Kyvernoのインストール
Kyverno Webページには、KubernetesクラスタにKyvernoをインストールするための2つの方法が説明されています。
- Helmチャート
- YAML
このチュートリアルでは、Helmチャートを使用してKyvernoをインストールする方法について説明します。
タスク1.1: helmのインストール
-
使用しているオペレーティング・システムに応じて、このガイドを参照し、
helmをインストールします。 -
次のコマンドを実行して、インストールが成功したかどうかを確認します。
$ helm version version.BuildInfo{Version:"v3.8.1", GitCommit:"5cb9af4b1b271d11d7a97a71df3ac337dd94ad37", GitTreeState:"clean", GoVersion:"go1.17.5"}
タスク1.2: スタンドアロン・モードでのKyvernoのインストール
-
Kyverno Helmリポジトリを追加します。
$ helm repo add kyverno https://kyverno.github.io/kyverno/ "kyverno" has been added to your repositories -
新しいリポジトリでチャートをスキャンします。
$ helm repo update Hang tight while we grab the latest from your chart repositories... ...Successfully got an update from the "kyverno" chart repository Update Complete. ⎈Happy Helming!⎈ -
kyvernoネームスペースにKyvernoをインストールします。$ helm install kyverno kyverno/kyverno -n kyverno --create-namespace NAME: kyverno LAST DEPLOYED: Fri Jun 16 17:50:48 2023 NAMESPACE: kyverno STATUS: deployed REVISION: 1 NOTES: Chart version: 3.0.1 Kyverno version: v1.10.0 Thank you for installing kyverno! Your release is named kyverno. The following components have been installed in your cluster: - CRDs - Admission controller - Reports controller - Cleanup controller - Background controller
タスク2: ClusterPolicyを使用したサービス・デフォルトの定義
-
enforce-internal-loadbalancer.yamlという名前のファイルを作成します。 -
次のテキストをコピーしてファイルに貼り付けます。
apiVersion: kyverno.io/v1 # The `ClusterPolicy` kind applies to the resources deployed in any namespace. kind: ClusterPolicy metadata: name: mutate-oci-services spec: rules: # As part of this rule we intend to mutate Flexible Load Balancers. - name: mutate-lb-services # We look for all requests to Create or Update Kubernetes Services. match: resources: kinds: - Service operations: - CREATE - UPDATE # We exclude services with the label: "service-type: external". exclude: resources: selector: matchLabels: service-type: "external" # Out of all the services we are interested in those of type Load Balancer where the annotation oci.oraclecloud.com/load-balancer-type is not present or equal to "nlb". preconditions: all: - key: "" operator: Equals value: LoadBalancer - key: "" operator: NotEquals value: "nlb" # We mutate the request by appending the annotations required to create an internal Load Balancer. mutate: patchStrategicMerge: metadata: annotations: service.beta.kubernetes.io/oci-load-balancer-internal: "true" # service.beta.kubernetes.io/oci-load-balancer-subnet1: "ocid1.subnet.oc1...3gi7y5a" # As part of this rule we intend to mutate Network Load Balancers. - name: mutate-nlb-services # We look for all requests to Create or Update Kubernetes Services with the annotation oci.oraclecloud.com/load-balancer-type: "nlb". match: resources: kinds: - Service annotations: oci.oraclecloud.com/load-balancer-type: "nlb" operations: - CREATE - UPDATE # We exclude services with the label: "service-type: external". exclude: resources: selector: matchLabels: service-type: "external" # Out of all the services we are interested in those of type Load Balancer. preconditions: all: - key: "" operator: Equals value: LoadBalancer # We mutate the request by appending the annotations required to create an internal NetworkLoad Balancer. mutate: patchStrategicMerge: metadata: annotations: oci-network-load-balancer.oraclecloud.com/internal: "true" # oci-network-load-balancer.oraclecloud.com/subnet: "ocid1.subnet.oc1...3gi7y5a" -
ファイルを保存します。
ノート: LoadBalancerタイプのサービスについてOKEでサポートされているすべての注釈のリストは、ロード・バランサおよびネットワーク・ロード・バランサの注釈の概要を参照してください。
-
クラスタに
ClusterPolicyを作成し、次のコマンドを実行してポリシーを適用します:$ kubectl apply -f enforce-internal-loadbalancer.yaml clusterpolicy.kyverno.io/mutate-oci-services created
タスク3: テスト
-
次のテキストを含む
test-service.yamlという名前のファイルを作成します:apiVersion: v1 kind: Service metadata: name: my-nginx-svc labels: app: nginx # service-type: external annotations: oci.oraclecloud.com/load-balancer-type: "lb" spec: type: LoadBalancer ports: - port: 80 selector: app: nginx -
次のコマンドを使用して、内部LoadBalancerの作成に必要な注釈が追加されているかどうかを検証します:
$ kubectl apply -f test_service.yaml --dry-run=server -o json | jq ".metadata.annotations" { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{\"oci.oraclecloud.com/load-balancer-type\":\"lb\"},\"labels\":{\"app\":\"nginx\"},\"name\":\"my-nginx-svc\",\"namespace\":\"default\"},\"spec\":{\"ports\":[{\"port\":80}],\"selector\":{\"app\":\"nginx\"},\"type\":\"LoadBalancer\"}}\n", "oci.oraclecloud.com/load-balancer-type": "lb", "policies.kyverno.io/last-applied-patches": "mutate-lb-services.mutate-oci-services.kyverno.io: added /metadata/annotations/service.beta.kubernetes.io~1oci-load-balancer-internal\n", "service.beta.kubernetes.io/oci-load-balancer-internal": "true" } -
次のコマンドを実行して、内部ロード・バランサが正常に作成されたかどうかを検証できます:
$ kubectl apply -f test_service.yaml
タスク4: クリーン・アップ
-
テスト中に作成されたロード・バランサを削除します。
$ kubectl delete -f test_service.yaml -
次のコマンドを実行してKyvernoをアンインストールします。
$ helm uninstall kyverno -n kyverno
関連リンク
謝辞
- 著者 - Andrei Ilas (プリンシパル・クラウド・アーキテクト)
その他の学習リソース
docs.oracle.com/learnで他のラボをご覧いただくか、Oracle Learning YouTubeチャネルでより無料のラーニング・コンテンツにアクセスしてください。また、education.oracle.com/learning-explorerにアクセスして、Oracle Learning Explorerになります。
製品ドキュメントについては、Oracle Help Centerを参照してください。
Enforce internal Oracle Container Engine for Kubernetes Load Balancer services using Kyverno
F83798-01
July 2023
Copyright © 2023, Oracle and/or its affiliates.