无法从kubernetes中的另一个Pod ping Postgres Pod

时间:2019-06-05 07:04:59

标签: kubernetes kubernetes-pod

我创建了一个忙碌的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

然后我在pod(kubectl exec -it mark-dummy-pod bash)内运行了 ping 172.31.6.149 ,但没有运行。(ping localhost在运行)

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获取广告连播,svc,ep -o宽给出

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

3 个答案:

答案 0 :(得分:2)

所以问题出在我的DNS吊舱的namespace = kube-system

我只是创建新的kubernetes设置,并确保DNS正常工作

有关新设置,请参阅我对另一个问题的回答

How to start kubelet service?

答案 1 :(得分:0)

postgres 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-

前缀

enter image description here

答案 2 :(得分:0)

您应该为数据库创建部署,然后提供针对此部署的服务,然后使用该服务ping为何使用ip ping?