-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-multi-service-wo-selector.sh
executable file
·72 lines (58 loc) · 1.57 KB
/
create-multi-service-wo-selector.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
#!/bin/sh
# This script does creates and exports a service without selector.
SVC_POD_NAME=nginx-mep-pod
SVC_NAME=nginx-mep-svc
SVC_PORT=80
TARGET_PORT=80
# Create a service without selector
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: $SVC_NAME
spec:
ports:
- protocol: TCP
port: $SVC_PORT
targetPort: $TARGET_PORT
EOF
# Start the endpoints file
cat << EOF > endpoints.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: $SVC_NAME
subsets:
- addresses:
EOF
# Get the pod IP
NUM_PODS=3
for i in $(seq 1 $NUM_PODS); do
# Start a pod running nginx
POD_INSTANCE_NAME=$SVC_POD_NAME-$i
kubectl run $POD_INSTANCE_NAME --image=nginx --restart=Never
# Get the pod IP
MAX_TRIES=10
for j in $(seq 1 $MAX_TRIES); do
SVC_POD_IP=$(kubectl get pods -n default $POD_INSTANCE_NAME -o wide | grep $POD_INSTANCE_NAME | awk '{print $6}')
if [ -z "${SVC_POD_IP}" ] || [ "${SVC_POD_IP}" == "<none>" ]; then
echo "IP address for pod $POD_INSTANCE_NAME not ready yet"
sleep 1
continue
else
echo "IP address for pod $POD_INSTANCE_NAME is $SVC_POD_IP"
break
fi
done
if [ -z "${SVC_POD_IP}" ] || [ "${SVC_POD_IP}" == "<none>" ]; then
echo "ERROR: Failed to get IP address for pod $POD_INSTANCE_NAME in $MAX_TRIES tries"
exit 1
fi
echo " - ip: $SVC_POD_IP" >> endpoints.yaml
done
# finish the endpoints file
echo " ports:" >> endpoints.yaml
echo " - port: $SVC_PORT" >> endpoints.yaml
kubectl apply -f endpoints.yaml
# Export serivice
subctl export service -n default $SVC_NAME