Commit 7a77074b authored by khadythiara's avatar khadythiara

charts

parent 72821b65
pipeline {
environment {
imagename = "khadydiagne/k8s_app"
registryCredential = 'docker'
SONAR_PROJECT_KEY = 'test_java'
SONAR_HOST_URL = 'http://192.168.230.128:9000'
SONAR_TOKEN = credentials('sonarqube') // Jeton d'accès SonarQube stocké dans Jenkins
KUBECONFIG = '/var/lib/jenkins/.kube/config' // Chemin vers le fichier kubeconfig dans Jenkins
}
agent any
stages {
stage('Cloning Git') {
steps {
git([url: 'https://github.com/khadythiara/test_sonarqube.git', branch: 'main'])
sh 'chmod +x ./mvnw'
}
}
stage('Building Image') {
steps {
script {
dockerImage = docker.build(imagename, ".")
}
}
}
stage('Push Image') {
steps {
script {
docker.withRegistry('', registryCredential) {
dockerImage.push("$BUILD_NUMBER")
dockerImage.push('latest')
}
}
}
}
stage('ArgoCD Deployment') {
steps {
script {
sh 'kubectl apply -f argocd_deploy.yaml --validate=false '
}
}
}
stage('Scale Deployment') {
steps {
script {
// Mise à l'échelle des réplicas à 5
sh 'kubectl scale deployment k8s-app-deployment --replicas=3'
// Vérification de la mise à l'échelle
sh 'kubectl get pods -o wide'
}
}
}
stage('Monitor Scaling') {
steps {
script {
// Affichage des métriques de ressources
sh 'kubectl top pods'
// Optionnel : vérifier l'état de la charge sur les nœuds
sh 'kubectl top nodes'
}
}
}
stage('Verify Deployment') {
steps {
script {
// Vérifier que les ressources Kubernetes sont correctement déployées
sh 'kubectl get all -n default'
sh 'kubectl get pv'
sh 'kubectl get pvc'
sh 'kubectl get hpa'
}
}
}
}
post {
always {
script {
sh 'kubectl delete pods --field-selector=status.phase=Succeeded'
}
echo 'Pipeline terminé et nettoyage effectué'
}
}
}
# 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/
apiVersion: v2
name: charts
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"
apiVersion: v1
name: charts
description: A Helm chart for Kubernetes
version: 1.0.0
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler # Assurez-vous que 'kind' est défini
metadata:
name: {{ .Values.appName }}-hpa
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ .Values.appName }}-deployment # Assurez-vous que 'appName' est correctement défini dans 'values.yaml'
minReplicas: {{ .Values.hpa.minReplicas }}
maxReplicas: {{ .Values.hpa.maxReplicas }}
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: {{ .Values.hpa.targetCPUUtilizationPercentage }}
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "charts.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "charts.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "charts.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "charts.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
{{/*
Expand the name of the chart.
*/}}
{{- define "charts.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 "charts.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 "charts.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Common labels
*/}}
{{- define "charts.labels" -}}
helm.sh/chart: {{ include "charts.chart" . }}
{{ include "charts.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "charts.selectorLabels" -}}
app.kubernetes.io/name: {{ include "charts.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{/*
Create the name of the service account to use
*/}}
{{- define "charts.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "charts.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.appName }}-deployment
labels:
app: {{ .Values.appName }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.appName }}
template:
metadata:
labels:
app: {{ .Values.appName }}
spec:
containers:
- name: {{ .Values.containerName }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
resources:
requests:
cpu: {{ .Values.resources.requests.cpu }}
memory: {{ .Values.resources.requests.memory }}
limits:
cpu: {{ .Values.resources.limits.cpu }}
memory: {{ .Values.resources.limits.memory }}
ports:
- containerPort: {{ .Values.containerPort }}
volumeMounts:
- mountPath: {{ .Values.volume.mountPath }}
name: {{ .Values.volume.name }}
volumes:
- name: {{ .Values.volume.name }}
persistentVolumeClaim:
claimName: {{ .Values.volume.persistentVolumeClaim }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Values.appName }}-ingress
spec:
ingressClassName: "{{ .Values.ingress.class }}" # Remplacez l'annotation par cette ligne
rules:
- host: {{ .Values.ingress.host }}
http:
paths:
- path: {{ .Values.ingress.path }}
pathType: {{ .Values.ingress.pathType }}
backend:
service:
name: {{ .Values.service.name }}
port:
number: {{ .Values.service.port }}
apiVersion: v1
kind: PersistentVolume
metadata:
name: {{ .Values.pv.name }}
labels:
app: {{ .Values.appName }}
spec:
capacity:
storage: {{ .Values.pv.capacity }}
accessModes:
- {{ .Values.pv.accessMode }}
persistentVolumeReclaimPolicy: {{ .Values.pv.reclaimPolicy }}
hostPath:
path: {{ .Values.pv.hostPath }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ .Values.pvc.name }}
labels:
app: {{ .Values.appName }}
spec:
accessModes:
- {{ .Values.pvc.accessMode }}
resources:
requests:
storage: {{ .Values.pvc.storage }}
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.appName }}-service
labels:
app: {{ .Values.appName }}
spec:
selector:
app: {{ .Values.appName }}
ports:
- protocol: TCP
port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
type: {{ .Values.service.type }}
apiVersion: v1
kind: Pod
metadata:
name: "{{ include "charts.fullname" . }}-test-connection"
labels:
{{- include "charts.labels" . | nindent 4 }}
annotations:
"helm.sh/hook": test
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['{{ include "charts.fullname" . }}:{{ .Values.service.port }}']
restartPolicy: Never
# Deployment configuration
appName: k8s-app
replicaCount: 2
containerName: k8s-app
image:
repository: khadydiagne/k8s_app
tag: latest
containerPort: 8086
resources:
requests:
cpu: "50m"
memory: "128Mi"
limits:
cpu: "200m"
memory: "512Mi"
volume:
name: storage-volume
mountPath: /data
persistentVolumeClaim: k8s-app-pvc
# Service configuration
service:
name: k8s-app-service # Nom du service à exposer dans l'Ingress
port: 80
targetPort: 8086
type: ClusterIP
# Ingress configuration
ingress:
class: "nginx" # Ajouter la classe ingress ici
rewriteTarget: "/"
host: k8s-app.local
path: /
pathType: Prefix
# Persistent Volume configuration
pv:
name: k8s-app-pv
capacity: 1Gi
accessMode: ReadWriteOnce
reclaimPolicy: Retain
hostPath: /mnt/data
# Persistent Volume Claim configuration
pvc:
name: k8s-app-pvc
accessMode: ReadWriteOnce
storage: 1Gi
# HPA configuration
hpa:
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 80
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment