Accessing Kubernetes service converted from Docker Compose

5/9/2019

I built an application using Docker Compose which included an Nginx instance accepting connections on port 80:

  nginx:
    image: nginx:1.15.12-alpine
    container_name: nginx
    volumes:
      - etc.
    ports:
      - 80:80

I'd like to spin up this application on Kubernetes running on my local machine (macOS). So I've run kompose convert, documented here.

This generated nginx-service.yaml which looks like this:

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.18.0 ()
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  ports:
  - name: "80"
    port: 80
    targetPort: 80
  selector:
    io.kompose.service: nginx
status:
  loadBalancer: {}

I ran kubectl apply with all of the YAML files produced by kompose, and then kubectl describe svc nginx:

Name:              nginx
Namespace:         myproject
Labels:            io.kompose.service=nginx
Annotations:       kompose.cmd=kompose convert
                   kompose.version=1.18.0 ()
                   kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{"kompose.cmd":"kompose convert","kompose.version":"1.18.0 ()"},"creationTimestamp":null,...
Selector:          io.kompose.service=nginx
Type:              ClusterIP
IP:                172.30.110.242
Port:              80  80/TCP
TargetPort:        80/TCP
Endpoints:         
Session Affinity:  None
Events:            <none>

However, I cannot access the web server by navigating to http://172.30.110.242:80 on the same machine.

There is documentation on accessing services running on clusters. I'm new to k8s and I'm not sure how to diagnose the problem and pick the right solution of the options they list.

Is it a defect in kompose that it did not generate a comparable service config file?

-- rgov
docker
docker-compose
kompose
kubernetes

1 Answer

5/9/2019

See:

https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport

Your "connect to" URL from your local machine to a K8 world will not be "172.x.x.x". It will probably be 192.168.99.100:33333 (port number wil be different).. run this:

minikube service myservicename -n "default" --url

see what that gives you

but basically, you need to "expose" the k8 world to the outside world.

this yml should help

apiVersion: v1
kind: Service
metadata: 
  name: myservicename
  namespace: mycustomnamespace
  labels: 
    name: myservicemetadatalabel
spec: 
  type: NodePort
  ports:
  - name: myfirstportname
    port: 80
    targetPort: 80    
  selector: 
    myexamplelabelone: mylabelonevalue
    myexamplelabeltwo: mylabeltwovalue

the selector will refer to your pod/container setup.

apiVersion: v1
kind: Pod
metadata:
  name: myfirstpodmetadataname
  namespace: mycustomnamespace
  labels:
    myexamplelabelone: mylabelonevalue
    myexamplelabeltwo: mylabeltwovalue

"selectors" is outside scope of this question...but the above will give you the breadcrumb you need

also see:

How to expose k8 pods to the public internet?

-- granadaCoder
Source: StackOverflow