diff --git a/nginx-webapp/.helmignore b/nginx-webapp/.helmignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/nginx-webapp/Chart.yaml b/nginx-webapp/Chart.yaml new file mode 100644 index 0000000000..08e1886ebf --- /dev/null +++ b/nginx-webapp/Chart.yaml @@ -0,0 +1,17 @@ +apiVersion: v2 +name: nginx-webapp +description: A Helm chart for deploying a simple nginx web application +type: application +version: 0.1.0 +appVersion: "1.25.3" +keywords: + - nginx + - web + - frontend +home: https://github.com/your-org/nginx-webapp +sources: + - https://github.com/your-org/nginx-webapp +maintainers: + - name: Your Name + email: your.email@company.com +dependencies: [] \ No newline at end of file diff --git a/nginx-webapp/templates/NOTES.txt b/nginx-webapp/templates/NOTES.txt new file mode 100644 index 0000000000..ca50ae6a70 --- /dev/null +++ b/nginx-webapp/templates/NOTES.txt @@ -0,0 +1,25 @@ +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 "nginx-webapp.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 the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "nginx-webapp.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "nginx-webapp.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 "nginx-webapp.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 }} + +2. Check the health endpoint: + curl http://your-app-url/health \ No newline at end of file diff --git a/nginx-webapp/templates/_helpers.tpl b/nginx-webapp/templates/_helpers.tpl new file mode 100644 index 0000000000..1f67e3ac9f --- /dev/null +++ b/nginx-webapp/templates/_helpers.tpl @@ -0,0 +1,60 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "nginx-webapp.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +*/}} +{{- define "nginx-webapp.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 "nginx-webapp.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "nginx-webapp.labels" -}} +helm.sh/chart: {{ include "nginx-webapp.chart" . }} +{{ include "nginx-webapp.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "nginx-webapp.selectorLabels" -}} +app.kubernetes.io/name: {{ include "nginx-webapp.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "nginx-webapp.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "nginx-webapp.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/nginx-webapp/templates/configmap.yaml b/nginx-webapp/templates/configmap.yaml new file mode 100644 index 0000000000..1752998a5d --- /dev/null +++ b/nginx-webapp/templates/configmap.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "nginx-webapp.fullname" . }}-config + labels: + {{- include "nginx-webapp.labels" . | nindent 4 }} +data: + nginx.conf: | + {{- .Values.nginx.config | nindent 4 }} + index.html: | + {{- .Values.nginx.html | nindent 4 }} \ No newline at end of file diff --git a/nginx-webapp/templates/deployment.yaml b/nginx-webapp/templates/deployment.yaml new file mode 100644 index 0000000000..dd76528dec --- /dev/null +++ b/nginx-webapp/templates/deployment.yaml @@ -0,0 +1,90 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "nginx-webapp.fullname" . }} + labels: + {{- include "nginx-webapp.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "nginx-webapp.selectorLabels" . | nindent 6 }} + template: + metadata: + annotations: + checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }} + {{- with .Values.podAnnotations }} + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "nginx-webapp.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "nginx-webapp.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 8080 + protocol: TCP + livenessProbe: + {{- toYaml .Values.livenessProbe | nindent 12 }} + readinessProbe: + {{- toYaml .Values.readinessProbe | nindent 12 }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: nginx-config + mountPath: /etc/nginx/nginx.conf + subPath: nginx.conf + readOnly: true + - name: nginx-html + mountPath: /usr/share/nginx/html/index.html + subPath: index.html + readOnly: true + - name: nginx-cache + mountPath: /var/cache/nginx + - name: nginx-run + mountPath: /var/run + - name: nginx-logs + mountPath: /var/log/nginx + - name: tmp-volume + mountPath: /tmp + volumes: + - name: nginx-config + configMap: + name: {{ include "nginx-webapp.fullname" . }}-config + - name: nginx-html + configMap: + name: {{ include "nginx-webapp.fullname" . }}-config + - name: nginx-cache + emptyDir: {} + - name: nginx-run + emptyDir: {} + - name: nginx-logs + emptyDir: {} + - name: tmp-volume + emptyDir: {} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} \ No newline at end of file diff --git a/nginx-webapp/templates/hpa.yaml b/nginx-webapp/templates/hpa.yaml new file mode 100644 index 0000000000..f3493a2db3 --- /dev/null +++ b/nginx-webapp/templates/hpa.yaml @@ -0,0 +1,24 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "nginx-webapp.fullname" . }} + labels: + {{- include "nginx-webapp.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "nginx-webapp.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/nginx-webapp/templates/ingress.yaml b/nginx-webapp/templates/ingress.yaml new file mode 100644 index 0000000000..7791fd4079 --- /dev/null +++ b/nginx-webapp/templates/ingress.yaml @@ -0,0 +1,41 @@ +{{- if .Values.ingress.enabled -}} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ include "nginx-webapp.fullname" . }} + labels: + {{- include "nginx-webapp.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if .Values.ingress.className }} + 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 }} + pathType: {{ .pathType }} + backend: + service: + name: {{ include "nginx-webapp.fullname" $ }} + port: + number: {{ $.Values.service.port }} + {{- end }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/nginx-webapp/templates/service.yaml b/nginx-webapp/templates/service.yaml new file mode 100644 index 0000000000..7c3eb85c5f --- /dev/null +++ b/nginx-webapp/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "nginx-webapp.fullname" . }} + labels: + {{- include "nginx-webapp.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: {{ .Values.service.targetPort }} + protocol: TCP + name: http + selector: + {{- include "nginx-webapp.selectorLabels" . | nindent 4 }} \ No newline at end of file diff --git a/nginx-webapp/templates/serviceaccount.yaml b/nginx-webapp/templates/serviceaccount.yaml new file mode 100644 index 0000000000..f2bf5d7534 --- /dev/null +++ b/nginx-webapp/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "nginx-webapp.serviceAccountName" . }} + labels: + {{- include "nginx-webapp.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/nginx-webapp/values.yaml b/nginx-webapp/values.yaml new file mode 100644 index 0000000000..b790e2f9c0 --- /dev/null +++ b/nginx-webapp/values.yaml @@ -0,0 +1,179 @@ +# Default values for nginx-webapp +replicaCount: 2 + +image: + repository: nginx + pullPolicy: IfNotPresent + tag: "1.25.3-alpine" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + create: true + annotations: {} + name: "" + +podAnnotations: {} + +podSecurityContext: + fsGroup: 101 + runAsNonRoot: true + runAsUser: 101 + +securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 101 + +service: + type: ClusterIP + port: 80 + targetPort: 8080 + +ingress: + enabled: false + className: "" + annotations: {} + hosts: + - host: webapp.example.com + paths: + - path: / + pathType: Prefix + tls: [] + +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 50m + memory: 64Mi + +autoscaling: + enabled: false + minReplicas: 2 + maxReplicas: 10 + targetCPUUtilizationPercentage: 80 + +nodeSelector: {} +tolerations: [] +affinity: {} + +# Nginx configuration +nginx: + config: | + user nginx; + worker_processes auto; + error_log /var/log/nginx/error.log notice; + pid /var/run/nginx.pid; + + + events { + worker_connections 1024; + } + + http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + server { + listen 8080; + server_name _; + root /usr/share/nginx/html; + index index.html index.htm; + + location / { + try_files $uri $uri/ =404; + } + + location /health { + access_log off; + return 200 "healthy\n"; + add_header Content-Type text/plain; + } + } + } + + # Custom HTML content + html: | + + +
+Server: Nginx
+Deployment: Kubernetes with Helm
+Status: Running
+This is a simple web application deployed using Helm chart with nginx.
+Health check endpoint: /health
+