From 8872ac0f181c783b280ebfd919403f4d9bbb0603 Mon Sep 17 00:00:00 2001 From: Bharath Date: Tue, 30 Nov 2021 03:06:32 +0000 Subject: [PATCH 1/2] added code for springboot app dockerfile and helm chart --- Dockerfile | 10 ++++ springbootapp/.helmignore | 23 +++++++++ springbootapp/Chart.yaml | 24 ++++++++++ springbootapp/templates/_helpers.tpl | 62 +++++++++++++++++++++++++ springbootapp/templates/deployment.yaml | 25 ++++++++++ springbootapp/templates/service.yaml | 16 +++++++ springbootapp/values.yaml | 13 ++++++ 7 files changed, 173 insertions(+) create mode 100644 Dockerfile create mode 100644 springbootapp/.helmignore create mode 100644 springbootapp/Chart.yaml create mode 100644 springbootapp/templates/_helpers.tpl create mode 100644 springbootapp/templates/deployment.yaml create mode 100644 springbootapp/templates/service.yaml create mode 100644 springbootapp/values.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cbf325c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM openjdk as base +COPY . . +RUN chmod +x mvnw +RUN ./mvnw package -DskipTests + + +FROM openjdk +COPY --from=base target/spring-boot-rest-api-tutorial-0.0.1-SNAPSHOT.jar ./ +ENTRYPOINT ["/bin/bash","-c"] +CMD ["java -jar spring-boot-rest-api-tutorial-0.0.1-SNAPSHOT.jar"] \ No newline at end of file diff --git a/springbootapp/.helmignore b/springbootapp/.helmignore new file mode 100644 index 0000000..f82e96d --- /dev/null +++ b/springbootapp/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/springbootapp/Chart.yaml b/springbootapp/Chart.yaml new file mode 100644 index 0000000..1e2aad4 --- /dev/null +++ b/springbootapp/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: webapp-demo +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/springbootapp/templates/_helpers.tpl b/springbootapp/templates/_helpers.tpl new file mode 100644 index 0000000..8287dbc --- /dev/null +++ b/springbootapp/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "test-dev.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "test-dev.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "test-dev.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "test-dev.labels" -}} +helm.sh/chart: {{ include "test-dev.chart" . }} +{{ include "test-dev.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "test-dev.selectorLabels" -}} +app.kubernetes.io/name: {{ include "test-dev.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "test-dev.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "test-dev.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/springbootapp/templates/deployment.yaml b/springbootapp/templates/deployment.yaml new file mode 100644 index 0000000..6729013 --- /dev/null +++ b/springbootapp/templates/deployment.yaml @@ -0,0 +1,25 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "test-dev.fullname" . }} + namespace: {{ .Values.namespace }} + labels: + {{- include "test-dev.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "test-dev.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "test-dev.selectorLabels" . | nindent 8 }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: {{ .Values.containePort }} + protocol: TCP \ No newline at end of file diff --git a/springbootapp/templates/service.yaml b/springbootapp/templates/service.yaml new file mode 100644 index 0000000..28dd6e9 --- /dev/null +++ b/springbootapp/templates/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "test-dev.fullname" . }} + namespace: {{ .Values.namespace }} + labels: + {{- include "test-dev.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: {{ .Values.containerPort }} + protocol: TCP + name: http + selector: + {{- include "test-dev.selectorLabels" . | nindent 4 }} diff --git a/springbootapp/values.yaml b/springbootapp/values.yaml new file mode 100644 index 0000000..2280a5c --- /dev/null +++ b/springbootapp/values.yaml @@ -0,0 +1,13 @@ + +replicaCount: 1 +containePort: 8080 +namespace: develop +image: + repository: 609474515704.dkr.ecr.us-east-1.amazonaws.com/springbootapp + pullPolicy: IfNotPresent + tag: "latest" + +imagePullSecrets: [] +service: + type: LoadBalancer + port: 80 From 3a18f426964f4d3af50e8f5adc4af94762a2d523 Mon Sep 17 00:00:00 2001 From: Bharath Date: Tue, 30 Nov 2021 03:12:09 +0000 Subject: [PATCH 2/2] added code for springboot app dockerfile and helm chart --- Jenkinsfile | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..1b2bf60 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,28 @@ +pipeline { + agent any + environment { + AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID') + AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY') + } + stages { + + stage ('Build Docker Image') { + steps { + sh ''' + sudo aws ecr get-login --region us-east-1 --no-include-email | xargs -n 1 -P 10 -I {} bash -c {} + sudo docker build -t 609474515704.dkr.ecr.us-east-1.amazonaws.com/springbootapp:latest . + sudo docker push 609474515704.dkr.ecr.us-east-1.amazonaws.com/springbootapp:latest + ''' + } + } + stage ('Deploy docker image to K8s') { + steps { + sh ''' + aws eks update-kubeconfig --name test-dev-eks --region us-east-1 + helm upgrade --install test-dev springbootapp + ''' + } + } + + } + } \ No newline at end of file