# K8S-Pod: Sidecar

# Intro

![Setting up a ProxySQL Sidecar Container](https://www.percona.com/blog/wp-content/uploads/2020/02/Docker-Sidecar.png align="left")

When the client wants to access to one container, must be processed by the sidecar container, a common scenario: format and aggregate logs.

# Goal for this chapter

We are going to create two containers:

* main container: an nginx web service
    
* sidecar container: allows the user to change the index page every 2 seconds
    

```bash
apiVersion: v1
kind: Pod
metadata:
  name: sidecar-test
spec:
  containers:
  - name: nginx-web
    image: nginx-k8s:latest
    imagePullPolicy: Never
    volumeMounts:
    - name: nginx-index
      mountPath: /usr/share/nginx/html
  - name: change-index
    image: busybox:latest
    imagePullPolicy: Never
    # Change the file content every 2 seconds
    command: ['sh', '-c', 'for i in $(seq 100); do echo index-$i > /testdir/index.html;sleep 2;done']
    volumeMounts:
    - name: nginx-index
      mountPath: /testdir
  volumes:
  - name: nginx-index
    emptyDir: {}
```

```bash
$ kubectl apply -f sidecar_practice.yaml
pod/sidecar-test created

$ kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
sidecar-test   2/2     Running   0          11s   10.244.2.4   kind-worker   <none>           <none>
```

Finally, let’s check the frequent changing page:

```bash
# curl 10.244.2.4
index-36
# curl 10.244.2.4
index-37
# curl 10.244.2.4
index-38
# curl 10.244.2.4
index-38
# curl 10.244.2.4
index-39
# curl 10.244.2.4
index-39
# curl 10.244.2.4
index-40
```
