Skip to content

Commit a7bb68d

Browse files
Jeffrey KwongJeffrey Kwong
Jeffrey Kwong
authored and
Jeffrey Kwong
committed
daemonset that creates static routes on worker nodes in bluemix kubernetes
1 parent 350a174 commit a7bb68d

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM alpine:latest
2+
3+
WORKDIR /root
4+
5+
RUN apk add --no-cache openssh-client bash git bc jq && \
6+
git clone https://github.com/JoeKuan/Network-Interfaces-Script.git && \
7+
apk del --no-cache git
8+
9+
ADD add-static-route.sh /root
10+
RUN chmod a+x /root/add-static-route.sh
11+
12+
CMD [ "/root/add-static-route.sh" ]

add-static-route.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# create my ssh key
4+
if [ ! -f /root/ssh-key ]; then
5+
ssh-keygen -t rsa -N '' -f /root/ssh-key
6+
fi
7+
8+
# add it to authorized_keys
9+
cat /root/ssh-key.pub >> /host/root/.ssh/authorized_keys
10+
11+
# discover my host IP
12+
myHostIP=`awk -f /root/Network-Interfaces-Script/readInterfaces.awk /host/interfaces device=eth0 | awk '{print $1;}'`
13+
echo "i am running on ${myHostIP}"
14+
15+
myNetmask=`awk -f /root/Network-Interfaces-Script/readInterfaces.awk /host/interfaces device=eth0 | awk '{print $2;}'`
16+
17+
# reset host fingerprint
18+
if [ -f /root/.ssh/known_hosts ]; then
19+
ssh-keygen -R ${myHostIP}
20+
fi
21+
22+
mkdir -p /root/.ssh
23+
touch /root/.ssh/known_hosts
24+
ssh-keyscan -t rsa ${myHostIP} 1>> /root/.ssh/known_hosts
25+
26+
# get my gateway, which is my network address + 1
27+
myNetworkAddr=`ipcalc -n ${myHostIP} ${myNetmask} | cut -d= -f2`
28+
29+
#convert to an int
30+
_oct1=`echo ${myNetworkAddr} | cut -d. -f1 | xargs -I{} echo "{} * 256 * 256 * 256" | bc`
31+
_oct2=`echo ${myNetworkAddr} | cut -d. -f2 | xargs -I{} echo "{} * 256 * 256" | bc`
32+
_oct3=`echo ${myNetworkAddr} | cut -d. -f3 | xargs -I{} echo "{} * 256" | bc`
33+
_oct4=`echo ${myNetworkAddr} | cut -d. -f4 | xargs -I{} echo "{}" | bc`
34+
_gatewayOct=`echo "${_oct1} + ${_oct2} + ${_oct3} + ${_oct4} + 1" | bc`
35+
36+
_oct1=`echo ${_gatewayOct} | xargs -I{} echo "{} / 256 / 256 / 256 % 256" | bc`
37+
_oct2=`echo ${_gatewayOct} | xargs -I{} echo "{} / 256 / 256 % 256" | bc`
38+
_oct3=`echo ${_gatewayOct} | xargs -I{} echo "{} / 256 % 256" | bc`
39+
_oct4=`echo ${_gatewayOct} | xargs -I{} echo "{} % 256" | bc`
40+
_gatewayAddr=`echo "${_oct1}.${_oct2}.${_oct3}.${_oct4}"`
41+
42+
myRoutes=`cat /var/run/configmaps/static-routes/static-routes.json | jq '.routes | join (" ")' | sed -e 's/\"//g'`
43+
44+
while [ 1 -eq 1 ]; do
45+
# SSH to it and add my routes
46+
for _route in ${myRoutes}; do
47+
echo "Adding route ${_route} via ${_gatewayAddr}"
48+
ssh -i /root/ssh-key root@${myHostIP} "ip route add ${_route} via ${_gatewayAddr}"
49+
done
50+
sleep 30
51+
done
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apiVersion: extensions/v1beta1
2+
kind: DaemonSet
3+
metadata:
4+
name: add-static-route
5+
labels:
6+
app: test
7+
spec:
8+
template:
9+
metadata:
10+
labels:
11+
app: test
12+
spec:
13+
containers:
14+
- name: add-static-route
15+
image: jkwong/add-static-route
16+
volumeMounts:
17+
- mountPath: "/host/root"
18+
name: host-root
19+
readOnly: false
20+
- mountPath: "/host/interfaces"
21+
name: host-interfaces
22+
readOnly: false
23+
- mountPath: "/var/run/configmaps/static-routes"
24+
name: static-routes
25+
readOnly: true
26+
volumes:
27+
- name: host-root
28+
hostPath:
29+
path: /root
30+
- name: host-interfaces
31+
hostPath:
32+
path: /etc/network/interfaces
33+
- name: static-routes
34+
configMap:
35+
name: static-routes
36+
items:
37+
- key: static-route.file
38+
path: static-routes.json
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
kind: ConfigMap
2+
apiVersion: v1
3+
metadata:
4+
creationTimestamp: 2016-02-18T19:14:38Z
5+
name: static-routes
6+
namespace: default
7+
data:
8+
static-route.file: |-
9+
{
10+
"routes": [
11+
"192.168.0.0/24",
12+
"192.168.1.0/24"
13+
]
14+
}

0 commit comments

Comments
 (0)