使用args进行Kubernetes部署

时间:2018-11-25 04:25:19

标签: deployment kubernetes dockerfile

我添加后,

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

对于部署文件,应用程序不会在描述日志中正常运行。部署成功,但是没有输出。这两个文件都在容器中创建。我可以在kubernetes部署中运行docker build吗?

下面是完整的部署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 个答案:

答案 0 :(得分:2)

您的问题是container is not found完成任务后。您被告知要对容器执行Shell脚本。然后,完成容器。这就是为什么您看不到文件是否已创建的原因。而且它没有放任何日志。因此,创建文件后,您需要保持容器活动。您可以通过放置无限的while循环来实现。来了:

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

将其保存到hello-deployment.yaml并运行,

$ 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