部署过程中将文件保存到Kubernetes容器中

4

我试图在pod初始化时将文件添加到pod的磁盘上,但没有成功。下面是我用来部署pod的deployment文件。该文件被下载到持久卷中,但pod没有准备好状态。几秒钟后,pod失败并重新构建,这会再次启动整个过程。

如果有帮助,将不胜感激。

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: mapserver
spec:
  selector:
    matchLabels:
      app: mapserver
  template:
    metadata:
      labels:
        app: mapserver
    spec:
      volumes:
      - name: storage
        persistentVolumeClaim:
          claimName: mapserver-pv-claim
      containers:
      - name: maptiles
        image: klokantech/tileserver-gl
        command: ["/bin/sh"]
        args:
        - -c
        - |
           echo "[INFO] Startingcontainer"; if [ $(DOWNLOAD_MBTILES) = "true" ]; then
             echo "[INFO] Download MBTILES_PLANET_URL";
             rm /data/*
             cd /data/
             curl -k -sSL -X GET -u user:ww $(MBTILES_PLANET_URL) -O
             echo "[INFO] Download finished";
           fi;
         env:
         - name: MBTILES_PLANET_URL
           value: 'https://abc-dev/nexus/repository/xyz-raw/2017-07-03_europe_netherlands.mbtiles'
         - name: DOWNLOAD_MBTILES
           value: 'true'
        livenessProbe:
          failureThreshold: 120
          httpGet:
            path: /health
            port: 80
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 5
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 120
          httpGet:
            path: /health
            port: 80
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          limits:
            cpu: 300m
            memory: 3Gi
          requests:
            cpu: 100m
            memory: 1Gi
        volumeMounts:
        - mountPath: "/data"
          name: storage
1个回答

3
我试图在 pod 初始化期间将文件添加到 pod 的磁盘上,但没有成功。在这种情况下,您可能希望改用 InitContainers。根据您的清单,您的主命令被执行(复制文件然后退出),终止容器(和相应的 pod)在此过程中。部署随后重新启动已退出的 pod 并重复循环。如果改用 InitContainers(使用与主容器相同的定义和相同的 PV),则应该使用 InitContaienrs 预填充数据,该容器运行完成,然后继续在正常容器中使用它(其命令/入口点应具有非退出的主进程)。注意:如果您不想使用 InitContainers 或仅作为快速测试,则可以在复制语句之后附加常规的非退出命令,并检查是否需要根据您的用例和保持容器运行的方式启动容器。

谢谢,使用 initcontainer 解决了我的问题。 - Martijn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接