kompose not exposing NodePort - Docker for Desktop Windows

1/7/2020

I have this docker-compose.yml and Docker for Desktop Windows with a local kubernetes cluster (the default docker-desktop context) running:

version: '3'
services:
  web:
    image: customnode
    ports:
    - "3000:3000"
    labels:
      kompose.service.type: nodeport

  datastore:
    image: custommongo
    ports:
    - "27017:27017"

docker-compose up -d works perfectly and exposes my NodeJS on port 3000 on 127.0.0.1.

I'm trying to migrate that the my k8s cluster, so following https://kompose.io/getting-started/

Page above says "If you don’t already have a Kubernetes cluster running, minikube is the best way to get started"... I am already running the OOTB Docker for Desktop cluster so I assume I don't need minikube.

kompose convert
INFO Kubernetes file "datastore-service.yaml" created
INFO Kubernetes file "web-service.yaml" created
INFO Kubernetes file "datastore-deployment.yaml" created
INFO Kubernetes file "web-deployment.yaml" created

Here are the web-deployment and web-service YAMLS:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.service.type: nodeport
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: web
  name: web
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: web
    spec:
      containers:
        image: customnode
        name: web
        ports:
        - containerPort: 3000
        resources: {}
      restartPolicy: Always
status: {}

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.service.type: nodeport
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: web
  name: web
spec:
  ports:
  - name: "3000"
    port: 3000
    targetPort: 3000
  selector:
    io.kompose.service: web
  type: NodePort
status:
  loadBalancer: {}

Finally, running kompose up:

kompose up
[36mINFO[0m We are going to create Kubernetes Deployments, Services and PersistentVolumeClaims for your Dockerized application. If you need different kind of resources, use the 'kompose convert' and 'kubectl create -f' commands instead.

INFO Deploying application in "default" namespace
INFO Successfully created Service: datastore
INFO Successfully created Service: web
INFO Successfully created Deployment: datastore
INFO Successfully created Deployment: web

Output of kubectl get svc:

kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
datastore    ClusterIP   10.103.***.***   <none>        27017/TCP        76s
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          23m
web          NodePort    10.106.***.***     <none>        3000:32033/TCP   76s

As you can see, no external IP as I'd expect. I'm sure this is a lack of knowledge on my part, rather than a bug, so what am I missing?

-- A. Gardner
docker
docker-compose
kompose
kubernetes

1 Answer

1/7/2020

Extenal IP is assigned only for LoadBalancer type service. A LoadBalancer controller must be installed on the cluster for LoadBalancer service to work. Else LoadBalancer service behaves exactly as a NodePort service. Most cloud providers have support for LoadBalancer services.

For NodePort type services, the service binds to a random port in node port range on all the nodes. In your case you can see, the service is bound to port 32033 - 3000:32033/TCP.

The Node Port range is configured for as an argument to Kubernetes API server with the option --service-node-port-range (by default 30000-32767). When you create a NodePort type service, a random free port is picked from this range. If you want to choose a custom port, you can specify nodePort attribute in the Port object.

Eg:

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.service.type: nodeport
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: web
  name: web
spec:
  ports:
  - name: "3000"
    port: 3000
    targetPort: 3000
    nodePort: 30002         ###### You can choose node port here if needed
  selector:
    io.kompose.service: web
  type: NodePort           ####### Change this line to LoadBalancer if you want an external IP
status:
  loadBalancer: {}
-- Shashank V
Source: StackOverflow