From bb5dfe4e23dd286cf8f92ab36a29a8331a09605a Mon Sep 17 00:00:00 2001 From: tusuii Date: Tue, 10 Mar 2026 22:41:08 +0530 Subject: [PATCH] added k8s files and manifests files --- Dockerfile | 12 +++--- Jenkinsfile | 96 +++++++++++++++++++++++++++++++++++++++++++++ k8s/deployment.yaml | 40 +++++++++++++++++++ k8s/ingress.yaml | 20 ++++++++++ k8s/service.yaml | 13 ++++++ 5 files changed, 175 insertions(+), 6 deletions(-) create mode 100644 Jenkinsfile create mode 100644 k8s/deployment.yaml create mode 100644 k8s/ingress.yaml create mode 100644 k8s/service.yaml diff --git a/Dockerfile b/Dockerfile index 3234636..f52250b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,17 @@ -FROM node:20 - -WORKDIR /vaishnaviecommerce - +FROM mirror.gcr.io/library/node:20-bullseye-slim +WORKDIR /app COPY package*.json ./ RUN npm install COPY . . -RUN npm run build +ARG VITE_API_BASE_URL +ENV VITE_API_BASE_URL=$VITE_API_BASE_URL +RUN npm run build EXPOSE 5173 -CMD [ "npm", "run","preview" ] \ No newline at end of file +CMD ["npm", "run", "preview"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..44331b9 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,96 @@ +pipeline { + agent any + + parameters { + string( + name: 'NODE_IP', + defaultValue: '192.168.108.200', + description: 'On-premise Kubernetes node IP' + ) + } + + environment { + HARBOR = '192.168.108.200:80' + IMAGE = '192.168.108.200:80/vaishnavi-ecommerce/website' + OLD_IMAGE = '192.168.49.2:30004/vaishnavi-ecommerce/website:latest' + TAG = "${env.BUILD_NUMBER}" + NAMESPACE = 'ecommerce' + } + + stages { + stage('Build Image') { + steps { + sh """ + docker build \ + --build-arg VITE_API_BASE_URL=http://${params.NODE_IP}:30080/api \ + -t ${IMAGE}:${TAG} \ + -t ${IMAGE}:latest \ + . + """ + } + } + + stage('Push to Harbor') { + steps { + withCredentials([usernamePassword( + credentialsId: 'harbor-credentials', + usernameVariable: 'HARBOR_USER', + passwordVariable: 'HARBOR_PASS' + )]) { + sh """ + echo "\$HARBOR_PASS" | docker login ${HARBOR} -u "\$HARBOR_USER" --password-stdin + docker push ${IMAGE}:${TAG} + docker push ${IMAGE}:latest + """ + } + } + } + + stage('Deploy') { + steps { + withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) { + sh """ + sed "s|${OLD_IMAGE}|${IMAGE}:${TAG}|g" \ + k8s/deployment.yaml | kubectl apply -f - + kubectl apply -f k8s/service.yaml + kubectl apply -f k8s/ingress.yaml + """ + } + } + } + + stage('Verify Rollout') { + steps { + withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) { + sh "kubectl rollout status deployment/website -n ${NAMESPACE} --timeout=180s" + } + } + } + + stage('Smoke Test') { + steps { + sh """ + STATUS=\$(curl -o /dev/null -sw "%{http_code}" http://${params.NODE_IP}:30081) + echo "HTTP status: \$STATUS" + [ "\$STATUS" = "200" ] + """ + } + } + } + + post { + always { + sh "docker rmi ${IMAGE}:${TAG} || true" + sh "docker rmi ${IMAGE}:latest || true" + } + success { + echo "Deploy successful. Website accessible at http://${params.NODE_IP}:30081" + } + failure { + withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) { + sh "kubectl get pods -n ${NAMESPACE} || true" + sh "kubectl describe deployment/website -n ${NAMESPACE} || true" + } + } + } +} diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml new file mode 100644 index 0000000..cd9f71e --- /dev/null +++ b/k8s/deployment.yaml @@ -0,0 +1,40 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: website + namespace: ecommerce +spec: + replicas: 1 + selector: + matchLabels: + app: website + template: + metadata: + labels: + app: website + spec: + containers: + - name: website + image: 192.168.49.2:30004/vaishnavi-ecommerce/website:latest + imagePullPolicy: Always + ports: + - containerPort: 5173 + resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "250m" + memory: "256Mi" + livenessProbe: + httpGet: + path: / + port: 5173 + initialDelaySeconds: 15 + periodSeconds: 15 + readinessProbe: + httpGet: + path: / + port: 5173 + initialDelaySeconds: 5 + periodSeconds: 5 diff --git a/k8s/ingress.yaml b/k8s/ingress.yaml new file mode 100644 index 0000000..7d8563c --- /dev/null +++ b/k8s/ingress.yaml @@ -0,0 +1,20 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: website-ingress + namespace: ecommerce + annotations: + nginx.ingress.kubernetes.io/proxy-body-size: "10m" +spec: + ingressClassName: nginx + rules: + - host: website.local + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: website + port: + number: 5173 diff --git a/k8s/service.yaml b/k8s/service.yaml new file mode 100644 index 0000000..4db9ec5 --- /dev/null +++ b/k8s/service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: website + namespace: ecommerce +spec: + type: NodePort + selector: + app: website + ports: + - port: 5173 + targetPort: 5173 + nodePort: 30081