Blame

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