Created
February 17, 2025 14:09
-
-
Save zs1621/9b94ade15fd4529afbbac293a3f37d4d to your computer and use it in GitHub Desktop.
deploy-nginx-write-different-log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # nginx-entrypoint.sh | |
| --- | |
| apiVersion: v1 | |
| kind: ConfigMap | |
| metadata: | |
| name: nginx-entrypoint | |
| data: | |
| entrypoint.sh: | | |
| !/bin/sh | |
| POD_NAME_SANITIZED=$(echo "$POD_NAME" | sed 's/[^a-zA-Z0-9_-]/_/g') | |
| echo "error_log /var/log/nginx/error-$POD_NAME_SANITIZED.log;" > /etc/nginx/conf.d/error-log.conf | |
| exec nginx -g "daemon off;" | |
| # nginx-config.yaml | |
| --- | |
| apiVersion: v1 | |
| kind: ConfigMap | |
| metadata: | |
| name: nginx-config | |
| data: | |
| nginx.conf: | | |
| load_module modules/ngx_http_perl_module.so; | |
| env POD_NAME; | |
| worker_processes 1; | |
| events { | |
| worker_connections 1024; | |
| } | |
| http { | |
| perl_set $pod_name 'sub { return $ENV{"POD_NAME"}; }'; | |
| access_log /var/log/nginx/access-$pod_name.log combined; | |
| error_log /var/log/nginx/error-$pod_name.log; | |
| include conf.d/*.conf; | |
| server { | |
| listen 80; | |
| server_name localhost; | |
| location / { | |
| root /usr/share/nginx/html; | |
| index index.html index.htm; | |
| } | |
| } | |
| } | |
| # storage.yaml | |
| --- | |
| apiVersion: v1 | |
| kind: PersistentVolume | |
| metadata: | |
| name: nginx-logs-pv | |
| spec: | |
| capacity: | |
| storage: 1Gi | |
| accessModes: | |
| - ReadWriteMany | |
| persistentVolumeReclaimPolicy: Retain | |
| hostPath: | |
| path: /var/log/nginx-pv | |
| type: DirectoryOrCreate | |
| --- | |
| apiVersion: v1 | |
| kind: PersistentVolumeClaim | |
| metadata: | |
| name: nginx-logs-pvc | |
| spec: | |
| accessModes: | |
| - ReadWriteMany | |
| resources: | |
| requests: | |
| storage: 1Gi | |
| volumeName: nginx-logs-pv | |
| --- | |
| # nginx-deployment.yaml | |
| apiVersion: apps/v1 | |
| kind: Deployment | |
| metadata: | |
| name: nginx-deployment | |
| spec: | |
| replicas: 2 | |
| selector: | |
| matchLabels: | |
| app: nginx | |
| template: | |
| metadata: | |
| labels: | |
| app: nginx | |
| spec: | |
| initContainers: | |
| - name: init-logs | |
| image: docker.gptd.cc/busybox:latest | |
| command: ['/bin/sh', '-c'] | |
| args: | |
| - | | |
| mkdir -p /var/log/nginx | |
| chmod -R 777 /var/log/nginx | |
| volumeMounts: | |
| - name: nginx-logs | |
| mountPath: /var/log/nginx | |
| containers: | |
| - name: nginx | |
| image: docker.gptd.cc/nginx:1.21-perl | |
| command: ["/bin/sh", "-c"] | |
| args: | |
| - | | |
| /etc/nginx/entrypoint.sh | |
| env: | |
| - name: POD_NAME | |
| valueFrom: | |
| fieldRef: | |
| fieldPath: metadata.name | |
| volumeMounts: | |
| - name: nginx-config | |
| mountPath: /etc/nginx/nginx.conf | |
| subPath: nginx.conf | |
| - name: nginx-entrypoint | |
| mountPath: /etc/nginx/entrypoint.sh | |
| subPath: entrypoint.sh | |
| - name: nginx-logs | |
| mountPath: /var/log/nginx | |
| volumes: | |
| - name: nginx-config | |
| configMap: | |
| name: nginx-config | |
| - name: nginx-entrypoint | |
| configMap: | |
| name: nginx-entrypoint | |
| defaultMode: 0777 | |
| - name: nginx-logs | |
| persistentVolumeClaim: | |
| claimName: nginx-logs-pvc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment