kubernetes deployment with args

11/25/2018

As soon as I add,

spec:
    containers:
      - args:
          - /bin/sh
          - '-c'
          - touch /tmp/healthy; touch /tmp/liveness
        env:

to the deployment file, the application is not coming up without any error in the description logs. The deployment succeed, but no output. Both files getting created in the container. Can I run docker build inside kubernetes deployment?

Below is the complete deployment yaml.

  apiVersion: apps/v1
  kind: Deployment
  metadata:
    labels:
      app: web
    name: web
    namespace: default
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: web
        version: prod
    template:
      metadata:
        annotations:
          prometheus.io/scrape: 'true'
        labels:
          app: web
          version: prod
      spec:
        containers:
          - args:
              - /bin/sh
              - '-c'
              - >-
                touch /tmp/healthy; touch /tmp/liveness; while true; do echo .;
                sleep 1; done
            env:
              - name: SUCCESS_RATE
                valueFrom:
                  configMapKeyRef:
                    key: SUCCESS_RATE
                    name: web-config-prod
            image: busybox
            livenessProbe:
              exec:
                command:
                  - cat
                  - /tmp/liveness
              initialDelaySeconds: 5
            name: web
            ports:
              - containerPort: 8080
              - containerPort: 8000
-- Subit Das
deployment
dockerfile
kubernetes

1 Answer

11/25/2018

The problem was in your case is container is not found after finishing it's task. You told to execute a shell script to your conatainer. And after doing that the container is finished. That's why you can't see whether the files were created or not. Also it didn't put any logs. So you need to keep alive the container after creating the files. You can do that by putting a infinite while loop. Here it comes:

apiVersion: apps/v1
kind: Deployment
metadata:
name: hello
labels:
    app: hi
spec:
replicas: 1
selector:
    matchLabels:
    app: hi
template:
    metadata:
    labels:
        app: hi
    spec:
    containers:
    - name: hi
        image: busybox
        args:
        - /bin/sh
        - "-c"
        - "touch /tmp/healthy; touch /tmp/liveness; while true; do echo .; sleep 1; done"
        ports:
        - containerPort: 80

Save it to hello-deployment.yaml and run,

$ kubectl create -f hello-deployment.yaml
$ pod_name=$(kubectl get pods -l app=hi -o jsonpath='{.items[0].metadata.name}')
$ kubectl logs -f $pod_name
$ kubectl exec -it -f $pod_name -- ls /tmp
-- Shudipta Sharma
Source: StackOverflow