Naming a service in Kubernetes prevents it from starting

10/18/2019

Here is an example of yaml file for deploying:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  backend:
    serviceName: gateway-service
    servicePort: 4001
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway-deployment
spec:
  selector:
    matchLabels:
      app: gateway
  replicas: 2
  template:
    metadata:
      labels:
        app: gateway
    spec:
      containers:
      - name: gateway
        image: hello-go-microservices_gateway
        imagePullPolicy: Never
        ports:
        - containerPort: 4001
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: gateway-service
spec:
  selector:
    app: gateway
  ports:
  - protocol: TCP
    port: 4001
    targetPort: 4001
    nodePort: 30001
  type: NodePort

As you can see, the service is called gateway-service. The problem is when I rename it to just gateway, it's pods won't start. When I rename it to gateway-service back, or rename it to gateway1, or to gateway-blablabla, everytging works well.

Kubectl's logs show:

Failed to decode: strconv.ParseInt: parsing "tcp://10.101.177.91:4001": invalid syntax
-- ligowsky
kubectl
kubernetes
yaml

3 Answers

10/18/2019

I finally figured out. Thanks to P Ekambaram's comment "some issue with the pod. are you using service name in pod?"

The issue was with .env file

GATEWAY_PORT=4001

It is for local development purpose. I renamed it to LOCAL_GATEWAY_PORT and now pods work with service named "gateway".

-- ligowsky
Source: StackOverflow

10/18/2019

Try below YAML. it should work

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  backend:
    serviceName: gateway
    servicePort: 4001
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway-deployment
spec:
  selector:
    matchLabels:
      app: gateway
  replicas: 2
  template:
    metadata:
      labels:
        app: gateway
    spec:
      containers:
      - name: gateway
        image: hello-go-microservices_gateway
        imagePullPolicy: Never
        ports:
        - containerPort: 4001
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  selector:
    app: gateway
  ports:
  - protocol: TCP
    port: 4001
    targetPort: 4001
    nodePort: 30001
  type: NodePort

i just deployed deployment and service. it worked without any issues

master $ kubectl get po,svc | grep gateway

pod/gateway-deployment-6549f895b4-5d9kj   1/1     Running   0          18s
pod/gateway-deployment-6549f895b4-vzqj6   1/1     Running   0          18s
service/gateway      NodePort    10.106.132.46   <none>        4001:30001/TCP   18s
master $ curl 10.106.132.46:4001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
-- P Ekambaram
Source: StackOverflow

10/18/2019

Thanks to P Ekambaram suggestion and just to provide more information for other community members in case they will have the same issue:

According to the Discovering services mechanism - once the service was created all information where applied to the PODS env:

Note:

When a Pod is run on a Node, the kubelet adds a set of environment variables for each active Service. It supports both Docker links compatible variables (see makeLinkVariables) and simpler {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT variables, where the Service name is upper-cased and dashes are converted to underscores.

Proper env variables should looks like:

GATEWAY_PORT_8080_TCP=tcp://10.4.13.154:8080
GATEWAY_PORT_8080_TCP_PROTO=tcp
GATEWAY_PORT_8080_TCP_PORT=8080
GATEWAY_PORT_8080_TCP_ADDR=10.4.13.154

GATEWAY_SERVICE_HOST=10.4.13.154
GATEWAY_SERVICE_PORT=8080

and values for: GATEWAY_PORT=tcp://10.4.13.154:8080 were overridden by your custom settings:

 GATEWAY_PORT=8080
-- Hanx
Source: StackOverflow