Accessing API endpoints through ingress

Before getting to the above-mentioned issue that I’m facing let me start with a proper description of what I have done so far,

Creating necessary docker and yaml files

  • Create a dockerfile on the root directory of scholarx backend project, (In the current configuration we’ll have to change the build method to jar from war in pom.xml)
touch Dockerfile
  • Put in the content
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
  • Create docker-compose.yaml file
touch docker-compose.yaml
  • Put in the content
version: '3'

services:

  mysql-db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=scholarx
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
    ports:
      - 3306:3306

  scholarx-app:
    image: scholarx
    depends_on:
      - mysql-db
    ports:
      - 8080:8080
    environment:
      - DATABASE_HOST=mysql-db
      - DATABASE_USER=user
      - DATABASE_PASSWORD=password
      - DATABASE_NAME=scholarx
      - DATABASE_PORT=3306
    labels:
      kompose.image-pull-policy: "Never"
  • Create Kubernetes deployment and service files using Kompose
kompose convert
  • Notice that the above command has created 4 configuration files for,
    • database deployment
    • database service
    • scholarx deployment
    • scholarx service
  • Create ingress.yaml file
touch ingress.yaml
  • Put in the content
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sef-ingress
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
        - path: /scholarx
          pathType: Prefix
          backend:
            service:
              name: scholarx-app
              port:
                number: 8080

Setting up the Kubernetes cluster

  • Start minikube
minikube start --vm-driver=hyperkit
  • Change docker environment to minikube
eval $(minikube docker-env)
  • Build docker image
docker build -t scholarx:latest .
  • Enable ingress addon on minikube
minikube addons enable ingress
  • Configure minikube cluster
~ kubectl apply -f mysql-db-deployment.yaml
~ kubectl apply -f mysql-db-service.yaml
~ kubectl apply -f scholarx-app-deployment.yaml
~ kubectl apply -f scholarx-app-service.yaml
~ kubectl apply -f ingress.yaml
  • Check whether the pods and services are all up and running
kubectl get all

output:

NAME                                READY   STATUS    RESTARTS   AGE
pod/mysql-db-6f986865d7-vjr5m       1/1     Running   0          4m2s
pod/scholarx-app-648f6f9457-kdg2k   1/1     Running   2          3m25s

NAME                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
service/kubernetes     ClusterIP   10.96.0.1      <none>        443/TCP    2d19h
service/mysql-db       ClusterIP   10.97.54.223   <none>        3306/TCP   3m55s
service/scholarx-app   ClusterIP   10.105.97.87   <none>        8080/TCP   3m17s

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysql-db       1/1     1            1           4m2s
deployment.apps/scholarx-app   1/1     1            1           3m25s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/mysql-db-6f986865d7       1         1         1       4m2s
replicaset.apps/scholarx-app-648f6f9457   1         1         1       3m25s
  • Check ingress
kubectl get ingress

output:

NAME          CLASS    HOSTS   ADDRESS         PORTS   AGE
sef-ingress   <none>   *       192.168.64.22   80      88s
  • Copy the address and go to {ADDRESS}/scholarx
    example: http://192.168.64.22/scholarx
    output:
2 Likes

Issue

Even though it shows the Whitelabel error page for /scholarx it shows the same for the path /scholarx/programs which needs to output a 200 status code with an empty array [] on the body.

To make sure the application is working, I got into a terminal inside the scholarx-app pod checked the same endpoint,

~ kubectl exec --stdin --tty scholarx-app-648f6f9457-kdg2k -- /bin/sh
~ wget --server-response http://localhost:8080/programs
Connecting to localhost:8080 (127.0.0.1:8080)
  HTTP/1.1 200
  Vary: Origin
  Vary: Access-Control-Request-Method
  Vary: Access-Control-Request-Headers
  X-Content-Type-Options: nosniff
  X-XSS-Protection: 1; mode=block
  Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  Pragma: no-cache
  Expires: 0
  X-Frame-Options: DENY
  Content-Type: application/json
  Transfer-Encoding: chunked
  Date: Wed, 06 Jan 2021 05:19:18 GMT
  Connection: close

programs             100% |********************************|     2  0:00:00 ETA
~ cat programs
[ ]

It returns the desired output which means the application is running properly.
Conclusion, it must be something wrong with the ingress.
@shyamal any tips on how to solve this??

I changed the ingress.yaml to this and now the issue is gone, but I still can’t understand why it does not work for path: /scholarx/ but works for path: /

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sef-ingress
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: scholarx-app
              port:
                number: 8080