consul/website/pages/docs/guides/kubernetes-reference.mdx

190 lines
8.8 KiB
Plaintext
Raw Normal View History

2020-04-06 20:27:35 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
page_title: Kubernetes Consul Reference Architecture
sidebar_current: docs-guides-k8s-reference-architecture
description: This document provides recommended practices and a reference architecture.
2019-03-21 00:20:05 +00:00
---
# Consul and Kubernetes Reference Architecture
Preparing your Kubernetes cluster to successfully deploy and run Consul is an
important first step in your production deployment process. In this guide you
will prepare your Kubernetes cluster, that can be running on any platform
(AKS, EKS, GKE, etc). However, we will call out cloud specific differences when
2019-03-21 00:20:05 +00:00
applicable. Before starting this guide you should have experience with
2020-04-06 20:27:35 +00:00
Kubernetes, and have `kubectl` and helm configured locally.
2019-03-21 00:20:05 +00:00
By the end of this guide, you will be able to select the right resource limits
for Consul pods, select the Consul datacenter design that meets your use case,
2020-04-06 20:27:35 +00:00
and understand the minimum networking requirements.
2019-03-21 00:20:05 +00:00
## Infrastructure Requirements
Consul server agents are responsible for the cluster state, responding to RPC
queries, and processing all write operations. Since the Consul servers are
highly active and are responsible for maintaining the cluster state, server
sizing is critical for the overall performance, efficiency, and health of the
2019-03-21 00:20:05 +00:00
Consul cluster. Review the [Consul Reference
2019-04-03 21:36:02 +00:00
Architecture](/consul/advanced/day-1-operations/reference-architecture#consul-servers)
2020-04-06 20:27:35 +00:00
guide for sizing recommendations for small and large Consul datacenters.
2019-03-21 00:20:05 +00:00
The CPU and memory recommendations can be used when you select the resources
limits for the Consul pods. The disk recommendations can also be used when
selecting the resources limits and configuring persistent volumes. You will
need to set both `limits` and `requests` in the Helm chart. Below is an example
2019-06-11 14:58:46 +00:00
snippet of Helm config for a Consul server in a large environment.
```yaml
# values.yaml
2019-03-21 00:20:05 +00:00
2020-04-06 20:27:35 +00:00
server
resources: |
requests:
memory: "32Gi"
cpu: "4"
limits:
2019-04-03 21:36:02 +00:00
memory: "32Gi"
2019-06-11 14:58:46 +00:00
cpu: "4"
storage: 50Gi
...
2019-03-21 00:20:05 +00:00
```
You should also set [resource limits for Consul
clients](https://www.consul.io/docs/platform/k8s/helm.html#v-client-resources),
so that the client pods do not unexpectedly consume more resources than
2020-04-06 20:27:35 +00:00
expected.
2019-03-21 00:20:05 +00:00
[Persistent
volumes](https://kubernetes.io/docs/concepts/storage/persistent-volumes/) (PV)
allow you to have a fixed disk location for the Consul data. This ensures that
if a Consul server is lost, the data will not be lost. This is an important
feature of Kubernetes, but may take some additional configuration. If you are
running Kubernetes on one of the major cloud platforms, persistent volumes
should already be configured for you; be sure to read their documentation for more
2019-04-03 21:36:02 +00:00
details. If you are setting up the persistent volumes resource in Kubernetes, you may need
2019-03-21 00:20:05 +00:00
to map the Consul server to that volume with the [storage class
parameter](https://www.consul.io/docs/platform/k8s/helm.html#v-server-storageclass).
Finally, you will need to enable RBAC on your Kubernetes cluster. Review
2019-04-03 21:36:02 +00:00
the [Kubernetes
RBAC](https://kubernetes.io/docs/reference/access-authn-authz/rbac/) documenation. You
should also review RBAC and authentication documentation if your Kubernetes cluster
is running on a major cloud platorom.
- [AWS](https://docs.aws.amazon.com/eks/latest/userguide/managing-auth.html).
- [GCP](https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control).
2020-04-06 20:27:35 +00:00
- [Azure](https://docs.microsoft.com/en-us/cli/azure/aks?view=azure-cli-latest#az-aks-create). In Azure, RBAC is enabled by default.
2019-03-21 00:20:05 +00:00
2020-04-06 20:27:35 +00:00
## Datacenter Design
2019-03-21 00:20:05 +00:00
There are many possible configurations for running Consul with Kubernetes. In this guide
2019-03-21 00:20:05 +00:00
we will cover three of the most common.
2020-04-06 20:27:35 +00:00
1. Consul agents can be solely deployed within Kubernetes.
2019-03-21 00:20:05 +00:00
1. Consul servers
2020-04-06 20:27:35 +00:00
can be deployed outside of Kubernetes and clients inside of Kubernetes.
1. Multiple Consul datacenters with agents inside and outside of Kubernetes.
2019-03-21 00:20:05 +00:00
Review the Consul Kubernetes-specific
2020-04-09 23:20:00 +00:00
[documentation](https://www.consul.io/docs/platform/k8s#use-cases)
2020-04-06 20:27:35 +00:00
for additional use case information.
2019-03-21 00:20:05 +00:00
Since all three use cases will also need catalog sync, review the
2020-04-06 20:27:35 +00:00
implementation [details for catalog sync](https://www.consul.io/docs/platform/k8s/service-sync.html).
2019-03-21 00:20:05 +00:00
2020-04-06 20:27:35 +00:00
### Consul Datacenter Deployed in Kubernetes
2019-03-21 00:20:05 +00:00
Deploying a Consul cluster, servers and clients, in Kubernetes can be done with
the official [Helm
chart](https://www.consul.io/docs/platform/k8s/helm.html#using-the-helm-chart).
This configuration is useful for managing services within Kubernetes and is
common for users who do not already have a production Consul datacenter.
2020-04-07 23:56:08 +00:00
![Reference Diagram](/img/k8s-consul-simple.png 'Consul in Kubernetes Reference Diagram')
2019-03-21 00:20:05 +00:00
The Consul datacenter in Kubernetes will function the same as a platform
2019-04-03 21:36:02 +00:00
independent Consul datacenter, such as Consul clusters deployed on bare metal servers
or virtual machines. Agents will communicate over LAN gossip, servers
will participate in the Raft consensus, and client requests will be
forwarded to the servers via RPCs.
2019-03-21 00:20:05 +00:00
### Consul Datacenter with a Kubernetes Cluster
To use an existing Consul cluster to manage services in Kubernetes, Consul
clients can be deployed within the Kubernetes cluster. This will also allow
Kubernetes-defined services to be synced to Consul. This design allows Consul tools
2020-04-06 20:27:35 +00:00
such as envconsul, consul-template, and more to work on Kubernetes.
2019-03-21 00:20:05 +00:00
2020-04-07 23:56:08 +00:00
![Reference Diagram](/img/k8s-cluster-consul-datacenter.png 'Consul and Kubernetes Reference Diagram')
2019-03-21 00:20:05 +00:00
This type of deployment in Kubernetes can also be set up with the official Helm
chart.
### Multiple Consul Clusters with a Kubernetes Cluster
Consul clusters in different datacenters running the same service can be joined
by WAN links. The clusters can operate independently and only communicate over
the WAN. This type datacenter design is detailed in the [Reference Architecture
2019-04-03 21:36:02 +00:00
guide](/consul/advanced/day-1-operations/reference-architecture#multiple-datacenters).
2019-03-21 00:20:05 +00:00
In this setup, you can have a Consul cluster running outside of Kubernetes and
2020-04-06 20:27:35 +00:00
a Consul cluster running inside of Kubernetes.
2019-03-21 00:20:05 +00:00
### Catalog Sync
To use catalog sync, you must enable it in the [Helm
chart](https://www.consul.io/docs/platform/k8s/helm.html#v-synccatalog).
Catalog sync allows you to sync services between Consul and Kubernetes. The
sync can be unidirectional in either direction or bidirectional. Read the
2019-03-21 00:20:05 +00:00
[documentation](https://www.consul.io/docs/platform/k8s/service-sync.html) to
2020-04-06 20:27:35 +00:00
learn more about the configuration.
2019-03-21 00:20:05 +00:00
Services synced from Kubernetes to Consul will be discoverable, like any other
service within the Consul datacenter. Read more in the [network
connectivity](#networking-connectivity) section to learn more about related
Kubernetes configuration. Services synced from Consul to Kubernetes will be
discoverable with the built-in Kubernetes DNS once a [Consul stub
domain](https://www.consul.io/docs/platform/k8s/dns.html) is deployed. When
bidirectional catalog sync is enabled, it will behave like the two
2020-04-06 20:27:35 +00:00
unidirectional setups.
2019-03-21 00:20:05 +00:00
2020-04-06 20:27:35 +00:00
## Networking Connectivity
2019-03-21 00:20:05 +00:00
2019-04-03 21:36:02 +00:00
When running Consul as a pod inside of Kubernetes, the Consul servers will be
2019-03-21 00:20:05 +00:00
automatically configured with the appropriate addresses. However, when running
Consul servers outside of the Kubernetes cluster and clients inside Kubernetes
as pods, there are additional [networking
considerations](/consul/advanced/day-1-operations/reference-architecture#network-connectivity).
### Network Connectivity for Services
When using Consul catalog sync, to sync Kubernetes services to Consul, you will
need to ensure the Kubernetes services are supported [service
types](https://www.consul.io/docs/platform/k8s/service-sync.html#kubernetes-service-types)
and configure correctly in Kubernetes. If the service is configured correctly,
2020-04-06 20:27:35 +00:00
it will be discoverable by Consul like any other service in the datacenter.
2019-03-21 00:20:05 +00:00
2020-04-06 20:27:35 +00:00
~> Warning: You are responsible for ensuring that external services can communicate
with services deployed in the Kubernetes cluster. For example, `ClusterIP` type services
may not be directly accessible by IP address from outside the Kubernetes cluster
2019-04-03 21:36:02 +00:00
for some Kubernetes configurations.
2019-03-21 00:20:05 +00:00
### Network Security
Finally, you should consider securing your Consul datacenter with
[ACLs](/consul/advanced/day-1-operations/production-acls). ACLs should be used with [Consul
2019-03-21 00:20:05 +00:00
Connect](https://www.consul.io/docs/platform/k8s/connect.html) to secure
service to service communication. The Kubernetes cluster should also be
2020-04-06 20:27:35 +00:00
secured.
2019-03-21 00:20:05 +00:00
2020-04-06 20:27:35 +00:00
## Summary
2019-03-21 00:20:05 +00:00
You are now prepared to deploy Consul with Kubernetes. In this
guide, you were introduced to several a datacenter design for a variety of use
cases. This guide also outlined the Kubernetes prerequisites, resource
requirements for Consul, and networking considerations. Continue onto the
[Deploying Consul with Kubernetes
2019-04-03 21:36:02 +00:00
guide](/consul/getting-started-k8s/helm-deploy) for
2019-03-21 00:20:05 +00:00
information on deploying Consul with the official Helm chart or continue
2020-04-06 20:27:35 +00:00
reading about Consul Operations in the [Day 1 Path](https://learn.hashicorp.com/consul/?track=advanced#advanced).