Marco Jakob
Marco Jakob

Reputation: 67

Kubernetes Service connection refused

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 which 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 access 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.

Upvotes: 1

Views: 8049

Answers (3)

Raam Mishra
Raam Mishra

Reputation: 75

Try "http://random-generator-svc.default.svc.cluster.local:5050" . You can replace the "default" with the namespace if you are using any.

Upvotes: 0

Sérgio Bernardes
Sérgio Bernardes

Reputation: 31

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

Upvotes: 1

ahmadnurus
ahmadnurus

Reputation: 1

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

Upvotes: 0

Related Questions