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

     1  ---
     2  layout: "docs"
     3  page_title: "Connect Sidecar - Kubernetes"
     4  sidebar_current: "docs-platform-k8s-connect"
     5  description: |-
     6    Connect is a feature built into to Consul that enables automatic service-to-service authorization and connection encryption across your Consul services. Connect can be used with Kubernetes to secure pod communication with other services.
     7  ---
     8  
     9  # Connect Sidecar on Kubernetes
    10  
    11  [Connect](/docs/connect/index.html) is a feature built into to Consul that enables
    12  automatic service-to-service authorization and connection encryption across
    13  your Consul services. Connect can be used with Kubernetes to secure pod
    14  communication with other pods and external Kubernetes services.
    15  
    16  The Connect sidecar running Envoy can be automatically injected into pods in
    17  your cluster, making configuration for Kubernetes automatic.
    18  This functionality is provided by the
    19  [consul-k8s project](https://github.com/hashicorp/consul-k8s) and can be
    20  automatically installed and configured using the
    21  [Consul Helm chart](/docs/platform/k8s/helm.html).
    22  
    23  ## Usage
    24  
    25  When the
    26  [Connect injector is installed](/docs/platform/k8s/connect.html#installation-and-configuration),
    27  the Connect sidecar is automatically added to all pods. This sidecar can both
    28  accept and establish connections using Connect, enabling the pod to communicate
    29  to clients and dependencies exclusively over authorized and encrypted
    30  connections.
    31  
    32  -> **Note:** The pod specifications in this section are valid and use
    33  publicly available images. If you've installed the Connect injector, feel free
    34  to run the pod specifications in this section to try Connect with Kubernetes.
    35  Please note the documentation below this section on how to properly install
    36  and configure the Connect injector.
    37  
    38  ### Accepting Inbound Connections
    39  
    40  An example pod is shown below with Connect enabled to accept inbound
    41  connections. Notice that the pod would still be fully functional without
    42  Connect. Minimal to zero modifications are required to pod specifications to
    43  enable Connect in Kubernetes.
    44  
    45  This pod specification starts a server that responds to any
    46  HTTP request with the static text "hello world".
    47  
    48  ```yaml
    49  apiVersion: v1
    50  kind: Pod
    51  metadata:
    52    name: static-server
    53    annotations:
    54      "consul.hashicorp.com/connect-inject": "true"
    55  spec:
    56    containers:
    57      - name: static-server
    58        image: hashicorp/http-echo:latest
    59        args:
    60          - -text="hello world"
    61          - -listen=:8080
    62        ports:
    63          - containerPort: 8080
    64            name: http
    65  ```
    66  
    67  The only change for Connect is the addition of the
    68  `consul.hashicorp.com/connect-inject` annotation. This enables injection
    69  for this pod. The injector can also be
    70  [configured](/docs/platform/k8s/connect.html#installation-and-configuration)
    71  to automatically inject unless explicitly disabled, but the default
    72  installation requires opt-in using the annotation shown above.
    73  
    74  This will start a Connect sidecar that listens on a random port registered
    75  with Consul and proxies valid inbound connections to port 8080 in the pod.
    76  To establish a connection to the pod using Connect, a client must use another Connect
    77  proxy. The client Connect proxy will use Consul service discovery to find
    78  all available upstream proxies and their public ports.
    79  
    80  In the example above, the server is listening on `:8080`. This means
    81  the server will still bind to the pod IP and allow external connections.
    82  This is useful to transition to Connect by allowing both Connect and
    83  non-Connect connections. To restrict access to only Connect-authorized clients,
    84  any listeners should bind to localhost only (such as `127.0.0.1`).
    85  
    86  ### Connecting to Connect-Enabled Services
    87  
    88  The example pod specification below configures a pod that is capable
    89  of establishing connections to our previous example "static-server" service. The
    90  connection to this static text service happens over an authorized and encrypted
    91  connection via Connect.
    92  
    93  ```yaml
    94  apiVersion: v1
    95  kind: Pod
    96  metadata:
    97    name: static-client
    98    annotations:
    99      "consul.hashicorp.com/connect-inject": "true"
   100      "consul.hashicorp.com/connect-service-upstreams": "static-server:1234"
   101  spec:
   102    containers:
   103      - name: static-client
   104        image: tutum/curl:latest
   105        # Just spin & wait forever, we'll use `kubectl exec` to demo
   106        command: [ "/bin/sh", "-c", "--" ]
   107        args: [ "while true; do sleep 30; done;" ]
   108  ```
   109  
   110  Pods must specify upstream dependencies with the
   111  [`consul.hashicorp.com/connect-service-upstreams` annotation](/docs/platform/k8s/connect.html#consul-hashicorp-com-connect-service-upstreams).
   112  This annotation declares the names of any upstream dependencies and a
   113  local port for the proxy to listen on. When a connection is established to that local
   114  port, the proxy establishes a connection to the target service
   115  ("static-server" in this example) using
   116  mutual TLS and identifying as the source service ("static-client" in this
   117  example).
   118  
   119  The injector will also set environment variables `<NAME>_CONNECT_SERVICE_HOST`
   120  and `<NAME>_CONNECT_SERVICE_PORT` in every container in the pod for every defined
   121  upstream. This is analogous to the standard Kubernetes service environment variables, but
   122  point instead to the correct local proxy port to establish connections via
   123  Connect.
   124  
   125  Any containers running in the pod that need to establish connections
   126  to dependencies must be reconfigured to use the local upstream address either
   127  directly or using the environment variables set by the injector (defined above).
   128  This means pods should not use Kubernetes service DNS or environment
   129  variables for these connections.
   130  
   131  
   132  We can verify access to the static text server using `kubectl exec`. Notice
   133  that we use the local address and port from the upstream annotation (1234)
   134  for this verification.
   135  
   136  ```sh
   137  $ kubectl exec static-client -- curl -s http://127.0.0.1:1234/
   138  "hello world"
   139  ```
   140  
   141  We can control access to the server using [intentions](/docs/connect/intentions.html).
   142  If you use the Consul UI or [CLI](/docs/commands/intention/create.html) to
   143  create a deny [intention](/docs/connect/intentions.html) between
   144  "static-client" and "static-server", connections are immediately rejected
   145  without updating either of the running pods. You can then remove this
   146  intention to allow connections again.
   147  
   148  ```sh
   149  $ kubectl exec static-client -- curl -s http://127.0.0.1:1234/
   150  command terminated with exit code 52
   151  ```
   152  
   153  ### Available Annotations
   154  
   155  Annotations can be used to configure the injection behavior.
   156  
   157  * `consul.hashicorp.com/connect-inject` - If this is "true" then injection
   158    is enabled. If this is "false" then injection is explicitly disabled.
   159    The default injector behavior requires pods to opt-in to injection by
   160    specifying this value as "true". This default can be changed in the
   161    injector's configuration if desired.
   162  
   163  * `consul.hashicorp.com/connect-service` - For pods that accept inbound
   164    connections, this specifies the name of the service that is being
   165    served. This defaults to the name of the first container in the pod.
   166  
   167  * `consul.hashicorp.com/connect-service-port` - For pods that accept inbound
   168    connections, this specifies the port to route inbound connections to. This
   169    is the port that the service is listening on. The service port defaults to
   170    the first exposed port on any container in the pod. If specified, the value
   171    can be the _name_ of a configured port, such as "http" or it can be a direct
   172    port value such as "8080". This is the port of the _service_, the proxy
   173    public listener will listen on a dynamic port.
   174  
   175  * `consul.hashicorp.com/connect-service-upstreams` - The list of upstream
   176    services that this pod needs to connect to via Connect along with a static
   177    local port to listen for those connections. Example: `db:1234,auth:6789`
   178    will start two local listeners for `db` on port 1234 and `auth` on port
   179    6789, respectively. The name of the service is the name of the service
   180    registered with Consul. This value defaults to no upstreams.
   181  
   182  ### Deployments, StatefulSets, etc.
   183  
   184  The annotations for configuring Connect must be on the pod specification.
   185  Since higher level resources such as Deployments wrap pod specification
   186  templates, Connect can be used with all of these higher level constructs, too.
   187  
   188  An example `Deployment` below shows how to enable Connect injection:
   189  
   190  ```yaml
   191  apiVersion: apps/v1
   192  kind: Deployment
   193  metadata:
   194    name: consul-example-deployment
   195  spec:
   196    replicas: 1
   197    selector:
   198      matchLabels:
   199        app: consul-example
   200    template:
   201      metadata:
   202        labels:
   203          app: consul-example
   204        annotations:
   205          "consul.hashicorp.com/connect-inject": "true"
   206      spec:
   207        containers:
   208          - name: example
   209            image: "nginx"
   210  ```
   211  
   212  ~> **A common mistake** is to set the annotation on the Deployment or
   213  other resource. Ensure that the injector annotations are specified on
   214  the _pod specification template_ as shown above.
   215  
   216  ## Installation and Configuration
   217  
   218  The Connect sidecar proxy is injected via a
   219  [mutating admission webhook](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#admission-webhooks)
   220  provided by the
   221  [consul-k8s project](https://github.com/hashicorp/consul-k8s).
   222  This enables the automatic pod mutation shown in the usage section above.
   223  Installation of the mutating admission webhook is automated using the
   224  [Helm chart](/docs/platform/k8s/helm.html).
   225  
   226  To install the Connect injector, enable the Connect injection feature using
   227  [Helm values](/docs/platform/k8s/helm.html#configuration-values-) and
   228  upgrade the installation using `helm upgrade` for existing installs or
   229  `helm install` for a fresh install. The Connect injector **also requires**
   230  [client agents](/docs/platform/k8s/helm.html#v-client) are enabled on
   231  the node with pods that are using Connect and that
   232  [gRPC is enabled](/docs/platform/k8s/helm.html#v-client-grpc).
   233  
   234  ```yaml
   235  connectInject:
   236    enabled: true
   237  
   238  client:
   239    enabled: true
   240    grpc: true
   241  ```
   242  
   243  This will configure the injector to inject when the
   244  [injection annotation](#)
   245  is present. Other values in the Helm chart can be used to limit the namespaces
   246  the injector runs in, enable injection by default, and more.
   247  
   248  As noted above, the Connect auto-injection requires that local client agents
   249  are configured. These client agents must be successfully joined to a Consul
   250  cluster.
   251  The Consul server cluster can run either in or out of a Kubernetes cluster.
   252  
   253  ### Verifying the Installation
   254  
   255  To verify the installation, run the
   256  ["Accepting Inbound Connections"](/docs/platform/k8s/connect.html#accepting-inbound-connections)
   257  example from the "Usage" section above. After running this example, run
   258  `kubectl get pod static-server -o yaml`. In the raw YAML output, you should
   259  see injected Connect containers and an annotation
   260  `consul.hashicorp.com/connect-inject-status` set to `injected`. This
   261  confirms that injection is working properly.
   262  
   263  If you do not see this, then use `kubectl logs` against the injector pod
   264  and note any errors.