How to configure a GKE load balancer for a golang tcp server?

5/3/2019

After deploying a golang server container and gke load balancer I can successfully connect to the external ip of the load balancer, but no data reaches the server container.

It works as expected when I run the server container locally and point the client at localhost. I changed it to serve http requests and it worked fine with the same kubernetes manifests. However, if I try to serve both tcp and http (on different ports) then neither work on gke but again works fine locally. So I suspect it probably has something to do with either how I configured the load balancer or how I'm listening for tcp connections in the server breaks something when running on gke but not locally.

K8s Service Manifest

apiVersion: v1
kind: Service
metadata:
  name: steel-server-service
spec:
  type: LoadBalancer
  selector:
    app: steel-server
  ports:
  - protocol: TCP
    name: tcp
    port: 12345
    targetPort: 12345

K8s Deployment Manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: steel-server-deployment
  labels:
    app: steel-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: steel-server
  template:
    metadata:
      labels:
        app: steel-server
    spec:
      containers:
      - name: steel-server
        image: gcr.io/<my-project-id>/steel-server:latest
        ports:
        - containerPort: 12345
          name: tcp

Relevent Go TCP Server Code

server, err := net.Listen("tcp", ":12345")
if err != nil {
    log.Fatalln("Couldn't start up tcp server: ", err)
}
-- Mars
go
google-kubernetes-engine
kubernetes
tcp

1 Answer

5/3/2019

Can you try first with kubectl get svc so you get to know which ports are open with load balancer as you got external ip from GCP as type:loadbalancer.

apiVersion: v1
kind: Service
metadata:
  name: steel-server-service
spec:
  type: LoadBalancer
  selector:
    app: steel-server
  ports:
  - protocol: TCP
    name: tcp
    port: 80
    targetPort: 12345

Try with this service config i change port to 80 while target port of same as container port.

-- Harsh Manvar
Source: StackOverflow