Search code examples
kubernetesconsulservice-discovery

Add consul connect sidecar to pods of a kubernetes helm deployment


I want to add Consul connect sidecar to kubernetes pods.

I have already installed consul injector in my Consul cluster. I have found this way of adding injection annotation in the documentation:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: static-server
---
apiVersion: v1
kind: Pod
metadata:
  name: static-server
  annotations:
    "consul.hashicorp.com/connect-inject": "true"
spec:
  containers:
    # This name will be the service name in Consul.
    - name: static-server
      image: hashicorp/http-echo:latest
      args:
        - -text="hello world"
        - -listen=:8080
      ports:
        - containerPort: 8080
          name: http
   # If ACLs are enabled, the serviceAccountName must match the Consul service name.
  serviceAccountName: static-server

However, in my K8s cluster there are currently statefull sets and deployments. I have found in the documentation that you cannot annotate deployments.

Has anyone tried establishing consul sidecar envoys with the pods of a deployment/statefull set?


Solution

  • An example to do it for deployment. You can do it in StatefulSet as well.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: consul-example-deployment
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: consul-example
      template:
        metadata:
          labels:
            app: consul-example
          annotations:
            "consul.hashicorp.com/connect-inject": "true"
        spec:
          containers:
            - name: consul-example
              image: "nginx"
          serviceAccountName: consul-example
    

    https://www.consul.io/docs/platform/k8s/connect.html#deployments-statefulsets-etc-