Distributed Cloud Container Platform for Kubernetes (ACK One) のApplication Load Balancer (ALB) マルチクラスターゲートウェイを使用して、トラフィックをgRPCサービスにルーティングできます。 このトピックでは、ALBマルチクラスタゲートウェイを使用してトラフィックをgRPCサービスにルーティングする方法について説明します。
前提条件
ALBが活性化される。
フリート管理機能が有効になっています。 詳細については、「マルチクラスター管理の有効化」をご参照ください。
ACK Oneフリートインスタンスは、ACK Oneフリートインスタンスと同じ仮想プライベートクラウド (VPC) にデプロイされている2つのACKクラスターに関連付けられています。 詳細については、「関連クラスターの管理」をご参照ください。
FleetインスタンスのkubeconfigファイルはDistributed Cloud Container Platform for Kubernetes (ACK One) コンソールで取得され、kubectlクライアントはFleetインスタンスに接続されています。
grpccurlがインストールされています。
信頼できる証明書が取得されます。 次のいずれかの方法で証明書を取得できます。
ステップ1: 自己署名CA証明書を生成する
証明書を所有していない場合は、自己署名証明書を生成できます。 この例では、grpc.example.com
ドメイン名は証明書にバインドされています。 ALBインスタンスの証明書を設定する方法の詳細については、「暗号化通信用のHTTPS証明書の設定」をご参照ください。 この例では、シークレットを使用してALBインスタンスの証明書を設定します。.
/tmp/openssl.cn f
という名前のファイルを作成し、次の内容をファイルにコピーします。[ req ] #default_bits = 2048 #default_md = sha256 #default_keyfile = privkey.pem distinguished_name = req_distinguished_name attributes = req_attributes req_extensions = v3_req [ req_distinguished_name ] countryName = Country Name (2 letter code) countryName_min = 2 countryName_max = 2 stateOrProvinceName = State or Province Name (full name) localityName = Locality Name (eg, city) 0.organizationName = Organization Name (eg, company) organizationalUnitName = Organizational Unit Name (eg, section) commonName = Common Name (eg, fully qualified host name) commonName_max = 64 emailAddress = Email Address emailAddress_max = 64 [ req_attributes ] challengePassword = A challenge password challengePassword_min = 4 challengePassword_max = 20 [v3_req] # Extensions to add to a certificate request basicConstraints = CA:FALSE keyUsage = nonRepudiation, digitalSignature, keyEncipherment subjectAltName = @alt_names [alt_names] DNS.1 = grpc.example.com
次のコマンドを実行して、証明書署名リクエストを生成します。
openssl req -new -nodes -keyout grpc.key -out grpc.csr -config /tmp/openssl.cnf -subj "/C=CN/ST=Zhejiang/L=Hangzhou/O=AlibabaCloud/OU=ContainerService/CN=grpc.example.com"
次のコマンドを実行して証明書に署名します。
openssl x509 -req -days 3650 -in grpc.csr -signkey grpc.key -out grpc.crt -extensions v3_req -extfile /tmp/openssl.cnf
コマンドの実行後、証明書ファイル
grpc.crt
と秘密鍵ファイルgrpc.key
が生成されます。次のコマンドを実行して、
gateway-demo
名前空間にgrpc-Secret
という名前のシークレットを作成します。 シークレットは、証明書を格納するために使用されます。kubectl create secret tls grpc-secret --key grpc.key --cert grpc.crt -ngateway-demo # Replace grpc.key with the actual name of your certificate file and grpc.crt with the actual name of your private key file.
手順2: 複数のクラスターにgRPCサービスをデプロイする
ACK Oneを使用すると、GitOpsとアプリケーション配布機能を使用して、アプリケーションを複数のクラスターにデプロイできます。 詳細については、「GitOpsの使用を開始する」、「マルチクラスタアプリケーションの作成」、および「アプリケーションの配布を開始する」をご参照ください。 このステップでは、GitOpsが使用されます。
ACK Oneコンソールにログインします。 左側のナビゲーションウィンドウで、 を選択します。
[マルチクラスターアプリケーション] ページの左上隅で、フリートインスタンス名の右側にある
をクリックし、ドロップダウンリストからフリートインスタンスを選択します。
を選択し、[マルチクラスターアプリケーションの作成-GitOps] ページに移動します。
[YAMLから作成] タブで、次のYAMLテンプレートをコードエディターにコピーします。 次に、[OK] をクリックしてアプリケーションをデプロイします。
説明次のYAMLテンプレートを使用して、関連する各クラスターの
gateway-demo
名前空間にアプリケーションをデプロイします。 [クイック作成] タブで、アプリケーションをデプロイするクラスターを選択することもできます。 [クイック作成] タブで行った構成の変更は、[YAMLから作成] タブのYAMLテンプレートに自動的に同期されます。gateway-demo
名前空間が存在しない場合は、作成する必要があります。
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: appset-grpc-demo namespace: argocd spec: template: metadata: name: '{{.metadata.annotations.cluster_name}}-grpc' namespace: argocd spec: destination: name: '{{.name}}' namespace: gateway-demo project: default source: repoURL: https://github.com/ivan-cai/gitops-demo.git path: manifests/directory/grpc targetRevision: main syncPolicy: automated: {} syncOptions: - CreateNamespace=true generators: - clusters: selector: matchExpressions: - values: - cluster key: argocd.argoproj.io/secret-type operator: In - values: - in-cluster key: name operator: NotIn goTemplateOptions: - missingkey=error syncPolicy: preserveResourcesOnDeletion: false goTemplate: true
手順3: ALBマルチクラスタゲートウェイとIngressの作成
AlbConfigを使用して、ACK One FleetインスタンスからALBマルチクラスタゲートウェイを作成できます。 クラスターをゲートウェイに関連付けることができます。 gRPCサービスにはHTTPSを使用する必要があります。 したがって、AlbConfig設定のリスナーパラメーターでHTTPSポートを指定する必要があります。 この例では、ポート443が使用される。
ACK One Fleetインスタンスが存在する仮想プライベートクラウド (VPC) に属する2つのvSwitchのIDを取得します。
gateway.yaml
という名前のファイルを作成し、次の内容をファイルにコピーします。説明${vsw-id1}
と${vsw-id2}
を、取得したvSwitch IDに置き換えます。${cluster1}
と${cluster2}
を関連するクラスターのIDに置き換えます。${cluster1}
と${cluster2}
のセキュリティグループを設定して、すべてのポートを開き、vSwitchのCIDRブロックのIPアドレスからのアクセスを許可します。
apiVersion: alibabacloud.com/v1 kind: AlbConfig metadata: name: ackone-gateway-demo annotations: # Specify the IDs of the clusters that you want to associate with the ALB instance. alb.ingress.kubernetes.io/remote-clusters: ${cluster1},${cluster2} spec: config: name: one-alb-demo addressType: Internet addressAllocatedMode: Fixed zoneMappings: - vSwitchId: ${vsw-id1} - vSwitchId: ${vsw-id2} listeners: - port: 8001 protocol: HTTP - port: 443 protocol: HTTPS --- apiVersion: networking.k8s.io/v1 kind: IngressClass metadata: name: alb spec: controller: ingress.k8s.alibabacloud/alb parameters: apiGroup: alibabacloud.com kind: AlbConfig name: ackone-gateway-demo
次のテンプレートを使用してIngressを作成します。
説明ingress設定に
alb.ingress.kubernetes.io/backend-protocol: grpc
およびalb. Ingress. kubernetes.io/listen-ports
アノテーションを追加する必要があります。 さらに、ステップ1で作成したシークレット証明書を指定する必要があります。apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: alb.ingress.kubernetes.io/listen-ports: | [{"HTTPS": 443}] alb.ingress.kubernetes.io/backend-protocol: grpc name: grpc-ingress namespace: gateway-demo spec: ingressClassName: alb rules: - host: grpc.example.com http: paths: - backend: service: name: grpc-service port: number: 50051 path: / pathType: Prefix tls: - hosts: - grpc.example.com secretName: grpc-secret
ステップ4: トラフィックが期待どおりに転送されるかどうかを確認する
次のコマンドを実行して、Ingressを表示します。
kubectl get ingress -ngateway-dmeo
期待される出力:
NAME CLASS HOSTS ADDRESS PORTS AGE grpc-ingress alb grpc.example.com alb-***** 80, 443 3m51s
grpcurl
を使用してサービスにアクセスします。# Replace <ADDRESS> with the domain name that is associated with the ABL Ingress. grpcurl -insecure -authority grpc.example.com <ADDRESS>:443 list
次の出力は、リクエストがバックエンドgRPCサービスに転送されたことを示します。
grpc.reflection.v1alpha.ServerReflection helloworld.Greeter