Create nginx deployment with custom website

Cluster information:

Client Version: version.Info{Major:“1”, Minor:“27”, GitVersion:“v1.27.1”, GitCommit:“4c9411232e10168d7b050c49a1b59f6df9d7ea4b”, GitTreeState:“clean”, BuildDate:“2023-04-14T13:21:19Z”, GoVersion:“go1.20.3”, Compiler:“gc”, Platform:“linux/amd64”}
Kustomize Version: v5.0.1
Server Version: version.Info{Major:“1”, Minor:“26”, GitVersion:“v1.26.1”, GitCommit:“8f94681cd294aa8cfd3407b8191f6c70214973a4”, GitTreeState:“clean”, BuildDate:“2023-01-18T15:51:25Z”, GoVersion:“go1.19.5”, Compiler:“gc”, Platform:“linux/amd64”}

Running Ubuntu Server 22.04.2 - No Docker installed on any nodes.

Hi all, I’m trying to fulfill a rather easy sounding task:
Create a nginx pod with a customized index.html page. Basically it’s a (very simple) HTML document that just say “Hello to machine 1”
This files should be placed in /var/www/htdocs and the nginx config needs to point to this location.
While you can manipulate the nginx config via “args” inside your yaml file, I’ve no idea how to “create” (if not present) the /var/www/htdocs folder + put the indexd.html file into it.
As this is a total static website, there’s no need for any ext. volumes.

So how can I write a YAML that deploys a nginx pod with my custom HTML page inside ?

Hi, I’m gonna reply to my own question:
Yes, it can be done, through config maps.

First create a config-map similar to this:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-m1
  namespace: test
data:
  index.html: |
    <HTML>
    <HEAD><TITLE>K8s nginx Machine 1</TITLE></HEAD>
    <BODY>
    <H1>Welcome to machine 1</H1>
    </BODY></HTML

Now create your deploy.yaml and link it to the config-map:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-t-m1
  namespace: test
  labels: 
    name: nginx-t-m1
spec:
  selector:
    matchLabels:
      name: nginx-t-m1
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        name: nginx-t-m1
    spec:
      containers:
        - name: nginx-t-m1
          image: nginx
          ports:
          volumeMounts:
          - name: config-volume1
            mountPath: /usr/share/nginx/html
      volumes:
        - name: config-volume1
          configMap:
            # Provide the name of the ConfigMap containing the files you want
            # to add to the container
            name: nginx-m1

Pay extra attention on the Metadata “name: nginx-m1”. That need to fit the name tag in your deploy.yaml

This is of course just an example of a very small HTML page. Mind that the max size of a config-map may not exceed 1MB. So if you plan to deploy “larger” pages you’ll need volumes with your pages configured/mounted.