How can I pass the nginx.conf
configuration file to an nginx instance running inside a Kubernetes cluster?
With this service-type, Kubernetes will assign this service on ports on the 30000+ range. Run the get svc command to see a summary of the service and the ports exposed. Now you can verify that the Nginx page is reachable on all nodes using the curl command. As you can see, the “WELCOME TO NGINX!” page can be reached.
The NGINX Ingress Controller for Kubernetes combines the benefits of using the Kubernetes control plane to manage load‑balancing configuration with the performance, reliability, and advanced features of NGINX and NGINX Plus.
To create a pod using the nginx image, run the command kubectl run nginx --image=nginx --restart=Never . This will create a pod named nginx, running with the nginx image on Docker Hub. And by setting the flag --restart=Never we tell Kubernetes to create a single pod rather than a Deployment.
You can create a ConfigMap object and then mount the values as files where you need them:
apiVersion: v1 kind: ConfigMap metadata: name: nginx-config data: nginx.conf: | your config comes here like this other.conf: | second file contents
And in you pod spec:
spec: containers: - name: nginx image: nginx volumeMounts: - name: nginx-config mountPath: /etc/nginx/nginx.conf subPath: nginx.conf - name: other.conf mountPath: /etc/nginx/other.conf subPath: other.conf volumes: - name: nginx-config configMap: name: nginx-config
(Take note of the duplication of the filename in mountPath and using the exact same subPath; same as bind mounting files.)
For more information about ConfigMap see: https://kubernetes.io/docs/user-guide/configmap/
Note: A container using a ConfigMap as a subPath volume will not receive ConfigMap updates.
I haven't found a good way to escape the nginx configuration contents in the ConfigMap. The best recourse for me was to use ConfigMap creation using files
Save the following as ./data/nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; 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; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
Now create the configMap kubectl create configmap confnginx --from-file=./data/nginx.conf
Save the following deployment and pod yaml as nginx.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: nginx labels: app: nginx spec: selector: matchLabels: app: nginx replicas: 1 # tells deployment to run 2 pods matching the template template: # create pods using pod definition in this template metadata: # unlike pod-nginx.yaml, the name is not included in the meta data as a unique name is # generated from the deployment name labels: app: nginx spec: containers: - name: nginx image: nginx:alpine ports: - containerPort: 80 volumeMounts: - name: nginx-config mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumes: - name: nginx-config configMap: name: confnginx
Now create it in k8 kubectl apply -f nginx.yaml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With