diff --git a/k8s/data.yaml b/k8s/data.yaml new file mode 100644 index 0000000..791d12a --- /dev/null +++ b/k8s/data.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + labels: + io.kompose.service: data + name: data +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 100Mi diff --git a/k8s/deploy.yaml b/k8s/deploy.yaml new file mode 100644 index 0000000..0483365 --- /dev/null +++ b/k8s/deploy.yaml @@ -0,0 +1,42 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + name.full: "TUSC-The Url Shortener Company" + labels: + io.kompose.service: shortener-app + name: shortener-app +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: shortener-app + strategy: + type: Recreate + template: + metadata: + annotations: + name.full: "TUSC-The Url Shortener Company" + labels: + io.kompose.network/k8s-default: "true" + io.kompose.service: shortener-app + spec: + containers: + - image: nmpl/shortener:k8s + name: shortener-app + ports: + - containerPort: 8080 + hostPort: 8080 + protocol: TCP + - containerPort: 3306 + hostPort: 3306 + protocol: TCP + + volumeMounts: + - mountPath: /var/lib/mysql + name: data + restartPolicy: Always + volumes: + - name: data + persistentVolumeClaim: + claimName: data diff --git a/k8s/docker-k8s/Dockerfile b/k8s/docker-k8s/Dockerfile new file mode 100644 index 0000000..61a9632 --- /dev/null +++ b/k8s/docker-k8s/Dockerfile @@ -0,0 +1,51 @@ +# syntax=docker/dockerfile:1 + +# Add the following lines to tag the image (replace 'your_username' and 'shortener-app' with your Docker Hub username and repository name) +ARG VERSION=k8s +ARG IMAGE_NAME=bigwiz/shortener +ARG TAG=$VERSION + +# Stage 1: Build the application +FROM maven:3.9.6-eclipse-temurin-21 AS builder + +# Clone the repository +RUN git clone https://github.com/aamitn/URLShortener.git + +WORKDIR /URLShortener + +# Build the application +RUN mvn clean install + +# Stage 2: Create the final image +FROM tomcat:10-jdk21-openjdk-slim + +# Set environment variables +ENV CATALINA_BASE /usr/local/tomcat +ENV CATALINA_HOME /usr/local/tomcat +ENV PATH $CATALINA_HOME/bin:$PATH + + +# Copy the WAR file from the builder stage +COPY --from=builder /URLShortener/target/shortener.war $CATALINA_BASE/webapps/ + + +# Add configuration for document base path +COPY --from=builder /URLShortener/server.xml $CATALINA_BASE/conf/server.xml + + +# Expose ports +EXPOSE 8080 +EXPOSE 3306 + + +# Copy the startup script +COPY shortener.sh /usr/local/tomcat/shortener.sh + +# Copy the sql file +COPY --from=builder /URLShortener/create.sql /usr/local/tomcat/create.sql + +# Grant execute permissions to the startup.sh script +RUN chmod +x /usr/local/tomcat/shortener.sh + +# Start Tomcat and MariaDB using the startup script +CMD ["sh", "/usr/local/tomcat/shortener.sh"] diff --git a/k8s/docker-k8s/docker-compose.yml b/k8s/docker-k8s/docker-compose.yml new file mode 100644 index 0000000..5f6deb3 --- /dev/null +++ b/k8s/docker-k8s/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3.8' + +services: + shortener-app: + labels: + - "TUSC" + + #Build from docker hub image .Comment/Uncomment Below + image: nmpl/shortener:k8s + + #Build from local Dockerfile.Comment/Uncomment Below + #build: + #dockerfile: Dockerfile + + ports: + - "8080:8080" + - "3306:3306" + + volumes: + - data:/var/lib/mysql + + restart: unless-stopped + + +volumes: + data: diff --git a/k8s/docker-k8s/shortener.sh b/k8s/docker-k8s/shortener.sh new file mode 100644 index 0000000..660198d --- /dev/null +++ b/k8s/docker-k8s/shortener.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +echo "Installing MariaDB..." +# Install MariaDB +apt-get update +apt-get install -y mariadb-server + +echo "Starting MariaDB service..." +# Start MariaDB service +service mariadb start + +echo "Waiting for MariaDB to start (adjust sleep time as needed)..." + +# Wait for MariaDB to start +while ! mysqladmin ping -hlocalhost -uroot --silent; do + echo "MariaDB is not yet available. Waiting..." + sleep 5 +done + +echo "Running SQL script to initialize the database..." +# Access MySQL Command Line and run SQL script to initialize the database +mysql -u root -e "source /usr/local/tomcat/create.sql" + +echo "Displaying databases and tables..." +# Display the databases and tables +mysql -u root -e "SHOW ENGINE PERFORMANCE_SCHEMA STATUS;SHOW ENGINE INNODB STATUS;" +mysql -u root -e "SHOW DATABASES; USE shortener; SHOW TABLES; SHOW TABLE STATUS\G;" + +echo "Altering user and reloading privileges..." +# Run SQL commands to alter user and reload privileges +mysql -u root <=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/k8s/shortener-chart/templates/service.yaml b/k8s/shortener-chart/templates/service.yaml new file mode 100644 index 0000000..157e0d6 --- /dev/null +++ b/k8s/shortener-chart/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "shortener-chart.fullname" . }} + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "shortener-chart.selectorLabels" . | nindent 4 }} diff --git a/k8s/shortener-chart/templates/serviceaccount.yaml b/k8s/shortener-chart/templates/serviceaccount.yaml new file mode 100644 index 0000000..9c8188d --- /dev/null +++ b/k8s/shortener-chart/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "shortener-chart.serviceAccountName" . }} + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} diff --git a/k8s/shortener-chart/templates/tests/test-connection.yaml b/k8s/shortener-chart/templates/tests/test-connection.yaml new file mode 100644 index 0000000..c5e7377 --- /dev/null +++ b/k8s/shortener-chart/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "shortener-chart.fullname" . }}-test-connection" + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "shortener-chart.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/k8s/shortener-chart/values.yaml b/k8s/shortener-chart/values.yaml new file mode 100644 index 0000000..64d7654 --- /dev/null +++ b/k8s/shortener-chart/values.yaml @@ -0,0 +1,107 @@ +# Default values for shortener-chart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nmpl/shortener + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "k8s" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Automatically mount a ServiceAccount's API credentials? + automount: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} +podLabels: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 8080 + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +livenessProbe: + httpGet: + path: / + port: http +readinessProbe: + httpGet: + path: / + port: http + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +# Additional volumes on the output Deployment definition. +volumes: [] +# - name: foo +# secret: +# secretName: mysecret +# optional: false + +# Additional volumeMounts on the output Deployment definition. +volumeMounts: [] +# - name: foo +# mountPath: "/etc/foo" +# readOnly: true + +nodeSelector: {} + +tolerations: [] + +affinity: {}