Connection Refused error while deploying nginx service in Google Cloud

5/17/2018

I am deploying nginx image using following deployment files in Google Cloud.

For Replicationcontroller :

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-web
  labels:
    name: nginx-web
    app: demo
spec:
  replicas: 2
  template:
    metadata:
      labels:
        name: nginx-web
    spec:
      containers:
        - name: nginx-web
          image: nginx
          ports:
            - containerPort: 5000
              name: http
              protocol: TCP

For Service Deployment

apiVersion: v1
kind: Service
metadata:
  name: nginx-web
  labels:
    name: nginx-web
    app: demo
spec:
  selector:
    name: nginx-web
  type: LoadBalancer
  ports:
   - port: 84
     targetPort: 5000
     protocol: TCP

But when I do curl on external_IP (I got from loadbalancer) on port 84, I get connection refused error. What might be the issue?

-- rishi007bansod
google-cloud-platform
google-kubernetes-engine
kubernetes

1 Answer

5/17/2018

The nginx image you are using in your replication controller is listening on port 80 (that's how the image is build).

You need to fix your replication controller spec like this:

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-web
  labels:
    name: nginx-web
    app: demo
spec:
  replicas: 2
  template:
    metadata:
      labels:
        name: nginx-web
    spec:
      containers:
        - name: nginx-web
          image: nginx
          ports:
            - containerPort: 80
              name: http
              protocol: TCP

And also adjust your service like this:

apiVersion: v1
kind: Service
metadata:
  name: nginx-web
  labels:
    name: nginx-web
    app: demo
spec:
  selector:
    name: nginx-web
  type: LoadBalancer
  ports:
   - port: 84
     targetPort: 80
     protocol: TCP
-- whites11
Source: StackOverflow