Kubernetes如何在容器运行之前将cfg文件复制到容器中?

时间:2019-03-22 02:03:44

标签: kubernetes

我的服务需要一个cfg文件,在容器开始运行之前需要对其进行更改。因此不适合将cfg打包到docker映像中。 我需要从群集复制到容器,然后容器中的服务启动并读取此cfg。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

我认为对于您的用例,Init Containers可能是最合适的。初始化容器就像小型脚本,可以在kubernetes pod中启动自己的容器之前运行,它们必须退出。您可以在Init容器和容器之间的共享持久卷中更新此配置文件。

以下文章为如何做到这一点提供了一个很好的例子

https://medium.com/@jmarhee/using-initcontainers-to-pre-populate-volume-data-in-kubernetes-99f628cd4519

更新:

我从stackoverflow中找到了另一个可能与之相关的答案,并为您提供了一种更好的处理方式

can i use a configmap created from an init container in the pod

答案 1 :(得分:1)

示例

输出

NAME          READY   STATUS    RESTARTS   AGE
cassandra-0   1/1     Running   0          70s
[root@green--1 ~]# k exec -it cassandra-0 -n green -- /bin/bash
root@cassandra-0:/# ls /config/cassandra/
cassandra.yaml

如何共享目录

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cassandra
  namespace: green
  labels:
    app: cassandra
spec:
  serviceName: cassandra
  replicas: 1
  selector:
    matchLabels:
      app: cassandra
  template:
    metadata:
      labels:
        app: cassandra
    spec:
      terminationGracePeriodSeconds: 1800
      initContainers:
       - name: copy
         image: busybox:1.28
         command: ["/bin/sh", "-c", "cp /config/cassandra.yaml /config/cassandra/"]
         volumeMounts:
         - name: tmp-config
           mountPath: /config/cassandra/
         - name: cassandraconfig
           mountPath: /config/
      containers:
      - name: cassandra
        #image: gcr.io/google-samples/cassandra:v13
        image: cassandra:3.11.6
        imagePullPolicy: Always
        #command: [ "/bin/sh","-c","su cassandra && mkdir -p /etc/cassandra/ && cp /config/cassandra/cassandra.yaml /etc/cassandra/" ]
        ports:
        - containerPort: 7000
          name: intra-node
        - containerPort: 7001
          name: tls-intra-node
        - containerPort: 7199
          name: jmx
        - containerPort: 9042
          name: cql
        resources:
          limits:
            cpu: "500m"
            memory: 4Gi
          requests:
            cpu: "500m"
            memory: 4Gi
        securityContext:
          capabilities:
            add:
              - IPC_LOCK
        lifecycle:
          postStart:
            exec:
              command: 
              - /bin/sh
              - -c
              - "cp /config/cassandra/cassandra.yaml /etc/cassandra/"
          preStop:
            exec:
              command: 
              - /bin/sh
              - -c
              - nodetool drain
        env:
          - name: MAX_HEAP_SIZE
            value: 1G
          - name: HEAP_NEWSIZE
            value: 100M
          - name: CASSANDRA_SEEDS
            value: "cassandra-0.cassandra.green.svc.cluster.local"
          - name: CASSANDRA_CLUSTER_NAME
            value: "green"
          - name: CASSANDRA_DC
            value: "ee1-green"
          - name: CASSANDRA_RACK
            value: "Rack1-green"
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
        readinessProbe:
          exec:
            command:
            - /bin/bash
            - -c
            - /ready-probe.sh
          initialDelaySeconds: 15
          timeoutSeconds: 55

        volumeMounts:
        - name: cassandradata
          mountPath: /cassandra_data

        - name: tmp-config
          mountPath: /config/cassandra/

      volumes: 
      - name: cassandraconfig
        configMap:
          name: cassandraconfig
      - name: tmp-config
        emptyDir: {}
      # 1 Creating a volume to move date from init container to main container without making mount ReadOnly

  # These are converted to volume claims by the controller
  # and mounted at the paths mentioned above.

  volumeClaimTemplates:
  - metadata:
      name: cassandradata
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: rook-ceph-block
      resources:
        requests:
          storage: 5Gi

答案 2 :(得分:0)

您可以使用ConfigMap。创建配置映射资源并配置您的容器以相应地加载配置映射。然后,您的容器将能够从其环境变量中加载那些变量。

这里是参考:https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/