containerlab 是一个使用容器技术构建虚拟网络平台的系统,类似于packet tracer,不过没有图形界面,更加轻量。
本文主要记录学习containerlab的第一个实验 srl01 搭建一个主机。
运行srl01
查看配置文件
more srl01.clab.yml
name: srl01
topology:
kinds:
srl:
type: ixrd3
image: ghcr.io/nokia/srlinux
nodes:
srl:
kind: srl
简单说明:
name 拓扑名称
kinds 定义类型,这里定义了一个srl(srlinux)类型
nodes 拓扑有哪些类型,这里只有一个srl类型
部署配置文件
containerlab deploy -t ./srl01.clab.yml
INFO[0001] Containerlab v0.44.3 started
INFO[0002] Parsing & checking topology file: srl01.clab.yml
INFO[0002] Creating docker network: Name="clab", IPv4Subnet="172.20.20.0/24", IPv6Subnet="2001:172:20:20::/64", MTU="1500"
WARN[0002] Unable to load kernel module "ip_tables" automatically "load ip_tables failed: exec format error"
INFO[0002] Creating lab directory: /root/containerlab/lab-examples/srl01/clab-srl01
WARN[0003] SSH_AUTH_SOCK not set, skipping pubkey fetching
INFO[0003] Creating container: "srl"
INFO[0005] Running postdeploy actions for Nokia SR Linux 'srl' node
INFO[0084] Adding containerlab host entries to /etc/hosts file
+---+----------------+--------------+-----------------------+------+---------+----------------+----------------------+
| # | Name | Container ID | Image | Kind | State | IPv4 Address | IPv6 Address |
+---+----------------+--------------+-----------------------+------+---------+----------------+----------------------+
| 1 | clab-srl01-srl | 0d2e3356aaf5 | ghcr.io/nokia/srlinux | srl | running | 172.20.20.2/24 | 2001:172:20:20::2/64 |
+---+----------------+--------------+-----------------------+------+---------+----------------+----------------------+
这里可以看到 部署了一个linux容器,id 是 0d2e3356aaf5 有一个IP地址 172.20.20.2
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d2e3356aaf5 ghcr.io/nokia/srlinux "/tini -- fixuid -q …" 9 minutes ago Up 9 minutes clab-srl01-srl
这个IP地址使用的是宿主机的 虚拟网桥的网段,这个地址仅仅用于和宿主机通信,并没有什么实际用途。
5: br-229c075377f7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:9d:bd:29:08 brd ff:ff:ff:ff:ff:ff
inet 172.20.20.1/24 brd 172.20.20.255 scope global br-229c075377f7
valid_lft forever preferred_lft forever
inet6 2001:172:20:20::1/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::42:9dff:febd:2908/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::1/64 scope link
valid_lft forever preferred_lft forever
进入容器查看:
docker exec -it 0d2e3356aaf5 bash
[root@srl /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: dummy-mgmt0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether f2:e8:db:8b:7d:49 brd ff:ff:ff:ff:ff:ff
inet 172.20.20.2/24 brd 172.20.20.255 scope global dummy-mgmt0
valid_lft forever preferred_lft forever
inet6 2001:172:20:20::2/64 scope global
valid_lft forever preferred_lft forever
3: gway-2800@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether ba:d2:11:c8:cc:55 brd ff:ff:ff:ff:ff:ff link-netns srbase-mgmt
inet6 fe80::b8d2:11ff:fec8:cc55/64 scope link
valid_lft forever preferred_lft forever
5: monit_in@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9234 qdisc noqueue state UP group default qlen 1000
link/ether 4e:ea:4f:ce:62:14 brd ff:ff:ff:ff:ff:ff link-netns monit
inet6 fe80::4cea:4fff:fece:6214/64 scope link
valid_lft forever preferred_lft forever
6: mgmt0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1514 qdisc noqueue state UP group default
link/ether 02:42:ac:14:14:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
7: mgmt0-0@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 1a:8b:28:f0:74:1f brd ff:ff:ff:ff:ff:ff link-netns srbase-mgmt
inet6 fe80::188b:28ff:fef0:741f/64 scope link
valid_lft forever preferred_lft forever
dummy-mgmt0 虚拟的网卡对应了IP地址172.20.20.2
这里注意到这个网卡还没有被UP起来,可以用ip命令up网卡:
ip link set dummy-mgmt0 up
现在就可以ping通本地的地址了:
ping 172.20.20.2
PING 172.20.20.2 (172.20.20.2) 56(84) bytes of data.
64 bytes from 172.20.20.2: icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from 172.20.20.2: icmp_seq=2 ttl=64 time=0.048 ms
^C
--- 172.20.20.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1026ms
rtt min/avg/max/mdev = 0.032/0.040/0.048/0.008 ms