<p>I am running grafana as a pod inside my Kubernetes cluster. Once Grafana is initialized, it create a DB on localhost and saves all data there. This means that whenever a pod is destroyed and recreated, the whole DB is reinitialized and I lose all previous Data.</p>

<p>The grafana config inside the Pod for DB is ::</p>

<pre><code>#################################### Database ####################################
[database]
# Either "mysql", "postgres" or "sqlite3", it's your choice
;type = sqlite3
;host = 127.0.0.1:3306
;name = grafana
;user = root
;password =
</code></pre>

<p>Inorder to get rid of this problem, I have to create an external DB and point my Grafana to use that DB instance everytime I create the Grafana Pod. My current default implementation to create the Grafana pod is ::</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: lb-grafana-service
spec:
  ports:
    - port: 4545
      targetPort: 4545
      protocol: TCP
  clusterIP: 10.100.10.100
----
apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/scrape: 'true'
  labels:
    app: grafana
    name: grafana
  name: grafana
spec:
  ports:
  - name: scrape
    port: 4545
    nodePort: 30999
    protocol: TCP
  type: NodePort
  selector:
    app: grafana
----
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
    name: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      name: grafana
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:develop
        env:
          - name: Prometheus_SERVICE_URL
            value: http://172.29.219.105:30901
          - name: GF_SECURITY_ADMIN_PASSWORD
            value: "grafana"
          - name: GF_SERVER_HTTP_PORT
            value: "4545"
        ports:
          - containerPort: 9101
        volumeMounts:
        - mountPath: /var
          name: grafana-storage
      volumes:
      - name: grafana-storage
        emptyDir: {}
</code></pre>

<p>So what I want to do is overwrite the <code>/etc/grafana/grafana.ini</code> file before Grafana pod comes online OR just rewrite the current file with new values. I have no idea how I can do that right now. A little guidance will be much appreciated. </p>

<p>In general, you could use <code>ConfigMaps</code> like the comment said.</p>

<p>The Grafana image itself provides the ability to provide all configuration parameters via environment variables. This is only mentioned in the GitHub <a href="https://github.com/grafana/grafana-docker/blob/5a6ae51eb9f15c2448234003eb5857069e269511/README.md" rel="nofollow noreferrer">readme</a>.</p>

<p>This way you could set the environment variables with Kubernetes, like:</p>

<pre><code>  spec:
    template:
      spec:
        containers:
        - name: grafana
          image: grafana/grafana:4.1.1
          env:
          - name: "GF_SERVER_ROOT_URL"
            value: "http://grafana.{{.clusterDomain}}"
          - name: "GF_DATABASE_TYPE"
            value: "{{.gfDatabaseType}}"
          - name: "GF_DATABASE_HOST"
            value: "{{.gfDatabaseHost}}"
          - name: "GF_DATABASE_NAME"
            value: "{{.gfDatabaseName}}"
          - name: "GF_DATABASE_USER"
            value: "{{.gfDatabaseUser}}"
          - name: "GF_DATABASE_PASSWORD"
            value: "{{.gfDatabasePassword}}"
          - name: "GF_DATABASE_SSL_MODE"
            value: "disable"
          - name: "GF_AUTH_ANONYMOUS_ENABLED"
            value: "true"
</code></pre>
