Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Created March 11, 2018 11:04
Show Gist options
  • Save petitviolet/d36f33d145d0bbf4b54eb187b79d0244 to your computer and use it in GitHub Desktop.
Save petitviolet/d36f33d145d0bbf4b54eb187b79d0244 to your computer and use it in GitHub Desktop.
sample Nginx configuration on Kubernetes using ConfigMap to configure nginx.
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf: |
user nginx;
worker_processes 3;
error_log /var/log/nginx/error.log;
events {
worker_connections 10240;
}
http {
log_format main
'remote_addr:$remote_addr\t'
'time_local:$time_local\t'
'method:$request_method\t'
'uri:$request_uri\t'
'host:$host\t'
'status:$status\t'
'bytes_sent:$body_bytes_sent\t'
'referer:$http_referer\t'
'useragent:$http_user_agent\t'
'forwardedfor:$http_x_forwarded_for\t'
'request_time:$request_time';
access_log /var/log/nginx/access.log main;
server {
listen 80;
server_name _;
location / {
root html;
index index.html index.htm;
}
}
include /etc/nginx/virtualhost/virtualhost.conf;
}
virtualhost.conf: |
upstream app {
server localhost:8080;
keepalive 1024;
}
server {
listen 80 default_server;
root /usr/local/app;
access_log /var/log/nginx/app.access_log main;
error_log /var/log/nginx/app.error_log;
location / {
proxy_pass http://app/;
proxy_http_version 1.1;
}
}
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx # mount nginx-conf volumn to /etc/nginx
readOnly: true
name: nginx-conf
- mountPath: /var/log/nginx
name: log
volumes:
- name: nginx-conf
configMap:
name: nginx-conf # place ConfigMap `nginx-conf` on /etc/nginx
items:
- key: nginx.conf
path: nginx.conf
- key: virtualhost.conf
path: virtualhost/virtualhost.conf # dig directory
- name: log
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
selector:
app: nginx
@Nj-kol
Copy link

Nj-kol commented Feb 28, 2020

@vacar Thanks for your reply, I was able to resolve the issue. The problem was not actually the mount, there was some config issue in nginx.conf which prevented nginx from starting up. Because by default, nginx write log under /var/log/nginx and not standard out, kubectl logs did not produce any output, hence rendering it very difficult to debug. Once I change nginx.conf to debug to standard out, things became clear

@therzv
Copy link

therzv commented Jul 29, 2020

i have some issue, all files under /etc/nginx is erased

@gmichaeljaison
Copy link

i have some issue, all files under /etc/nginx is erased

Only mount specific file instead of entire directory.

          volumeMounts:
            - name: nginx-conf-volume
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
              readOnly: true

@sravan-kumard
Copy link

sravan-kumard commented Oct 12, 2020

apiVersion: apps/v1beta1 deployment version should be changed to apiVersion: apps/v1

@rajibdas909
Copy link

I need help to optimize the Nginx

  1. What should be the worker_Processes configuration? I thought it should be a number per core. So for example, if we are giving the 1000m CPU ( request ) to the POD, what should be worker_processes in this case - I think 1?
  2. Similarly what should be the memory request I should give that to Pod?
  3. What should be a number of worker_connections?

Thanks

@pbkh-kimheang
Copy link

Can we provide the server configuration wihtout having the config map?
I don't want to use config map, I want to apply the setting configuration in the deployment directly. Is it possible?

@MuhammadMunir12
Copy link

Hello,

I have created config map to mount nginx.conf inside the nginx contianer.
But I am seeing the nginx default page inside pod not the one I have mounted via configmap.
PS: I am trying this on OpenShift 4.6 cluster.

My files are:

Config map

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  namespace: nginx-ingress
data:
  nginx.conf: |-
    user nginx;
    worker_processes  10;
    events {
      worker_connections  10240;
    }
    http {
      server {
          listen       80;
          server_name  localhost;
          location / {
            root   /usr/share/nginx/html; #Change this line
            index  index.html index.htm;
        }
      }
    }

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: nginx-ingress
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        compute1: worker1
      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf
          items:
            - key: nginx.conf
              path: nginx.conf
      containers:
      - name: nginx-alpine-perl
        image: docker.io/library/nginx@sha256:51212c2cc0070084b2061106d5711df55e8aedfc6091c6f96fabeff3e083f355
        ports:
        - containerPort: 80
        securityContext:
          allowPrivilegeEscalation: false
          #runAsUser: 0
        volumeMounts:
            - name: nginx-conf
              mountPath: /etc/nginx
              #subPath: nginx.conf
              readOnly: true

I tried it with specific path as well like: /etc/nginx/nginx.conf but it doesn't create container.

Any help would be appreciated?

@Majid-cloud
Copy link

Do i need to install nginx (using yum etc) or configmap will create nginx files automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment