2020-10-09 19:54:15 +00:00
---
layout: docs
2022-09-14 22:20:05 +00:00
page_title: Custom Resource Definitions for Consul on Kubernetes
2020-10-09 19:54:15 +00:00
description: >-
2022-09-16 15:28:32 +00:00
Consul on Kubernetes supports Consul's configuration entry kind through Custom Resource Definitions (CRDs). Learn how to configure Helm charts to enable CRDs and use kubectl to create, manage, and delete mesh components like gateways and intentions on k8s.
2020-10-09 19:54:15 +00:00
---
2022-09-14 22:20:05 +00:00
# Custom Resource Definitions (CRDs) for Consul on Kubernetes
2020-10-09 19:54:15 +00:00
2023-01-25 16:52:43 +00:00
This topic describes how to manage Consul [configuration entries](/consul/docs/agent/config-entries)
2022-11-17 23:04:29 +00:00
with Kubernetes Custom Resources. Configuration entries provide cluster-wide defaults for the service mesh.
2020-10-09 23:58:21 +00:00
2021-11-30 19:18:12 +00:00
## Supported Configuration Entries
You can specify the following values in the `kind` field. Click on a configuration entry to view its documentation:
2023-01-25 16:52:43 +00:00
- [`Mesh`](/consul/docs/connect/config-entries/mesh)
- [`ExportedServices`](/consul/docs/connect/config-entries/exported-services)
- [`PeeringAcceptor`](/consul/docs/connect/cluster-peering/k8s#peeringacceptor)
- [`PeeringDialer`](/consul/docs/connect/cluster-peering/k8s#peeringdialer)
- [`ProxyDefaults`](/consul/docs/connect/config-entries/proxy-defaults)
- [`ServiceDefaults`](/consul/docs/connect/config-entries/service-defaults)
- [`ServiceSplitter`](/consul/docs/connect/config-entries/service-splitter)
- [`ServiceRouter`](/consul/docs/connect/config-entries/service-router)
- [`ServiceResolver`](/consul/docs/connect/config-entries/service-resolver)
- [`ServiceIntentions`](/consul/docs/connect/config-entries/service-intentions)
- [`IngressGateway`](/consul/docs/connect/config-entries/ingress-gateway)
- [`TerminatingGateway`](/consul/docs/connect/config-entries/terminating-gateway)
2020-10-09 23:58:21 +00:00
## Installation
2021-11-30 19:18:12 +00:00
Verify that the minimum version of the helm chart (`0.28.0`) is installed:
2020-10-09 23:58:21 +00:00
```shell-session
$ helm search repo hashicorp/consul
NAME CHART VERSION APP VERSION DESCRIPTION
2020-12-16 16:57:43 +00:00
hashicorp/consul 0.28.0 1.9.1 Official HashiCorp Consul Chart
2020-10-09 23:58:21 +00:00
```
2021-11-30 19:18:12 +00:00
Update your helm repository cache if necessary:
2020-10-09 23:58:21 +00:00
```shell-session
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "hashicorp" chart repository
Update Complete. ⎈Happy Helming!⎈
```
2023-01-25 16:52:43 +00:00
Refer to [Install with Helm Chart](/consul/docs/k8s/installation/install) for further installation
2020-10-09 23:58:21 +00:00
instructions.
2022-11-17 23:04:29 +00:00
**Note**: Configuration entries require `connectInject` to be enabled, which is a default behavior in the official Helm Chart. If you disabled this setting, you must re-enable it to use CRDs.
2020-11-13 23:19:21 +00:00
## Upgrading An Existing Cluster to CRDs
If you have an existing Consul cluster running on Kubernetes you may need to perform
2023-01-25 16:52:43 +00:00
extra steps to migrate to CRDs. Refer to [Upgrade An Existing Cluster to CRDs](/consul/docs/k8s/crds/upgrade-to-crds) for full instructions.
2020-11-13 23:19:21 +00:00
2020-10-09 23:58:21 +00:00
## Usage
2020-10-15 17:35:26 +00:00
Once installed, you can use `kubectl` to create and manage Consul's configuration entries.
2020-10-09 23:58:21 +00:00
### Create
2022-11-17 23:04:29 +00:00
You can create configuration entries with `kubectl apply`.
2020-10-09 23:58:21 +00:00
```shell-session
2022-01-12 23:05:01 +00:00
$ cat <<EOF | kubectl apply --filename -
2020-10-09 23:58:21 +00:00
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceDefaults
metadata:
name: foo
spec:
protocol: "http"
EOF
servicedefaults.consul.hashicorp.com/foo created
```
2023-01-25 16:52:43 +00:00
Refer to [Configuration Entries](/consul/docs/agent/config-entries) for detailed schema documentation.
2020-10-09 23:58:21 +00:00
### Get
2020-10-15 17:35:26 +00:00
You can use `kubectl get [kind] [name]` to get the status of the configuration entry:
2020-10-09 23:58:21 +00:00
```shell-session
$ kubectl get servicedefaults foo
NAME SYNCED
foo True
```
The `SYNCED` status shows whether the configuration entry was successfully created
in Consul.
### Describe
2020-10-15 17:35:26 +00:00
You can use `kubectl describe [kind] [name]` to investigate the status of the
2022-11-17 23:04:29 +00:00
configuration entry. If `SYNCED` is false, the status contains the reason
2020-10-09 23:58:21 +00:00
why.
```shell-session
$ kubectl describe servicedefaults foo
Status:
Conditions:
Last Transition Time: 2020-10-09T21:15:50Z
Status: True
Type: Synced
```
### Edit
2020-10-15 17:35:26 +00:00
You can use `kubectl edit [kind] [name]` to edit the configuration entry:
2020-10-09 23:58:21 +00:00
2022-11-17 23:04:29 +00:00
```shell-session
2020-10-09 23:58:21 +00:00
$ kubectl edit servicedefaults foo
# change protocol: http => protocol: tcp
servicedefaults.consul.hashicorp.com/foo edited
```
2020-10-15 17:35:26 +00:00
You can then use `kubectl get` to ensure the change was synced to Consul:
2020-10-09 23:58:21 +00:00
```shell-session
$ kubectl get servicedefaults foo
NAME SYNCED
foo True
```
### Delete
2020-10-15 17:35:26 +00:00
You can use `kubectl delete [kind] [name]` to delete the configuration entry:
2020-10-09 23:58:21 +00:00
```shell-session
$ kubectl delete servicedefaults foo
servicedefaults.consul.hashicorp.com "foo" deleted
```
2020-10-15 17:35:26 +00:00
You can then use `kubectl get` to ensure the configuration entry was deleted:
2020-10-09 23:58:21 +00:00
```shell-session
$ kubectl get servicedefaults foo
Error from server (NotFound): servicedefaults.consul.hashicorp.com "foo" not found
```
#### Delete Hanging
If running `kubectl delete` hangs without exiting, there may be
2020-10-15 17:35:26 +00:00
a dependent configuration entry registered with Consul that prevents the target configuration entry from being
2022-11-17 23:04:29 +00:00
deleted. For example, if you set the protocol of your service to `http` in `ServiceDefaults` and then
create a `ServiceSplitter`, you will not be able to delete `ServiceDefaults`.
2020-10-09 23:58:21 +00:00
This is because by deleting the `ServiceDefaults` config, you are setting the
2022-11-17 23:04:29 +00:00
protocol back to the default which is `tcp`. Because `ServiceSplitter` requires
2020-10-09 23:58:21 +00:00
that the service has an `http` protocol, Consul will not allow the `ServiceDefaults`
to be deleted since that would put Consul into a broken state.
2020-10-15 17:35:26 +00:00
In order to delete the `ServiceDefaults` config, you would need to first delete
2020-10-09 23:58:21 +00:00
the `ServiceSplitter`.
2021-01-07 19:22:06 +00:00
## Kubernetes Namespaces
### Consul OSS
Consul Open Source (Consul OSS) ignores Kubernetes namespaces and registers all services into the same
global Consul registry based on their names. For example, service `web` in Kubernetes namespace
2022-11-17 23:04:29 +00:00
`web-ns` and service `admin` in Kubernetes namespace `admin-ns` are registered into
2021-01-07 19:22:06 +00:00
Consul as `web` and `admin` with the Kubernetes source namespace ignored.
When creating custom resources to configure these services, the namespace of the
custom resource is also ignored. For example, you can create a `ServiceDefaults`
custom resource for service `web` in the Kubernetes namespace `admin-ns` even though
the `web` service is actually running in the `web-ns` namespace (although this is not recommended):
```yaml
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceDefaults
metadata:
name: web
namespace: admin-ns
spec:
protocol: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: web-ns
spec: ...
```
2022-11-17 23:04:29 +00:00
~> **Note:** If you create two custom resources with identical `kind` and `name` values in different Kubernetes namespaces, the last one you create is not able to sync.
2021-01-07 19:22:06 +00:00
#### ServiceIntentions Special Case
`ServiceIntentions` are different from the other custom resources because the
name of the resource doesn't matter. For other resources, the name of the resource
determines which service it configures. For example, this resource configures
the service `web`:
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="4">
2021-01-07 19:22:06 +00:00
```yaml
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceDefaults
metadata:
name: web
spec:
protocol: http
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2021-01-07 19:22:06 +00:00
For `ServiceIntentions`, because we need to support the ability to create
wildcard intentions (e.g. `foo => * (allow)` meaning that `foo` can talk to **any** service),
and because `*` is not a valid Kubernetes resource name, we instead use the field `spec.destination.name`
to configure the destination service for the intention:
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="6-8,18-20">
2021-01-07 19:22:06 +00:00
```yaml
# foo => * (allow)
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceIntentions
metadata:
name: name-does-not-matter
spec:
destination:
name: '*'
sources:
- name: foo
action: allow
---
# foo => web (allow)
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceIntentions
metadata:
name: name-does-not-matter
spec:
destination:
name: web
sources:
- name: foo
action: allow
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2022-11-17 23:04:29 +00:00
~> **Note:** If two `ServiceIntentions` resources set the same `spec.destination.name`, the
last one created is not synced.
2021-01-07 19:22:06 +00:00
### Consul Enterprise <EnterpriseAlert inline />
Consul Enterprise supports multiple configurations for how Kubernetes namespaces are mapped
to Consul namespaces. The Consul namespace that the custom resource is registered
into depends on the configuration being used but in general, you should create your
2022-11-17 23:04:29 +00:00
custom resources in the same Kubernetes namespace as the service they configure.
2021-01-07 19:22:06 +00:00
The details on each configuration are:
2022-11-17 23:04:29 +00:00
1. **Mirroring** - The Kubernetes namespace is mirrored into Consul. For example, the
service `web` in Kubernetes namespace `web-ns` is registered as service `web`
2021-01-07 19:22:06 +00:00
in the Consul namespace `web-ns`. In the same vein, a `ServiceDefaults` custom resource with
2022-11-17 23:04:29 +00:00
name `web` in Kubernetes namespace `web-ns` configures that same service.
2021-01-07 19:22:06 +00:00
2023-01-25 16:52:43 +00:00
This is configured with [`connectInject.consulNamespaces`](/consul/docs/k8s/helm#v-connectinject-consulnamespaces):
2021-01-07 19:22:06 +00:00
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="6-7">
2021-01-07 19:22:06 +00:00
```yaml
global:
name: consul
enableConsulNamespaces: true
image: hashicorp/consul-enterprise:<tag>-ent
connectInject:
consulNamespaces:
mirroringK8S: true
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2022-11-17 23:04:29 +00:00
1. **Mirroring with prefix** - The Kubernetes namespace is mirrored into Consul
with a prefix added to the Consul namespace. For example, if the prefix is `k8s-` then service `web` in Kubernetes namespace `web-ns` will be registered as service `web`
2021-01-07 19:22:06 +00:00
in the Consul namespace `k8s-web-ns`. In the same vein, a `ServiceDefaults` custom resource with
2022-11-17 23:04:29 +00:00
name `web` in Kubernetes namespace `web-ns` configures that same service.
2021-01-07 19:22:06 +00:00
2023-01-25 16:52:43 +00:00
This is configured with [`connectInject.consulNamespaces`](/consul/docs/k8s/helm#v-connectinject-consulnamespaces):
2021-01-07 19:22:06 +00:00
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="8">
2021-01-07 19:22:06 +00:00
```yaml
global:
name: consul
enableConsulNamespaces: true
image: hashicorp/consul-enterprise:<tag>-ent
connectInject:
consulNamespaces:
mirroringK8S: true
mirroringK8SPrefix: k8s-
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2021-01-07 19:22:06 +00:00
1. **Single destination namespace** - The Kubernetes namespace is ignored and all services
2022-11-17 23:04:29 +00:00
are registered into the same Consul namespace. For example, if the destination Consul
namespace is `my-ns` then service `web` in Kubernetes namespace `web-ns` is registered as service `web` in Consul namespace `my-ns`.
2021-01-07 19:22:06 +00:00
In this configuration, the Kubernetes namespace of the custom resource is ignored.
For example, a `ServiceDefaults` custom resource with the name `web` in Kubernetes
2022-11-17 23:04:29 +00:00
namespace `admin-ns` configures the service with name `web` even though that
2021-01-07 19:22:06 +00:00
service is running in Kubernetes namespace `web-ns` because the `ServiceDefaults`
resource ends up registered into the same Consul namespace `my-ns`.
2023-01-25 16:52:43 +00:00
This is configured with [`connectInject.consulNamespaces`](/consul/docs/k8s/helm#v-connectinject-consulnamespaces):
2021-01-07 19:22:06 +00:00
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="7">
2021-01-07 19:22:06 +00:00
```yaml
global:
name: consul
enableConsulNamespaces: true
image: hashicorp/consul-enterprise:<tag>-ent
connectInject:
consulNamespaces:
consulDestinationNamespace: 'my-ns'
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2022-11-17 23:04:29 +00:00
~> **Note:** In this configuration, if two custom resources are created in two Kubernetes namespaces with identical `name` and `kind` values, the last one created is not synced.
2021-01-07 19:22:06 +00:00
#### ServiceIntentions Special Case (Enterprise)
`ServiceIntentions` are different from the other custom resources because the
2022-11-17 23:04:29 +00:00
name of the resource does not matter. For other resources, the name of the resource
2021-01-07 19:22:06 +00:00
determines which service it configures. For example, this resource configures
the service `web`:
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="4">
2021-01-07 19:22:06 +00:00
```yaml
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceDefaults
metadata:
name: web
spec:
protocol: http
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2021-01-07 19:22:06 +00:00
For `ServiceIntentions`, because we need to support the ability to create
2022-11-17 23:04:29 +00:00
wildcard intentions (e.g. `foo => * (allow)` meaning that `foo` can talk to any service),
2021-01-07 19:22:06 +00:00
and because `*` is not a valid Kubernetes resource name, we instead use the field `spec.destination.name`
to configure the destination service for the intention:
2021-07-31 01:37:33 +00:00
<CodeBlockConfig highlight="6-8,18-20">
2021-01-07 19:22:06 +00:00
```yaml
# foo => * (allow)
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceIntentions
metadata:
name: name-does-not-matter
spec:
destination:
name: '*'
sources:
- name: foo
action: allow
---
# foo => web (allow)
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceIntentions
metadata:
name: name-does-not-matter
spec:
destination:
name: web
sources:
- name: foo
action: allow
```
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2021-01-07 19:22:06 +00:00
In addition, we support the field `spec.destination.namespace` to configure
the destination service's Consul namespace. If `spec.destination.namespace`
2022-11-17 23:04:29 +00:00
is empty, then the Consul namespace used is the same as the other
2021-01-07 19:22:06 +00:00
config entries as outlined above.