You can use an ASM Gateway to secure your gRPC services with mutual Transport Layer Security (mTLS). This ensures that only authorized clients can access your services, enforcing end-to-end encryption and two-way authentication to prevent eavesdropping, tampering, and unauthorized access.
Prerequisites
-
Create an Enterprise Edition instance of Service Mesh (ASM). For more information, see Create an ASM instance.
An ACK managed cluster is created. For more information, see Create an ACK managed cluster.
-
Deploy an ingress gateway. For more information, see Create an ingress gateway.
-
Deploy an application in the cluster associated with the ASM instance. For more information, see Deploy an application in a cluster that is associated with an ASM instance.
Background
The traffic management feature of Service Mesh allows you to access internal gRPC services through an ingress gateway. Because gRPC is based on the HTTP/2 protocol, you can use Transport Layer Security (TLS) or mutual TLS (mTLS) to encrypt data in transit and ensure data security. ASM Gateway also supports gRPC over TLS/mTLS, which allows the gateway to perform TLS termination for encrypted TCP streams. This eliminates the need for separate TLS configurations in applications within the service mesh.
Procedure
Step 1: Deploy a sample application
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
At the top of the Deployments page, select a namespace from the Namespace drop-down list and click Create Resources in YAML.
-
On the Create page, paste the following YAML into the editor and click Create.
NoteDue to Istio's protocol selection mechanism, the value of the
nameparameter in theportsfield of the Service configuration must start withhttp2-orgrpc-. Otherwise, Istio cannot correctly identify the service protocol.
Step 2: Deploy an ingress gateway
This example uses the default port 443 to expose the service. For more information, see Create an ingress gateway.
Step 3: Configure Service Mesh routing rules
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the target instance, or click Manage in the Actions column.
-
Create a Gateway.
-
On the details page of the ASM instance, choose in the left-side navigation pane.
-
On the Gateway page, click Create from YAML.
-
On the Create page, select default from the Namespaces drop-down list, paste the following YAML into the editor, and then click Create.
apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: gw-grpc-443 namespace: default spec: selector: istio: ingressgateway servers: - hosts: - '*' port: name: https number: 443 protocol: HTTPS tls: credentialName: example-credential mode: MUTUAL
-
-
Create a VirtualService.
-
On the details page of the ASM instance, choose in the left-side navigation pane.
-
On the VirtualService page, click Create from YAML.
-
On the Create page, select default from the Namespaces drop-down list, paste the following YAML into the editor, and then click Create.
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: grpc-vs spec: hosts: - "*" gateways: - gw-grpc-443 http: - match: - port: 443 route: - destination: host: istio-grpc-server
-
Step 4: Configure certificates
gRPC clients typically require certificates with a Subject Alternative Name (SAN). We recommend that you use the sample certificates from the grpc-go repository. You must configure these certificates in the istio-system namespace of the ACK cluster, as they are used by the ASM Gateway.
-
For ASM instances earlier than v1.17
Run the following command to create a Secret in the istio-system namespace:
kubectl create -n istio-system secret generic example-credential --from-file=tls.key=server_key.pem --from-file=tls.crt=server_cert.pem --from-file=ca.crt=client_ca_cert.pemNoteThe Secret name must match the credentialName value specified in the Gateway.
-
For ASM instances of v1.17 and later
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the target instance. In the left-side navigation pane, choose .
-
On the Certificate Management page, click Create. In the Certificate Information panel, configure the parameters and click OK.
-
Step 5: Run the gRPC client
This tutorial uses the official grpc-go sample as the gRPC mTLS client.
-
Follow the official grpc-go tutorial to install the gRPC dependencies. For more information, see Quick start for gRPC in Go.
-
Follow the official grpc-go tutorial to clone the grpc-go code repository. For more information, see the grpc-go repository.
-
Replace the content of the /grpc-go/examples/helloworld/greeter_client/main.go file with the following code. Change the value of
addressto".:443" -
After you replace the code, navigate to the examples directory and run the following command.
go run helloworld/greeter_client/main.goExpected output:
Greeting: Hello worldIf the certificates are not correctly configured, the client returns an error similar to the following. If this occurs, follow the steps in this topic to configure the certificates again.
2022/09/23 15:44:44 could not greet: rpc error: code = Unavailable desc = connection error: desc = "transport: authentication handshake failed: x509: certificate signed by unknown authority" exit status 1