Getting Started with Consul Service Mesh for Kubernetes
This tutorial installs and configures Consul service mesh on a Kubernetes cluster.
By the end of this tutorial, you will be able to identify the installation prerequisites, install Consul with the official Helm chart, and deploy an example application.
Prerequisites
The following components are required to complete this tutorial:
- Docker.
- A local Kubernetes cluster. Popular options for starting clusters include Minikube, kind, and k3s. This tutorial uses Minikube.
- Helm
- Consul K8s CLI v1.0.2
Clone the Demo Configuration and Application
The learn-consul-service-mesh-deploy
repository contains resources to help you understand how to use Consul in your Kubernetes clusters. Clone the repository to download the demo configuration and application used in this tutorial:
Change to the repository directory, which contains all of the artifacts used in this tutorial.
Deploy Consul
First, start a Kubernetes cluster.
After you start Kubernetes, you can deploy Consul with Helm or with the consul-k8s
command line tool.
To deploy Consul service mesh using Helm, you need a copy of the official chart. You can add the official HashiCorp Consul Helm chart repo from the command line using the Helm CLI.
Update your local Helm repositories to ensure you have the latest Consul Helm chart.
Deploy the Consul Helm chart using the values in consul-values.yaml
. This configuration deploys the control plane for the service mesh, which includes a Consul server, client, controller, and injector in a dedicated consul
Kubernetes namespace. It also installs a demo instance of Prometheus and Grafana for metrics.
Validate the installation by checking for running Consul pods.
Interact with Consul
To access the Consul UI, set up port forwarding to port 8500
.
Then, open your browser and go to http://localhost:8500. Explore the Consul UI by clicking the Nodes and Services tabs.
You can use kubectl exec
to get direct access to any container, including
the Consul server. Use this command to check for a list of Consul's
servers and clients. Consul servers configure and control the service mesh,
while clients help with service discovery.
Deploy a demo application
You can explore Consul service mesh by installing a demo application. The demo application, named HashiCups, includes a set of services and a PostgreSQL database. It allows you to order a cup of coffee.
Tip
If you are using Docker Desktop and the demo application fails to launch, the reason may be that the default resource limits for containers are set too low. We recommend setting the RAM to 4GB or more and CPUs to 4 or more.
Open a new terminal and deploy the demo application to your cluster.
The services use an annotation that allows Consul to automatically inject a proxy for each service. The proxies create a data plane to handle requests between services based on configuration from Consul. You can check for applications with proxies by selecting the label for Consul injection.
Explore the application
You can now explore the demo application and how it uses Consul service mesh.
Forward the nginx
service to port 18080
.
The HashiCups UI will be available at http://localhost:18080 in your browser. Refresh the page a few times.
Go to the Consul UI on http://localhost:8500. You'll find the HashiCups services registered to Consul service mesh with proxies.
Select the products-api
service. The Topology tab indicates that requests to
the public-api
goes to the product-api
before accessing the database.
Consul embeds a graph aggregating successful requests and error metrics.
Clean up
After you complete this tutorial, you can stop Consul, the demo application, and the Kubernetes cluster.
Remove all HashiCups resources.
Remove all Consul resources.
Terminate you local Kubernetes cluster.
Next steps
In this tutorial, you configured Consul service mesh on an existing Kubernetes cluster using the official Helm chart. You also deployed an example application to explore the service topology and track requests.
For a detailed explanation of Consul service mesh, review our tutorial explaining its design and features.
Learn more about using Consul on Kubernetes in the following tutorials: