> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/tposs/latest/configuration/sidecars.md).

# Sidecar containers

TPOSS Helm chart allows you to deploy [sidecar containers](https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/) for TPOSS. These are the configuration options that you can use to deploy and configure sidecar containers:

- `sidecars`
- `initContainerSidecars`
- `sidecarPVCs`
- `sidecarSecretVolumes`

## Sidecar containers deployment

You can use the `sidecars` option to deploy one or multiple sidecar containers. This configuration option expects the list of [container](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#Container) specifications.

For example, you can add a debug sidecar using this configuration:

```yaml
sidecars:
  - name: tposs-debugging-sidecar
    image: registry.cennso.com/cennso/nettools:v1.19.1
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo \"$(date -u) Debugging sidecar alive!\"; sleep 86400;done"]
    securityContext:
      runAsUser: 0
      runAsGroup: 0
```

After the TPOSS pods are deployed, each of them will have the `tposs-debugging-sidecar` container with the following additional utilities:

- `bpfc`
- `curl`
- `drill`
- `iperf3`
- `iproute2`
- `netsniff-ng`
- `socat`
- `tcpdump`
- `telnet`
- `trafgen`

Apart from sidecar containers, TPOSS runs [init containers](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) to wait for the database to start and execute the database migration. If you need a sidecar container that runs independently of TPOSS init containers and starts before them, use the `initContainerSidecars` option. These sidecar containers can start, stop, or restart without affecting the main application container or other init containers. For more information, see the [Kubernetes documentation](https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#pod-sidecar-containers).

For example, you can use this option to set up [HAProxy](https://www.haproxy.org/) to forward queries to multiple HAProxy instances. These instances, in turn, forward queries to multiple MySQL instances using a Kubernetes [headless service](https://kubernetes.io/docs/concepts/services-networking/service/#headless-services). This setup operates independently from the TPOSS init containers, which handle database migrations.

```yaml
initContainerSidecars:
  - name: haproxy-tposs-sidecar
    image: haproxy:3.1.3
    imagePullPolicy: IfNotPresent
    ports:
      - containerPort: 3306
        name: mysql
    volumeMounts:
      - name: config-volume
        mountPath: /usr/local/etc/haproxy/
    configMap:
      name: config-volume
      data:
        haproxy.cfg: |
          global
            daemon
            maxconn 2000
            log stdout format raw local0 debug

          defaults
            log global
            mode tcp
            option tcplog
            option dontlognull
            option log-separate-errors
            timeout connect 10s
            timeout client 30m
            timeout server 30m

          resolvers kubedns
            nameserver dns1 10.0.0.10:53
            accepted_payload_size 8192

          frontend mysql_front
            bind *:3306
            default_backend mysql
            option tcplog
            option clitcpka
            log global

          backend mysql
            log global
            balance roundrobin
            server-template mysql 2 mysql-cluster-ps-db-haproxy-headless:3306 check resolvers kubedns
```

> [!NOTE]
> Any [ConfigMap](https://kubernetes.io/docs/concepts/configuration/configmap/) should be specified in the `sidecars[container].configMap` or `initContainerSidecars[container].configMap` of your sidecar container specification. The resulting ConfigMap name is generated as:
> - `{TPOSS_HELM_RELEASE_NAME}-{CONFIGMAP_NAME}-config` for the ConfigMaps specified in **sidecars**
> - `{TPOSS_HELM_RELEASE_NAME}-{CONFIGMAP_NAME}-init-config` for the ConfigMap specified in **initContainerSidecars**

> [!CAUTION]
> Do not use long names in **sidecars[N].configMap** and **initContainerSidecars[N].configMap** as the generated name can exceed the length limit (253 characters) of Kubernetes object names.

## Volumes for sidecar containers

TPOSS provides two additional configuration options to set up [Persistent Volumes](https://kubernetes.io/docs/concepts/storage/persistent-volumes/) and [Secrets](https://kubernetes.io/docs/concepts/configuration/secret/) for the sidecar containers:

- `sidecarPVCs`
- `sidecarSecretVolumes`

You can deploy Persistent Volume Claims (PVCs) using the following configuration option:

```yaml
sidecarPVCs:
  - name: haproxy-pvc
    spec:
      storageClassName: "local-path"
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
```

You must also specify the PVC name and mount path in your sidecar container using the `volumeMounts` section. 

For example:

```yaml
sidecars:
  - name: haproxy-tposs-sidecar
    image: haproxy:3.1.3
    imagePullPolicy: IfNotPresent
    lifecycle:
      preStop:
        exec:
          command:
            - kill
            - -TERM
            - "1"
    ports:
      - containerPort: 3306
        name: mysql
    volumeMounts:
      - name: haproxy-pvc
        mountPath: /opt/haproxy-pvc
```

> [!NOTE]
> After deployment, the name of your PVC is automatically generated based on the volume name using this format: `{TPOSS_HELM_RELEASE_NAME}-{VOLUME_NAME}-{POD_NAME}`.

To create a Secret, use the following configuration option:

```yaml
sidecarSecretVolumes:
  - name: haproxy-secret
    secret:
      secretName: password
```

> [!NOTE]
> After deployment, the name of your Secret is automatically generated based on the volume name using this format: `{TPOSS_HELM_RELEASE_NAME}-{SECRET_NAME}-secret`.
