How to create a kubernetes deployment if the container listens to no ports?

9/7/2018

I have a container with a backend processing application that only connects to other services, but does not expose any ports it listens to. For example in my case it connects to a JMS broker and uses the Rest API of another service.

I want to deploy that container along with the JMS broker and the server with the Rest API to kubernetes. Therefore I'm currently having these kubernetes API objects for the backend processing application:

---
  kind: "Deployment"
  apiVersion: "extensions/v1beta1"
  metadata: 
    name: "foo-processing-module"
    namespace: "foo-4"
    labels: 
      foo.version: "0.0.1-SNAPSHOT"
      k8s-app: "foo-processing-module"
    annotations: 
      deployment.kubernetes.io/revision: "1"
      description: "Processing Modules App for foo"
  spec: 
    replicas: 1
    selector: 
      matchLabels: 
        foo.version: "0.0.1-SNAPSHOT"
        k8s-app: "foo-processing-module"
    template: 
      metadata: 
        name: "foo-processing-module"
        labels: 
          foo.version: "0.0.1-SNAPSHOT"
          k8s-app: "foo-processing-module"
        annotations: 
          description: "Processing Modules App for foo"
      spec: 
        containers: 
          - 
            name: "foo-processing-module"
            image: "foo/foo-processing-module-docker:0.0.1-SNAPSHOT"
            resources: {}
            terminationMessagePath: "/dev/termination-log"
            terminationMessagePolicy: "File"
            imagePullPolicy: "IfNotPresent"
            securityContext: 
              privileged: false
        restartPolicy: "Always"
        terminationGracePeriodSeconds: 30
        dnsPolicy: "ClusterFirst"
        securityContext: {}
        schedulerName: "default-scheduler"
    strategy: 
      type: "RollingUpdate"
      rollingUpdate: 
        maxUnavailable: "25%"
        maxSurge: "25%"
    revisionHistoryLimit: 10
    progressDeadlineSeconds: 600
---
  kind: "Service"
  apiVersion: "v1"
  metadata: 
    name: "foo-processing-module"
    namespace: "foo-4"
    labels: 
      foo.version: "0.0.1-SNAPSHOT"
      k8s-app: "foo-processing-module"
    annotations: 
      description: "Processing Modules App for foo"
  spec: 
    selector: 
      foo.version: "0.0.1-SNAPSHOT"
      k8s-app: "foo-processing-module"
    type: "LoadBalancer"
    sessionAffinity: "None"
    externalTrafficPolicy: "Cluster"

However when I use kubectl create I get the following error message when the above API objects should be created:

Error from server (Invalid): error when creating "foo.yml": Service "foo-processing-module" is invalid: spec.ports: Required value
error converting YAML to JSON: yaml: line 22: did not find expected <document start>

What do I have to do to resolve the error? Is a kubernetes Service even the correct API object to use in this case?

-- SpaceTrucker
deployment
kubernetes
service

2 Answers

9/7/2018

Simply remove the entire Service object. Since you have an app that doesn't need to communicate via the network, you don't need a service. Think of the service as a kind of specialized load-balancer in front of an (HTTP?) API your pods expose. Since you don't have that API, you don't need it. The Deployment does the actual supervision of the worker pods, that is, whatever goes on in foo/foo-processing-module-docker:0.0.1-SNAPSHOT.

Also, always use kubectl apply and not create and if you want to keep track of the revisions deployed, add the --record option so you can access the history.

-- Michael Hausenblas
Source: StackOverflow

9/7/2018

You don't need a service for the backend processing application, since it doesn't receive any incoming connections.

You only need a service when you want to expose a part of a deployment to the cluster/internet to receive incoming connections.

Also, when defining a LoadBalancer service you need to specify a port.

-- bn4t
Source: StackOverflow