-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoproxy.sh
executable file
·92 lines (83 loc) · 2.04 KB
/
oproxy.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
#!/bin/bash
if [[ $1 != '' ]]; then
docker rm -f kubeproxy
exit 0
fi
PORTMAP=""
STARTPORT=${STARTPORT:-9090}
CONFIG=""
NEXTPORT=""
MSG=""
function getNextPort() {
PORT=$1
while
ret=$(lsof -i :"${PORT}")
if [[ -z ${ret} ]]; then
NEXTPORT=${PORT}
break
else
PORT=$((PORT+1))
fi
do :; done
}
function getLBServices() {
CLUSTERNAME=$1 #cluster name
lbservices=$(kubectl --context="kind-${CLUSTERNAME}" get services -A -o \
jsonpath='{range .items[?(@.spec.type == "LoadBalancer")]}{.metadata.name}{","}{.status.loadBalancer.ingress[0].ip}{","}{.spec.ports[*].port}{"\n"}{end}')
# get lbservice list
IFS=$'\n' lbservices=($(echo "${lbservices}"))
for lbs in "${lbservices[@]}"; do
# split each service to servie name in index 1, external ip in index 2, and ports in index 3
IFS=$',' sv=($(echo "${lbs}"))
IFS=$' ' svports=($(echo "${sv[2]}"))
for port in "${svports[@]}"; do
getNextPort "${STARTPORT}"
PORTMAP="${PORTMAP} -p ${NEXTPORT}:${NEXTPORT}"
# Generate the config content
CONFIG=$(cat << EOF
${CONFIG}
server {
listen ${NEXTPORT};
proxy_pass ${sv[1]}:${port};
}
EOF
)
MSG=$(cat << EOF
${MSG}
${sv[0]}@${CLUSTERNAME} ${sv[1]}:${port} ==> ${NEXTPORT}
EOF
)
STARTPORT=$((NEXTPORT+1))
done
done
}
function doClusters() {
allnames=$(kind get clusters)
allclusters=($(echo ${allnames}))
for cluster in "${allclusters[@]}"; do
echo "Processing cluster ${cluster}..."
getLBServices "${cluster}"
done
}
# Remove the running docker if there is one
docker rm -f kubeproxy &> /dev/null || true
# Generate nginx configuration and messages
doClusters
# Create the the nginx configuration
request_uri='$request_uri'
cat <<EOF > /tmp/kubeproxy.conf
worker_processes 5;
events {
worker_connections 4096;
}
stream {
${CONFIG}
}
EOF
# Now start up the proxy container
#
echo ""
thecmd=$(echo docker run --name kubeproxy -d --network=kind ${PORTMAP} \
-v /tmp/kubeproxy.conf:/etc/nginx/nginx.conf:ro nginx:latest)
eval ${thecmd}
echo "${MSG}"