I am now reviewing my mistakes. I would like to know if I need to run a command . . .

Frankie Hung:
I am now reviewing my mistakes. I would like to know if I need to run a command in a pod like this
/bin/sh -c “tail -f /var/log”

Is this a correct way to do it in the below yaml? I am not sure how to add the double quote near the tail command.

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - command: ["/bin/sh"]
    args: ["-c", "tail -f /var/log"]
    image: busybox
    name: busybox

Hasan Alsaedi:
@Frankie Hung Don’t worry I am sure next time you will pass with a very high score. There are two methods for adding command to container config: [1] command: [‘sh’, ‘-c’, ‘tail tail -f /var/log’] all in one line. see this example: https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#init-containers-in-use

[2] from kubectl like:

root@controlplane:~# kubectl run nginx --image=nginx --dry-run=client -o yaml – sh -c “tail -f /var/log”
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:

  • args:
    • sh
    • -c
    • tail -f /var/log
      image: nginx
      name: nginx
      resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      status: {}

Notice the (–) to start the command variables after dry-run this is important.

Sri:
Hi

This will give you experience and you will sure pass the next time.

You can use commands directly in the args section or in the command and args.

cat <<EOF >> logtest.yaml
apiVersion: v1
kind: Pod
metadata:
  name: counter
spec:
  containers:
  - name: count
    image: busybox
    args:
    - /bin/sh
    - -c
    - >
      i=0;
      while true;
      do
        echo "$i: $(date)" >> /var/log/1.log;
        echo "$(date) INFO $i" >> /var/log/2.log;
        i=$((i+1));
        sleep 1;
      done      
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: count-log-1
    image: busybox
    args: [/bin/sh, -c, 'tail -f /var/log/1.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: count-log-2
    image: busybox
    command: ["/bin/sh"]
    args: ["-c", "tail -f /var/log/2.log"]
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}
EOF

kubectl logs counter -c count-log-1
kubectl logs counter -c count-log-2

Sri:
https://kubernetes.io/docs/concepts/cluster-administration/logging/#streaming-sidecar-container

Frankie Hung:
Thanks a lot! This sidecar demo is exactly what I am looking for!