-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalidate-ingress-lb.sh
executable file
·143 lines (132 loc) · 2.97 KB
/
validate-ingress-lb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
set -o errexit
# pull docker image
docker pull hashicorp/http-echo:0.2.3
# tag the image for local registry
docker tag hashicorp/http-echo:0.2.3 localhost:5001/http-echo:0.2.3
# push the docker image
docker push localhost:5001/http-echo:0.2.3
# create foo-app, bar-app pods
kubectl apply -f - <<EOF
kind: Pod
apiVersion: v1
metadata:
name: foo-app
labels:
name: foo-app
app: http-echo
spec:
containers:
- name: foo-app
image: localhost:5001/http-echo:0.2.3
args:
- "-text=foo"
---
kind: Pod
apiVersion: v1
metadata:
name: bar-app
labels:
name: bar-app
app: http-echo
spec:
containers:
- name: bar-app
image: localhost:5001/http-echo:0.2.3
args:
- "-text=bar"
---
kind: Service
apiVersion: v1
metadata:
name: foo-service
spec:
selector:
name: foo-app
ports:
# Default port used by the image
- port: 5678
---
kind: Service
apiVersion: v1
metadata:
name: bar-service
spec:
selector:
name: bar-app
ports:
# Default port used by the image
- port: 5678
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- http:
paths:
- pathType: Prefix
path: /foo(/|$)(.*)
backend:
service:
name: foo-service
port:
number: 5678
- pathType: Prefix
path: /bar(/|$)(.*)
backend:
service:
name: bar-service
port:
number: 5678
---
kind: Service
apiVersion: v1
metadata:
name: foo-service-lb
spec:
type: LoadBalancer
selector:
name: foo-app
app: http-echo
ports:
# Default port used by the image
- port: 5678
---
kind: Service
apiVersion: v1
metadata:
name: bar-service-lb
spec:
type: LoadBalancer
selector:
name: bar-app
app: http-echo
ports:
# Default port used by the image
- port: 5678
EOF
# wait for the controllers to take effect
sleep 10
# validate the services
# validate cluster ips via Ingress object
# should output "foo-app"
echo "validating foo-app via Ingress object"
curl localhost/foo/hostname
# should output "bar-app"
echo "validating bar-app via Ingress object"
curl localhost/bar/hostname
# validate load balancer service
echo "validating loadbalancer, note on macOS and Windows, docker does not expose the docker network to the host. Because of this limitation, containers (including kind nodes) are only reachable from the host via port-forwards, however other containers/pods can reach other things running in docker including loadbalancers"
FOO_LB_IP=$(kubectl get svc/foo-service-lb -n default -o=jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "FOO_LB_IP: ${FOO_LB_IP}"
BAR_LB_IP=$(kubectl get svc/bar-service-lb -n default -o=jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "BAR_LB_IP: ${BAR_LB_IP}"
# should output foo and bar on separate lines
for _ in {1..10}; do
curl ${FOO_LB_IP}:5678
curl ${BAR_LB_IP}:5678
done