3 min read

Deploying and Using Envoy in Kubernetes: A Step-by-Step Guide

Deploying and Using Envoy in Kubernetes: A Step-by-Step Guide

Introduction

Envoy is a popular open-source edge and service proxy that provides advanced load balancing, routing, and observability features. Kubernetes is a container orchestration platform that simplifies the deployment and management of containerized applications.

Deploying Envoy in Kubernetes can provide a highly scalable and reliable service mesh infrastructure for your microservices-based application. In this tutorial, we will go through the process of deploying and using Envoy in Kubernetes.

Prerequisites

Before we begin, ensure that you have the following prerequisites:

  • A Kubernetes cluster up and running
  • kubectl command-line tool installed and configured
  • Basic knowledge of Kubernetes concepts such as pods, deployments, and services
  • Basic knowledge of Envoy concepts such as listeners, clusters, and routes

Deploying Envoy in Kubernetes

The easiest way to deploy Envoy in Kubernetes is to use the official Envoy Docker image and deploy it as a Kubernetes deployment. Here are the steps to do this:

  1. Create a file named envoy-deployment.yaml with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: envoy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: envoy
  template:
    metadata:
      labels:
        app: envoy
    spec:
      containers:
      - name: envoy
        image: envoyproxy/envoy:v1.18.3
        ports:
        - containerPort: 8080
        - containerPort: 9901

This YAML file defines a Kubernetes deployment that will create a single replica of the Envoy container.

  1. Deploy the Envoy deployment by running the following command:
kubectl apply -f envoy-deployment.yaml

This command will create the Envoy deployment in your Kubernetes cluster.

  1. Create a file named envoy-service.yaml with the following contents:
apiVersion: v1
kind: Service
metadata:
  name: envoy
spec:
  selector:
    app: envoy
  ports:
  - name: http
    port: 80
    targetPort: 8080
  - name: admin
    port: 9901
    targetPort: 9901

This YAML file defines a Kubernetes service that will expose the Envoy container to the outside world.

  1. Deploy the Envoy service by running the following command:
kubectl apply -f envoy-service.yaml

This command will create the Envoy service in your Kubernetes cluster.

Using Envoy in Kubernetes

Now that Envoy is deployed in your Kubernetes cluster, you can start using it to route traffic to your microservices. Here are the steps to do this:

  1. Create a file named envoy-config.yaml with the following contents:
static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address:
        address: 0.0.0.0
        port_value: 80
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: backend
              domains:
              - "*"
              routes:
              - match:
                  prefix: "/service-a"
                route:
                  cluster: service-a
              - match:
                  prefix: "/service-b"
                route:
                  cluster: service-b
  1. The envoy-config.yaml file defines the Envoy listener and the routing configuration. In this example, we have defined a listener on port 80 that will forward traffic to two microservices named service-a and service-b.
  2. Deploy the Envoy configuration by running the following command:
kubectl create configmap envoy-config --from-file=envoy-config.yaml

This command will create a Kubernetes configmap with the Envoy configuration.

  1. Modify the envoy-deployment.yaml file to mount the Envoy configuration as a volume in the container. Update the file with the following lines:
spec:
  containers:
  - name: envoy
    image: envoyproxy/envoy:v1.18.3
    ports:
    - containerPort: 8080
    - containerPort: 9901
    volumeMounts:
    - name: config-volume
      mountPath: /etc/envoy
  volumes:
  - name: config-volume
    configMap:
      name: envoy-config

This will mount the Envoy configuration as a volume in the container at /etc/envoy.

  1. Deploy the updated Envoy deployment by running the following command:
kubectl apply -f envoy-deployment.yaml

This will update the Envoy deployment to mount the Envoy configuration as a volume.

  1. Verify that Envoy is running correctly by accessing the Envoy admin endpoint at http://<envoy-service-ip>:9901/stats.

Best Practices

Here are some best practices to keep in mind when deploying and using Envoy in Kubernetes:

  • Use a separate Envoy container for each microservice. This provides better isolation and makes it easier to scale the microservices independently.
  • Use Kubernetes ConfigMaps to manage the Envoy configuration. This makes it easier to update the configuration without redeploying the Envoy container.
  • Use Kubernetes Services to expose the Envoy container to the outside world. This provides load balancing and high availability for the Envoy container.
  • Use Kubernetes Ingress resources to define the Envoy listener and routing configuration. This provides a more flexible and scalable way to manage the Envoy configuration.

Conclusion

Deploying and using Envoy in Kubernetes can provide a highly scalable and reliable service mesh infrastructure for your microservices-based application. In this tutorial, we went through the process of deploying and using Envoy in Kubernetes, including configuring Envoy for routing traffic to microservices. We also discussed some best practices for deploying and using Envoy in Kubernetes.