Call to Kubernetes service failed

4/29/2020

kubernetes version: 1.5.2
os: centos 7
etcd version: 3.4.0

First, I create an etcd pod, the etcd dockerfile and etcd pod YAML file like this:
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 file

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

After created the docker image and push to dockerhub, I run the command kubectl apply -f etcd.yaml to create the etcd pod. The ip of etcd pod is 10.254.140.117, I ran the command use ETCDCTL_API=3 etcdctl --endpoints=175.24.47.64:12379 put 1 1 and got OK.
My service yaml:

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

apply the yaml file to create the service.run the command kubectl get services -n storageplatform, I got these infomation.

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

After all, I run the command

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

or

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

I got Error: context deadline exceeded.

What's the matter? How to make the service useful?

-- Karl Doenitz
kubernetes
kubernetes-pod
kubernetes-service

2 Answers

4/29/2020

You defined a service that is available inside the kubernetes network using the service name/ip (10.254.140.117 ) and the service port (12379) and which is available on ALL nodes of the kubernetes cluster even outside the kubernetes network with the node port (32379)

You need to fix the service in order to map to the correct container port: targetPort must match the pod containerPort (and the port in the dockerfile).

If the error Error: context deadline exceeded persits, it hints at a communication problem. This can be explained when using the internal service ip with the external node port (your first get 1). For the node port (your second command) I assume that either the etcd pod is not running properly, or the port is firewalled on the node.

-- Thomas
Source: StackOverflow

4/29/2020

Change the service to refer to containerPort instead of hostPort

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