<p>I am currently working on Spring micro-service(Eureka Implementation) project. To manage the distributed configuration we are using Consul KV. We are deploying services on Kubernetes cluster.</p>
<p>The issue I am facing that, whenever I restart the cluster for Consul it deletes all the data of KV. I am creating Kubernetes cluster on local with docker image by having Deployment.yaml file.
Please refer the below Deployment.yaml file for consul.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: consul
  labels:
    app: consul
spec:
  clusterIP: None
  ports:
    - port: 8500
      name: consul
  selector:
    app: consul
    
---
    
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: consul
spec:
  serviceName: consul
  replicas: 1
  selector:
    matchLabels:
      app: consul
  template:
    metadata:
      labels:
        app: consul
    spec:
      containers:
      - name: consul
        image: hashicorp/consul:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8500
---

apiVersion: v1
kind: Service
metadata:
  name: consul-lb
  labels:
    app: consul
spec:
  selector:
    app: consul
  type: NodePort
  ports:
  - port: 80
    targetPort: 8500
</code></pre>
<p>After some research I found that we can specify the -data-dir location in config, so I have modified StatefulSet kind yaml as below:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: consul
spec:
  serviceName: consul
  replicas: 1
  selector:
    matchLabels:
      app: consul
  template:
    metadata:
      labels:
        app: consul
    spec:
      containers:
      - name: consul
        image: hashicorp/consul:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8500
        args:
            - &quot;agent&quot;
            - &quot;-server&quot;
            - &quot;-data-dir=/home/consul/data&quot;
</code></pre>
<p>But after this Consul UI is not getting started, so wanted some help to resolve so it stores the data even after I delete Consul cluster.
PS: I tried deploying cluster with helm, and it was persisting the data but I did not know how to make that cluster StatefulSet so I can refer it in other services with static url.
Thanks!</p>

Bhaumik Thakkar
<p>Please note that k8s <code>pods</code> are by default ephemeral even if you deploy them as <code>StatefulSet</code>.</p>
<p><code>StatefulSet</code> is providing you option for <code>pod</code> with define name eg. <code>consul-0</code> rather that standard <code>consul-&lt;&lt;random string&gt;&gt;</code>. It also keeps track of where to deploy <code>pod</code> in case you have different zones and you need to deploy <code>pod</code> in the same zone as <code>storage</code>.</p>
<p>What is missing in your manifest is <code>volumeMounts</code> and <code>volumeClaimTemplates</code> sections . If you set your data directory to <code>/home/consul/data</code> your manifest should looks similar to this:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: consul
spec:
  serviceName: consul
  replicas: 1
  selector:
    matchLabels:
      app: consul
  template:
    metadata:
      labels:
        app: consul
    spec:
      containers:
      - name: consul
        image: hashicorp/consul:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8500
        args:
          - &quot;agent&quot;
          - &quot;-server&quot;
          - &quot;-data-dir=/home/consul/data&quot;
        volumeMounts:
        - name: consul-data
          mountPath: /home/consul/data
  volumeClaimTemplates:                       # volume claim template will create volume for you so you don't need to define PVC
  - metadata:
      name: consul-data
    spec:
      accessModes: [ &quot;ReadWriteOnce&quot; ]
      storageClassName: &quot;my-storage-class&quot;    # you can get this with kubectl get sc
      resources:
        requests:
          storage: 1Gi
</code></pre>
<p>Regarding you second problem with <code>consul</code> UI I would not help much since I never use <code>consul</code> but I can advise to deploy <code>helm chart</code> once again and check how arguments are passed there.</p>
