DevOps Buzz
Search…
Bash / Shell
Bitbucket
Distros
Elasticsearch
General
Guidelines / Standards
microk8s
Prometheus
RabbitMQ
VirtualBox
Ingress
K8s Load Balancer

NGINX Ingress

Deploy NGINX ingress

Deploy NGINX ingress:
1
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
Copied!
Deploy NGINX ingress service (example for bare-metal):
1
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml
Copied!
Check if the service was created:
1
kubectl get services --namespace=ingress-nginx
Copied!
Output example:
1
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
2
ingress-nginx NodePort 10.111.35.186 <none> 80:32022/TCP,443:31845/TCP 29m
Copied!
Note port(s) of the service. It will be used later to access your host.

Test a deployment

Let's create two different deployments to test.
Create the first deployment:
1
kubectl create -f - <<EOF
2
3
---
4
apiVersion: v1
5
kind: ConfigMap
6
metadata:
7
name: cm-first-deployment
8
data:
9
index.html: |-
10
TEST 001
11
---
12
apiVersion: apps/v1
13
kind: Deployment
14
metadata:
15
name: first-deployment
16
labels:
17
app: first-deployment
18
spec:
19
selector:
20
matchLabels:
21
app: first-deployment
22
template:
23
metadata:
24
labels:
25
app: first-deployment
26
spec:
27
28
volumes:
29
- name: v-nginx-001
30
configMap:
31
name: cm-first-deployment
32
33
containers:
34
- name: first-deployment-api
35
image: nginx
36
37
ports:
38
- containerPort: 80
39
40
volumeMounts:
41
- name: v-nginx-001
42
mountPath: /usr/share/nginx/html/index.html
43
subPath: index.html
44
45
---
46
kind: Service
47
apiVersion: v1
48
metadata:
49
name: first-service
50
spec:
51
selector:
52
app: first-deployment
53
ports:
54
- protocol: TCP
55
port: 80
56
type: NodePort
57
58
EOF
Copied!
Create the second deployment:
1
kubectl create -f - <<EOF
2
3
---
4
apiVersion: v1
5
kind: ConfigMap
6
metadata:
7
name: cm-second-deployment
8
data:
9
index.html: |-
10
TEST 002
11
---
12
apiVersion: apps/v1
13
kind: Deployment
14
metadata:
15
name: second-deployment
16
labels:
17
app: second-deployment
18
spec:
19
selector:
20
matchLabels:
21
app: second-deployment
22
template:
23
metadata:
24
labels:
25
app: second-deployment
26
spec:
27
28
volumes:
29
- name: v-nginx-001
30
configMap:
31
name: cm-second-deployment
32
33
containers:
34
- name: second-deployment-api
35
image: nginx
36
37
ports:
38
- containerPort: 80
39
40
volumeMounts:
41
- name: v-nginx-001
42
mountPath: /usr/share/nginx/html/index.html
43
subPath: index.html
44
45
---
46
kind: Service
47
apiVersion: v1
48
metadata:
49
name: second-service
50
spec:
51
selector:
52
app: second-deployment
53
ports:
54
- protocol: TCP
55
port: 80
56
type: NodePort
57
58
EOF
Copied!

Create ingress

Text:
1
kubectl create -f - <<EOF
2
3
apiVersion: extensions/v1beta1
4
kind: Ingress
5
metadata:
6
name: virtual-host
7
annotations:
8
# Target URI where the traffic must be redirected
9
nginx.ingress.kubernetes.io/rewrite-target: /
10
spec:
11
rules:
12
- host: test-001.com
13
http:
14
paths:
15
- backend:
16
serviceName: first-service
17
servicePort: 80
18
- host: test-002.com
19
http:
20
paths:
21
- backend:
22
serviceName: second-service
23
servicePort: 80
24
25
- host: test-003.com
26
http:
27
paths:
28
- path: /test-001
29
backend:
30
serviceName: first-service
31
servicePort: 80
32
- path: /test-002
33
backend:
34
serviceName: second-service
35
servicePort: 80
36
37
EOF
Copied!
Check the ingress:
1
kubectl get ingress
Copied!

Test

Get your cluster IP:
1
kubectl cluster-info
Copied!
Edit your hosts file accordingly, for example:
1
192.168.99.101 test-001.com
2
192.168.99.101 test-002.com
3
192.168.99.101 test-003.com
Copied!
Use you ingress service port.
References