Consul on Kubernetes

repository·main·Indexed 20 days ago

https://github.com/hashicorp/consul-k8s

Integration between Consul and Kubernetes enabling service mesh, API gateway, and service catalog synchronization. Includes the consul-k8s CLI for installation and debugging, Helm charts, and a custom implementation of the Kubernetes Gateway API (v1beta1) using the consul.hashicorp.com API group.

Tokens
67.9K
Snippets
154
Records
301
Agent score
72%

What's inside consul-k8s

  1. Overview of Consul on Kubernetes features

    main

    The consul-k8s-control-plane binary provides first-class integrations between Consul and Kubernetes. Key features include:

    • Consul Service Mesh: Injects Envoy sidecars and registers Pods with Consul to enable service mesh capabilities.
    • Consul API Gateway: Enables north/south traffic into the Consul Service Mesh.
    • Catalog Sync: Synchronizes Consul services with Kubernetes services (and vice versa), allowing Kubernetes to access external services and non-Kubernetes nodes to discover Kubernetes services.
  2. What is an HTTPRoute and how does it work?

    main

    An HTTPRoute is a Kubernetes Gateway API resource used to specify how HTTP requests from a Gateway listener should be routed to backend API objects (typically Kubernetes Services).

    An HTTPRoute consists of three main components in its specification:

    1. parentRefs: Defines which Gateways the route attaches to.
    2. hostnames (optional): Defines which Host headers the route matches.
    3. rules: Defines the logic for matching requests (via matches) and taking actions (via filters and backendRefs).

    Incoming requests are first matched against hostnames. If a match occurs, the HTTPRoute is selected, and its rules are evaluated to determine the final routing behavior.

  3. What is Kubernetes Gateway API?

    main

    Gateway API is a collection of Kubernetes resources designed to enable robust service networking through expressive, extensible, and role-oriented interfaces. It serves as a successor to the standard Ingress API, offering explicit support for multiple protocols including HTTP, TLS, TCP, and UDP, along with tightly integrated Transport Layer Security (TLS) support.

    Key characteristics:

    • Resource-centric: Centered around Gateway resources which represent underlying network gateways or proxy servers.
    • Implementation-agnostic: It is a Custom Resource Definition (CRD) based API. Kubernetes does not provide a default implementation; instead, users must install an implementation (e.g., Consul, Istio, NGINX) that suits their needs.
    • Superset of Ingress: It provides more advanced networking concepts than the standard Ingress API.
  4. What is GRPCRoute and when to use it

    main

    The GRPCRoute is an experimental Kubernetes Gateway API resource designed specifically for routing gRPC traffic. While gRPC can technically be routed using HTTPRoute resources, GRPCRoute is preferred because it provides a better user experience and avoids the limitations of routing gRPC at the HTTP layer (such as handling gRPC-specific URI encoding, trailers via Transfer-Encoding, and the lack of support for query parameters or non-POST methods in standard gRPC usage).

    Scope and Limitations:

    • Transport: Limited to HTTP/2.
    • IDL: Limited to Protocol Buffers.
    • Cross-Serving: GRPCRoute and HTTPRoute cannot share the same hostnames on the same Listener. If you attempt to attach a GRPCRoute to a Listener that already has an HTTPRoute with overlapping hostnames, the implementation must reject the new route.
  5. What is the GAMMA Initiative?

    main

    The GAMMA Initiative (Gateway API for Mesh Management and Administration) is a dedicated workstream focused on using the Gateway API for service mesh use-cases.

    It is a collaborative effort involving several service mesh projects including:

    • Consul
    • Cilium Service Mesh
    • Istio
    • Kuma
    • Linkerd
    • NGINX Service Mesh
    • Open Service Mesh

    The initiative aims to deliver enhancement proposals (GEPs) for mesh-specific requirements, such as service-to-service traffic, authentication, and authorization policies.

  6. Understand Gateway API Conformance Profiles

    main

    Conformance in the Gateway API is defined by the specific resources an implementation supports. Support is categorized into 'Core' and 'Extended' functionality.

    Required Resources (Core Support): All implementations must support the Core functionality for:

    • GatewayClass
    • Gateway
    • ReferenceGrant

    Optional Resources: Implementations may choose to support these resources, which have defined behaviors if implemented:

    • HTTPRoute
    • TLSRoute
    • TCPRoute
    • UDPRoute

    Conformance is versioned based on the API's bundle version (e.g., v0.4.0), not the individual CRD version (e.g., v1alpha2). This allows implementations to claim support for a specific version of the Gateway API specification.

  7. Use ServiceImport as a ParentRef in Mesh (GAMMA)

    main

    Under the GAMMA (Gateway API Mesh Acceleration) initiative, a ServiceImport can be used as a parentRef for a Route. When a ServiceImport is specified as a parent, the mesh intercepts traffic destined for the ClusterSetIP and applies the policies or routing decisions defined in the Route. This allows the mesh to manage and redirect traffic across the entire ClusterSet.

    # Example: Using ServiceImport as a ParentRef for mesh interception
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: mesh-route
    spec:
      parentRefs:
        - name: store
          kind: ServiceImport
      rules:
        - matches:
            - path: { type: PathPrefix, value: /cart }
          backendRefs:
            - name: cart
              kind: ServiceImport
              port: 80
  8. Alternative: Bind HTTPRoute to a Mesh resource

    main

    An HTTPRoute can be bound directly to a cluster-scoped Mesh object (as defined in GEP-1291). This allows the route to be part of the mesh context. This approach can be used alone or as a peer to a Service resource parentRef to define routing rules for a specific service on a specific mesh.

    Example of binding to a Mesh:

    spec:
      parentRefs:
      - kind: Mesh
        name: cool-mesh

    Example of using Mesh and Service as peer parentRefs:

    spec:
      parentRefs:
      - kind: Mesh
        name: cool-mesh
      - kind: Service
        name: foo
  9. Select a controller using controllerName

    main

    The spec.controllerName field (referred to in documentation as spec.controller) determines which controller implementation is responsible for managing the GatewayClass.

    To avoid conflicts in a cluster with multiple controllers, it is recommended to use a unique domain/path combination (e.g., example.net/gateway-controller). You can also implement versioning by encoding the version into the path, such as example.net/gateway/v1 or example.net/gateway/v2.1.

  10. Achieve Session Persistence via Cookies, Headers, or URLs

    main

    Session persistence is achieved using application-layer attributes. There are three primary mechanisms:

    1. Cookie-Based Session Persistence: The most common method. A proxy uses a value from the set-cookie HTTP response header to identify the client in subsequent requests via the cookie request header.
    2. Header-Based Session Persistence: A backend or gateway provides a custom HTTP response header, which the client includes in subsequent requests. The proxy uses this header to maintain the connection.
    3. URL-Encoded Session Persistence: Session information is encoded directly into the request URL. The server rewrites the URL to include this information and decodes it to identify the session.

    Session Initiation Rules

    • Backend-initiated: If the backend sets the session attribute, the gateway should allow it and not force new connections unless configured otherwise. The gateway may decode/alter cookies to manage persistence.
    • Gateway-initiated: If the gateway sets the attribute, the backend will receive these session attributes even if it did not explicitly enable them.
    • Client-side: A client can signal the desire to start a new session by removing the session cookie or identifier.