简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM