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.
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.
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
After some research I found that we can specify the -data-dir location in config, so I have modified StatefulSet kind yaml as below:
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:
- "agent"
- "-server"
- "-data-dir=/home/consul/data"
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!
Please note that k8s
podsare by default ephemeral even if you deploy them asStatefulSet.StatefulSetis providing you option forpodwith define name eg.consul-0rather that standardconsul-<<random string>>. It also keeps track of where to deploypodin case you have different zones and you need to deploypodin the same zone asstorage.What is missing in your manifest is
volumeMountsandvolumeClaimTemplatessections . If you set your data directory to/home/consul/datayour manifest should looks similar to this:Regarding you second problem with
consulUI I would not help much since I never useconsulbut I can advise to deployhelm chartonce again and check how arguments are passed there.