Search code examples
spring-bootkubernetesnetflix-eureka

How to create the deployment, statefulset of service-registry(eureka-server) in kubernetes?


I have been trying to create a statefulset of service-registry (eureka-server) in a springboot application. The reason i am doing this because i want to attach pre-defined name to the service-registry pod so that its able to communicate with all the eureka clients even after it restarts. Even though i have been able to create the services (headless and nodeport) with the configuration, but it doesn't create the pod/deployment and the PersistentVolumeClaim itself. Please check the below deployment yaml and suggest the changes.

# Define a 'Persistent Volume Claim'(PVC) for Storage, dynamically provisioned by cluster
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: rtb
  name: service-registry-pv-claim # name of PVC essential for identifying the storage data
  labels:
    app: eureka
spec:
  accessModes:
    - ReadWriteOnce   #This specifies the mode of the claim that we are trying to create.
  resources:
    requests:
      storage: 1Gi    #This will tell kubernetes about the amount of space we are trying to claim.


---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: rtb
  name: eureka-cm
data:
  eureka_service_address: http://eureka-0.eureka:8761/eureka

---

apiVersion: v1
kind: Service
metadata:
  namespace: rtb
  name: eureka
  labels:
    app: eureka
spec:
  clusterIP: None
  ports:
    - port: 8761
      name: eureka
  selector:
    app: eureka

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: rtb
  name: eureka
spec:
  serviceName: "eureka"
  replicas: 1
  selector:
    matchLabels:
      app: eureka
  template:
    metadata:
      labels:
        app: eureka
    spec:
      containers:
        - name: eureka
          image: my-image
          imagePullPolicy: Always
          ports:
            - containerPort: 8761
          env:
            - name: EUREKA_SERVER_ADDRESS
              valueFrom:
                configMapKeyRef:
                  name: eureka-cm
                  key: eureka_service_address
          volumeMounts: # Mounting volume obtained from Persistent Volume Claim
            - name: service-registry-persistent-storage
              mountPath: /var/lib/eureka #This is the path in the container on which the mounting will take place.
      volumes:
        - name: service-registry-persistent-storage # Obtaining 'volume' from PVC
          persistentVolumeClaim:
            claimName: service-registry-pv-claim

---

apiVersion: v1
kind: Service
metadata:
  namespace: rtb
  name: eureka-lb
  labels:
    app: eureka
spec:
  selector:
    app: eureka
  type: NodePort
  ports:
    - port: 80
      targetPort: 8761

and below is the application.yml file

server:
  port: 8761

eureka:
  instance:
    hostname: "${HOSTNAME}.eureka"
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: ${EUREKA_SERVER_ADDRESS}

this is how the eureka client apps are referring to eureka server

eureka:
  instance:
    preferIpAddress: true
    hostname: eureka-0

I am new to Kubernetes, so please suggest the changes.

Configuration after adding the PesistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: rtb
  name: my-pv
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostpath:
    path: /run/desktop/mnt/host/c/Users/User/Documents/kubernetesbkp

---
# Define a 'Persistent Volume Claim'(PVC) for Storage, dynamically provisioned by cluster
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: rtb
  name: service-registry-pv-claim # name of PVC essential for identifying the storage data
  labels:
    app: eureka
spec:
  accessModes:
    - ReadWriteOnce   #This specifies the mode of the claim that we are trying to create.
  resources:
    requests:
      storage: 1Gi    #This will tell kubernetes about the amount of space we are trying to claim.


---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: rtb
  name: eureka-cm
data:
  eureka_service_address: http://eureka-0.eureka:8761/eureka

---

apiVersion: v1
kind: Service
metadata:
  namespace: rtb
  name: eureka
  labels:
    app: eureka
spec:
  clusterIP: None
  ports:
    - port: 8761
      name: eureka
  selector:
    app: eureka

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: rtb
  name: eureka
spec:
  serviceName: "eureka"
  replicas: 1
  selector:
    matchLabels:
      app: eureka
  template:
    metadata:
      labels:
        app: eureka
    spec:
      containers:
        - name: eureka
          image: my-image
          imagePullPolicy: Always
          ports:
            - containerPort: 8761
          env:
            - name: EUREKA_SERVER_ADDRESS
              valueFrom:
                configMapKeyRef:
                  name: eureka-cm
                  key: eureka_service_address
          volumeMounts: # Mounting volume obtained from Persistent Volume Claim
            - name: service-registry-persistent-storage
              mountPath: /var/lib/eureka #This is the path in the container on which the mounting will take place.
      volumes:
        - name: service-registry-persistent-storage # Obtaining 'volume' from PVC
          persistentVolumeClaim:
            claimName: service-registry-pv-claim

---

apiVersion: v1
kind: Service
metadata:
  namespace: rtb
  name: eureka-lb
  labels:
    app: eureka
spec:
  selector:
    app: eureka
  type: NodePort
  ports:
    - port: 80
      targetPort: 8761

Solution

  • This is the below deployment configuration which worked for me, but just one thing, i applied all of the configurations one by one, otherwise it doesn't work on a single apply and the statefulset isn't created then. Also, i am mentioning it as a single configuration file here for convenience.

    Would be helpful if someone can point out why it doesn't work in a single apply command. Thanks!

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eureka-cm
    data:
      eureka_service_address: http://eureka-0.eureka:8761/eureka
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: eureka
      labels:
        app: eureka
    spec:
      clusterIP: None
      ports:
        - port: 8761
          name: eureka
      selector:
        app: eureka
    
    ---
    
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: eureka
    spec:
      serviceName: "eureka"
      replicas: 1
      selector:
        matchLabels:
          app: eureka
      template:
        metadata:
          labels:
            app: eureka
        spec:
          containers:
            - name: eureka
              image: my-image
              imagePullPolicy: Always
              ports:
                - containerPort: 8761
              env:
                - name: EUREKA_SERVER_ADDRESS
                  valueFrom:
                    configMapKeyRef:
                      name: eureka-cm
                      key: eureka_service_address
    
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: eureka-lb
      labels:
        app: eureka
    spec:
      selector:
        app: eureka
      type: NodePort
      ports:
        - port: 80
          targetPort: 8761