2018-09-12 15:44:30 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
2022-01-12 23:05:01 +00:00
page_title: Installing Consul on Kubernetes
2020-04-07 18:55:19 +00:00
description: >-
Consul can run directly on Kubernetes, both in server or client mode. For
pure-Kubernetes workloads, this enables Consul to also exist purely within
Kubernetes. For heterogeneous workloads, Consul agents can join a server
running inside or outside of Kubernetes.
2018-09-12 15:44:30 +00:00
---
2019-11-28 21:23:12 +00:00
# Installing Consul on Kubernetes
2018-09-12 15:44:30 +00:00
Consul can run directly on Kubernetes, both in server or client mode.
For pure-Kubernetes workloads, this enables Consul to also exist purely
within Kubernetes. For heterogeneous workloads, Consul agents can join
a server running inside or outside of Kubernetes.
2021-09-16 23:32:13 +00:00
You can install Consul on Kubernetes using the following methods:
2021-12-17 04:26:48 +00:00
1. [Consul K8s CLI install](#consul-k8s-cli-installation)
2021-11-05 23:33:11 +00:00
1. [Helm chart install](#helm-chart-installation)
2021-09-16 23:32:13 +00:00
2021-09-14 19:25:24 +00:00
Refer to the [architecture](/docs/k8s/installation/install#architecture) section to learn more about the general architecture of Consul on Kubernetes.
For a hands-on experience with Consul as a service mesh
for Kubernetes, follow the [Getting Started with Consul service
mesh](https://learn.hashicorp.com/tutorials/consul/service-mesh-deploy?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial.
2018-09-12 15:44:30 +00:00
2021-11-05 23:33:11 +00:00
## Consul K8s CLI Installation
2022-02-01 01:26:44 +00:00
We recommend using the [Consul K8s CLI](/docs/k8s/k8s-cli) to install Consul on Kubernetes for single-cluster deployments. You can install Consul on Kubernetes using the Consul K8s CLI tool after installing the CLI.
2021-11-05 23:33:11 +00:00
2022-01-12 23:05:01 +00:00
Before beginning the installation process, verify that `kubectl` is already configured to authenticate to the Kubernetes cluster using a valid `kubeconfig` file.
2021-11-05 23:33:11 +00:00
The [Homebrew](https://brew.sh) package manager is required to complete the following installation instructions.
2022-01-20 16:26:31 +00:00
-> ** NOTE:** To deploy a previous version of Consul on Kubernetes via the CLI, you will need to first download the specific version of the CLI that matches the version of the control plane that you would like to deploy. Please follow [Install a specific version of Consul K8s CLI](/docs/k8s/installation/install-cli#install-a-specific-version-of-the-cli).
2021-11-05 23:33:11 +00:00
1. Install the HashiCorp `tap`, which is a repository of all Homebrew packages for HashiCorp:
```shell-session
2022-01-12 23:05:01 +00:00
$ brew tap hashicorp/tap
2021-11-05 23:33:11 +00:00
```
2022-01-12 23:05:01 +00:00
2021-11-05 23:33:11 +00:00
1. Install the Consul K8s CLI with the `hashicorp/tap/consul` formula.
```shell-session
2022-01-12 23:05:01 +00:00
$ brew install hashicorp/tap/consul-k8s
2021-11-05 23:33:11 +00:00
```
2022-01-12 23:05:01 +00:00
2022-02-01 01:26:44 +00:00
1. Issue the `install` subcommand to install Consul on Kubernetes. Refer to the [Consul K8s CLI reference](/docs/k8s/k8s-cli) for details about all commands and available options. Without any additional options passed, the `consul-k8s` CLI will install Consul on Kubernetes by using the Consul Helm chart's default values. Below is an example that installs Consul on Kubernetes with Service Mesh and CRDs enabled. If you did not set the `-auto-approve` option to `true`, you will be prompted to proceed with the installation if the pre-install checks pass.
2021-11-05 23:33:11 +00:00
2022-02-01 01:26:44 +00:00
-> The pre-install checks may fail if existing `PersistentVolumeClaims` (PVC) are detected. Refer to the [uninstall instructions](/docs/k8s/operations/uninstall#uninstall-consul) for information about removing PVCs.
```shell-session
$ consul-k8s install -set connectInject.enabled=true -set controller.enabled=true
2021-11-05 23:33:11 +00:00
2022-02-01 01:26:44 +00:00
==> Pre-Install Checks
No existing installations found.
2021-11-05 23:33:11 +00:00
✓ No previous persistent volume claims found
✓ No previous secrets found
2022-02-01 01:26:44 +00:00
==> Consul Installation Summary
Installation name: consul
Namespace: consul
Overrides:
connectInject:
enabled: true
controller:
enabled: true
Proceed with installation? (y/N) y
==> Running Installation
✓ Downloaded charts
--> creating 1 resource(s)
--> creating 45 resource(s)
--> beginning wait for 45 resources with timeout of 10m0s
✓ Consul installed into namespace "consul"
```
2022-02-03 18:40:06 +00:00
1. (Optional) Issue the `consul-k8s status` command to quickly glance at the status of the installed Consul cluster.
2022-02-01 01:26:44 +00:00
```shell-session
$ consul-k8s status
==> Consul-K8s Status Summary
NAME | NAMESPACE | STATUS | CHARTVERSION | APPVERSION | REVISION | LAST UPDATED
---------+-----------+----------+--------------+------------+----------+--------------------------
consul | consul | deployed | 0.40.0 | 1.11.2 | 1 | 2022/01/31 16:58:51 PST
==> Config:
connectInject:
enabled: true
controller:
enabled: true
global:
name: consul
✓ Consul servers healthy (3/3)
✓ Consul clients healthy (3/3)
```
2021-11-05 23:33:11 +00:00
2019-11-28 21:23:12 +00:00
## Helm Chart Installation
2018-09-12 15:44:30 +00:00
2021-11-05 23:33:11 +00:00
We recommend using the Consul Helm chart to install Consul on Kubernetes for multi-cluster installations that involve cross-partition of cross datacenter communication. The Helm chart installs and configures all necessary components to run Consul. The configuration enables you to run a server cluster, a client cluster, or both.
2019-11-28 21:23:12 +00:00
2021-05-25 19:36:09 +00:00
Step-by-step tutorials for how to deploy Consul to Kubernetes, please see
our [Deploy to Kubernetes](https://learn.hashicorp.com/collections/consul/kubernetes-deploy)
2021-09-14 19:25:24 +00:00
collection. This collection includes configuration caveats for single-node deployments.
2018-09-12 15:44:30 +00:00
2021-09-14 19:25:24 +00:00
The Helm chart exposes several useful configurations and automatically
sets up complex resources, but it **does not automatically operate Consul.**
You must still become familiar with how to monitor, backup,
2018-09-12 15:44:30 +00:00
upgrade, etc. the Consul cluster.
2018-09-13 21:45:40 +00:00
The Helm chart has no required configuration and will install a Consul
2021-09-14 19:25:24 +00:00
cluster with default configurations. We strongly recommend [learning about the configuration options](/docs/k8s/helm#configuration-values) prior to going to production.
2018-09-12 15:44:30 +00:00
2019-01-08 01:46:44 +00:00
~> **Security Warning:** By default, the chart will install an insecure configuration
of Consul. This provides a less complicated out-of-box experience for new users,
2021-09-14 19:25:24 +00:00
but is not appropriate for a production setup. We strongly recommend using
a properly-secured Kubernetes cluster or making sure that you understand and enable
2022-01-10 23:36:16 +00:00
the [recommended security features](/docs/security). Currently,
2019-01-08 01:46:44 +00:00
some of these features are not supported in the Helm chart and require additional
manual configuration.
2019-11-28 21:23:12 +00:00
### Prerequisites
2021-10-28 19:27:00 +00:00
The Consul Helm only supports Helm 3.2+. Install the latest version of the Helm CLI here:
2021-06-29 16:18:08 +00:00
[Installing Helm](https://helm.sh/docs/intro/install/).
2018-09-12 15:44:30 +00:00
### Installing Consul
2021-09-14 19:25:24 +00:00
1. Add the HashiCorp Helm Repository:
2018-09-12 15:44:30 +00:00
2021-09-14 19:25:24 +00:00
```shell-session
$ helm repo add hashicorp https://helm.releases.hashicorp.com
"hashicorp" has been added to your repositories
```
2019-11-28 21:23:12 +00:00
2021-09-14 19:25:24 +00:00
1. Verify that you have access to the consul chart:
2020-04-28 18:11:26 +00:00
2021-09-20 17:25:06 +00:00
```shell-session
$ helm search repo hashicorp/consul
NAME CHART VERSION APP VERSION DESCRIPTION
2021-12-17 04:26:48 +00:00
hashicorp/consul 0.39.0 1.11.1 Official HashiCorp Consul Chart
2021-09-20 17:25:06 +00:00
```
2019-11-28 21:23:12 +00:00
2021-10-28 19:27:00 +00:00
1. Prior to installing via Helm, ensure that the `consul` Kubernetes namespace does not exist, as installing on a dedicated namespace
2022-01-12 23:05:01 +00:00
is recommended.
2021-10-28 19:27:00 +00:00
```shell-session
$ kubectl get namespace
NAME STATUS AGE
default Active 18h
kube-node-lease Active 18h
kube-public Active 18h
kube-system Active 18h
```
2022-01-12 23:05:01 +00:00
1. Issue the following command to install Consul with the default configuration using Helm. You could also install Consul on a dedicated
2021-10-28 19:27:00 +00:00
namespace of your choosing by modifying the value of the `-n` flag for the Helm install.
2019-11-28 21:23:12 +00:00
2021-09-20 17:25:06 +00:00
```shell-session
2022-01-12 23:05:01 +00:00
$ helm install consul hashicorp/consul --set global.name=consul --create-namespace --namespace consul
2021-09-20 17:25:06 +00:00
NAME: consul
...
```
2021-09-14 19:25:24 +00:00
The Helm chart does everything to set up a recommended Consul-on-Kubernetes deployment.
2021-09-16 16:54:55 +00:00
After installation, a Consul cluster will be formed, a leader will be elected, and every node will have a running Consul agent.
2018-09-12 15:44:30 +00:00
2019-11-28 21:23:12 +00:00
### Customizing Your Installation
2020-04-06 20:27:35 +00:00
2019-11-28 21:23:12 +00:00
If you want to customize your installation,
create a `config.yaml` file to override the default settings.
2020-04-28 18:11:26 +00:00
You can learn what settings are available by running `helm inspect values hashicorp/consul`
2020-05-11 21:15:59 +00:00
or by reading the [Helm Chart Reference](/docs/k8s/helm).
2019-11-28 21:23:12 +00:00
2022-02-03 18:40:06 +00:00
#### Minimal `config.yaml` for Consul Service Mesh
The minimal settings to enable [Consul Service Mesh]((/docs/k8s/connect)) would be captured in the following `config.yaml` config file:
2020-04-28 18:11:26 +00:00
2021-07-31 01:37:33 +00:00
<CodeBlockConfig filename="config.yaml">
2020-04-28 18:11:26 +00:00
```yaml
global:
2021-10-22 16:14:35 +00:00
name: consul
2020-04-28 18:11:26 +00:00
connectInject:
2021-10-22 16:14:35 +00:00
enabled: true
2021-02-17 19:01:52 +00:00
controller:
2021-10-22 16:14:35 +00:00
enabled: true
2021-09-20 18:04:04 +00:00
```
2020-04-28 18:11:26 +00:00
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2022-01-12 23:05:01 +00:00
Once you've created your `config.yaml` file, run `helm install` with the `--values` flag:
2018-09-12 15:44:30 +00:00
2020-05-19 18:32:38 +00:00
```shell-session
2022-01-12 23:05:01 +00:00
$ helm install consul hashicorp/consul --create-namespace --namespace consul --values config.yaml
2020-04-28 18:11:26 +00:00
NAME: consul
...
2019-11-28 21:23:12 +00:00
```
2022-02-03 18:40:06 +00:00
#### Enable Consul Service Mesh on select namespaces
By default, Consul Service Mesh is enabled on almost all namespaces (with the exception of `kube-system` and `local-path-storage`) within a Kubernetes cluster. You can restrict this to a subset of namespaces by specifying a `namespaceSelector` that matches a label attached to each namespace denoting whether to enable Consul service mesh. In order to default to enabling service mesh on select namespaces by label, the `connectInject.default` value must be set to `true`.
<CodeBlockConfig filename="config.yaml">
```yaml
global:
name: consul
connectInject:
enabled: true
default: true
namespaceSelector: |
matchLabels:
connect-inject : enabled
controller:
enabled: true
```
</CodeBlockConfig>
Label the namespace(s), where you would like to enable Consul Service Mesh.
```shell-session
$ kubectl create ns foo
$ kubectl label namespace foo connect-inject=enabled
```
Next, run `helm install` with the `--values` flag:
```shell-session
$ helm install consul hashicorp/consul --create-namespace --namespace consul --values config.yaml
NAME: consul
...
```
#### Updating your Consul on Kubernetes configuration
2019-11-28 21:23:12 +00:00
If you've already installed Consul and want to make changes, you'll need to run
2022-01-10 23:36:16 +00:00
`helm upgrade`. See [Upgrading](/docs/k8s/upgrade) for more details.
2019-11-28 21:23:12 +00:00
## Viewing the Consul UI
2018-09-12 15:44:30 +00:00
The Consul UI is enabled by default when using the Helm chart.
2019-11-28 21:23:12 +00:00
For security reasons, it isn't exposed via a `LoadBalancer` Service by default so you must
2020-05-11 21:15:59 +00:00
use `kubectl port-forward` to visit the UI.
#### TLS Disabled
If running with TLS disabled, the Consul UI will be accessible via http on port 8500:
2018-09-12 15:44:30 +00:00
2020-05-19 18:32:38 +00:00
```shell-session
2022-01-12 23:05:01 +00:00
$ kubectl port-forward service/consul-server --namespace consul 8500:8500
2018-09-12 15:44:30 +00:00
...
```
2019-11-28 21:23:12 +00:00
Once the port is forwarded navigate to [http://localhost:8500](http://localhost:8500).
2020-05-11 21:15:59 +00:00
#### TLS Enabled
If running with TLS enabled, the Consul UI will be accessible via https on port 8501:
2020-05-19 18:32:38 +00:00
```shell-session
2022-01-12 23:05:01 +00:00
$ kubectl port-forward service/consul-server --namespace consul 8501:8501
2020-05-11 21:15:59 +00:00
...
```
Once the port is forwarded navigate to [https://localhost:8501](https://localhost:8501).
~> You'll need to click through an SSL warning from your browser because the
Consul certificate authority is self-signed and not in the browser's trust store.
#### ACLs Enabled
If ACLs are enabled, you will need to input an ACL token into the UI in order
to see all resources and make modifications.
To retrieve the bootstrap token that has full permissions, run:
2020-05-19 18:32:38 +00:00
```shell-session
2021-09-24 01:47:40 +00:00
$ kubectl get secrets/consul-bootstrap-acl-token --template='{{.data.token | base64decode }}'
2020-05-11 21:15:59 +00:00
e7924dd1-dc3f-f644-da54-81a73ba0a178%
```
Then paste the token into the UI under the ACLs tab (without the `%`).
~> NOTE: If using multi-cluster federation, your kubectl context must be in the primary datacenter
to retrieve the bootstrap token since secondary datacenters use a separate token
with less permissions.
### Exposing the UI via a service
2019-11-28 21:23:12 +00:00
If you want to expose the UI via a Kubernetes Service, configure
2020-05-11 21:15:59 +00:00
the [`ui.service` chart values](/docs/k8s/helm#v-ui-service).
2019-11-28 21:23:12 +00:00
This service will allow requests to the Consul servers so it should
not be open to the world.
## Accessing the Consul HTTP API
2018-09-12 15:44:30 +00:00
The Consul HTTP API should be accessed by communicating to the local agent
running on the same node. While technically any listening agent (client or
server) can respond to the HTTP API, communicating with the local agent
has important caching behavior, and allows you to use the simpler
2020-04-09 23:46:54 +00:00
[`/agent` endpoints for services and checks](/api/agent).
2018-09-12 15:44:30 +00:00
For Consul installed via the Helm chart, a client agent is installed on
2020-09-14 17:37:35 +00:00
each Kubernetes node. This is explained in the [architecture](/docs/k8s/installation/install#client-agents)
2018-09-12 15:44:30 +00:00
section. To access the agent, you may use the
[downward API](https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/).
2018-10-01 17:35:09 +00:00
An example pod specification is shown below. In addition to pods, anything
with a pod template can also access the downward API and can therefore also
access Consul: StatefulSets, Deployments, Jobs, etc.
2018-09-12 15:44:30 +00:00
```yaml
apiVersion: v1
kind: Pod
metadata:
name: consul-example
spec:
containers:
- name: example
2020-04-06 20:27:35 +00:00
image: 'consul:latest'
2018-09-12 15:44:30 +00:00
env:
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
command:
2020-04-06 20:27:35 +00:00
- '/bin/sh'
- '-ec'
2018-09-12 15:44:30 +00:00
- |
2020-04-06 20:27:35 +00:00
export CONSUL_HTTP_ADDR="${HOST_IP}:8500"
consul kv put hello world
2018-09-12 15:44:30 +00:00
restartPolicy: Never
```
2018-10-01 17:35:09 +00:00
An example `Deployment` is also shown below to show how the host IP can
be accessed from nested pod specifications:
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="18-28">
2018-10-01 17:35:09 +00:00
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: consul-example-deployment
spec:
replicas: 1
selector:
matchLabels:
app: consul-example
template:
metadata:
labels:
app: consul-example
spec:
containers:
- name: example
2020-04-06 20:27:35 +00:00
image: 'consul:latest'
2018-10-01 17:35:09 +00:00
env:
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
command:
2020-04-06 20:27:35 +00:00
- '/bin/sh'
- '-ec'
2018-10-01 17:35:09 +00:00
- |
2020-04-06 20:27:35 +00:00
export CONSUL_HTTP_ADDR="${HOST_IP}:8500"
consul kv put hello world
2018-10-01 17:35:09 +00:00
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2021-02-08 18:27:20 +00:00
## Next Steps
If you are still considering a move to Kubernetes, or to Consul on Kubernetes specifically, our [Migrate to Microservices with Consul Service Mesh on Kubernetes](https://learn.hashicorp.com/collections/consul/microservices?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS)
collection uses an example application written by a fictional company to illustrate why and how organizations can
migrate from monolith to microservices using Consul service mesh on Kubernetes. The case study in this collection
should provide information valuable for understanding how to develop services that leverage Consul during any stage
of your microservices journey.