Set up Service Mesh for your Application using kubectl
To set up Service Mesh for your application, you must configure a bunch of Service Mesh resources.
This section provides an example of managing Service Mesh with kubectl (for more information see, Managing Service Mesh with Kubernetes). An assumption is that a Kubernetes namespace <app-namespace> is created and the application is deployed in that namespace. You can create the Service Mesh custom resources in the same namespace as your application or a different namespace. In this example, we use the <app-namespace> for Service Mesh custom resources.
Application design
This example assumes an application composed of the following.
A front-end microservice named ui.
Two backend microservices ms1 and ms2.
The backend microservice ms2 has two versions
ms2-v1 and ms2-v2.
The following are the assumptions for the Kubernetes cluster.
Each of the microservices ui, ms1, and
ms2 have a Kubernetes service defined with the same name to
enable DNS-based hostname lookup for them in the cluster.
The ui Kubernetes service definition has a selector that
matches the ui pod.
The ms1 Kubernetes service definition has a selector that
matches the ms1 pod.
The ms2 Kubernetes service definition has a selector that
matches the ms2-v1 and ms2-v2 pods.
The cluster has an ingress Kubernetes service of type load balancer to allow
ingress traffic into the cluster and has a selector that matches the
ui pod.
Kubernetes Service Mesh resource creation YAML configuration data must be in a
particular order.
Mesh
Virtual Service
Virtual Deployment
Virtual Service Route Table
Ingress Gateway
Ingress Gateway Route Table
Access Polices
Virtual Deployment Binding
Ingress Gateway Deployment
Whether your Kubernetes configuration resources are in a single YAML file or
multiple YAML files, the ordering of resources remains the same.
Create Service Mesh Resources 🔗
To enable Service Mesh for your application, you need to create two sets of
resources:
Service Mesh Control Plane resources
Service Mesh binding resources.
In this example, we manage the Service Mesh with kubectl and create
custom resources in the Kubernetes cluster to create the control plane resources.
The Service Mesh binding resources are always created as custom resources in the
Kubernetes cluster.
You create the Service Mesh control plane resources based on your application design.
The following suggestion is how you would model the Service Mesh resources based on
the preceding application design.
Mesh: Create a service mesh named app-name
Virtual Service: Create three virtual services (ui,
ms1, ms2) corresponding to the three
microservices
Virtual Deployment: Create four virtual deployments, one for each version of the
microservice (ui, ms1,
ms2-v1, ms2-v2)
Virtual Service Route Table: Create three virtual service route tables, one for
each of the virtual services to define the traffic split to the virtual service
versions
Ingress Gateway: Create one ingress gateway to enable ingress into the mesh
Ingress Gateway Route Table: Create one ingress gateway route table, to define
the traffic routing for incoming traffic on the ingress gateway
Access Policies: Create one access policy with rules enabling access for traffic
between the microservices in the mesh
Create the Service Mesh control plane resources using a local Service Mesh
configuration file on your system:
List all objects in custom resource definition by replacing the name of the
custom resource in <service-mesh-crd-name> and the namespace
where the custom resource is located (<crd-namespace>).
Copy
kubectl get <service-mesh-crd-name> -n <crd-namespace>
NAME ACTIVE AGE
app-namespace/app-name True 1h
With these steps completed, your service mesh resources are available in the
console.
Service Mesh Binding Resources 🔗
The next step is to bind the Service Mesh control plane resources with your
infrastructure, in this case the pods in the Kubernetes Cluster. This binding
resource enables automatic sidecar injection and pod discovery for proxy software.
For more information on binding resources, see: Architecture and Concepts.
The following are the binding resources.
Virtual Deployment Binding: Create four virtual deployment binding resources to
associate each of the four virtual deployments in the control plane to the
corresponding pods representing those virtual deployments.
Ingress Gateway Deployment: Create one ingress gateway deployment to deploy the
ingress gateway defined in the control plane.
Ensure you have enabled sidecar injection in your Kubernetes namespace by running the
following command. If you do not enable sidecar injection, the proxies are not
injected in your application pods.
So far we have setup and deployed the ingress gateway but the incoming traffic must
be redirected to it. Assuming you have an ingress service of type LoadBalancer in
your Kubernetes Cluster you have to update it to point to the ingress gateway.
To allow outgoing egress traffic from your service mesh, an access policy must be
configured. To create an egress access policy, use the kubectl
apply command. For example:
Copy
kubectl apply -f egress-access-policy.yaml
The following sample yaml configuration file creates an egress access policy. The
policy defines two egress rules, one for HTTP and one for HTTPS. For an external
service, three protocols are supported: HTTP, HTTPS, and TCP. The protocols
correlate with the httpExternalService, httpsExternalService, and the
tcpExternalService keys in Kubernetes. Host names and ports can be specified to for
each entry.
Now that your application has Service Mesh support, you can add logging features.
After adding logging features, you can see your logs in the OCI Logging Service.
From the console, go to Observability &
Management under Logging select
Logs.
Click the name of the log you created in the preceding step.
Locate the OCID field and click
Copy. Save the OCID in a text file.
On your system, create the logconfig.json configuration
file using the following sample file. Ensure to put in the OCID for your custom
log in the logObjectId field.
To add Kubernetes monitoring and graphing support for your application, you need to
have Prometheus and Grafana installed as specified in the prerequisites. In
addition, you need to configure Prometheus to enable scraping metrics from the
Service Mesh proxies.
The service mesh proxies expose the metrics on the /stats/prometheus
endpoint. When creating the ClusterRole for the prometheus service,
include /stats/prometheus in the "nonResourceURLs." See the
following ClusterRole configuration example.
As a part of the prometheus scrape config you need to add a job to scrape metrics
from the Service Mesh proxy endpoints. See the following
scrape_config example.