How to deploy nginx.config file in kubernetes

10/16/2019

Recently I started to study Kubernetes and right now I can deploy nginx with default options. But how I can deploy my nginx.conf in Kubernetes ? Maybe somebody have a simple example ?

-- noute
docker
kubernetes
linux

2 Answers

10/16/2019

Create yaml for nginx deployment:

kubectl run --image=nginx nginx -oyaml --dry-run
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

Create config ConfigMap with nginx configurtion

kubectl create configmap nginx-conf --from-file=./nginx.conf

Mount file:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: nginx-conf
          subPath: nginx.conf
      volumes:
      - configMap:
          name: nginx-conf
        name: nginx-conf
-- FL3SH
Source: StackOverflow

10/16/2019

You can build your own image on top of nginx default image, then copy your own nginx.conf into that image. Once the image is created, you can push it to Dockerhub/your own private repository, and then use that image instead of the default nginx one in kubernetes.

This answer covers the process of creating a custom nginx image.

Once the image can be pulled into your kubernetes cluster you can deploy it to your cluster like so -
kubectl run nginx --image=<your-docker-hub-username>/<customr-nginx>

-- Yaron Idan
Source: StackOverflow