ceth
ceth

Reputation: 45325

Connect to service inside cluster

I am reading "Kubernetes in Action" and trying samples from the book. I have created a pod:

apiVersion: v1
kind: Pod
metadata:
  name: kubia-manual
spec:
  containers:
  - image: luksa/kubia
    name: kubia
    ports:
    - containerPort: 8080
      protocol: TCP

and service:

apiVersion: v1
kind: Service
metadata:
  name: kubia
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: kubia

Here is my current state:

# ~/work_folder/code/kubernetes/kubernetes-in-action/Chapter05 ±master » k get pods
NAME                                READY   STATUS    RESTARTS   AGE
azure-vote-back-77dff7bbd5-z22ff    1/1     Running   0          2d18h
azure-vote-front-5bc759676c-8v8zp   1/1     Running   0          2d18h
fortune                             2/2     Running   0          2d8h
kubia-manual                        1/1     Running   0          2d7h

# ~/work_folder/code/kubernetes/kubernetes-in-action/Chapter05 ±master » k get svc
NAME               TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)        AGE
azure-vote-back    ClusterIP      10.0.216.167   <none>          6379/TCP       2d18h
azure-vote-front   LoadBalancer   10.0.125.207   20.40.139.148   80:31909/TCP   2d18h
fortune            ClusterIP      10.0.236.103   <none>          80/TCP         2d7h
fortune-nodeport   NodePort       10.0.69.172    <none>          80:30132/TCP   2d7h
kubernetes         ClusterIP      10.0.0.1       <none>          443/TCP        2d18h
kubia              ClusterIP      10.0.234.108   <none>          80/TCP         8m58s

Now I am trying to call the service from pod inside my cluster:

# ~/work_folder/code/kubernetes/kubernetes-in-action/Chapter05 ±master » k exec kubia-manual -- curl -s 10.0.234.108               7 ↵
command terminated with exit code 7

# ~/work_folder/code/kubernetes/kubernetes-in-action/Chapter05 ±master » k exec kubia-manual -- curl 10.0.234.108                  7 ↵
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to 10.0.234.108 port 80: Connection refused
command terminated with exit code 7

Why did I fail and how can I fix it ?

Upvotes: 0

Views: 58

Answers (1)

Arghya Sadhu
Arghya Sadhu

Reputation: 44677

You have a selector app: kubia in the service. So the pods need have a label app=kubia otherwise the service will not have Endpoints populated with Pod IPs and does not know where to send the traffic . You can use kubectl describe svc kubia command to check if the service has Pod IPs in the Endpoints section.

Check the docs for more debugging with service.

Upvotes: 3

Related Questions