Skip to content
ImagineWorld

Observability and Traffic Splitting Using Linkerd

node_idnode-book_edge-computing-systems-with-kubernetes_chapter-08-observability-and-traffic-splitting-using-linkerd
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num8
chapter_titleObservability and Traffic Splitting Using Linkerd
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 8 — Observability and Traffic Splitting Using Linkerd

Section titled “Chapter 8 — Observability and Traffic Splitting Using Linkerd”

Observability is important when you develop microservices or applications using containers, as it provides insights into complex systems. Monitoring mechanisms, analytics, and observability give you an idea of how your applications will work in production as a system. In production, observability provides logging, metrics, and traces of how services interact with one another to provide functionality. Service meshes are often used to implement observability in your services. A service mesh is a powerful tool that helps you to implement observability and other functionalities such as retries or timeout management, without modifying your applications. This chapter discusses golden metrics, commonly used metrics for understanding systems, how to implement observability using Linkerd for an application with an ingress controller, and how to implement traffic routing using a sample application.

In this chapter, we’re going to cover the following main topics:

  • Observability, monitoring, and analytics
  • Introduction to service meshes and Linkerd
  • Implementing observability and traffic splitting with Linkerd
  • Testing observability and traffic splitting with Linkerd
  • Uninstalling Linkerd
  • Ideas to implement using service meshes

In this chapter, to implement observability with Linkerd, you will need the following:

  • A single or multi-node K3s cluster using ARM devices with MetalLB installed and with the option to avoid Traefik being installed as the default ingress controller.
  • Kubectl configured to be used in your local machine to avoid using the --kubeconfig parameter.
  • Helm command installed.
  • Clone the repository at https://github.com/PacktPublishing/Edge-Computing-Systems-with-Kubernetes/tree/main/ch8 if you want to run the YAML configuration by using kubectl apply instead of copying the code from the book. Take a look at the yaml directory for the YAML examples, inside the ch8 directory.

We are going to install Linkerd to implement observability and traffic splitting on this cluster. So, let’s get started with the basic theory to understand the benefits of observability and how to implement it.

To start, let’s get familiar with the observability concept. Peter Waterhouse mentioned, in his article in The New Stack, that ” observability is a measure of how well internal states of a system can be inferred from knowledge of its external outputs.” He also mentioned that observability is more of a property of a system and not something that you actually do.

There are two concepts that are close to each other in this context: monitoring and observability. In Steve Waterworth’s article, available at dzone.com, he mentioned this relation with the phrase, ” If you are observable, I can monitor you.”

What this means is that observability is achieved when data about systems is managed. Monitoring, on the other hand, it is the actual task of collecting and displaying this data. Finally, the analysis occurs after collecting data with a monitoring tool, and you perform it either manually or automatically.

This relationship is represented by the Pyramid of Power:

Figure 8.1 – Pyramid of Power

The Pyramid of Power represents how analysis and monitoring are the base to implement observability. Together, they can bring the property to know the state of your system; this is what we call observability. Service meshes give observability to the system by measuring metrics that reflect the state of the system. These metrics are called golden metrics. Let’s explore golden metrics in the next section.

Golden metrics were first introduced in the Google Site Reliability Engineering book and were defined as the minimum metrics required to monitor services. This is how the Pyramid of Power gets a place in the discussion about monitoring and observability. These metrics were also defined as a model, as a foundation for building monitoring around applications.

According to the Linkerd service mesh glossary web page, golden metrics are also called golden signals; these are the core metrics of application health. These metrics are defined or based on latency, traffic volume, error rate, and saturation. With these metrics, you can figure out the health of your application to finally build the property of observability in your applications and system. Golden metrics are the base for monitoring services and building observable systems.

Let’s explore, in the next section, how service meshes implement these golden metrics to bring observability to your system.

Introduction to service meshes and Linkerd

Section titled “Introduction to service meshes and Linkerd”

George Mirando, in his book, The Service Mesh, says that a service mesh ” is a dedicated infrastructure layer for handling service-to-service communication in order to make it visible, manageable, and controlled. The exact details of its architecture vary between implementations, but generally speaking, every service mesh is implemented as a series of interconnected network proxies designed to better manage service traffic.” In general, we can adopt the idea of a service mesh being built by this interconnected network of proxies that provides manageable, stable, and controlled service-to-service communication.

Now, let’s see how this is implemented, starting with the explanation given in the following diagram:

Figure 8.2 – Service mesh implementation with a sidecar container

Sidecar is a design pattern used on distributed systems that only have a single node. This pattern is commonly used in Kubernetes when deploying applications that use multiple containers. In this context, the sidecar pattern is made with two containers; the first container contains the application container (which is the core container), and the second sidecar container is a proxy that provides functionalities for a reliable network for your application, and both live inside a pod (which is an abstraction of a group of containers in Kubernetes for an application). This pod lives inside a data plane that contains all the services interconnected by proxies. To exemplify this, let’s look at the following diagram:

Figure 8.3 – Service mesh control plane and data plane

These proxies ask the control plane what to do with the incoming traffic, for example, block or encrypt the traffic. The control plane also evaluates and decides the corrective action to run in the proxies, such as a retry or redirect if a timeout occurs. The control plane contains rules to be applied to each service connect across the mesh. Collecting data to provide golden metrics makes the services observable. Some service meshes also provide a basic UI to manage all these service mesh functionalities.

The need for service meshes exists because of wrong assumptions regarding distributed systems, such as the following:

  • The network is reliable.
  • Latency is zero.
  • Bandwidth is infinite.
  • The network is secure.
  • Topology doesn’t change.
  • There is one administrator.
  • The transport cost is zero.
  • The network is homogeneous.

Service meshes exist to address all the wrong assumptions, helping to manage distributed systems from the logic in your application code and creating a reliable network for your application. In general, service meshes provide this reliability by just injecting a proxy as a sidecar without modifying the code of your application.

Finally, the relationship between service meshes and observability is that these proxies can generate the golden metrics when the proxies intercept network traffic, providing a graphical dashboard to provide a way to visualize the state of your applications; in other words, creating the observability property for your system.

Linkerd is a service designed to run on Kubernetes. It provides debugging, observability, reliability, and security to your applications deployed on Kubernetes without modifying your application’s source code. So, Linkerd not only provides observability but also provides more features, such as the following:

  • HTTP, HTTP/2, and gRPC proxying
  • Retries and timeouts
  • Telemetry and monitoring
  • Load balancing
  • Authorization policy
  • Automatic proxy injection
  • Distributed tracing
  • Fault injection
  • Traffic split
  • Service profiles
  • Multi-cluster communication

Linkerd is also a fully open source software, part of the graduated projects of the Cloud Native Computing Foundation (CNCF). Linkerd is built by Buoyant.

As we explored in the introduction to service meshes, Linkerd works with a data plane and a control plane, and it has the Linkerd CLI to manage its installation. It also comes with a UI to explore the different graphics that show golden metrics for your injected services.

In order to use Linkerd, first, you have to inject your application with the Linkerd proxy using the Linkerd CLI, and then Linkerd will be ready to start collecting metrics and enable your application to communicate with other inject services across the data plane; and, of course, Linkerd will be ready to configure your application with all its features such as traffic splitting.

Linkerd was designed to be fast without consuming a lot of resources and to be easy to use compared to other service meshes such as Istio. Istio includes a full package of tools for implementing not only a service mesh functionality but also tracing and ingress controller functionalities, which could be too much for some solutions. Linkerd reduces the complexity, and it was built to work as a modular service mesh piece of software that can integrate with your current technology solution stack to add an observability layer to your system. Linkerd meets edge computing requirements supporting ARM architectures and low resource consumption and is simple to use. In this way, Linkerd could be an option to look at before considering another solution based on Envoy such as Istio.

It’s important to mention that, because service meshes work using proxies, some ingress controllers or cloud native proxies could match your needs before choosing a full service mesh solution such as Traefik, Emissary, and Contour. Some important features to consider while picking a service mesh or a cloud native proxy are security and rate limit implementations. You can explore some articles comparing these solutions in the Further reading section. But now, it’s time to understand how to implement observability and traffic splitting in the next section.

Implementing observability and traffic splitting with Linkerd

Section titled “Implementing observability and traffic splitting with Linkerd”

To explain how we are going to use Linkerd for observability and traffic splitting, let’s explore the following diagram:

Figure 8.4 – Traffic splitting with Linkerd

First of all, you have to install Linkerd in your Kubernetes cluster. For this small scenario, we are going to use two deployments. The first deployment is a simple API deployment that returns the message Meshed application app1 with Linkerd, and the second, a deployment that always returns error code 500.

All the traffic will be sent by a client (in our case a loop that sends requests to the endpoint of the application) that is a load balancer created by your ingress controller service and used by an ingress definition. Every time the ingress object detects the traffic, the traffic will be split by 50% to the API deployment and 50% to the faulty deployment. This is going to simulate an error rate of 50% in your requests and 50% for traffic without errors.

It’s necessary to inject the ingress, the application, and the faulty deployment that simulates errors. In this way, these services will communicate with each other using the Linkerd proxy injected on each deployment.

While the traffic is moving across the services, it is generating the golden metrics that the Linkerd dashboard can visualize with Grafana and other reports that Linkerd implements in its UI.

Now, we are ready to start installing Linkerd in the next section.

So, let’s begin with the installation of Linkerd in your cluster. For this you have to follow the next steps:

  1. First, install the Linkerd CLI by running the following command:
    $ curl --proto '=https' --tlsv1.2 -sSfL https://run.linkerd.io/install | sh

If you are using macOS, you can install the Linkerd CLI using the brew command:

$ brew install linkerd
  1. Add the directory where Linkerd is installed to your path:
    $ echo "export PATH=\$PATH:/home/ubuntu/.linkerd2/bin" >> ~/.bashrc

Run the following command to load the new path, instead of logging in again to load the new path:

$ source ~/.bashrc
  1. To check whether the cluster fits the requirements to install Linkerd, run the following:
    $ linkerd check --pre
  2. Next, install Linkerd by running the following command:
    $ linkerd install | kubectl apply -f -
  3. Now, install the Linkerd dashboard by running the following command:
    $ linkerd viz install | kubectl apply -f -

This command is going to wait while Linkerd is being installed before installing the Linkerd dashboard.

  1. To check whether the installation was successful, run the following:
    $ linkerd check
  2. To open the Linkerd dashboard once everything is running, run the following command:
    $ linkerd viz dashboard --address 0.0.0.0

The previous command will expose the Linkerd dashboard inside your device. To run this command, we are assuming that the command was run inside the devices, so you need to run the following line to resolve the URL http://web.linkerd-viz.svc.cluster.local:50750 to point to your device:

$ IP_CLUSTER=<YOUR_IP_CLUSTER>
$ sudo echo $IP_CLUSTER" WEB.linkerd-viz.svc.cluster.local" >> /etc/hosts

IP_CLUSTER is the IP address of your cluster.

Now, access the next URL to open the dashboard: http://web.linkerd-viz.svc.cluster.local:50750.

Now, it’s time to install the NGINX ingress controller to be used in this implementation. Let’s explore this in the next section.

Installing and injecting the NGINX ingress controller

Section titled “Installing and injecting the NGINX ingress controller”

In this scenario, we are going to use the NGINX ingress controller, using Helm to install it by following the given steps:

  1. Create the nginx-ingress namespace:
    $ kubectl create ns nginx-ingress
  2. Add the NGINX ingress controller Helm chart and update the repositories configured in Helm:
    $ helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
    $ helm repo update
  3. Install the NGINX ingress controller:
    $ helm install nginx-ingress ingress-nginx/ingress-nginx -n nginx-ingress
  4. Now, to inject the NGINX ingress controller pod, run the following command:
    $ kubectl get -n nginx-ingress deploy nginx-ingress-ingress-nginx-controller -o yaml \
    | linkerd inject - \
    | kubectl apply -f -

Your ingress controller is now ready to be installed and injected. Let’s create the applications that we need in the next section.

Creating a demo application and faulty pods

Section titled “Creating a demo application and faulty pods”

Now, let’s create our sample application and faulty pod to experiment with the traffic splitting feature and get some faulty traffic to simulate error requests. For this, follow the given steps:

  1. Create the myapps namespace for your pods:
    $ kubectl create ns myapps
  2. Create the sample application, app1, by running the following command:
    $ cat <<EOF | linkerd inject - | kubectl apply -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    labels:
    app: app1
    name: app1
    namespace: myapps
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: app1
    template:
    metadata:
    labels:
    app: app1
    spec:
    containers:
    - image: czdev/app1demo
    name: app1demo
    env:
    - name: MESSAGE
    value: "Meshed application app1 with Linkerd"
    - name: PORT
    value: "5000"
    EOF

Important Note

The linkerd inject command inserts the linkerd.io/inject: enabled label in the annotations sections of your deployment or pod. This label is used by Linkerd to inject the services with the Linkerd proxy. You can also add this label manually in your YAML definitions to have a better approach using declarative definitions for your pods and deployments. To customize the code of app1demo check the link https://github.com/sergioarmgpl/containers/tree/main/app1demo.

  1. To create our faulty pod, we are going to use NGINX as a web server and a custom configuration to return a request with a 500 code error in order for Linkerd to detect and count the request as an error. For this, let’s create the configuration by running the following command:
    $ cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: error-injector
    namespace: myapps
    data:
    nginx.conf: |-
    events {}
    http {
    server {
    listen 5000;
    location / {
    return 500;
    }
    }
    }
    EOF
  2. Now, let’s create the deployment that returns a 500 error in port 5000 when accessing the pod in the / path:
    $ cat <<EOF | linkerd inject - | kubectl apply -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: error-injector
    namespace: myapps
    labels:
    app: error-injector
    spec:
    selector:
    matchLabels:
    app: error-injector
    replicas: 1
    template:
    metadata:
    labels:
    app: error-injector
    spec:
    containers:
    - name: nginx
    image: nginx:alpine
    volumeMounts:
    - name: nginx-config
    mountPath: /etc/nginx/nginx.conf
    subPath: nginx.conf
    volumes:
    - name: nginx-config
    configMap:
    name: error-injector
    EOF
  3. Now that our applications have been deployed, let’s configure the services for these applications. Let’s start with the error-injector service:
    $ cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
    name: error-injector
    namespace: myapps
    spec:
    ports:
    - name: service
    port: 5000
    selector:
    app: error-injector
    EOF
  4. Now, create the service for your application by running the following command:
    $ cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
    name: app1
    namespace: myapps
    spec:
    ports:
    - name: service
    port: 5000
    selector:
    app: app1
    EOF
  5. Now, let’s use the Service Mesh Interface (SMI) specification to configure the traffic splitting. With this configuration, the traffic will be split by 50% to the app1 service and the other half for error-injector, so we are going to expect a 50% success rate:
    $ cat <<EOF | kubectl apply -f -
    apiVersion: split.smi-spec.io/v1alpha1
    kind: TrafficSplit
    metadata:
    name: error-split
    namespace: myapps
    spec:
    service: app1
    backends:
    - service: app1
    weight: 500m
    - service: error-injector
    weight: 500m
    EOF
  6. Finally, let’s create our ingress rule to expose the endpoint to send traffic to this application using traffic splitting:
    $ cat <<EOF | kubectl apply -f -
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: ingress
    namespace: myapps
    annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/service-upstream: "true"
    spec:
    ingressClassName: nginx
    rules:
    - http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: app1
    port:
    number: 5000
    EOF

Important Note

Depending on which Kubernetes version you are using, you have to use the syntax for your ingress controller definition, v1beta1 or v1. For more information, you can check https://kubernetes.io/docs/concepts/services-networking/ingress, and change from different Kubernetes versions.

Now, we are ready to test the observability and traffic splitting configured with Linkerd. Let’s explore this in the next section.

Testing observability and traffic splitting with Linkerd

Section titled “Testing observability and traffic splitting with Linkerd”

Now, it’s time to test the observability. To start exploring the dashboard and see the observability, follow the given steps:

  1. Open your dashboard by running the following command:
    $ linkerd viz dashboard

This will automatically open the dashboard to the URL http://localhost:50750.

The dashboard will look as in the following screenshot:

Figure 8.5 – Linkerd dashboard

To load the right information, select the MYAPPS namespace in the combo box in the left sidebar, and then click on the Deployments icon to load the HTTP Metrics and TCP Metrics information.

To see similar information as the previous dashboard, execute the following command to start sending traffic to our deployment:

$ ENDPOINT=$(kubectl get svc nginx-ingress-ingress-nginx-controller --output jsonpath='{.status.loadBalancer.ingress[0].ip}' -n nginx-ingress)
$ while true; do curl http://$ENDPOINT;echo " "; done

The first command assigns the load balancer IP address that your NGINX ingress controller is using as the endpoint to expose services using ingress definitions. Then, while the command sends traffic, it also shows the result of each request, showing a similar message to the following:

Host:app1-555485df49-rjf4vMeshed application app1 with Linkerd

Or, the following output error is displayed:

<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.21.6</center>
</body>
</html>

This is a frequency of 50% for the message and 50% for the error, on average.

  1. If you click on the orange Grafana icon (let’s say, for example, in the HTTP Metrics section), you will see a similar Grafana graph to the following:

Figure 8.6 – Grafana Linkerd HTTP Metrics graph

In this graph, you can see the golden metrics and the success rate of the application for the app1 deployment, the requests per second (RPS), and the latency of each request; these metrics represent the golden metrics for your application, which give you the basic observability feature for your system and your application.

  1. If you click on Traffic Splits while the myapps namespace is selected, you will see a traffic splitting representation like this:

Figure 8.7 – Linkerd traffic splitting dashboard

In this dashboard, you will see, in real time, how the traffic splitting configuration sends 50% of the traffic to the app1 Kubernetes services and 50% to the error injector. The red color represents failure requests (requests that return the 500 request error code), while the green color represents valid traffic from the app1 service returning a 200 request code. This, in general, gives you the live state of your application, which is the goal of implementing observability.

This basic implementation simulates a failure request for an application using a service mesh. You can also use the same implementation to split your traffic between applications or implement advanced deployment strategies such as blue/green deployments. This was a simple use case to implement observability in your applications and the power of traffic management from a service mesh. Now, let’s explore some useful commands if you want to use Linkerd using the CLI.

In some cases where no UI is available, maybe for security reasons, it could be useful to use the Linkerd CLI. So, let’s explore four basic command-line options: routes, top, tap, and edges:

  • routes shows the current routes that other applications or clients are using to access your application. Using our previous scenario as an example, you can show the routes of app1 in the myapps namespace with the following command:
    $ linkerd viz routes deployment/app1 --namespace myapps
  • top displays the traffic and path of your application. The following command is going to show how the ingress controller forwards the traffic to your applications, shows a counter to access the / path, and shows the success rate of the requests:
    $ linkerd viz top deployment/app1 --namespace myapps
  • tap displays the information of the requests in real time for app1; for this, you have to run the following command:
    $ linkerd viz tap deployment/app1 --namespace myapps
  • edges shows a table displaying how your application is connected with other injected applications in your cluster, and the source and destiny of each connection. For this, you have to run the following command for app1:
    $ linkerd viz edges po -n myapps

With this, you have an idea of how to use Linkerd with the CLI. Now, let’s move to the next section to learn how to uninstall Linkerd.

If you are evaluating Linkerd or doing some management in your clusters, for example, it could be useful to uninstall Linkerd. For this, follow the next steps:

  1. Uninstall support for additional features of Linkerd (called viz) as follows:
    $ linkerd viz uninstall | kubectl delete -f -
  2. Uninstall the Linkerd control plane. This is going to uninstall the rest of the core Linkerd components. For this, run the following command:
    $ linkerd uninstall | kubectl delete -f -

Now, Linkerd is uninstalled from your cluster. To end this chapter, let’s move to the last section to explore some useful ideas of where you can use Linkerd.

Ideas to implement when using service meshes

Section titled “Ideas to implement when using service meshes”

To end this chapter, here are some ideas of how you can get the advantages of using service meshes at the edge. These ideas are not specific to the edge and could be used in a common infrastructure:

  • Implement rate limits: You can use a service mesh to configure some rate limits in your applications, managing in this way how much input traffic is accepted. There are some awesome projects to implement this, including Linkerd and Envoy-based service meshes such as Istio and Ambassador.
  • Traffic splitting: You can use this feature of service meshes to implement blue/green deployments and canary deployments; an example of this is the implementation of Argo Rollouts, which can use Linkerd to implement this kind of deployment strategy. You can also implement some chaos engineering tests using service meshes.
  • Security policies: You can use service meshes to restrict traffic and encrypt end-to-end traffic. This could be useful to increase the security of your services.
  • Multi-cluster connection: With a service mesh, you can connect your clusters without complex configurations. Kuma is a control plane for microservices and service meshes that can help you to connect multiple clusters; it was built on top of Envoy. You can also do the same using Linkerd and other Envoy-based service meshes.
  • Scaling based on networking: You can use Prometheus metrics generated by service meshes to generate alerts or scale your services. You can also implement machine learning models to implement some intelligent scaling. You can use them with projects such as Kubernetes-based Event-Driven Autoscaling (KEDA), which reads information from an API to scale your services.

These are some ideas that you can explore when using service meshes. Now, it’s time to finish the chapter.

In this chapter, we learned how to implement observability and how to use a service mesh to set up traffic splitting. We focused on implementing this scenario using Linkerd, running a sample application that shows a message, and using traffic splitting. When the application receives the traffic, we showed how to explore the different graphics that can be used to get the real-time state of your system. We also learned how to use Linkerd with the CLI uninstalled. The chapter ended with some implementation ideas to explore when using service meshes and how this can impact your system. All of this forms the base to implement observability and basic traffic splitting in systems using a Linkerd service mesh. In the next chapter, we are going to learn how to implement serverless functions and simple event-driven pipelines using Knative.

Here are a few questions to validate your new knowledge:

  • How do service meshes help you to implement observability?
  • What are the features that service meshes provide to systems?
  • How do service meshes work internally?
  • What does Linkerd provide for users implementing observability?
  • How can Linkerd be compared to other service meshes?
  • What are the common use cases for service meshes?

You can refer to the following resources for more information on the topics covered in this chapter: