如何设置动态IP到属性文件?

How to set dynamic IP to property file?

我已经部署了 2 个 pods,它需要与另一个 pod(比如说 Pod A)通信。 Pod A 需要已部署服务的 IP 地址 pods.So 我需要在 Pod A 所需的配置 属性 文件中设置这些 IP 地址。 由于 IP 地址是动态的,即如果 pod 崩溃,它会 changed.So 需要动态设置它。

目前我部署了 2 pods 并执行

kubectl get ep

并在配置 属性 文件中设置这些 IP 地址并构建 Dockerfile 并推送它并使用该映像进行部署。

这是我的 deplyment 和 svc 文件,其中图像 djtijare/a2ipricing 引用配置文件

    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

那么如何在配置文件中动态设置那 2 个 pods 的 IP 并构建和推送 docker 图像。

我认为你应该考虑使用 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.

对于您的示例,如果您将服务设置为 spec.clusterIP = None,您可以 nslookup -type=A spring-boot-demo-pricing 这将向您显示附加到此 service.

pods 的 IP
/ # 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

这是我用过的yaml

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