How to create a Kubernetes ingress to work with SparkJava port 4567?

6/13/2019

I have created a Java based web service which utilizes SparkJava. By default this web service binds and listens to port 4567. My company requested this be placed in a Docker container. I created a Dockerfile and created the image, and when I run I expose port 4567...

docker run -d -p 4567:4567 -t myservice

I can invoke my web service for testing my calling a CURL command...

curl -i -X "POST" -H "Content-Type: application/json" -d "{}" "http://localhost:4567/myservice"

... and this is working. My company then says it wants to put this in Amazon EKS Kubernetes so I publish my Docker image to the company's private Dockerhub. I create three yaml files...

  • deployment.yaml
  • service.yaml
  • ingress.yaml

I see my objects are created and I can get a /bin/bash command line to my container running in Kubernetes and from there test localhost access to my service is working correctly including references to external web service resources, so I know my service is good.

I am confused by the ingress. I need to expose a URI to get to my service and I am not sure how this is supposed to work. Many examples show using NGINX, but I am not using NGINX.

Here are my files and what I have tested so far. Any guidance is appreciated.

service.yaml

kind: Service
apiVersion: v1
metadata:
  name: my-api-service
spec:
  selector:
    app: my-api
  ports:
    - name: main
      protocol: TCP
      port: 4567
      targetPort: 4567

deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-api-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
      - name: my-api-container
        image: hub.mycompany.net/myproject/my-api-service
        ports:
        - containerPort: 4567

ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-api-ingress
spec:
  backend:
    serviceName: my-api-service
    servicePort: 4567

when I run the command ...

kubectl get ingress my-api-ingress

... shows ...

NAME                    HOSTS   ADDRESS   PORTS   AGE
my-api-ingress          *                 80      9s

when I run the command ...

kubectl get service my-api-service

... shows ...

NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
my-api-service          ClusterIP   172.20.247.225   <none>        4567/TCP   16h

When I run the following command...

kubectl cluster-info

... I see ...

Kubernetes master is running at https://12CA0954AB5F8E1C52C3DD42A3DBE645.yl4.us-east-1.eks.amazonaws.com

As such I try to hit the end point using CURL by issuing...

curl -i -X "POST" -H "Content-Type: application/json" -d "{}" "http://12CA0954AB5F8E1C52C3DD42A3DBE645.yl4.us-east-1.eks.amazonaws.com:4567/myservice"

After some time I receive a time-out error...

curl: (7) Failed to connect to 12CA0954AB5F8E1C52C3DD42A3DBE645.yl4.us-east-1.eks.amazonaws.com port 4567: Operation timed out

I believe my ingress is at fault but I am having difficulties finding non-NGINX examples to compare.

Thoughts?

-- barrypicker
docker
kubernetes
kubernetes-ingress

1 Answer

6/17/2019

barrypicker.

Your service should be "type: NodePort" This example is very similar (however tested in GKE).

kind: Service
apiVersion: v1
metadata:
  name: my-api-service
spec:
  selector:
    app: my-api
  ports:
    - name: main
      protocol: TCP
      port: 4567
      targetPort: 4567
  type: NodePort

---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-api
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
      - name: my-api-container
        image: hashicorp/http-echo:0.2.1
        args = ["-listen=:4567", "-text='Hello api'"]
        ports:
        - containerPort: 4567
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-api-ingress
spec:
  backend:
    serviceName: my-api-service
    servicePort: 4567

in your ingress kubectl get ingress <your ingress> you should see an external ip address.

You can find specific AWS implementation here. In addition more information about exposing services you can find here

-- Hanx
Source: StackOverflow