无法从 kubernetes 中的另一个 pod ping postgres pod

Can't ping postgres pod from another pod in kubernetes

我创建了一个繁忙的 pod 来通过以下 yaml 测试数据库连接

pod.yaml

kind: Pod
apiVersion: v1
metadata:
  name: marks-dummy-pod
spec:
  containers:
    - name: marks-dummy-pod
      image: djtijare/ubuntuping:v1
      command: ["/bin/bash", "-ec", "while :; do echo '.'; sleep 5 ; done"]
  restartPolicy: Never

使用的 Dockerfile :-

FROM ubuntu
RUN apt-get update && apt-get install -y iputils-ping
CMD bash

我将服务创建为

postgresservice.yaml

kind: Service
apiVersion: v1
metadata:
 name: postgressvc
spec:
 type: ClusterIP
 ports:
 - port: 5432
   targetPort: 5432

创建服务的端点为

kind: Endpoints
apiVersion: v1
metadata:
 name: postgressvc
subsets:
 - addresses:
     - ip: 172.31.6.149
   ports:
     - port: 5432

然后我 运行 ping 172.31.6.149 inside pod (kubectl exec -it mark-dummy-pod bash) 但不工作。(ping本地主机正在工作)

kubectl get pods,svc,ep -o wide

的输出
NAME                                       READY   STATUS    RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
pod/marks-dummy-pod                        1/1     Running   0          43m     192.168.1.63   ip-172-31-11-87   <none>           <none>


NAME                       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE     SELECTOR
service/postgressvc        ClusterIP   10.107.58.81     <none>        5432/TCP         33m     <none>


NAME                         ENDPOINTS           AGE
endpoints/postgressvc        172.31.6.149:5432   32m

P Ekambaram 回答的输出

kubectl get pods,svc,ep -o wide 给出

NAME                            READY   STATUS    RESTARTS   AGE    IP             NODE              NOMINATED NODE   READINESS GATES
pod/postgres-855696996d-w6h6c   1/1     Running   0          44s    192.168.1.66   ip-172-31-11-87   <none>           <none>


NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE    SELECTOR
service/postgres        NodePort    10.110.203.204   <none>        5432:31076/TCP   44s    app=postgres



 NAME                      ENDPOINTS           AGE
endpoints/postgres        192.168.1.66:5432   44s

postgres pod 丢失了吗?

您是创建端点对象还是自动生成的?

共享 pod 定义 YAML

您不应该创建端点。这是错误的。遵循以下 postgres 部署。

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: example
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:11
          imagePullPolicy: Always
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-data
      volumes:
        - name: postgres-data
          emptyDir:
---       
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres

取消部署 postgres 服务和端点并部署上述 YAML。 它应该工作

为什么NODE ip的前缀是ip-

您应该为您的数据库创建部署,然后创建一个针对此部署的服务,然后使用此服务进行 ping 为什么要使用 ip 进行 ping?

所以问题出在我的命名空间中的 DNS pod=kube-system

我刚刚创建了新的 kubernetes 设置并确保 DNS 正常工作

新设置参考我另一个问题的回答

?