Blame

6c6328 Ralph Thesen 2023-09-07 21:29:24 1
# Howto: NGINX-public_html
2
3
1. create a `~/public_html/domain.tld` on `gate0.t3m4.net`
4
2. `sudo` or ping <mail@redimp.de> to add the folder to `/etc/exports` and run `exportfs -a`
5
3. create a `domain-tld.yaml` definition for a deployment, a service and an ingress and `kubectl apply -f domain-tld.yaml` it.
6
c475e2 Ralph Thesen 2025-07-26 08:52:20 7
> [!WARNING]
8
> Please consider all files in the exported folders as public, since any k8s pod can mount it via nfs. name the directories accordingly, e.g. `public_html` or `share`.
6c6328 Ralph Thesen 2023-09-07 21:29:24 9
e51d54 Ralph Thesen 2023-09-30 13:10:00 10
## One nginx for multiple domains: k8s deployment, services and ingress
11
12
An example for an nginx deployment with multiple virtualhosts can be found below. Make sure to replace all the
13
14
- domain1.tld
15
- doman2.tld
16
- username
17
18
```yaml
19
---
20
apiVersion: v1
21
kind: ConfigMap
22
metadata:
23
name: nginx-conf
24
namespace: username-vserver
25
data:
26
virtualhost.conf: |
27
server {
28
listen 80;
29
server_name domain1.tld www.domain1.tld;
30
root /var/www/html/domain1.tld;
31
index index.html;
32
location / {
33
try_files $uri $uri/ =404;
34
}
35
}
36
37
server {
38
listen 80;
39
server_name domain2.tld www.domain2.tld;
40
root /var/www/html/domain2.tld;
41
index index.html;
42
location / {
43
try_files $uri $uri/ =404;
44
}
45
}
46
---
47
apiVersion: apps/v1
48
kind: Deployment
49
metadata:
50
name: nginx-static
51
namespace: username-vserver
52
spec:
53
selector:
54
matchLabels:
55
app: nginx-static
56
replicas: 1
57
template:
58
metadata:
59
labels:
60
app: nginx-static
61
spec:
62
containers:
63
- name: nginx
64
image: nginx:1.14.2
65
command:
66
- /bin/sh
67
- -c
68
- |
69
sed -i '/^nginx/d' /etc/passwd
70
sed -i '/^nginx/d' /etc/group
71
echo "nginx:x:101:101:nginx user,,,:/nonexistent:/bin/false" >> /etc/passwd
72
echo "nginx:x:101:" >> /etc/group
73
nginx -g 'daemon off;'
74
ports:
75
- containerPort: 80
76
volumeMounts:
77
- name: nginx-conf
78
subPath: virtualhost.conf
79
mountPath: /etc/nginx/conf.d/virtualhost.conf
80
- name: nfs-public-html
81
mountPath: /var/www/html
82
volumes:
83
- name: nginx-conf
84
configMap:
85
name: nginx-conf
86
items:
87
- key: virtualhost.conf
88
path: virtualhost.conf
89
- name: nfs-public-html
90
nfs:
91
server: 10.1.0.2
92
path: /home/username/public_html
93
---
94
apiVersion: v1
95
kind: Service
96
metadata:
97
name: nginx-static
98
namespace: username-vserver
99
spec:
100
ports:
101
- port: 80
102
targetPort: 80
103
protocol: TCP
104
selector:
105
app: nginx-static
106
---
107
apiVersion: networking.k8s.io/v1
108
kind: Ingress
109
metadata:
110
name: nginx-static
111
namespace: username-vserver
112
annotations:
113
cert-manager.io/cluster-issuer: "letsencrypt-production"
114
nginx.ingress.kubernetes.io/ssl-redirect: "true"
115
nginx.ingress.kubernetes.io/server-alias: "www.domain1.tld,www.domain2.tld,domain2.tld"
116
spec:
117
ingressClassName: "nginx"
118
tls:
119
- hosts:
120
- domain1.tld
121
- www.domain1.tld
122
- domain2.tld
123
- www.domain2.tld
124
secretName: nginx-static-tls
125
rules:
126
- host: domain1.tld
127
http:
128
paths:
129
- path: /
130
pathType: Prefix
131
backend:
132
service:
133
name: nginx-static
134
port:
135
number: 80
136
- host: domain2.tld
137
http:
138
paths:
139
- path: /
140
pathType: Prefix
141
backend:
142
service:
143
name: nginx-static
144
port:
145
number: 80
146
147
```
148
149
## One nginx per domain: k8s deployment, service and ingress
6c6328 Ralph Thesen 2023-09-07 21:29:24 150
151
An example for an nginx deployment can be found below. Make sure to replace all the
152
153
- domain-tld
154
- domain.tld
155
- username
156
157
entries.
158
159
```yaml
160
---
161
apiVersion: apps/v1
162
kind: Deployment
163
metadata:
164
name: nginx-domain-tld
165
namespace: username-vserver
166
spec:
167
selector:
168
matchLabels:
169
app: nginx-domain-tld
170
replicas: 1
171
template:
172
metadata:
173
labels:
174
app: nginx-domain-tld
175
spec:
176
containers:
177
- name: nginx
178
image: nginx:1.14.2
179
command:
180
- /bin/sh
181
- -c
182
- |
183
sed -i '/^nginx/d' /etc/passwd
184
sed -i '/^nginx/d' /etc/group
185
echo "nginx:x:101:101:nginx user,,,:/nonexistent:/bin/false" >> /etc/passwd
186
echo "nginx:x:101:" >> /etc/group
187
nginx -g 'daemon off;'
188
ports:
189
- containerPort: 80
190
volumeMounts:
191
- name: nfs-public-html
192
mountPath: /usr/share/nginx/html
193
volumes:
194
- name: nfs-public-html
195
nfs:
196
server: 10.1.0.2
197
path: /home/username/public_html/domain.tld
198
---
199
apiVersion: v1
200
kind: Service
201
metadata:
202
name: nginx-domain-tld
a7b404 Ralph Thesen 2023-09-30 12:40:16 203
namespace: username-vserver
6c6328 Ralph Thesen 2023-09-07 21:29:24 204
spec:
205
ports:
206
- port: 80
207
targetPort: 80
208
protocol: TCP
209
selector:
210
app: nginx-domain-tld
211
---
212
apiVersion: networking.k8s.io/v1
213
kind: Ingress
214
metadata:
215
name: nginx-domain-tld
a7b404 Ralph Thesen 2023-09-30 12:40:16 216
namespace: username-vserver
6c6328 Ralph Thesen 2023-09-07 21:29:24 217
annotations:
218
cert-manager.io/cluster-issuer: "letsencrypt-production"
219
nginx.ingress.kubernetes.io/ssl-redirect: "true"
220
nginx.ingress.kubernetes.io/server-alias: "www.domain.tld"
221
spec:
222
ingressClassName: "nginx"
223
tls:
224
- hosts:
225
- domain.tld
226
- www.domain.tld
227
secretName: domain-tld-tls
228
rules:
229
- host: domain.tld
230
http:
231
paths:
232
- path: /
233
pathType: Prefix
234
backend:
235
service:
236
name: nginx-domain-tld
237
port:
238
number: 80
239
240
```