Ingress Gateways on Kubernetes

1.9.0+: This feature is available in Consul versions 1.9.0 and higher

This topic requires familiarity with Ingress Gateways.

This page describes how to enable external access to Connect Service Mesh services running inside Kubernetes using Consul ingress gateways. See Ingress Gateways for more information on use-cases and how it works.

Adding an ingress gateway is a multi-step process that consists of the following steps:

  • Setting the Helm chart configuration
  • Deploying the Helm chart
  • Configuring the gateway
  • Defining an Intention (if ACLs are enabled)
  • Deploying your application to Kubernetes
  • Connecting to your application

Setting the helm chart configuration

When deploying the Helm chart you must provide Helm with a custom YAML file that contains your environment configuration.

  1. global:
  2. name: consul
  3. connectInject:
  4. enabled: true
  5. controller:
  6. enabled: true
  7. ingressGateways:
  8. enabled: true
  9. gateways:
  10. - name: ingress-gateway
  11. service:
  12. type: LoadBalancer

Ingress Gateways - 图1

config.yaml

Note: this will create a public unauthenticated LoadBalancer in your cluster, please take appropriate security considerations.

The YAML snippet is the launching point for a valid configuration that must be supplied when installing using the official consul-helm chart. Information on additional options can be found in the Helm reference. Configuration options for ingress gateways reside under the ingressGateways entry.

The gateways stanza is where you will define and configure the set of ingress gateways you want deployed to your environment. The only required field for each entry is name, though entries may contain any of the fields found in the defaults stanza. Values in this section override the values from the defaults stanza for the given ingress gateway with one exception: the annotations from the defaults stanza will be appended to any user-defined annotations defined in the gateways stanza rather than being overridden. Please refer to the ingress gateway configuration documentation for a detailed explanation of each option.

Deploying the Helm chart

Ensure you have the latest consul-helm chart and install Consul via helm using the following guide while being sure to provide the yaml configuration as previously discussed.

Configuring the gateway

Now that Consul has been installed with ingress gateways enabled, you can configure the gateways via the IngressGateway custom resource.

Here is an example IngressGateway resource:

  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: IngressGateway
  3. metadata:
  4. name: ingress-gateway
  5. spec:
  6. listeners:
  7. - port: 8080
  8. protocol: http
  9. services:
  10. - name: static-server

Ingress Gateways - 图2

ingress-gateway.yaml

Note: The ‘name’ field for the IngressGateway resource must match the name specified when creating the gateway in the Helm chart. In the above example, the name “ingress-gateway” is the default name used by the Helm chart when enabling ingress gateways.

Apply the IngressGateway resource with kubectl apply:

  1. $ kubectl apply -f ingress-gateway.yaml
  2. ingressgateway.consul.hashicorp.com/ingress-gateway created

Since we’re using protocol: http, we also need to set the protocol of our service static-server to http. To do that, we create a ServiceDefaults custom resource:

  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceDefaults
  3. metadata:
  4. name: static-server
  5. spec:
  6. protocol: http

Ingress Gateways - 图3

service-defaults.yaml

Apply the ServiceDefaults resource with kubectl apply:

  1. $ kubectl apply -f service-defaults.yaml
  2. servicedefaults.consul.hashicorp.com/static-server created

Ensure both resources have synced to Consul successfully:

  1. $ kubectl get servicedefaults
  2. NAME SYNCED AGE
  3. static-server True 45s
  4. $ kubectl get ingressgateway
  5. NAME SYNCED AGE
  6. ingress-gateway True 13m

Viewing the UI

You can confirm the ingress gateways have been configured as expected by viewing the ingress-gateway service instances in the Consul UI.

To view the UI, use the kubectl port-forward command. See Viewing The Consul UI for full instructions.

Once you’ve port-forwarded to the UI, navigate to the Ingress Gateway instances: http://localhost:8500/ui/dc1/services/ingress-gateway/instances

If TLS is enabled, use https://localhost:8501/ui/dc1/services/ingress-gateway/instances.

Defining an Intention

If ACLs are enabled (via the global.acls.manageSystemACLs setting), you must define an intention to allow the ingress gateway to route to the upstream services defined in the IngressGateway resource (in the example above the upstream service is static-server).

To create an intention that allows the ingress gateway to route to the service static-server, create a ServiceIntentions resource:

  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceIntentions
  3. metadata:
  4. name: static-server
  5. spec:
  6. destination:
  7. name: static-server
  8. sources:
  9. - name: ingress-gateway
  10. action: allow

Ingress Gateways - 图4

service-intentions.yaml

Apply the ServiceIntentions resource with kubectl apply:

  1. $ kubectl apply -f service-intentions.yaml
  2. serviceintentions.consul.hashicorp.com/ingress-gateway created

For detailed instructions on how to configure zero-trust networking with intentions please refer to this guide.

Deploying your application to Kubernetes

Now you will deploy a sample application which echoes “hello world”

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: static-server
  5. spec:
  6. selector:
  7. app: static-server
  8. ports:
  9. - protocol: TCP
  10. port: 80
  11. targetPort: 8080
  12. ---
  13. apiVersion: v1
  14. kind: ServiceAccount
  15. metadata:
  16. name: static-server
  17. ---
  18. apiVersion: apps/v1
  19. kind: Deployment
  20. metadata:
  21. name: static-server
  22. spec:
  23. replicas: 1
  24. selector:
  25. matchLabels:
  26. app: static-server
  27. template:
  28. metadata:
  29. name: static-server
  30. labels:
  31. app: static-server
  32. annotations:
  33. 'consul.hashicorp.com/connect-inject': 'true'
  34. spec:
  35. containers:
  36. - name: static-server
  37. image: hashicorp/http-echo:latest
  38. args:
  39. - -text="hello world"
  40. - -listen=:8080
  41. ports:
  42. - containerPort: 8080
  43. name: http
  44. serviceAccountName: static-server

Ingress Gateways - 图5

static-server.yaml

  1. $ kubectl apply -f static-server.yaml

Connecting to your application

You can validate the service is running and registered in the Consul UI by navigating to http://localhost:8500/ui/dc1/services/static-server/instances

If TLS is enabled, use: https://localhost:8501/ui/dc1/services/static-server/instances

You can also validate the connectivity of the application from the ingress gateway using curl:

  1. $ EXTERNAL_IP=$(kubectl get services | grep ingress-gateway | awk ‘{print $4}’)
  2. $ echo "Connecting to \"$EXTERNAL_IP\""
  3. $ curl -H "Host: static-server.ingress.consul" "http://$EXTERNAL_IP:8080"
  4. "hello world"

Security Warning: Please be sure to delete the application and services created here as they represent a security risk through leaving an open and unauthenticated load balancer alive in your cluster.

To delete the ingress gateway, set enabled to false in your Helm configuration:

  1. global:
  2. name: consul
  3. connectInject:
  4. enabled: true
  5. controller:
  6. enabled: true
  7. ingressGateways:
  8. enabled: false # Set to false
  9. gateways:
  10. - name: ingress-gateway
  11. service:
  12. type: LoadBalancer

Ingress Gateways - 图6

config.yaml

And run Helm upgrade:

  1. $ helm upgrade consul hashicorp/consul -f config.yaml