-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1-container_manual.sh
executable file
·145 lines (144 loc) · 6.9 KB
/
1-container_manual.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
144
145
#!/bin/bash
echo Current network namespaces are:
echo ==========================================================================
lsns -t net
echo ==========================================================================
echo
printf "Press enter to launch a container and see the difference..."
read
echo "Starting container with no network... "
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> container_id=$(docker run --platform $(dpkg --print-architecture) \'
echo ' --rm -d --network=none --name=netless lllamnyp/rebrain-utils'
echo '--> container_pid=$(docker inspect ${container_id} \'
echo " -f '{{ .State.Pid }}')"
echo ----------------------------------RESULT----------------------------------
container_id=$(docker run --platform $(dpkg --print-architecture) --rm -d --network=none --name=netless lllamnyp/rebrain-utils)
echo container id is $container_id
container_pid=$(docker inspect ${container_id} -f '{{ .State.Pid }}')
echo container pid is $container_pid
echo ==========================================================================
echo
echo The container network namespace:
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> lsns -t net -p ${container_pid}'
echo ----------------------------------RESULT----------------------------------
lsns -t net -p ${container_pid}
echo ==========================================================================
echo
printf "Press enter to examine the host and container network interfaces..."
read
echo Host interfaces:
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> ifconfig'
echo ----------------------------------RESULT----------------------------------
ifconfig
echo ==========================================================================
echo
echo Container interfaces:
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> nsenter -t ${container_pid} -n ifconfig'
echo ----------------------------------RESULT----------------------------------
nsenter -t ${container_pid} -n ifconfig
echo ==========================================================================
echo
echo Docker0 bridge info:
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> brctl show docker0'
echo ----------------------------------RESULT----------------------------------
brctl show docker0
echo ==========================================================================
echo
echo Press enter to set up networking for the container...
read
echo Creating veth pair
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> ip link add manual-a type veth peer name manual-b'
echo '--> ip link set up manual-a'
echo '--> ip link set up manual-b'
echo '--> ip link show manual-a'
echo '--> ip link show manual-b'
echo ----------------------------------RESULT----------------------------------
ip link add manual-a type veth peer name manual-b
ip link set up manual-a
ip link set up manual-b
ip link show manual-a
ip link show manual-b
echo ==========================================================================
echo
echo Press enter to connect veth to bridge...
read
echo Attaching one end of veth pair to bridge
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> brctl addif docker0 manual-a'
echo '--> brctl show docker0'
echo ----------------------------------RESULT----------------------------------
brctl addif docker0 manual-a
brctl show docker0
echo ==========================================================================
echo
echo Press enter to get a reference to the net ns...
read
echo Symlinking the network namespace from procfs to /var/run/netns...
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '-->mkdir -p /var/run/netns/'
echo '-->ln -sfT /proc/${container_pid}/ns/net /var/run/netns/netless'
echo '-->ls -al /var/run/netns/netless'
echo ----------------------------------RESULT----------------------------------
mkdir -p /var/run/netns/
ln -sfT /proc/${container_pid}/ns/net /var/run/netns/netless
ls -al /var/run/netns/netless
echo ==========================================================================
echo
echo Press enter to connect the veth to the container...
read
echo Connecting other end of veth pair to container network namespace...
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> ip link set manual-b netns netless'
echo '--> ip netns exec netless ip a list'
echo '--> ip netns exec netless ip link set up manual-b'
echo '--> ip netns exec netless ip addr add 172.17.0.100/16 dev manual-b'
echo '--> ip netns exec netless ip a show manual-b'
echo ----------------------------------RESULT----------------------------------
ip link set manual-b netns netless
ip netns exec netless ip a list
ip netns exec netless ip link set up manual-b
ip netns exec netless ip addr add 172.17.0.100/16 dev manual-b
ip netns exec netless ip a show manual-b
echo ==========================================================================
echo
echo Press enter to configure the default route for the container...
read
echo Setting up routes...
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> ip netns exec netless ip route add default via 172.17.0.1'
echo '--> ip netns exec netless ip r'
echo ----------------------------------RESULT----------------------------------
ip netns exec netless ip route add default via 172.17.0.1
ip netns exec netless ip r
echo ==========================================================================
echo
echo Press enter to test TCP connectivity...
read
echo Testing from host ns...
echo ==========================================================================
echo ---------------------------------COMMANDS---------------------------------
echo '--> curl 172.17.0.100'
echo ----------------------------------RESULT----------------------------------
curl 172.17.0.100
echo ==========================================================================
echo "It works! Press enter to clean up..."
read
echo Cleaning up...
rm /var/run/netns/netless
docker rm -f netless