0

我有一个像这样的豆荚列表:

❯ kubectl get pods -l app=test-pod                                                                                                                              (base)
NAME                               READY   STATUS    RESTARTS   AGE
test-deployment-674667c867-jhvg4   1/1     Running   0          14m
test-deployment-674667c867-ssx6h   1/1     Running   0          14m
test-deployment-674667c867-t4crn   1/1     Running   0          14m

我有服务

kubectl get services                                                                                                                          (base)
NAMESPACE     NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                        AGE
default       test-service                        ClusterIP   10.100.4.138     <none>        4000/TCP                       15m

我执行一个 dns 查询:

❯ kubectl exec -ti test-deployment-674667c867-jhvg4  -- /bin/bash                                                                                               (base)
root@test-deployment-674667c867-jhvg4:/# busybox nslookup test-service
Server:     10.100.0.10
Address:    10.100.0.10:53

Name:   test-service.default.svc.cluster.local
Address: 10.100.4.138

我的配置:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test-pod
  template:
    metadata:
      labels:
        app: test-pod
    spec:
      containers:
      - name: python-http-server
        image: python:2.7
        command: ["/bin/bash"]
        args: ["-c", "echo \" Hello from $(hostname)\" > index.html; python -m SimpleHTTPServer 80"]
        ports:
        - name: http
          containerPort: 80
kind: Service
apiVersion: v1
metadata:
  name: test-service
spec:
  selector:
    app: test-pod
  ports:
  - protocol: TCP
    port: 4000
    targetPort: http

我怎样才能通过 dns 查询获取所有 pod 的 IP 地址列表?

理想情况下,我想执行nslookup一个名称并获取列表中所有 pod 的 ips 的列表。

4

1 回答 1

2

您必须使用带有选择器的无头服务。它返回 pod 的 IP 地址。

见这里: https ://kubernetes.io/docs/concepts/services-networking/service/#headless-services

.spec.clusterIP 必须为“无”

于 2021-02-11T09:07:21.823 回答