github.com/KyaXTeam/consul@v1.4.5/website/source/docs/platform/k8s/service-sync.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Service Sync - Kubernetes"
     4  sidebar_current: "docs-platform-k8s-service-sync"
     5  description: |-
     6    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.
     7  ---
     8  
     9  # Syncing Kubernetes and Consul Services
    10  
    11  The services in Kubernetes and Consul can be automatically synced so that Kubernetes
    12  services are available to Consul agents and services in Consul can be available
    13  as first-class Kubernetes services. This functionality is provided by the
    14  [consul-k8s project](https://github.com/hashicorp/consul-k8s) and can be
    15  automatically installed and configured using the
    16  [Consul Helm chart](/docs/platform/k8s/helm.html).
    17  
    18  **Why sync Kubernetes services to Consul?** Kubernetes services synced to the
    19  Consul catalog enable Kubernetes services to be accessed by any node that
    20  is part of the Consul cluster, including other distinct Kubernetes clusters.
    21  For non-Kubernetes nodes, they can access services using the standard
    22  [Consul DNS](/docs/agent/dns.html) or HTTP API.
    23  
    24  **Why sync Consul services to Kubernetes?** Syncing Consul services to
    25  Kubernetes services enables non-Kubernetes services (such as external to
    26  the cluster) to be accessed in a native Kubernetes way: using kube-dns,
    27  environment variables, etc. This makes it very easy to automate external
    28  service discovery, including hosted services like databases.
    29  
    30  ## Installation and Configuration
    31  
    32  The service sync is done using an external long-running process in the
    33  [consul-k8s project](https://github.com/hashicorp/consul-k8s). This process
    34  can run either in or out of a Kubernetes cluster. However, running this within
    35  the Kubernetes cluster is generally easier since it is automated using the
    36  [Helm chart](/docs/platform/k8s/helm.html).
    37  
    38  The Consul server cluster can run either in or out of a Kubernetes cluster.
    39  The Consul server cluster does not need to be running on the same machine
    40  or same platform as the sync process. The sync process needs to be configured
    41  with the address to the Consul cluster as well as any additional access
    42  information such as ACL tokens.
    43  
    44  To install the sync, enable the catalog sync feature using
    45  [Helm values](/docs/platform/k8s/helm.html#configuration-values-) and
    46  upgrade the installation using `helm upgrade` for existing installs or
    47  `helm install` for a fresh install.
    48  
    49  ```yaml
    50  syncCatalog:
    51    enabled: true
    52  ```
    53  
    54  This will enable services to sync _in both directions_. You can also choose
    55  to only sync Kubernetes services to Consul or vice versa by disabling a direction.
    56  See the [Helm configuration](/docs/platform/k8s/helm.html#configuration-values-)
    57  for more information.
    58  
    59  -> **Before installing,** please read the introduction paragraphs for the
    60  reference documentation below for both
    61  [Kubernetes to Consul](/docs/platform/k8s/service-sync.html#kubernetes-to-consul) and
    62  [Consul to Kubernetes](/docs/platform/k8s/service-sync.html#consul-to-kubernetes)
    63  sync to understand how the syncing works.
    64  
    65  ### Authentication
    66  
    67  The sync process must authenticate to both Kubernetes and Consul to read
    68  and write services.
    69  
    70  For Kubernetes, a valid kubeconfig file must be provided with cluster
    71  and authentication information. The sync process will look into the default locations
    72  for both in-cluster and out-of-cluster authentication. If `kubectl` works,
    73  then the sync program should work.
    74  
    75  For Consul, if ACLs are configured on the cluster, a Consul
    76  [ACL token](https://learn.hashicorp.com/consul/advanced/day-1-operations/acl-guide)
    77  will need to be provided. Review the [ACL rules](/docs/agent/acl-rules.html)
    78  when creating this token so that it only allows the necessary privileges. The catalog
    79  sync process accepts this token by using the [`CONSUL_HTTP_TOKEN`](/docs/commands/index.html#consul_http_token)
    80  environment variable. This token should be set as a
    81  [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/#creating-your-own-secrets)
    82  and referenced in the Helm chart.
    83  
    84  ## Kubernetes to Consul
    85  
    86  This sync registers Kubernetes services to the Consul catalog automatically.
    87  
    88  This enables discovery and connection to Kubernetes services using native
    89  Consul service discovery such as DNS or HTTP. This is particularly useful for
    90  non-Kubernetes nodes. This also causes all discoverable services to be part of
    91  a central service catalog in Consul for further syncing into alternate
    92  Kubernetes clusters or other platforms.
    93  
    94  ### Kubernetes Service Types
    95  
    96  Not all Kubernetes services are externally accessible. The sync program by
    97  default will only sync services of the following types or configurations.
    98  If a service type is not listed below, then the sync program will ignore that
    99  service type.
   100  
   101  #### NodePort
   102  
   103  [NodePort services](https://kubernetes.io/docs/concepts/services-networking/service/#nodeport)
   104  register a static port that every node in the K8S cluster listens on.
   105  
   106  For NodePort services, a Consul service instance will be created for each
   107  node that has the representative pod running. While Kubernetes configures
   108  a static port on all nodes in the cluster, this limits the number of service
   109  instances to be equal to the nodes running the target pods.
   110  
   111  The service instances will be registered to the Kubernetes node name
   112  that each instance lives on. This is guaranteed unique by Kubernetes. An
   113  existing node entry will be used if it is already part of the Consul
   114  cluster (for example if you're running a client agent on all Kubernetes
   115  nodes). This allows the normal agent health checks for that node to continue
   116  working.
   117  
   118  #### LoadBalancer
   119  
   120  For LoadBalancer services, a single service instance will be registered with
   121  the external IP of the created load balancer. Because this is already a load
   122  balancer, only one service instance will be registered with Consul rather
   123  than registering each individual pod endpoint.
   124  
   125  #### External IPs
   126  
   127  Any service type may specify an
   128  "[external IP](https://kubernetes.io/docs/concepts/services-networking/service/#external-ips)"
   129  configuration. The external IP must be configured by some other system, but
   130  any service discovery will resolve to this set of IP addresses rather than a
   131  virtual IP.
   132  
   133  If an external IP list is present, a service instance in Consul will be created
   134  for each external IP. It is assumed that if an external IP is present that it
   135  is routable and configured by some other system.
   136  
   137  #### ClusterIP
   138  
   139  ClusterIP services are synced by default as of `consul-k8s` version 0.3.0. In 
   140  many Kubernetes clusters, ClusterIPs may not be accessible outside of the cluster,
   141  so you may end up with services registered in Consul that are not routeable. To
   142  skip syncing ClusterIP services, set [`syncClusterIPServices`](/docs/platform/k8s/helm.html#v-synccatalog-clusterip-sync)
   143  to `false` in the Helm chart values file.
   144  
   145  ### Sync Enable/Disable
   146  
   147  By default, all valid services (as explained above) are synced. This default can
   148  be changed using the [configuration](/docs/platform/k8s/helm.html#v-synccatalog-default).
   149  Syncing can also be explicitly enabled or disabled using an
   150  annotation:
   151  
   152  ```yaml
   153  kind: Service
   154  apiVersion: v1
   155  metadata:
   156    name: my-service
   157    annotations:
   158      "consul.hashicorp.com/service-sync": "false"
   159  ```
   160  
   161  ### Service Name
   162  
   163  When a Kubernetes service is synced to Consul, the name of the service in Consul
   164  by default will be the value of the "name" metadata on that Kubernetes service.
   165  This makes it so that service sync works with zero configuration changes.
   166  This can be overridden using an annotation to specify the Consul service name:
   167  
   168  ```yaml
   169  kind: Service
   170  apiVersion: v1
   171  metadata:
   172    name: my-service
   173    annotations:
   174      "consul.hashicorp.com/service-name": my-consul-service
   175  ```
   176  
   177  **If a conflicting service name exists in Consul,** the sync program will
   178  register additional instances to that same service. Therefore, services inside
   179  and outside of Kubernetes should have different names unless you want either
   180  side to potentially connect. This default behavior also enables gracefully
   181  transitioning a service from outside of K8S to inside, and vice versa.
   182  
   183  ### Service Ports
   184  
   185  When syncing the Kubernetes service to Consul, the Consul service port will be
   186  the first defined port in the service. Additionally, all ports will be
   187  registered in the service instance metadata with the key "port-X" where X is
   188  the name of the port and the value is the externally accessible port.
   189  
   190  The default service port can be overridden using an annotation:
   191  
   192  ```yaml
   193  kind: Service
   194  apiVersion: v1
   195  metadata:
   196    name: my-service
   197    annotations:
   198      "consul.hashicorp.com/service-port": "http"
   199  ```
   200  
   201  The annotation value may a name of a port (recommended) or an exact port value.
   202  
   203  ### Service Tags
   204  
   205  A service registered in Consul from Kubernetes will always have the tag "k8s" added
   206  to it. Additional tags can be specified with a comma-separated annotation value
   207  as shown below. This will also automatically include the "k8s" tag which can't
   208  be disabled. The values should be specified comma-separated without any
   209  additional whitespace.
   210  
   211  ```yaml
   212  kind: Service
   213  apiVersion: v1
   214  metadata:
   215    name: my-service
   216    annotations:
   217      "consul.hashicorp.com/service-tags": "primary,foo"
   218  ```
   219  
   220  ### Service Meta
   221  
   222  A service registered in Consul from Kubernetes will set the `external-source` key to
   223  "kubernetes". This can be used by API consumers, the UI, CLI, etc. to filter
   224  service instances that are set in k8s. The Consul UI (in Consul 1.2.3 and later)
   225  will read this value to show a Kubernetes icon next to all externally
   226  registered services from Kubernetes.
   227  
   228  Additional metadata can be specified using annotations. The "KEY" below can be
   229  set to any key. This allows setting multiple meta values:
   230  
   231  ```yaml
   232  kind: Service
   233  apiVersion: v1
   234  metadata:
   235    name: my-service
   236    annotations:
   237      "consul.hashicorp.com/service-meta-KEY": "value"
   238  ```
   239  
   240  ## Consul to Kubernetes
   241  
   242  This syncs Consul services into first-class Kubernetes services.
   243  Each Consul service is synced to an
   244  [ExternalName](https://kubernetes.io/docs/concepts/services-networking/service/#externalname)
   245  service in Kubernetes. The external name is configured to be the Consul
   246  DNS entry.
   247  
   248  This enables external services to be discovered using native Kubernetes
   249  tooling. This can be used to ease software migration into or out of Kubernetes,
   250  across platforms, to and from hosted services, and more.
   251  
   252  -> **Requires Consul DNS via CoreDNS in Kubernetes:** This feature requires that
   253  [Consul DNS](/docs/platform/k8s/dns.html) is configured within Kubernetes.
   254  Additionally, **[CoreDNS](https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/#config-coredns)
   255  is required (instead of kube-dns)** to resolve an
   256  issue with resolving `externalName` services pointing to custom domains.
   257  In the future we hope to remove this requirement by syncing the instance
   258  addresses directly into service endpoints.
   259  
   260  ### Sync Enable/Disable
   261  
   262  All Consul services visible to the sync process based on its given ACL token
   263  will be synced to Kubernetes.
   264  
   265  There is no way to change this behavior per service. For the opposite sync
   266  direction (Kubernetes to Consul), you can use Kubernetes annotations to disable
   267  a sync per service. This is not currently possible for Consul to Kubernetes
   268  sync and the ACL token must be used to limit what services are synced.
   269  
   270  In the future, we hope to support per-service configuration.
   271  
   272  ### Service Name
   273  
   274  When a Consul service is synced to Kubernetes, the name of the Kubernetes
   275  service will exactly match the name of the Consul service.
   276  
   277  To change this default exact match behavior, it is possible to specify a
   278  prefix to be added to service names within Kubernetes by using the
   279  `-k8s-service-prefix` flag. This can also be specified in the Helm
   280  configuration.
   281  
   282  **If a conflicting service is found,** the service will not be synced. This
   283  does not match the Kubernetes to Consul behavior, but given the current
   284  implementation we must do this because Kubernetes can't mix both CNAME and
   285  Endpoint-based services.
   286  
   287  ### Kubernetes Service Labels and Annotations
   288  
   289  Any Consul services synced to Kubernetes will be labeled and annotated.
   290  An annotation `consul.hashicorp.com/synced` will be set to "true" to note
   291  that this is a synced service from Consul.
   292  
   293  Additionally, a label `consul=true` will be specified so that label selectors
   294  can be used with `kubectl` and other tooling to easily filter all Consul-synced
   295  services.
   296