2019-07-23 00:16:06 +00:00
|
|
|
|
---
|
2020-04-07 18:55:19 +00:00
|
|
|
|
name: Consul-Kubernetes Deployment Guide
|
2019-07-23 00:16:06 +00:00
|
|
|
|
content_length: 14
|
|
|
|
|
id: kubernetes-production-deploy
|
|
|
|
|
products_used:
|
2020-04-07 18:55:19 +00:00
|
|
|
|
- Consul
|
|
|
|
|
description: >-
|
|
|
|
|
This guide covers the necessary steps to install and configure a new Consul
|
|
|
|
|
cluster on Kubernetes.
|
|
|
|
|
level: Advanced
|
2020-04-06 20:27:35 +00:00
|
|
|
|
---
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
This guide covers the necessary steps to install and configure a new Consul
|
|
|
|
|
cluster on Kubernetes, as defined in the [Consul Reference Architecture
|
|
|
|
|
guide](/consul/day-1-operations/kubernetes-reference#consul-datacenter-deployed-in-kubernetes).
|
|
|
|
|
By the end of this guide, you will be able to identify the installation
|
|
|
|
|
prerequisites, customize the Helm chart to fit your environment requirements,
|
2020-04-06 20:27:35 +00:00
|
|
|
|
and interact with your new Consul cluster.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
~> You should have the following configured before starting this guide: Helm
|
|
|
|
|
installed and configured locally, tiller running in the Kubernetes cluster, and
|
2020-04-06 20:27:35 +00:00
|
|
|
|
the Kubernetes CLI configured.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
## Configure Kubernetes Permissions to Deploy Consul
|
|
|
|
|
|
|
|
|
|
Before deploying Consul, you will need to create a new Kubernetes service
|
|
|
|
|
account with the correct permissions and to authenticate it on the command
|
|
|
|
|
line. You will need Kubernetes operators permissions to create and modify
|
|
|
|
|
policies, deploy services, access the Kubernetes dashboard, create secrets, and
|
|
|
|
|
create RBAC objects. You can find documentation for RBAC and service accounts
|
2020-04-06 20:27:35 +00:00
|
|
|
|
for the following cloud providers.
|
|
|
|
|
|
|
|
|
|
- [AKS](https://docs.microsoft.com/en-us/azure/aks/kubernetes-service-principal)
|
|
|
|
|
- [EKS](https://docs.aws.amazon.com/eks/latest/userguide/install-aws-iam-authenticator.html)
|
2019-07-23 00:16:06 +00:00
|
|
|
|
- [GCP](https://console.cloud.google.com/iam-admin/serviceaccounts)
|
|
|
|
|
|
|
|
|
|
Note, Consul can be deployed on any properly configured Kubernetes cluster in
|
2020-04-06 20:27:35 +00:00
|
|
|
|
the cloud or on premises.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
Once you have a service account, you will also need to add a permission to
|
2020-04-06 20:27:35 +00:00
|
|
|
|
deploy the helm chart. This is done with the `clusterrolebinding` method.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
|
```shell-session
|
2019-07-23 00:16:06 +00:00
|
|
|
|
$ kubectl create clusterrolebinding kubernetes-dashboard -n kube-system --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Finally, you may need to create Kubernetes secrets to store Consul data. You
|
2020-04-06 20:27:35 +00:00
|
|
|
|
can reference these secrets in the customized Helm chart values file.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
- If you have purchased Enterprise Consul, the enterprise license file should be
|
2020-04-06 20:27:35 +00:00
|
|
|
|
used with the official image, `hashicorp/consul-enterprise:1.5.0-ent`.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
- Enable
|
2020-09-01 15:14:13 +00:00
|
|
|
|
[encryption](/docs/agent/encryption#gossip-encryption) to secure gossip traffic within the Consul cluster.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
~> Note, depending on your environment, the previous secrets may not be
|
2020-04-06 20:27:35 +00:00
|
|
|
|
necessary.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
|
## Configure Helm Chart
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
Now that you have prepared your Kubernetes cluster, you can customize the Helm
|
2020-04-06 20:27:35 +00:00
|
|
|
|
chart. First, you will need to download the latest official Helm chart.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
|
```shell-session
|
2020-04-06 20:27:35 +00:00
|
|
|
|
$ git clone https://github.com/hashicorp/consul-helm.git
|
2019-07-23 00:16:06 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `consul-helm` directory will contain a `values.yaml` file with example
|
|
|
|
|
parameters. You can update this file to customize your Consul deployment. Below
|
|
|
|
|
we detail some of the parameters you should customize and provide an example
|
|
|
|
|
file, however you should consider your particular production needs when
|
2020-04-06 20:27:35 +00:00
|
|
|
|
configuring your chart.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
### Global Values
|
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
|
The global values will affect all the other parameters in the chart.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
To enable all of the Consul components in the Helm chart, set `enabled` to
|
|
|
|
|
`true`. This means servers, clients, Consul DNS, and the Consul UI will be
|
|
|
|
|
installed with their defaults. You should also set the following global
|
2020-04-06 20:27:35 +00:00
|
|
|
|
parameters based on your specific environment requirements.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
|
- `image` is the name and tag of the Consul Docker image.
|
|
|
|
|
- `imagek8s` is the name and tag of the Docker image for the consul-k8s binary.
|
|
|
|
|
- `datacenter` the name of your Consul datacenter.
|
|
|
|
|
- `domain` the domain Consul uses for DNS queries.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
|
For security, set the `bootstrapACLs` parameter to true. This will enable
|
2019-07-23 00:16:06 +00:00
|
|
|
|
Kubernetes to initially setup Consul's [ACL
|
2020-09-01 15:14:13 +00:00
|
|
|
|
system](/docs/acl/acl-system).
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
Read the Consul Helm chart documentation to review all the [global
|
2020-09-01 15:14:13 +00:00
|
|
|
|
parameters](/docs/platform/k8s/helm#v-global).
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
### Consul UI
|
|
|
|
|
|
|
|
|
|
To enable the Consul web UI update the `ui` section to your values file and set
|
2020-04-06 20:27:35 +00:00
|
|
|
|
`enabled` to `true`.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
Note, you can also set up a [loadbalancer
|
|
|
|
|
resource](https://github.com/hashicorp/demo-consul-101/tree/master/k8s#implement-load-balancer)
|
2020-04-06 20:27:35 +00:00
|
|
|
|
or other service type in Kubernetes to make it easier to access the UI.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
### Consul Servers
|
|
|
|
|
|
|
|
|
|
For production deployments, you will need to deploy [3 or 5 Consul
|
2020-09-01 15:14:13 +00:00
|
|
|
|
servers](/docs/internals/consensus#deployment-table)
|
2019-07-23 00:16:06 +00:00
|
|
|
|
for quorum and failure tolerance. For most deployments, 3 servers are adequate.
|
|
|
|
|
|
|
|
|
|
In the server section set both `replicas` and `bootstrapExpect` to 3. This will
|
|
|
|
|
deploy three servers and cause Consul to wait to perform leader election until
|
|
|
|
|
all three are healthy. The `resources` will depend on your environment; in the
|
2020-04-06 20:27:35 +00:00
|
|
|
|
example at the end of the guide, the resources are set for a large environment.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
#### Affinity
|
|
|
|
|
|
|
|
|
|
To ensure the Consul servers are placed on different Kubernetes nodes, you will
|
|
|
|
|
need to configure affinity. Otherwise, the failure of one Kubernetes node could
|
|
|
|
|
cause the loss of multiple Consul servers, and result in quorum loss. By
|
2020-04-06 20:27:35 +00:00
|
|
|
|
default, the example `values.yaml` has affinity configured correctly.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
#### Enterprise License
|
|
|
|
|
|
|
|
|
|
If you have an [Enterprise
|
|
|
|
|
license](https://www.hashicorp.com/products/consul/enterprise) you should
|
|
|
|
|
reference the Kubernetes secret in the `enterpriseLicense` parameter.
|
|
|
|
|
|
|
|
|
|
Read the Consul Helm chart documentation to review all the [server
|
2020-09-01 15:14:13 +00:00
|
|
|
|
parameters](/docs/platform/k8s/helm#v-server)
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
### Consul Clients
|
|
|
|
|
|
|
|
|
|
A Consul client is deployed on every Kubernetes node, so you do not need to
|
|
|
|
|
specify the number of clients for your deployments. You will need to specify
|
|
|
|
|
resources and enable gRPC. The resources in the example at the end of this guide
|
|
|
|
|
should be
|
|
|
|
|
sufficient for most production scenarios since Consul clients are designed for
|
|
|
|
|
horizontal scalability. Enabling `grpc` enables the GRPC listener on port 8502
|
|
|
|
|
and exposes it to the host. It is required to use Consul Connect.
|
|
|
|
|
|
|
|
|
|
Read the Consul Helm chart documentation to review all the [client
|
2020-09-01 15:14:13 +00:00
|
|
|
|
parameters](/docs/platform/k8s/helm#v-client)
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
### Consul Connect Injection Security
|
|
|
|
|
|
|
|
|
|
Even though you enabled Consul server communication over Connect in the server section, you will also
|
|
|
|
|
need to enable `connectInject` by setting `enabled` to `true`. In the
|
|
|
|
|
`connectInject` section you will also configure security features. Enabling the
|
|
|
|
|
`default` parameter will allow the injector to automatically inject the Connect
|
|
|
|
|
sidecar into all pods. If you would prefer to manually annotate which pods to inject, you
|
2020-04-06 20:27:35 +00:00
|
|
|
|
can set this to false. Setting the 'aclBindingRuleSelector`parameter to`serviceaccount.name!=default` ensures that new services do not all receive the
|
2019-07-23 00:16:06 +00:00
|
|
|
|
same token if you are only using a default service account. This setting is
|
|
|
|
|
only necessary if you have enabled ACLs in the global section.
|
|
|
|
|
|
|
|
|
|
Read more about the [Connect Inject
|
2020-09-01 15:14:13 +00:00
|
|
|
|
parameters](/docs/platform/k8s/helm#v-connectinject).
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
## Complete Example
|
|
|
|
|
|
|
|
|
|
Your finished values file should resemble the following example. For more
|
|
|
|
|
complete descriptions of all the available parameters see the `values.yaml`
|
2020-04-06 20:27:35 +00:00
|
|
|
|
file provided with the Helm chart and the [reference
|
2020-09-01 15:14:13 +00:00
|
|
|
|
documentation](/docs/platform/k8s/helm).
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
# Configure global settings in this section.
|
|
|
|
|
global:
|
|
|
|
|
# Enable all the components within this chart by default.
|
|
|
|
|
enabled: true
|
|
|
|
|
# Specify the Consul and consul-k8s images to use
|
2020-04-06 20:27:35 +00:00
|
|
|
|
image: 'consul:1.5.0'
|
|
|
|
|
imagek8s: 'hashicorp/consul-k8s:0.8.1'
|
2019-07-23 00:16:06 +00:00
|
|
|
|
domain: consul
|
|
|
|
|
datacenter: primarydc
|
|
|
|
|
# Bootstrap ACLs within Consul. This is highly recommended.
|
|
|
|
|
bootstrapACLs: true
|
|
|
|
|
# Gossip encryption
|
|
|
|
|
gossipEncryption: |
|
|
|
|
|
secretName: "encrypt-key"
|
|
|
|
|
secretKey: "key
|
|
|
|
|
# Configure your Consul servers in this section.
|
|
|
|
|
server:
|
|
|
|
|
enabled: true
|
|
|
|
|
connect: true
|
|
|
|
|
# Specify three servers that wait till all are healthy to bootstrap the Consul cluster.
|
|
|
|
|
replicas: 3
|
|
|
|
|
bootstrapExpect: 3
|
|
|
|
|
# Specify the resources that servers request for placement. These values will serve a large environment.
|
|
|
|
|
resources: |
|
|
|
|
|
requests:
|
|
|
|
|
memory: "32Gi"
|
|
|
|
|
cpu: "4"
|
|
|
|
|
disk: "50Gi"
|
|
|
|
|
limits:
|
|
|
|
|
memory: "32Gi"
|
|
|
|
|
cpu: "4"
|
|
|
|
|
disk: "50Gi"
|
|
|
|
|
# If using Enterprise, reference the Kubernetes secret that holds your license here
|
|
|
|
|
enterpriseLicense:
|
2020-04-06 20:27:35 +00:00
|
|
|
|
secretName: 'consul-license'
|
|
|
|
|
secretKey: 'key'
|
2019-07-23 00:16:06 +00:00
|
|
|
|
# Prevent Consul servers from co-location on Kubernetes nodes.
|
|
|
|
|
affinity: |
|
2020-04-06 20:27:35 +00:00
|
|
|
|
podAntiAffinity:
|
|
|
|
|
requiredDuringSchedulingIgnoredDuringExecution:
|
|
|
|
|
- labelSelector:
|
|
|
|
|
matchLabels:
|
|
|
|
|
app: {{ template "consul.name" . }}
|
|
|
|
|
release: "{{ .Release.Name }}"
|
|
|
|
|
component: server
|
|
|
|
|
topologyKey: kubernetes.io/hostname
|
2019-07-23 00:16:06 +00:00
|
|
|
|
# Configure Consul clients in this section
|
|
|
|
|
client:
|
|
|
|
|
enabled: true
|
2020-04-06 20:27:35 +00:00
|
|
|
|
# Specify the resources that clients request for deployment.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
resources: |
|
|
|
|
|
requests:
|
|
|
|
|
memory: "8Gi"
|
|
|
|
|
cpu: "2"
|
|
|
|
|
disk: "15Gi"
|
|
|
|
|
limits:
|
|
|
|
|
memory: "8Gi"
|
|
|
|
|
cpu: "2"
|
|
|
|
|
disk: "15Gi"
|
|
|
|
|
grpc: true
|
|
|
|
|
# Enable and configure the Consul UI.
|
|
|
|
|
ui:
|
|
|
|
|
enabled: true
|
|
|
|
|
# Configure security for Consul Connect pod injection
|
|
|
|
|
connectInject:
|
|
|
|
|
enabled: true
|
|
|
|
|
default: true
|
2020-04-06 20:27:35 +00:00
|
|
|
|
namespaceSelector: 'my-namespace'
|
|
|
|
|
aclBindingRuleSelector: “serviceaccount.name!=default”
|
2019-07-23 00:16:06 +00:00
|
|
|
|
```
|
2020-04-06 20:27:35 +00:00
|
|
|
|
|
|
|
|
|
## Deploy Consul
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
Now that you have customized the `values.yml` file, you can deploy Consul with
|
|
|
|
|
Helm. This should only take a few minutes. The Consul pods should appear in the
|
|
|
|
|
Kubernetes dashboard immediately and you can monitor the deployment process
|
|
|
|
|
there.
|
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
|
```shell-session
|
2020-04-06 20:27:35 +00:00
|
|
|
|
$ helm install ./consul-helm -f values.yaml
|
2019-07-23 00:16:06 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To check the deployment process on the command line you can use `kubectl`.
|
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
|
```shell-session
|
2020-04-06 20:27:35 +00:00
|
|
|
|
$ kubectl get pods
|
2019-07-23 00:16:06 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Summary
|
|
|
|
|
|
|
|
|
|
In this guide, you configured Consul, using the Helm chart, for a production
|
|
|
|
|
environment. This involved ensuring that your cluster had a properly
|
|
|
|
|
distributed server cluster, specifying enough resources for your agents,
|
|
|
|
|
securing the cluster with ACLs and gossip encryption, and enabling other Consul
|
2020-04-06 20:27:35 +00:00
|
|
|
|
functionality including Connect and the Consul UI.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
|
Now you can interact with your Consul cluster through the UI or CLI.
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
If you exposed the UI using a load balancer it will be available at the
|
|
|
|
|
`LoadBalancer Ingress` IP address and `Port` that is output from the following
|
|
|
|
|
command. Note, you will need to replace _consul server_ with the server name
|
|
|
|
|
from your cluster.
|
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
|
```shell-session
|
2020-04-06 20:27:35 +00:00
|
|
|
|
$ kubectl describe services consul-server
|
|
|
|
|
```
|
2019-07-23 00:16:06 +00:00
|
|
|
|
|
|
|
|
|
To access the Consul CLI, open a terminal session using the Kubernetes CLI.
|
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
|
```shell-session
|
2020-04-06 20:27:35 +00:00
|
|
|
|
$ kubectl exec <pod name> -it /bin/ash
|
2019-07-23 00:16:06 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To learn more about how to interact with your Consul cluster or use it for
|
|
|
|
|
service discovery, configuration or segmentation, try one of Learn’s
|
|
|
|
|
[Operations or Development tracks](/consul/#advanced). Follow the [Security and
|
|
|
|
|
Networking track](/consul/?track=security-networking#security-networking) to
|
|
|
|
|
learn more about securing your Consul cluster.
|