Setting up a kubernetes cluster for the scholarx backend

Hello friends,

I’ve gotten assigned to do some research on the above-mentioned topic and implement it on the local machine so that everyone could set it up and move on with frontend and backend development.

I followed the following steps to get a Kubernetes cluster set up in the local machine

  • Installed Minikube and hyper kit and started Minikube
  • Dockerized the scholarx backend and put it on docker hub
  • Created two secrets for mysql root password and mysql db url using kubectl (Automatically gets installed when installing minikube) inside the Minikube cluster to be used when creating the mysql pod. kubectl create secret generic <define>
kubectl create secret generic mysql-root-pass --from-literal=password=${PASSWORD}
kubectl create secret generic mysql-db-url --from-literal=database=scholarx --from-literal=url='jdbc:mysql://scholarx-app-mysql/scholarx?useSSL=false&useUnicode=true&characterEncoding=UTF-8'
  • Wrote two yaml files to deploy the spring boot app and mysql db to the server. (Above secrets are used in this yaml files)

spring-dep.yaml

apiVersion: apps/v1           # API version
kind: Deployment              # Type of kubernetes resource
metadata:
  name: scholarx-app-server    # Name of the kubernetes resource
  labels:                     # Labels that will be applied to this resource
    app: scholarx-app-server
spec:
  replicas: 1                 # No. of replicas/pods to run in this deployment
  selector:
    matchLabels:              # The deployment applies to any pods mayching the specified labels
      app: scholarx-app-server
  template:                   # Template for creating the pods in this deployment
    metadata:
      labels:                 # Labels that will be applied to each Pod in this deployment
        app: scholarx-app-server
    spec:                     # Spec for the containers that will be run in the Pods
      containers:
      - name: scholarx-app-server
        image: gravewalker666/spring-test-api:latest
        imagePullPolicy: IfNotPresent
        ports:
          - name: http
            containerPort: 8080 # The port that the container exposes
        resources:
          limits:
            cpu: 0.2
            memory: "200Mi"
        env:                  # Environment variables supplied to the Pod
        - name: SPRING_DATASOURCE_USERNAME # Name of the environment variable
          value: root
        - name: SPRING_DATASOURCE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-root-pass
              key: password
        - name: SPRING_DATASOURCE_URL
          valueFrom:
            secretKeyRef:
              name: mysql-db-url
              key: url
---
apiVersion: v1                # API version
kind: Service                 # Type of the kubernetes resource
metadata:
  name: scholarx-app-server    # Name of the kubernetes resource
  labels:                     # Labels that will be applied to this resource
    app: scholarx-app-server
spec:
  type: NodePort              # The service will be exposed by opening a Port on each node and proxying it.
  selector:
    app: scholarx-app-server   # The service exposes Pods with label `app=polling-app-server`
  ports:                      # Forward incoming connections on port 8080 to the target port 8080
  - name: http
    port: 8080
    targetPort: 8080

mysql-dep.yaml

apiVersion: v1
kind: PersistentVolume            # Create a PersistentVolume
metadata:
  name: mysql-pv
  labels:
    type: local
spec:
  storageClassName: standard      # Storage class. A PV Claim requesting the same storageClass can be bound to this volume.
  capacity:
    storage: 250Mi
  accessModes:
    - ReadWriteOnce
  hostPath:                       # hostPath PersistentVolume is used for development and testing. It uses a file/directory on the Node to emulate network-attached storage
    path: "/mnt/data"
  persistentVolumeReclaimPolicy: Retain  # Retain the PersistentVolume even after PersistentVolumeClaim is deleted. The volume is considered “released”. But it is not yet available for another claim because the previous claimant’s data remains on the volume.
---
apiVersion: v1
kind: PersistentVolumeClaim        # Create a PersistentVolumeClaim to request a PersistentVolume storage
metadata:                          # Claim name and labels
  name: mysql-pv-claim
  labels:
    app: scholarx-app
spec:                              # Access mode and resource limits
  storageClassName: standard       # Request a certain storage class
  accessModes:
    - ReadWriteOnce                # ReadWriteOnce means the volume can be mounted as read-write by a single Node
  resources:
    requests:
      storage: 250Mi
---
apiVersion: v1                    # API version
kind: Service                     # Type of kubernetes resource
metadata:
  name: scholarx-app-mysql         # Name of the resource
  labels:                         # Labels that will be applied to the resource
    app: scholarx-app
spec:
  ports:
    - port: 3306
  selector:                       # Selects any Pod with labels `app=polling-app,tier=mysql`
    app: scholarx-app
    tier: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment                    # Type of the kubernetes resource
metadata:
  name: scholarx-app-mysql           # Name of the deployment
  labels:                           # Labels applied to this deployment
    app: scholarx-app
spec:
  selector:
    matchLabels:                    # This deployment applies to the Pods matching the specified labels
      app: scholarx-app
      tier: mysql
  strategy:
    type: Recreate
  template:                         # Template for the Pods in this deployment
    metadata:
      labels:                       # Labels to be applied to the Pods in this deployment
        app: scholarx-app
        tier: mysql
    spec:                           # The spec for the containers that will be run inside the Pods in this deployment
      containers:
      - image: mysql:5.6            # The container image
        name: mysql
        env:                        # Environment variables passed to the container
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:                # Read environment variables from kubernetes secrets
            secretKeyRef:
              name: mysql-root-pass
              key: password
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
              name: mysql-db-url
              key: database
        - name: MYSQL_USER
          value: root
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-root-pass
              key: password
        ports:
        - containerPort: 3306        # The port that the container exposes
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage  # This name should match the name specified in `volumes.name`
          mountPath: /var/lib/mysql
      volumes:                       # A PersistentVolume is mounted as a volume to the Pod
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
  • Ran the command kubectl apply -f <filename> (replace filename with the spring-dep.yaml and mysql-dep.yaml)

The next step is to Install Kong as the ingress contoller and expose the backend endpoint through kong.

4 Likes

Thanks, @Gravewalker for the detailed explanation! This is so helpful!

I installed kong to the minikube cluster and set up an ingress controller to access the backend service. As I had perceived reading kong docs with this setup I should be able to access the backend service inside the cluster via ingress controller but I still could not figure out how to access the service even though the ingress controller is perfectly up and running.

Here’s how I installed kong and setup the ingress controller to the scholarx backend.

  1. Installed kong by using the command,
kubectl apply -f https://bit.ly/kong-ingress-dbless
  1. Setup kong ingress controller
    kong-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: scholarx-ingress
  annotations:
    kubernetes.io/ingress.class: kong
spec:
  backend:
    serviceName: scholarx-app-server
    servicePort: 8080

command:

kubectl apply -f kong-ingress.yaml