How to set dynamic IP to property file?

6/11/2019

I had deployed 2 pods which needed to talk to another pod (let say Pod A). Pod A requires Ip address of services of deployed pods.So i need to set those IP address in config property file needed for pod A. As Ip address are dynamic i.e if pod crashed it get changed.So need to set it dynamically.

Currently I deployed 2 pods and do

kubectl get ep

and set those Ip address in config property file and build Dockerfile and push it and use that image for deployment.

This is my deplyment and svc file in which image djtijare/a2ipricing refers to config file

    apiVersion: v1
   kind: Service
metadata:
  name: spring-boot-demo-pricing
spec:
  ports:
  - name: spring-boot-pricing
    port: 8084
    targetPort: 8084
  selector:
    app: spring-boot-demo-pricing

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: spring-boot-demo-pricing
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: spring-boot-demo-pricing
    spec:
      containers:
      - name: spring-boot-demo-pricing
        image: djtijare/a2ipricing:v1
        imagePullPolicy: IfNotPresent
       # envFrom:
        #- configMapRef:
        #    name: spring-boot-demo-config-map
        resources:
          requests:
            cpu: 100m
            memory: 1Gi
        ports:
        - containerPort: 8084
      nodeSelector:
         disktype: ssd

So How to set IP's of those 2 pods dynamically in config file and build and push docker image.

--
kubernetes
kubernetes-pod

1 Answer

6/12/2019

I think you should think about using Headless services.

Sometimes you don’t need or want load-balancing and a single service IP. In this case, you can create what are termed “headless” Services, by explicitly specifying "None" for the cluster IP (.spec.clusterIP).

You can use a headless Service to interface with other service discovery mechanisms, without being tied to Kubernetes’ implementation. For example, you could implement a custom [Operator]( be built upon this API.

For such Services, a cluster IP is not allocated, kube-proxy does not handle these services, and there is no load balancing or proxying done by the platform for them. How DNS is automatically configured depends on whether the service has selectors defined.

For your example if you set service to spec.clusterIP = None you could nslookup -type=A spring-boot-demo-pricing which will show you IPs of pods attached to this service.

/ # nslookup -type=A spring-boot-demo-pricing
Server:         10.11.240.10
Address:        10.11.240.10:53

Name:   spring-boot-demo-pricing.default.svc.cluster.local
Address: 10.8.2.20
Name:   spring-boot-demo-pricing.default.svc.cluster.local
Address: 10.8.1.12
Name:   spring-boot-demo-pricing.default.svc.cluster.local
Address: 10.8.1.13

And here are the yaml I've used:

apiVersion: v1
kind: Service
metadata:
  name: spring-boot-demo-pricing
  labels:
    app: spring-boot-demo-pricing
spec:
  ports:
  - name: spring-boot-pricing
    port: 8084
    targetPort: 8084
  clusterIP: None
  selector:
    app: spring-boot-demo-pricing

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-boot-demo-pricing
  labels:
    app: spring-boot-demo-pricing
spec:
  replicas: 3
  selector:
    matchLabels:
       app: spring-boot-demo-pricing
  template:
    metadata:
      labels:
        app: spring-boot-demo-pricing
    spec:
      containers:
      - name: spring-boot-demo-pricing
        image: djtijare/a2ipricing:v1
        imagePullPolicy: IfNotPresent
       # envFrom:
        #- configMapRef:
        #    name: spring-boot-demo-config-map
        resources:
          requests:
            cpu: 100m
            memory: 1Gi
        ports:
        - containerPort: 8084
-- Crou
Source: StackOverflow