Kubernetes Service connection refused

1/6/2019

I want to create a sample application with Kubernetes but i get connection refused if i try to connect to the responsive service in Kubernetes.

For example if i connect from another pod to http://random-generator-svc:5050/ i get an error which says connection refused.

this is the yaml file to create the Service and the Deployment for the Random Generator:

apiVersion: v1
  kind: Service
  metadata:
    name: random-generator-svc
    labels:
      app: rand-gen
 spec:
   selector:
     app: rand-gen
     type: NodePort
     ports:
     - protocol: "TCP"
       port: 5050
       targetPort: 5050
       name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: random-generator-deployment
  labels:
    app: rand-gen
spec:
  replicas: 2
  selector:
    matchLabels:
      app: rand-gen
  template:
    metadata:
    labels:
      app: rand-gen
  spec:
    containers:
    - name: random-generator-container
      image: toky03/random-generator-image:1.2
      ports:
      - containerPort: 5050

This is the yaml File wich specifies the Service and the Deployment of the "Caller" Application:

apiVersion: v1
kind: Service
metadata:
  name: middle-tier-svc
  labels:
    app: rand-gen
spec:
  selector:
    app: rand-gen
  type: NodePort
  ports:
  - protocol: "TCP"
    port: 7070
    targetPort: 7070
    name: http

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: middle-tier-controller
  labels:
    app: rand-gen
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rand-gen
  template:
    metadata:
      labels:
        app: rand-gen
    spec:
      containers:
      - name: random-controller-container
        image: toky03/random-controller-image:1.2
        ports:
        - containerPort: 7070

I changed the type to NodePort to try if this error also exists there but i am able to acess the service from outside of a cluster. Is there probably a problem with my Kubernetes DNS resolver?

Thank you very much in advance for your help.

-- Marco Jakob
docker-image
kubernetes
kubernetes-pod
kubernetes-service
spring-boot

2 Answers

1/6/2019

That's because of a slight typo in your yaml indent. try this

apiVersion: v1
kind: Service
metadata:
  name: random-generator-svc
  labels:
    app: rand-gen
spec:
  selector:
    app: rand-gen
  type: NodePort
  ports:
  - protocol: "TCP"
    port: 5050
    targetPort: 5050
    name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: random-generator-deployment
  labels:
    app: rand-gen
spec:
  replicas: 2
  selector:
    matchLabels:
      app: rand-gen
  template:
    metadata:
      labels:
        app: rand-gen
    spec:
      containers:
      - name: random-generator-container
        image: toky03/random-generator-image:1.2
        ports:
        - containerPort: 5050

if you only want your app to be exposed inside your cluster, just remove type: NodePort eg:

apiVersion: v1
kind: Service
metadata:
  name: random-generator-svc
  labels:
    app: rand-gen
spec:
  selector:
    app: rand-gen
  ports:
  - protocol: "TCP"
    port: 5050
    targetPort: 5050
    name: http
-- ahmadnurus
Source: StackOverflow

1/21/2020

By default, kubernetes creates a virtual proxy. You can then access your service after port forwarding it.

kubectl port-forward svc random-generator-svc 5050:5050
-- Sérgio Bernardes
Source: StackOverflow