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.