Skip to content
This repository was archived by the owner on Dec 13, 2020. It is now read-only.

Commit d7e78fe

Browse files
committed
Added example files
Signed-off-by: Kevin Carter <[email protected]>
0 parents  commit d7e78fe

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The repository was created to show how to run services from within
2+
a network namespace. The files within this repository are within
3+
the directories that they would need to be in on a local system
4+
which has been done to highlight the file structure and to provide
5+
context.
6+
7+
This example repository is setup to run haproxy from within a
8+
namespace however nothing in the systemd service units is bound to
9+
only haproxy.
10+
11+
See https://cloudnull.io/running-services-in-network-name-spaces-with-systemd/
12+
for more details on how these files are used.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Unit]
2+
3+
JoinsNamespaceOf = [email protected]
4+
5+
Documentation = file:/var/run/netns/haproxy
6+
7+
[Service]
8+
CPUAccounting = true
9+
BlockIOAccounting = true
10+
MemoryAccounting = true
11+
TasksAccounting = true
12+
PrivateNetwork = true
13+
PrivateTmp = true
14+
Slice = haproxy.slice

etc/systemd/system/[email protected]

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[Unit]
2+
Description=Named network namespace %I
3+
Documentation=https://github.com/openstack/ansible-role-systemd_service
4+
After=syslog.target network.target systemd-netns@%i.service
5+
Before=%i.service
6+
BindsTo=systemd-netns@%i.service
7+
8+
9+
[Service]
10+
Type=oneshot
11+
RemainAfterExit=true
12+
13+
# Create system process
14+
ExecStartPre=-/usr/bin/env ip link add mv-int link ${GATEWAY_DEVICE} type macvlan mode bridge
15+
ExecStartPre=-/usr/bin/env ip link set mv-int up
16+
ExecStartPre=-/usr/bin/env sysctl -w net.ipv4.ip_forward=1
17+
18+
# Pivot link
19+
ExecStart=/usr/bin/env ip link add mv0 link mv-int type macvlan mode bridge
20+
ExecStart=/usr/bin/env ip link set mv0 netns %i name mv0
21+
22+
# Configure link
23+
ExecStart=-/usr/bin/env ip netns exec %i ip link set lo up
24+
ExecStart=-/usr/bin/env ip netns exec %i ip link set dev mv0 up
25+
ExecStop=/usr/bin/env if [[ -e "/usr/local/bin/ns-%i" ]]; then bash /usr/local/bin/ns-%i start %i; fi
26+
27+
ExecStop=/usr/bin/env if [[ -e "/usr/local/bin/ns-%i" ]]; then bash /usr/local/bin/ns-%i stop; fi
28+
29+
[Install]
30+
WantedBy=multi-user.target
31+
WantedBy=network-online.target

etc/systemd/system/[email protected]

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[Unit]
2+
Description=Named network namespace %i
3+
Documentation=https://github.com/openstack/ansible-role-systemd_service
4+
JoinsNamespaceOf=systemd-netns@%i.service
5+
BindsTo=systemd-netns-access@%i.service
6+
PartOf=%i.service
7+
After=syslog.target network.target systemd-netns@%i.service
8+
9+
[Service]
10+
Type=oneshot
11+
RemainAfterExit=true
12+
PrivateNetwork=true
13+
14+
# Start process
15+
ExecStartPre=-/usr/bin/env ip netns delete %I
16+
ExecStart=/usr/bin/env ip netns add %I
17+
ExecStart=/usr/bin/env ip netns exec %I ip link set lo up
18+
ExecStart=/usr/bin/env umount /var/run/netns/%I
19+
ExecStart=/usr/bin/env mount --bind /proc/self/ns/net /var/run/netns/%I
20+
21+
# Stop process
22+
ExecStop=/usr/bin/env ip netns delete %I
23+
24+
[Install]
25+
WantedBy=multi-user.target
26+
WantedBy=network-online.target

usr/local/bin/ns-haproxy

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
4+
function start {
5+
/usr/bin/env ip netns exec $2 sysctl -w net.ipv4.conf.mv0.forwarding=1
6+
/usr/bin/env ip netns exec $2 sysctl -w net.ipv4.conf.mv0.arp_notify=1
7+
/usr/bin/env ip netns exec $2 sysctl -w net.ipv4.conf.mv0.arp_announce=2
8+
/usr/bin/env ip netns exec $2 sysctl -w net.ipv4.conf.mv0.use_tempaddr=0
9+
/usr/bin/env ip netns exec $2 ip address add 172.16.26.1/22 dev mv0
10+
/usr/bin/env ip netns exec $2 ip address add 172.16.26.2/22 dev mv0
11+
/usr/bin/env ip route add 172.16.26.1/32 dev mv-int metric 100 table local
12+
/usr/bin/env ip route add 172.16.26.2/32 dev mv-int metric 100 table local
13+
}
14+
15+
16+
function stop {
17+
/usr/bin/env ip route del 172.16.26.1/32 dev mv-int metric 100 table local
18+
/usr/bin/env ip route del 172.16.26.2/32 dev mv-int metric 100 table local
19+
}
20+
21+
22+
case "$1" in
23+
start)
24+
start
25+
;;
26+
stop)
27+
stop
28+
;;
29+
restart)
30+
stop
31+
start
32+
;;
33+
*)
34+
echo "Usage: $0 {start|stop|restart}"
35+
esac

0 commit comments

Comments
 (0)