【问题标题】:Call to Kubernetes service failed调用 Kubernetes 服务失败
【发布时间】:2020-04-29 11:28:52
【问题描述】:

kubernetes 版本:1.5.2
操作系统:centos 7
etcd 版本:3.4.0

首先,我创建一个 etcd pod、etcd dockerfile 和 etcd pod YAML 文件,如下所示:
etcd dockerfile:

FROM alpine

COPY . /usr/bin
WORKDIR /usr/bin

CMD etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379

EXPOSE 2379

pod yaml 文件

apiVersion: v1
kind: Pod
metadata:
  name: etcd
  namespace: storageplatform
  labels:
    app: etcd
spec:
  containers:
    - name: etcd
      image: "karldoenitz/etcd:3.4.0"
      ports:
        - containerPort: 2379
          hostPort: 12379

创建 docker 镜像并推送到 dockerhub 后,我运行命令 kubectl apply -f etcd.yaml 来创建 etcd pod。 etcd pod的ip是10.254.140.117,我运行命令useETCDCTL_API=3 etcdctl --endpoints=175.24.47.64:12379 put 1 1得到OK
我的服务 yaml:

apiVersion: v1
kind: Service
metadata:
  name: storageservice
  namespace: storageplatform
spec:
  type: NodePort
  ports:
    - port: 12379
      targetPort: 12379
      nodePort: 32379
  selector:
    app: etcd

应用 yaml 文件来创建服务。运行命令kubectl get services -n storageplatform,我得到了这些信息。

NAMESPACE         NAME                   CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE
storageplatform   storageservice         10.254.140.117   <nodes>       12379:32379/TCP   51s

毕竟我是运行命令

ETCDCTL_API=3 etcdctl --endpoints=10.254.140.117:32379 get 1

ETCDCTL_API=3 etcdctl --endpoints={host-ip}:32379 get 1

我收到了Error: context deadline exceeded

怎么了?如何使服务有用?

【问题讨论】:

    标签: kubernetes kubernetes-pod kubernetes-service


    【解决方案1】:

    您使用服务名称/ip (10.254.140.117) 和服务端口 (12379) 定义了一个在 kubernetes 网络内可用的服务,该服务在 kubernetes 集群的所有节点上都可用,甚至在 kubernetes 网络之外也可用节点端口(32379)

    您需要修复服务才能映射到正确的容器端口:targetPort 必须匹配 pod containerPort(以及 dockerfile 中的端口)。

    如果错误Error: context deadline exceeded 仍然存在,则表明存在通信问题。这可以在将内部服务 ip 与外部节点端口(您的第一个获取 1)一起使用时进行解释。对于节点端口(您的第二个命令),我假设 etcd pod 运行不正常,或者端口在节点上被防火墙保护。

    【讨论】:

      【解决方案2】:

      将服务改为引用containerPort而不是hostPort

      apiVersion: v1
      kind: Service
      metadata:
        name: storageservice
        namespace: storageplatform
      spec:
        type: NodePort
        ports:
          - port: 2379
            targetPort: 2379
            nodePort: 32379
        selector:
          app: etcd
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-22
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多