kubernetes deployment with args

断了今生、忘了曾经 提交于 2019-12-24 10:23:42

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/53464642/kubernetes-deployment-with-args

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!