2018-09-24 00:12:14 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
2022-09-14 22:26:14 +00:00
page_title: Service Sync for Consul on Kubernetes
2020-04-07 18:55:19 +00:00
description: >-
2022-09-16 15:28:32 +00:00
Service sync is a Consul on Kubernetes feature that makes Kubernetes and Consul services available to each other. Learn how to configure Helm values so services can communicate and make Kubernetes services appear in the Consul UI.
2018-09-24 00:12:14 +00:00
---
2022-09-14 22:26:14 +00:00
# Service Sync for Consul on Kubernetes
2018-09-24 00:12:14 +00:00
The services in Kubernetes and Consul can be automatically synced so that Kubernetes
services are available to Consul agents and services in Consul can be available
as first-class Kubernetes services. This functionality is provided by the
[consul-k8s project](https://github.com/hashicorp/consul-k8s) and can be
automatically installed and configured using the
2023-01-25 16:52:43 +00:00
[Consul Helm chart](/consul/docs/k8s/installation/install).
2018-09-24 00:12:14 +00:00
2022-01-11 16:16:24 +00:00
![screenshot of a Kubernetes service in the UI](/img/k8s-service.png)
2022-01-10 20:57:15 +00:00
2018-09-24 00:12:14 +00:00
**Why sync Kubernetes services to Consul?** Kubernetes services synced to the
2018-09-26 02:55:32 +00:00
Consul catalog enable Kubernetes services to be accessed by any node that
is part of the Consul cluster, including other distinct Kubernetes clusters.
For non-Kubernetes nodes, they can access services using the standard
2023-01-25 16:52:43 +00:00
[Consul DNS](/consul/docs/discovery/dns) or HTTP API.
2018-09-24 00:12:14 +00:00
**Why sync Consul services to Kubernetes?** Syncing Consul services to
2022-11-17 23:04:29 +00:00
Kubernetes services enables non-Kubernetes services to be accessed using kube-dns and Kubernetes-specific
environment variables. This integration makes it very easy to automate external
2018-09-24 00:12:14 +00:00
service discovery, including hosted services like databases.
2022-11-17 23:04:29 +00:00
## Installation and configuration
2018-09-24 00:12:14 +00:00
2023-01-25 16:52:43 +00:00
~> Enabling both Service Mesh and Service Sync on the same Kubernetes services is not supported, as Service Mesh also registers Kubernetes service instances to Consul. Ensure that Service Sync is only enabled for namespaces and services that are not injected with the Consul sidecar for Service Mesh as described in [Sync Enable/Disable](/consul/docs/k8s/service-sync#sync-enable-disable).
2022-01-25 23:02:19 +00:00
2022-11-17 23:04:29 +00:00
The service sync uses an external long-running process in the
2018-09-24 00:12:14 +00:00
[consul-k8s project](https://github.com/hashicorp/consul-k8s). This process
2022-11-17 23:04:29 +00:00
can run either inside or outside of a Kubernetes cluster. However, running this process within
2018-09-24 00:12:14 +00:00
the Kubernetes cluster is generally easier since it is automated using the
2023-01-25 16:52:43 +00:00
[Helm chart](/consul/docs/k8s/helm).
2018-09-24 00:12:14 +00:00
2018-09-26 13:41:25 +00:00
The Consul server cluster can run either in or out of a Kubernetes cluster.
The Consul server cluster does not need to be running on the same machine
or same platform as the sync process. The sync process needs to be configured
2020-02-04 19:05:25 +00:00
with the address to a Consul agent as well as any additional access
2018-09-26 13:41:25 +00:00
information such as ACL tokens.
2020-02-04 19:05:25 +00:00
To install the sync process, enable the catalog sync feature using
2023-01-25 16:52:43 +00:00
[Helm values](/consul/docs/k8s/helm#configuration-values) and
2018-09-24 00:12:14 +00:00
upgrade the installation using `helm upgrade` for existing installs or
`helm install` for a fresh install.
```yaml
syncCatalog:
enabled: true
```
2022-11-17 23:04:29 +00:00
This value enables service syncing in both direction. You can also disable a direction so that only Kubernetes services sync to Consul, or only Consul services sync to Kubernetes.
2019-09-06 09:33:13 +00:00
To only enable syncing Consul services to Kubernetes use the config:
```yaml
syncCatalog:
enabled: true
toConsul: false
toK8S: true
```
To only enable syncing Kubernetes services to Consul use:
```yaml
syncCatalog:
enabled: true
toConsul: true
toK8S: false
```
2023-01-25 16:52:43 +00:00
Refer to the [Helm configuration](/consul/docs/k8s/helm#v-synccatalog)
2018-09-24 00:12:14 +00:00
for more information.
### Authentication
The sync process must authenticate to both Kubernetes and Consul to read
and write services.
2022-11-17 23:04:29 +00:00
If running `consul-k8s` using the Helm chart, then this authentication is handled for you.
2019-09-06 09:33:13 +00:00
If running `consul-k8s` outside of Kubernetes, a valid kubeconfig file must be provided with cluster
2022-11-17 23:04:29 +00:00
and authentication information. The sync process looks into the default locations
2018-09-24 00:12:14 +00:00
for both in-cluster and out-of-cluster authentication. If `kubectl` works,
then the sync program should work.
2022-11-17 23:04:29 +00:00
If ACLs are configured on the Consul cluster, you need to provide a Consul
2023-01-25 16:52:43 +00:00
[ACL token](/consul/tutorials/security/access-control-setup-production). Review the [ACL rules](/consul/docs/security/acl/acl-rules)
2019-02-07 21:26:17 +00:00
when creating this token so that it only allows the necessary privileges. The catalog
2023-01-25 16:52:43 +00:00
sync process accepts this token by using the [`CONSUL_HTTP_TOKEN`](/consul/commands#consul_http_token)
2019-02-07 21:26:17 +00:00
environment variable. This token should be set as a
2019-01-29 23:37:13 +00:00
[Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/#creating-your-own-secrets)
and referenced in the Helm chart.
2018-09-24 00:12:14 +00:00
## Kubernetes to Consul
This sync registers Kubernetes services to the Consul catalog automatically.
2022-11-17 23:04:29 +00:00
This sync enables discovery and connection to Kubernetes services using native
Consul service discovery protocols such as DNS or HTTP. This is particularly useful for
2018-09-24 00:12:14 +00:00
non-Kubernetes nodes. This also causes all discoverable services to be part of
a central service catalog in Consul for further syncing into alternate
Kubernetes clusters or other platforms.
2022-11-17 23:04:29 +00:00
Each synced service is registered onto a Consul node called `k8s-sync`. This node
is not a real node. Instead, the catalog sync process is monitoring Kubernetes
2020-08-12 23:34:55 +00:00
and syncing the services to Consul.
2022-11-17 23:04:29 +00:00
### Kubernetes service types
2018-09-24 00:12:14 +00:00
Not all Kubernetes services are externally accessible. The sync program by
2022-11-17 23:04:29 +00:00
default only syncs services of the following types or configurations.
If a service type is not listed below, then the sync program ignores that
2018-09-24 00:12:14 +00:00
service type.
#### NodePort
[NodePort services](https://kubernetes.io/docs/concepts/services-networking/service/#nodeport)
register a static port that every node in the K8S cluster listens on.
2022-11-17 23:04:29 +00:00
For NodePort services, a Consul service instance is created for each
2018-09-24 00:12:14 +00:00
node that has the representative pod running. While Kubernetes configures
a static port on all nodes in the cluster, this limits the number of service
instances to be equal to the nodes running the target pods.
2022-11-17 23:04:29 +00:00
By default it uses the external IP of the node but this can be configured through
2023-01-25 16:52:43 +00:00
the [`nodePortSyncType` helm option](/consul/docs/k8s/helm#v-synccatalog-nodeportsynctype).
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
The service instance's port is set to the first defined node port of the service unless
2023-01-25 16:52:43 +00:00
set specifically in the `consul.hashicorp.com/service-port` annotation. Refer to [Service Ports](/consul/docs/k8s/service-sync#service-ports) for more information.
2018-09-24 00:12:14 +00:00
#### LoadBalancer
2022-11-17 23:04:29 +00:00
For LoadBalancer services, a single service instance is registered with
2018-09-24 00:12:14 +00:00
the external IP of the created load balancer. Because this is already a load
2022-11-17 23:04:29 +00:00
balancer, only one service instance is registered with Consul rather
2018-09-24 00:12:14 +00:00
than registering each individual pod endpoint.
2022-11-17 23:04:29 +00:00
The service instance's port is set to the first defined port of the
2023-01-25 16:52:43 +00:00
service unless set specifically in the `consul.hashicorp.com/service-port` annotation. Refer to [Service Ports](/consul/docs/k8s/service-sync#service-ports) for more information.
2020-08-12 23:34:55 +00:00
2018-09-24 00:12:14 +00:00
#### External IPs
Any service type may specify an
"[external IP](https://kubernetes.io/docs/concepts/services-networking/service/#external-ips)"
configuration. The external IP must be configured by some other system, but
2022-11-17 23:04:29 +00:00
any service discovery resolves to this set of IP addresses rather than a
2018-09-24 00:12:14 +00:00
virtual IP.
2022-11-17 23:04:29 +00:00
If an external IP list is present, a service instance in Consul is created
2018-09-24 00:12:14 +00:00
for each external IP. It is assumed that if an external IP is present that it
is routable and configured by some other system.
2022-11-17 23:04:29 +00:00
The service instance's port is set to the _first_ defined port of the
2023-01-25 16:52:43 +00:00
service unless set specifically with the `consul.hashicorp.com/service-port` annotation. Refer to [Service Ports](/consul/docs/k8s/service-sync#service-ports) for more information.
2020-08-12 23:34:55 +00:00
2018-12-08 00:04:35 +00:00
#### ClusterIP
2020-08-12 23:34:55 +00:00
ClusterIP services are synced by default as of `consul-k8s` version 0.3.0.
2022-11-17 23:04:29 +00:00
Each pod that is an endpoint for the service is synced as a Consul service
2020-08-12 23:34:55 +00:00
instance with its IP set to the pod IP and its port set to the `targetPort`.
2023-01-25 16:52:43 +00:00
The service instance's port can be overridden with the `consul.hashicorp.com/service-port` annotation. Refer to [Service Ports](/consul/docs/k8s/service-sync#service-ports) for more information.
2020-08-12 23:34:55 +00:00
-> In Kubernetes clusters where pod IPs are not accessible outside the cluster,
the services registered in Consul may not be routable. To
2023-01-25 16:52:43 +00:00
skip syncing ClusterIP services, set [`syncClusterIPServices`](/consul/docs/k8s/helm#v-synccatalog-syncclusteripservices)
2018-12-08 00:04:35 +00:00
to `false` in the Helm chart values file.
2022-11-17 23:04:29 +00:00
### Enable and disable sync
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
By default, all valid service types are synced from every Kubernetes
namespace except for `kube-system` and `kube-public`.
To only sync specific services, first modify the annotation to set the default to `false`:
2020-02-04 19:05:25 +00:00
```yaml
syncCatalog:
enabled: true
default: false
```
2022-11-17 23:04:29 +00:00
Then, explicitly enable syncing specific services with the `consul.hashicorp.com/service-sync` annotation:
2018-09-24 00:12:14 +00:00
```yaml
kind: Service
apiVersion: v1
metadata:
name: my-service
annotations:
2020-04-06 20:27:35 +00:00
'consul.hashicorp.com/service-sync': 'true'
2020-02-04 19:05:25 +00:00
```
2022-11-17 23:04:29 +00:00
-> **Note:** If the annotation is set to `false` when the default sync is `true`, the service does not sync.
2020-02-04 19:05:25 +00:00
You can allow or deny syncing from specific Kubernetes namespaces by setting the
`k8sAllowNamespaces` and `k8sDenyNamespaces` keys:
```yaml
syncCatalog:
enabled: true
default: true
2020-04-06 20:27:35 +00:00
k8sAllowNamespaces: ['*']
k8sDenyNamespaces: ['kube-system', 'kube-public']
2020-02-04 19:05:25 +00:00
```
2022-11-17 23:04:29 +00:00
In the default configuration, services from all namespaces except for
`kube-system` and `kube-public` are synced.
2020-02-04 19:05:25 +00:00
2022-11-17 23:04:29 +00:00
To only sync from specific namespaces, you can list only those
2020-02-04 19:05:25 +00:00
namespaces in the `k8sAllowNamespaces` key:
```yaml
syncCatalog:
enabled: true
default: true
2020-04-06 20:27:35 +00:00
k8sAllowNamespaces: ['my-ns-1', 'my-ns-2']
2020-02-04 19:05:25 +00:00
k8sDenyNamespaces: []
```
2022-11-17 23:04:29 +00:00
To sync from every namespace except specific namespaces, use `*` in the allow list and then specify the non-syncing namespaces in the deny list:
2020-02-04 19:05:25 +00:00
```yaml
syncCatalog:
enabled: true
default: true
2020-04-06 20:27:35 +00:00
k8sAllowNamespaces: ['*']
k8sDenyNamespaces: ['no-sync-ns-1', 'no-sync-ns-2']
2018-09-24 00:12:14 +00:00
```
2022-11-17 23:04:29 +00:00
-> **Note:** The deny list takes precedence over the allow list. If a namespace
is listed in both lists, it does not sync.
2020-02-04 19:05:25 +00:00
2022-11-17 23:04:29 +00:00
### Service name
2018-09-24 00:12:14 +00:00
When a Kubernetes service is synced to Consul, the name of the service in Consul
2022-11-17 23:04:29 +00:00
by default is the value of the `name` metadata on that Kubernetes service.
2018-09-24 00:12:14 +00:00
This makes it so that service sync works with zero configuration changes.
2022-11-17 23:04:29 +00:00
This setting can be overridden using an annotation to specify the Consul service name:
2018-09-24 00:12:14 +00:00
```yaml
kind: Service
apiVersion: v1
metadata:
name: my-service
annotations:
2020-04-06 20:27:35 +00:00
'consul.hashicorp.com/service-name': my-consul-service
2018-09-24 00:12:14 +00:00
```
2022-11-17 23:04:29 +00:00
**If a conflicting service name exists in Consul,** the sync program
registers additional instances to that same service. Therefore, services inside
2018-09-24 00:12:14 +00:00
and outside of Kubernetes should have different names unless you want either
side to potentially connect. This default behavior also enables gracefully
2022-11-17 23:04:29 +00:00
transitioning a service between deployments inside and outside of Kubernetes.
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
### Service ports
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
When syncing the Kubernetes service to Consul, the Consul service port is the first defined port in the service. Additionally, all ports become registered in the service instance metadata with the key "port-X," where X is
2018-09-24 00:12:14 +00:00
the name of the port and the value is the externally accessible port.
The default service port can be overridden using an annotation:
```yaml
kind: Service
apiVersion: v1
metadata:
name: my-service
annotations:
2020-04-06 20:27:35 +00:00
'consul.hashicorp.com/service-port': 'http'
2018-09-24 00:12:14 +00:00
```
2019-09-17 19:05:04 +00:00
The annotation value may be a name of a port (recommended) or an exact port value.
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
### Service tags
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
A service registered in Consul from Kubernetes always has the tag "k8s" added
to it. Additional tags can be specified with a comma-separated annotation value. These custom tags automatically include the "k8s" tag, which can't be disabled. When specifying values, use commas without whitespace.
2018-09-24 00:12:14 +00:00
```yaml
kind: Service
apiVersion: v1
metadata:
name: my-service
annotations:
2020-04-06 20:27:35 +00:00
'consul.hashicorp.com/service-tags': 'primary,foo'
2018-09-24 00:12:14 +00:00
```
2022-11-17 23:04:29 +00:00
### Service meta
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
A service registered in Consul from Kubernetes sets the `external-source` key to
`kubernetes`. This can be used from the APLI, CLI and UI to filter
service instances that are set in k8s. The Consul UI displays a Kubernetes icon next to all externally
2018-09-26 02:55:32 +00:00
registered services from Kubernetes.
2018-09-24 00:12:14 +00:00
Additional metadata can be specified using annotations. The "KEY" below can be
2022-11-17 23:04:29 +00:00
set to any key. This allows setting multiple meta values.
2018-09-24 00:12:14 +00:00
```yaml
kind: Service
apiVersion: v1
metadata:
name: my-service
annotations:
2020-04-06 20:27:35 +00:00
'consul.hashicorp.com/service-meta-KEY': 'value'
2018-09-24 00:12:14 +00:00
```
2020-02-04 19:05:25 +00:00
### Consul Enterprise Namespaces
Consul Enterprise supports Consul namespaces. These can be used when syncing
2022-11-17 23:04:29 +00:00
from Kubernetes to Consul. However, namespaces are not supported when syncing from Consul to Kubernetes.
2020-02-04 19:05:25 +00:00
There are three options available:
2020-04-06 20:27:35 +00:00
1. **Single Destination Namespace** – Sync all Kubernetes services, regardless of namespace,
into the same Consul namespace.
2020-02-04 19:05:25 +00:00
2020-04-09 00:09:01 +00:00
```yaml
global:
enableConsulNamespaces: true
2020-04-06 20:27:35 +00:00
2020-04-09 00:09:01 +00:00
syncCatalog:
enabled: true
consulNamespaces:
consulDestinationNamespace: 'my-consul-ns'
```
2020-04-06 20:27:35 +00:00
2022-11-17 23:04:29 +00:00
1. **Mirror Namespaces** - Each Kubernetes service is synced to a Consul namespace with the same
2020-04-09 00:09:01 +00:00
name as its Kubernetes namespace.
2020-04-06 20:27:35 +00:00
2022-11-17 23:04:29 +00:00
For example, service `foo` in Kubernetes namespace `ns-1` is synced to the Consul namespace `ns-1`.
If a mirrored namespace does not exist in Consul, it is created automatically.
2020-04-07 18:55:19 +00:00
2020-04-09 00:09:01 +00:00
```yaml
2020-02-04 19:05:25 +00:00
global:
2020-04-09 00:09:01 +00:00
enableConsulNamespaces: true
2020-04-06 20:27:35 +00:00
2020-04-09 00:09:01 +00:00
syncCatalog:
enabled: true
consulNamespaces:
mirroringK8S: true
2020-02-04 19:05:25 +00:00
2020-04-09 00:09:01 +00:00
addK8SNamespaceSuffix: false
```
2020-04-07 18:55:19 +00:00
2022-11-17 23:04:29 +00:00
1. **Mirror Namespaces With Prefix** - Each Kubernetes service is synced to a Consul namespace
with the same name as its Kubernetes namespace with a prefix. For example, given a prefix
`k8s-`, service `foo` in Kubernetes namespace `ns-1` syncs to the Consul namespace
2020-04-09 00:09:01 +00:00
`k8s-ns-1`.
2020-04-06 20:27:35 +00:00
2020-04-09 00:09:01 +00:00
```yaml
global:
enableConsulNamespaces: true
2020-04-06 20:27:35 +00:00
2020-04-09 00:09:01 +00:00
syncCatalog:
enabled: true
consulNamespaces:
mirroringK8S: true
mirroringK8SPrefix: 'k8s-'
2020-04-06 20:27:35 +00:00
2020-04-09 00:09:01 +00:00
addK8SNamespaceSuffix: false
```
2020-02-04 19:05:25 +00:00
2022-11-17 23:04:29 +00:00
-> Note that in both mirroring examples, `addK8SNamespaceSuffix` is set to `false`. If set to its default value, `true`, the Kubernetes namespace is added as a suffix to each
2020-02-04 19:05:25 +00:00
Consul service name. For example Kubernetes service `foo` in namespace `k8s-ns`
would be registered into Consul with the name `foo-k8s-ns`.
This is useful when syncing from multiple Kubernetes namespaces to
2022-11-17 23:04:29 +00:00
a single Consul namespace. However, you may want to disable this behavior
when mirroring namespaces so that services do not overlap with services from
2020-02-04 19:05:25 +00:00
other namespaces.
2018-09-24 00:12:14 +00:00
## Consul to Kubernetes
This syncs Consul services into first-class Kubernetes services.
2022-11-17 23:04:29 +00:00
The sync creates an [`ExternalName`](https://kubernetes.io/docs/concepts/services-networking/service/#externalname) for each Consul service. The "external name" is the Consul DNS name.
2019-09-06 09:33:13 +00:00
2022-11-17 23:04:29 +00:00
For example, given a Consul service `foo`:
2019-09-06 09:33:13 +00:00
```yaml
apiVersion: v1
kind: Service
metadata:
name: foo
...
spec:
externalName: foo.service.consul
type: ExternalName
```
2019-09-06 09:34:11 +00:00
With Consul To Kubernetes syncing enabled, DNS requests of the form `<consul-service-name>`
2022-11-17 23:04:29 +00:00
are serviced by Consul DNS. From a different Kubernetes namespace than where Consul
2019-09-06 09:34:11 +00:00
is deployed, the DNS request would need to be `<consul-service-name>.<consul-namespace>`.
2018-09-24 00:12:14 +00:00
2023-01-25 16:52:43 +00:00
-> **Note:** Consul to Kubernetes syncing is not required if you enabled [Consul DNS on Kubernetes](/consul/docs/k8s/dns).
2022-11-17 23:04:29 +00:00
All you need to do is address services in the form `<consul-service-name>.service.consul`, so you do not need Kubernetes `Service` objects created.
2018-09-24 00:12:14 +00:00
2019-09-06 09:34:11 +00:00
~> **Requires Consul DNS via CoreDNS in Kubernetes:** This feature requires that
2023-01-25 16:52:43 +00:00
[Consul DNS](/consul/docs/k8s/dns) is configured within Kubernetes.
2022-11-17 23:04:29 +00:00
Additionally, [CoreDNS](https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/#config-coredns)
is required instead of kube-dns to resolve an
2018-09-26 04:37:21 +00:00
issue with resolving `externalName` services pointing to custom domains.
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
### Enable and disable sync
2018-09-24 00:12:14 +00:00
2018-09-26 02:55:32 +00:00
All Consul services visible to the sync process based on its given ACL token
2022-11-17 23:04:29 +00:00
are synced to Kubernetes.
2018-09-24 00:12:14 +00:00
There is no way to change this behavior per service. For the opposite sync
direction (Kubernetes to Consul), you can use Kubernetes annotations to disable
a sync per service. This is not currently possible for Consul to Kubernetes
sync and the ACL token must be used to limit what services are synced.
In the future, we hope to support per-service configuration.
### Service Name
When a Consul service is synced to Kubernetes, the name of the Kubernetes
2022-11-17 23:04:29 +00:00
service matches the name of the Consul service exactly.
2018-09-24 00:12:14 +00:00
2018-09-26 02:55:32 +00:00
To change this default exact match behavior, it is possible to specify a
prefix to be added to service names within Kubernetes by using the
`-k8s-service-prefix` flag. This can also be specified in the Helm
configuration.
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
**If a conflicting service is found**: the service is not synced. This
2018-09-24 00:12:14 +00:00
does not match the Kubernetes to Consul behavior, but given the current
2022-11-17 23:04:29 +00:00
implementation we must do this because Kubernetes cannott mix both CNAME and
2018-09-24 00:12:14 +00:00
Endpoint-based services.
2022-11-17 23:04:29 +00:00
### Kubernetes service labels and annotations
2018-09-24 00:12:14 +00:00
2022-11-17 23:04:29 +00:00
Any Consul services synced to Kubernetes are labeled and annotated.
An annotation `consul.hashicorp.com/synced` is set to `true` to note
2018-09-24 00:12:14 +00:00
that this is a synced service from Consul.
2022-11-17 23:04:29 +00:00
Additionally, a label `consul=true` is specified so that label selectors
2018-09-24 00:12:14 +00:00
can be used with `kubectl` and other tooling to easily filter all Consul-synced
services.