Search code examples
kubernetesyamlkubectl

Naming a service in Kubernetes prevents it from starting


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

Solution

  • 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