SDN作业2 Open vSwitch 应用实践1本文地址：https://type.dayiyi.top/index.php/archives/292/https://blog.dayi.ink/?p=162https://www.cnblogs.com/rabbit-dayi/p/17868059.htmlhttps://cmd.dayi.ink/ltQtj5aOSi-x2Xc6xOuHGg本实验均在root用户下进行如果失败了，可能是OVS的问题，请参考文章末尾su sudo su预制启动OVS服务ovs-ctl start然后想法开两个shell任务1先清空下终端1：mn -c mn终端2（另外开一个终端）：ovs-ofctl dump-flows s1没有流表：PingALL#CLI 终端1 mininet> pingall *** Ping: testing ping reachability h1 -> h2 h2 -> h1 *** Results: 0% dropped (2/2 received) mininet> 查看流表：#终端2 ovs-ofctl dump-flows s1建立拓扑# 终端1 # 清理并且建立 1个交换机4个主机 mn -c && mn --topo single,4查看流表# 终端2 # 查看流表 ovs-ofctl dump-flows s1 #这里应该是空的<!-- ### 设置IP更新内容mininet> h1 ifconfig h1-eth0 10.0.0.1 mininet> h2 ifconfig h2-eth0 10.0.0.2 mininet> h3 ifconfig h3-eth0 10.0.0.3 mininet> h4 ifconfig h4-eth0 10.0.0.4PINGALL#终端1 mininet> pingall *** Ping: testing ping reachability h1 -> h2 h3 h4 h2 -> h1 h3 h4 h3 -> h1 h2 h4 h4 -> h1 h2 h3 *** Results: 0% dropped (12/12 received) mininet>这里全部可以PING通查看目前的流表：#终端2 root@ubuntu:/home/sdn# ovs-ofctl dump-flows s1 cookie=0x0, duration=55.843s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=ca:76:64:1b:37:77,arp_spa=10.0.0.2,arp_tpa=10.0.0.1,arp_op=2 actions=output:"s1-eth1" cookie=0x0, duration=55.841s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=ca:76:64:1b:37:77,arp_spa=10.0.0.3,arp_tpa=10.0.0.1,arp_op=2 actions=output:"s1-eth1" cookie=0x0, duration=55.839s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=ca:76:64:1b:37:77,arp_spa=10.0.0.4,arp_tpa=10.0.0.1,arp_op=2 actions=output:"s1-eth1" cookie=0x0, duration=55.836s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=22:87:c2:36:ab:33,arp_spa=10.0.0.3,arp_tpa=10.0.0.2,arp_op=2 actions=output:"s1-eth2" cookie=0x0, duration=55.833s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=22:87:c2:36:ab:33,arp_spa=10.0.0.4,arp_tpa=10.0.0.2,arp_op=2 actions=output:"s1-eth2" cookie=0x0, duration=55.829s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=12:18:0f:59:6d:59,arp_spa=10.0.0.4,arp_tpa=10.0.0.3,arp_op=2 actions=output:"s1-eth3" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=12:18:0f:59:6d:59,arp_spa=10.0.0.4,arp_tpa=10.0.0.3,arp_op=1 actions=output:"s1-eth3" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=22:87:c2:36:ab:33,arp_spa=10.0.0.3,arp_tpa=10.0.0.2,arp_op=1 actions=output:"s1-eth2" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=ca:76:64:1b:37:77,arp_spa=10.0.0.2,arp_tpa=10.0.0.1,arp_op=1 actions=output:"s1-eth1" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=22:87:c2:36:ab:33,arp_spa=10.0.0.4,arp_tpa=10.0.0.2,arp_op=1 actions=output:"s1-eth2" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=ca:76:64:1b:37:77,arp_spa=10.0.0.3,arp_tpa=10.0.0.1,arp_op=1 actions=output:"s1-eth1" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=ca:76:64:1b:37:77,arp_spa=10.0.0.4,arp_tpa=10.0.0.1,arp_op=1 actions=output:"s1-eth1" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=c2:4d:1b:f3:02:51,arp_spa=10.0.0.3,arp_tpa=10.0.0.4,arp_op=2 actions=output:"s1-eth4" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=12:18:0f:59:6d:59,arp_spa=10.0.0.2,arp_tpa=10.0.0.3,arp_op=2 actions=output:"s1-eth3" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=22:87:c2:36:ab:33,arp_spa=10.0.0.1,arp_tpa=10.0.0.2,arp_op=2 actions=output:"s1-eth2" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=12:18:0f:59:6d:59,arp_spa=10.0.0.1,arp_tpa=10.0.0.3,arp_op=2 actions=output:"s1-eth3" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=c2:4d:1b:f3:02:51,arp_spa=10.0.0.1,arp_tpa=10.0.0.4,arp_op=2 actions=output:"s1-eth4" cookie=0x0, duration=50.806s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,arp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=c2:4d:1b:f3:02:51,arp_spa=10.0.0.2,arp_tpa=10.0.0.4,arp_op=2 actions=output:"s1-eth4" cookie=0x0, duration=55.843s, table=0, n_packets=3, n_bytes=294, idle_timeout=60, priority=65535,icmp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=22:87:c2:36:ab:33,nw_src=10.0.0.1,nw_dst=10.0.0.2,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth2" cookie=0x0, duration=55.843s, table=0, n_packets=1, n_bytes=98, idle_timeout=60, priority=65535,icmp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=ca:76:64:1b:37:77,nw_src=10.0.0.2,nw_dst=10.0.0.1,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth1" cookie=0x0, duration=55.841s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=12:18:0f:59:6d:59,nw_src=10.0.0.1,nw_dst=10.0.0.3,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth3" cookie=0x0, duration=55.841s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=ca:76:64:1b:37:77,nw_src=10.0.0.3,nw_dst=10.0.0.1,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth1" cookie=0x0, duration=55.839s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=c2:4d:1b:f3:02:51,nw_src=10.0.0.1,nw_dst=10.0.0.4,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth4" cookie=0x0, duration=55.839s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=ca:76:64:1b:37:77,nw_src=10.0.0.4,nw_dst=10.0.0.1,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth1" cookie=0x0, duration=55.838s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=ca:76:64:1b:37:77,nw_src=10.0.0.2,nw_dst=10.0.0.1,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth1" cookie=0x0, duration=55.838s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=22:87:c2:36:ab:33,nw_src=10.0.0.1,nw_dst=10.0.0.2,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth2" cookie=0x0, duration=55.835s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=12:18:0f:59:6d:59,nw_src=10.0.0.2,nw_dst=10.0.0.3,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth3" cookie=0x0, duration=55.835s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=22:87:c2:36:ab:33,nw_src=10.0.0.3,nw_dst=10.0.0.2,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth2" cookie=0x0, duration=55.833s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=c2:4d:1b:f3:02:51,nw_src=10.0.0.2,nw_dst=10.0.0.4,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth4" cookie=0x0, duration=55.833s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=22:87:c2:36:ab:33,nw_src=10.0.0.4,nw_dst=10.0.0.2,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth2" cookie=0x0, duration=55.832s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=ca:76:64:1b:37:77,nw_src=10.0.0.3,nw_dst=10.0.0.1,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth1" cookie=0x0, duration=55.831s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=12:18:0f:59:6d:59,nw_src=10.0.0.1,nw_dst=10.0.0.3,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth3" cookie=0x0, duration=55.830s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=22:87:c2:36:ab:33,nw_src=10.0.0.3,nw_dst=10.0.0.2,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth2" cookie=0x0, duration=55.830s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=12:18:0f:59:6d:59,nw_src=10.0.0.2,nw_dst=10.0.0.3,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth3" cookie=0x0, duration=55.829s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=c2:4d:1b:f3:02:51,nw_src=10.0.0.3,nw_dst=10.0.0.4,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth4" cookie=0x0, duration=55.828s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=12:18:0f:59:6d:59,nw_src=10.0.0.4,nw_dst=10.0.0.3,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth3" cookie=0x0, duration=55.827s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=ca:76:64:1b:37:77,nw_src=10.0.0.4,nw_dst=10.0.0.1,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth1" cookie=0x0, duration=55.827s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth1",vlan_tci=0x0000,dl_src=ca:76:64:1b:37:77,dl_dst=c2:4d:1b:f3:02:51,nw_src=10.0.0.1,nw_dst=10.0.0.4,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth4" cookie=0x0, duration=55.826s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=22:87:c2:36:ab:33,nw_src=10.0.0.4,nw_dst=10.0.0.2,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth2" cookie=0x0, duration=55.826s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth2",vlan_tci=0x0000,dl_src=22:87:c2:36:ab:33,dl_dst=c2:4d:1b:f3:02:51,nw_src=10.0.0.2,nw_dst=10.0.0.4,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth4" cookie=0x0, duration=55.825s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth4",vlan_tci=0x0000,dl_src=c2:4d:1b:f3:02:51,dl_dst=12:18:0f:59:6d:59,nw_src=10.0.0.4,nw_dst=10.0.0.3,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:"s1-eth3" cookie=0x0, duration=55.825s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, priority=65535,icmp,in_port="s1-eth3",vlan_tci=0x0000,dl_src=12:18:0f:59:6d:59,dl_dst=c2:4d:1b:f3:02:51,nw_src=10.0.0.3,nw_dst=10.0.0.4,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:"s1-eth4" root@ubuntu:/home/sdn#修改流表阻止H1访问其他的主机# 终端1 # 重新开一下 CTRL+D mn -c && mn --topo single,4 # 终端2 ovs-ofctl add-flow s1 icmp,nw_src=10.0.0.1,icmp_type=8,action=drop测试：#终端1 mininet> pingall *** Ping: testing ping reachability h1 -> X X X h2 -> h1 h3 h4 h3 -> h1 h2 h4 h4 -> h1 h2 h3 *** Results: 25% dropped (9/12 received) mininet>失败了？把内核降到了5.8，然后又重新装了下OVS 2.17，记得删除内核模块哦。大体就这样，挺麻烦的。虚拟机放这里了，这样省脑子：这个是我自己用的版本，没经过测试，可能多少有点不一样，OVS要手动启动哦。Ubuntu_20.04_sdn_ovs_2.17.8-LTS-fix1链接：https://pan.baidu.com/s/1fwvV2B_eH6D3xEQ2bYJnlQ?pwd=6y8l 提取码：6y8l --来自百度网盘超级会员V6的分享
SDN 作业1任务：要求1、将代码上传作业 2、用python3运行代码，使用cli实现pingall、nodes、net、dump iperf h1 h2等命令更好阅读：https://type.dayiyi.top/index.php/archives/284/https://https://blog.dayi.ink/?p=150可能仍然有BUG，请反馈。打开虚拟机我这里打开的是Ubuntu_20.04_sdn_ovs-2.17.8-LTS这个虚拟机。具体的文件在群文件中打开miniedit.pysudo python2 /opt/sdn/mininet/examples/miniedit.py画图照着图画一个，然后连起来即可。设置IP右键这个主机，然后选properties分别设置IP地址即可。设置链路速率右键这个线，然后选properties然后参数这样设置：100,5ms设置，进入CLI模式Edit->Preferences->Start CLICLI：保存文件这里记得存两份，保存出的python文件是不能再变成图片的！保存mnFile->Save导出python文件导出Level 2 Script另外，这里导出到过程不应该出现报错这里不应该有python报错。运行测试进入导出文件的目录，我这里是/home/sdn运行：python3 ovo.py测试 PING（4，5交换机未连接控制器）# 在mn的CLI下 net.pingall() pingall()因为那个没连接控制器，所以会ping不通，只有部分可以通。测试PING（4/5连接控制器）mininet> pingall *** Ping: testing ping reachability h6 -> h8 h1 h5 h4 h2 h3 h7 h8 -> h6 h1 h5 h4 h2 h3 h7 h1 -> h6 h8 h5 h4 h2 h3 h7 h5 -> h6 h8 h1 h4 h2 h3 h7 h4 -> h6 h8 h1 h5 h2 h3 h7 h2 -> h6 h8 h1 h5 h4 h3 h7 h3 -> h6 h8 h1 h5 h4 h2 h7 h7 -> h6 h8 h1 h5 h4 h2 h3 *** Results: 0% dropped (56/56 received) mininet>测试延迟：20ms（去5ms，回来5ms）mininet> h1 ping h2 PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data. 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=46.7 ms 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=23.4 ms 64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=21.9 ms 64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=22.5 ms 64 bytes from 10.0.0.2: icmp_seq=5 ttl=64 time=23.1 ms 64 bytes from 10.0.0.2: icmp_seq=6 ttl=64 time=22.9 ms 64 bytes from 10.0.0.2: icmp_seq=7 ttl=64 time=23.8 ms ^C --- 10.0.0.2 ping statistics --- 7 packets transmitted, 7 received, 0% packet loss, time 6007ms rtt min/avg/max/mdev = 21.987/26.393/46.792/8.345 ms mininet>nodesnodes结果：mininet> nodes available nodes are: c0 h1 h2 h3 h4 h5 h6 h7 h8 s1 s2 s3 s4 s5 mininet>netnetmininet> net h7 h7-eth0:s3-eth2 h3 h3-eth0:s4-eth2 h4 h4-eth0:s4-eth3 h5 h5-eth0:s5-eth2 h1 h1-eth0:s1-eth1 h8 h8-eth0:s3-eth3 h6 h6-eth0:s5-eth3 h2 h2-eth0:s1-eth2 s2 lo: s2-eth1:s1-eth3 s2-eth2:s3-eth1 s2-eth3:s4-eth1 s2-eth4:s5-eth1 s4 lo: s4-eth1:s2-eth3 s4-eth2:h3-eth0 s4-eth3:h4-eth0 s1 lo: s1-eth1:h1-eth0 s1-eth2:h2-eth0 s1-eth3:s2-eth1 s3 lo: s3-eth1:s2-eth2 s3-eth2:h7-eth0 s3-eth3:h8-eth0 s5 lo: s5-eth1:s2-eth4 s5-eth2:h5-eth0 s5-eth3:h6-eth0 c0 mininet>dumpdumpmininet> dump <Host h7: h7-eth0:10.0.0.7 pid=6926> <Host h3: h3-eth0:10.0.0.3 pid=6928> <Host h4: h4-eth0:10.0.0.4 pid=6930> <Host h5: h5-eth0:10.0.0.5 pid=6932> <Host h1: h1-eth0:10.0.0.1 pid=6934> <Host h8: h8-eth0:10.0.0.8 pid=6936> <Host h6: h6-eth0:10.0.0.6 pid=6938> <Host h2: h2-eth0:10.0.0.2 pid=6940> <OVSSwitch s2: lo:127.0.0.1,s2-eth1:None,s2-eth2:None,s2-eth3:None,s2-eth4:None pid=6909> <OVSSwitch s4: lo:127.0.0.1,s4-eth1:None,s4-eth2:None,s4-eth3:None pid=6912> <OVSSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None pid=6915> <OVSSwitch s3: lo:127.0.0.1,s3-eth1:None,s3-eth2:None,s3-eth3:None pid=6918> <OVSSwitch s5: lo:127.0.0.1,s5-eth1:None,s5-eth2:None,s5-eth3:None pid=6921> <Controller c0: 127.0.0.1:6633 pid=6899> mininet>iperf测速iperf h1 h2*** Starting CLI: mininet> iperf h1 h2 *** Iperf: testing TCP bandwidth between h1 and h2 *** Results: ['78.1 Mbits/sec', '79.7 Mbits/sec'] mininet> iperf h1 h2 *** Iperf: testing TCP bandwidth between h1 and h2 *** Results: ['81.0 Mbits/sec', '96.0 Mbits/sec'] mininet> mininet> iperf h1 h2 *** Iperf: testing TCP bandwidth between h1 and h2 *** Results: ['74.6 Mbits/sec', '87.2 Mbits/sec'] mininet>mininet> iperf h1 h2 *** Iperf: testing TCP bandwidth between h1 and h2 *** Results: ['78.1 Mbits/sec', '94.0 Mbits/sec'] mininet> iperfudp h1 h2 invalid number of args: iperfudp bw src dst bw examples: 10M mininet> iperfudp 100M h1 h2 *** Iperf: testing UDP bandwidth between h1 and h2 *** Results: ['100M', '23.7 Mbits/sec', '23.7 Mbits/sec'] mininet>调试过程发现pingall有不通的做了好久好久，换了内核，重装了好多查了好久，发现必须要连接那个controller才可以。代码from mininet.net import Mininet from mininet.node import Controller, RemoteController, OVSController from mininet.node import CPULimitedHost, Host, Node from mininet.node import OVSKernelSwitch, UserSwitch from mininet.node import IVSSwitch from mininet.cli import CLI from mininet.log import setLogLevel, info from mininet.link import TCLink, Intf from subprocess import call def myNetwork(): net = Mininet( topo=None, build=False, ipBase='10.0.0.0/8') info( '*** Adding controller\n' ) c0=net.addController(name='c0', controller=Controller, protocol='tcp', port=6633) info( '*** Add switches\n') s4 = net.addSwitch('s4', cls=OVSKernelSwitch) s3 = net.addSwitch('s3', cls=OVSKernelSwitch) s1 = net.addSwitch('s1', cls=OVSKernelSwitch) s2 = net.addSwitch('s2', cls=OVSKernelSwitch) s5 = net.addSwitch('s5', cls=OVSKernelSwitch) info( '*** Add hosts\n') h3 = net.addHost('h3', cls=Host, ip='10.0.0.3/24', defaultRoute=None) h2 = net.addHost('h2', cls=Host, ip='10.0.0.2/24', defaultRoute=None) h4 = net.addHost('h4', cls=Host, ip='10.0.0.4/24', defaultRoute=None) h5 = net.addHost('h5', cls=Host, ip='10.0.0.5/24', defaultRoute=None) h1 = net.addHost('h1', cls=Host, ip='10.0.0.1/24', defaultRoute=None) h7 = net.addHost('h7', cls=Host, ip='10.0.0.7/24', defaultRoute=None) h8 = net.addHost('h8', cls=Host, ip='10.0.0.8/24', defaultRoute=None) h6 = net.addHost('h6', cls=Host, ip='10.0.0.6/24', defaultRoute=None) info( '*** Add links\n') s1h1 = {'bw':100,'delay':'5ms'} net.addLink(s1, h1, cls=TCLink , **s1h1) s1h2 = {'bw':100,'delay':'5ms'} net.addLink(s1, h2, cls=TCLink , **s1h2) s1s2 = {'bw':100,'delay':'5ms'} net.addLink(s1, s2, cls=TCLink , **s1s2) s2s3 = {'bw':100,'delay':'5ms'} net.addLink(s2, s3, cls=TCLink , **s2s3) s2s4 = {'bw':100,'delay':'5ms'} net.addLink(s2, s4, cls=TCLink , **s2s4) s2s5 = {'bw':100,'delay':'5ms'} net.addLink(s2, s5, cls=TCLink , **s2s5) s3h7 = {'bw':100,'delay':'5ms'} net.addLink(s3, h7, cls=TCLink , **s3h7) s3h8 = {'bw':100,'delay':'5ms'} net.addLink(s3, h8, cls=TCLink , **s3h8) s4h3 = {'bw':100,'delay':'5ms'} net.addLink(s4, h3, cls=TCLink , **s4h3) s4h4 = {'bw':100,'delay':'5ms'} net.addLink(s4, h4, cls=TCLink , **s4h4) s5h5 = {'bw':100,'delay':'5ms'} net.addLink(s5, h5, cls=TCLink , **s5h5) s5h6 = {'bw':100,'delay':'5ms'} net.addLink(s5, h6, cls=TCLink , **s5h6) info( '*** Starting network\n') net.build() info( '*** Starting controllers\n') for controller in net.controllers: controller.start() info( '*** Starting switches\n') net.get('s4').start([c0]) net.get('s3').start([c0]) net.get('s1').start([c0]) net.get('s2').start([c0]) net.get('s5').start([c0]) info( '*** Post configure switches and hosts\n') CLI(net) net.stop() if __name__ == '__main__': setLogLevel( 'info' ) myNetwork() root@ubuntu:/home/sdn# python3 wk3.py *** Adding controller *** Add switches *** Add hosts *** Add links (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) *** Starting network *** Configuring hosts h3 h2 h4 h5 h1 h7 h8 h6 *** Starting controllers *** Starting switches (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) (100.00Mbit 5ms delay) *** Post configure switches and hosts *** Starting CLI: mininet> pingall *** Ping: testing ping reachability h3 -> h2 h4 h5 h1 h7 h8 h6 h2 -> h3 h4 h5 h1 h7 h8 h6 h4 -> h3 h2 h5 h1 h7 h8 h6 h5 -> h3 h2 h4 h1 h7 h8 h6 h1 -> h3 h2 h4 h5 h7 h8 h6 h7 -> h3 h2 h4 h5 h1 h8 h6 h8 -> h3 h2 h4 h5 h1 h7 h6 h6 -> h3 h2 h4 h5 h1 h7 h8 *** Results: 0% dropped (56/56 received)
云与虚拟化：实验2 Docker镜像常用命令的使用
云与虚拟化：实验2 Docker镜像常用命令的使用实验要求了解Docker镜像，掌握Docker镜像常用命令的使用。前置准备要求实验主机能够连接外网，已经正确安装Docker，并关闭防火墙和selinux。实验步骤步骤1：拉取nginx:latest、busybox:latest和centos:latest镜像docker pull centos:latest docker pull nginx:latest docker pull busybox:latest步骤2：列出所有本地镜像docker images步骤3：搜索收藏数不小于5的redhat镜像，并且完整显示镜像描述信息docker search -s 5 --no-trunc redhat docker search --filter stars=5 redhat其他的命令也可以哦curl -s "https://registry.hub.docker.com/v2/repositories/redhat/" | jq '.results[] | select(.pull_count >= 5) | .description'步骤4：拉取收藏数是高的redhat镜像docker pull redhat/ubi8步骤5：将redhat镜像标签名修改为redhat:v8.0docker tag redhat/ubi8:latest redhat:v8.0步骤6：将nginx:latest镜像导出，命名为nginx.tardocker save -o nginx.tar nginx:latest步骤7：删除nginx:latest镜像，并运行docker images命令查看docker rmi -f nginx:latest步骤8：将nginx.tar导入，并运行docker images命令查看docker load --input nginx.tar docker images步骤9：删除本地主机中所有的镜像，要求逐一删除docker rmi nginx:latest docker rmi busybox:latest docker rmi centos:latest docker rmi redhat:v7.0 docker rmi hjd48/redhat:latest docker rm -v $(docker ps -aq -f status=created) docker rm -v $(docker ps -aq -f status=exited)这里还有两个冲突：
K8SK8S部署先创建一个debian模板选择iso快速安装一下即可：安装完成克隆成模板：Master节点直接克隆一个就可以克隆两个本地nodesed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list echo "export PATH=$PATH:/usr/sbin:/home/$(whoami)/.local/bin" >> ~/.bashrc source ~/.bashrc apt install ufw apt install sudo vim wget curl -y sudo hostnamectl set-hostname dayi-cloud-k8s-master 这里HOSTS用了tailscile 来进行相互访问curl -fsSL https://tailscale.com/install.sh | sh这样就配好hosts了dayi-cloud-k8s-master dayi-cloud-k8s-node1 dayi-cloud-k8s-node2Mastersudo ufw allow 6443/tcp sudo ufw allow 2379/tcp sudo ufw allow 2380/tcp sudo ufw allow 10250/tcp sudo ufw allow 10251/tcp sudo ufw allow 10252/tcp sudo ufw allow 10255/tcp sudo ufw reload cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf overlay br_netfilter EOF sudo modprobe overlay sudo modprobe br_netfilter cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-k8s.conf net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 EOF sudo sysctl --systemOthersudo ufw allow 10250/tcp sudo ufw allow 30000:32767/tcp sudo ufw reload cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf overlay br_netfilter EOF sudo modprobe overlay sudo modprobe br_netfilter cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-k8s.conf net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 EOF sudo sysctl --systemSSH-密钥复制mkdir ~/.ssh echo "ssh-rsa [binary] root@dayi-cloud-k8s-maste" >> ~/.ssh/authorized_keys 安装containerdapt install containerd containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1 sudo vi /etc/containerd/config.toml scp /etc/containerd/config.toml dayi-cloud-k8s-node1:/etc/containerd/config.toml scp /etc/containerd/config.toml dayi-cloud-k8s-node2:/etc/containerd/config.toml sudo systemctl restart containerd sudo systemctl enable containerd安装K8S仓库 sudo apt install gnupg gnupg2 curl software-properties-common -y curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/cgoogle.gpg apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" 安装K8Sapt update apt install kubelet kubeadm kubectl -y apt-mark hold kubelet kubeadm kubectl初始化集群：kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.234.0.0/16 --control-plane-endpoint=dayi-cloud-k8s-master --v=5虚拟化K8S安装# 设置主机名 hostnamectl set-hostname m1 hostnamectl set-hostname node1 hostnamectl set-hostname node2 、 ===== 192.168.0.202 node2 # 使用scp复制/etc/hosts到node1和node2 scp /etc/hosts root@192.168.0.201:/etc/hosts scp /etc/hosts root@192.168.0.202:/etc/hosts # 关闭swap并配置内核参数 swapoff -a sed -i 's/.*swap.*/#&/' /etc/fstab echo -e 'net.bridge.bridge-nf-call-iptables = 1 \nnet.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.conf # 安装基本软件包 yum -y install wget ntpdate # 配置时间同步 ntpdate ntp1.aliyun.com crontab -e # 添加以下内容 */1 * * * * /usr/sbin/ntpdate ntp1.aliyun.com systemctl restart crond.service reboot # 配置yum源及下载Kubernetes和Docker相关软件包 cd /etc/yum.repos.d rm -f CentOS-* wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo vi /etc/yum.repos.d/kubernetes.repo # 添加kubernetes源的配置信息 wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg rpm -import rpm-package-key.gpg wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum clean all yum makecache fast cd # 安装docker-ce并启动服务 yum -y install docker-ce systemctl start docker systemctl enable docker # 配置Docker的镜像加速器 vi /etc/docker/daemon.json # 添加以下内容 { "registry-mirrors": ["https://x3nqjrcg.mirror.aliyuncs.com"] } # Docker降级为20.10 yum downgrade --setopt=obsoletes=0 -y docker-ce-20.10.24 docker-ce-selinux-20.10.24 containerd.io # 修改Docker Cgroup Driver为systemd vi /etc/docker/daemon.json # 添加以下内容 { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://x3nqjrcg.mirror.aliyuncs.com"] } # 重启docker服务 systemctl daemon-reload systemctl restart docker # 安装kubeadm、kubectl、kubelet yum install -y kubelet-1.23.17 kubeadm-1.23.17 kubectl-1.23.17 systemctl start kubelet systemctl enable kubelet # 下载Kubernetes镜像 kubeadm config images list --kubernetes-version v1.23.17 kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers # 初始化Kubernetes集群 kubeadm init \ --kubernetes-version=v1.23.17 \ --pod-network-cidr=10.244.0.0/16 \ --service-cidr=10.96.0.0/12 \ --apiserver-advertise-address=192.168.8.10 \ --image-repository=registry.aliyuncs.com/google_containers # 配置环境变量 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config # 安装Flannel网络 wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml kubectl apply -f kube-flannel.yml # 将节点加入到集群 kubeadm join 192.168.8.10:6443 --token [token] --discovery-token-ca-cert-hash [hash] # 查看集群状态 kubectl get nodes Debian安装K8S2哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈换CENTOS 换大牛Rencher真男人直接下载：修改主机名：ssh-keygen -t ecdsa -b 256 ssh-copy-id node1 ssh-copy-id node2 复制密钥：ssh-copy-id node2复制hostsscp /etc/hosts root@node1:/etc/hosts scp /etc/hosts root@node2:/etc/hosts 修改内核配置/etc/sysctl.confvi:echo -e 'net.bridge.bridge-nf-call-iptables = 1 \nnet.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.conf开启模块modprobe br_netfilter ls /proc/sys/net/bridge/ echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf sysctl -p发送到从节点：scp /etc/sysctl.conf root@node1:/etc/sysctl.conf scp /etc/sysctl.conf root@node2:/etc/sysctl.conf #从节点执行： modprobe br_netfilter ls /proc/sys/net/bridge/ echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf sysctl -p防火墙 SELINUX：systemctl stop firewalld systemctl disable firewalld systemctl status firewalld setenforce 0 vi /etc/selinux/config #修改： SELINUX=disabled reboot发送到从节点scp /etc/selinux/config root@node1:/etc/selinux/config scp /etc/selinux/config root@node2:/etc/selinux/config查看SELINUX状态sestatusNTP时间同步yum -y install wget ntpdate crontab -e */1 * * * * /usr/sbin/ntpdate ntp1.aliyun.com systemctl restart crond.service添加源 vi /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg 安装yum downgrade --setopt=obsoletes=0 -y docker-ce-20.10.24 docker-ce-selinux-20.10.24 containerd.io yum install -y kubelet-1.23.17 kubeadm-1.23.17 kubectl-1.23.17 systemctl start kubelet systemctl enable kubelet systemctl status kubeletkubeadm config images list --kubernetes-version v1.23.17初始化mv /etc/containerd/config.toml /root/config.toml.baksystemctl restart containerd/etc/kubernetes/manifests/kube-scheduler.yaml rm -rf /etc/kubernetes/manifests/kube-apiserver.yamlrm -rf /etc/kubernetes/manifests/kube-controller-manager.yaml rm -rf /etc/kubernetes/manifests/kube-scheduler.yaml rm -rf /etc/kubernetes/manifests/etcd.yamlkubeadm init \--pod-network-cidr=10.244.0.0/16 \--service-cidr=10.96.0.0/12 \--apiserver-advertise-address=192.168.59.137 \--image-repository=registry.aliyuncs.com/google_containers--v=5rm -f /etc/kubernetes/controller-manager.confrm -f /etc/kubernetes/scheduler.confrm -f /etc/kubernetes/scheduler.confrm -f /etc/kubernetes/admin.confrm -f /etc/kubernetes/kubelet.confmkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/configkubeadm join 192.168.59.137:6443 --token y8m7u7.3ur44mc2ezb87fmq \ --discovery-token-ca-cert-hash sha256:cb9d1bfa0c3adaabd9a34cd072e1d27c13c9cf9e6e3ef4a0c810b84d6fdf4c9f节点:节点：状态：
云与虚拟化_实验12 Rancher平台部署应用实验要求掌握 Rancher 平台部署。前置准备要求实验主机能够连接外网，已经正确安装 Docker，并关闭防火墙和 selinux。实验步骤这个东西蛮大的。docker pull rancher/rancher:stable docker run -d --restart=unless-stopped --privileged -p 8443:443 rancher/rancher:stableFRP:FRP：进入：加入：节点添加：步骤1：拉取 Rancher 镜像。[root@localhost ~]# docker pull rancher/server:stable步骤2：利用 Rancher 镜像生成 Rancher 容器。[root@localhost ~]# docker run -d --restart=unless-stopped -p 8080:8080 rancher/server:stable主机上复制 Rancher 脚本代码。[root@localhost ~]# sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.11 http://192.168.5.100:8080/v1/scripts/33363BDEA2105F76C889:1577750400000:7pDzaq5dOUcHCFdLqfZ2lyvGex0步骤11：添加主机成功。移动窗口底部，点击“创建”按钮。不过..更新也容易卡住..好像不是很稳定的样子..
云与虚拟化_实验10 Compose编排工具应用实验要求掌握Compose编排工具的安装和使用。前置准备要求实验主机能够连接外网，已经正确安装Docker ce，版本号为18.03.0-ce，并关闭防火墙和selinux。实验步骤步骤1：安装Compose编排工具直接用docker官方安装源代的插件也可以滴！测试安装结果。root@registry:/home/dayi# root@registry:/home/dayi# find / -name docker-compose /usr/libexec/docker/cli-plugins/docker-compose root@registry:/home/dayi# ln -s /usr/libexec/docker/cli-plugins/docker-compose /bin/docker-compose root@registry:/home/dayi# docker-compose --version Docker Compose version v2.21.0 root@registry:/home/dayi# 步骤2：编排启动镜像创建文件目录。[root@docker01 ~]# mkdir /opt/my_wordpress/ [root@docker01 ~]# cd /opt/my_wordpress/编写编排文件。[root@docker01 my_wordpress]# vi docker-compose.ymlversion: '3' services: db: image: mysql:5.7 volumes: - /data/db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress ports: - "3306:3306" wordpress: depends_on: - db image: wordpress:latest volumes: - /data/web_data:/var/www/html ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress启动。[root@docker01 my_wordpress]# docker-compose up -d # 启动方法：docker-compose up # 后台启动方法：docker-compose up -d在浏览器上访问http://192.168.5.100:8000，进行WordPress的安装即可。删库跑路
云与虚拟化_实验7 Docker网络管理应用实验要求了解Docker常用网络模式，掌握Docker常用网络模式的使用。本实验主要任务是利用busybox镜像建立容器，容器名称为test_busybox1和test_busybox2，将网络模式设置为none，并为容器配置IP地址，容器test_busybox1的IP设置为172.17.0.100，容器test_busybox2的IP设置为172.17.0.200，要求实现两容器互通。前置准备要求实验主机能够连接外网，已经正确安装Docker，并关闭防火墙和selinux。实验过程步骤1-3# 步骤1.1: 创建容器test_busybox1，设置网络模式为none docker run -dit --name=test_busybox1 --net=none busybox:latest # 进入容器test_busybox1 docker exec -it test_busybox1 /bin/sh # 步骤1.2: 查看IP地址（容器test_busybox1） ip addr # 从现象可以得知容器test_busybox1没有IP地址。 # 退出容器test_busybox1 exit# 步骤2.1: 创建容器test_busybox2，设置网络模式为none docker run -dit --name test_busybox2 --net=none busybox:latest # 进入容器test_busybox2 docker exec -it test_busybox2 /bin/sh # 步骤2.2: 查看IP地址（容器test_busybox2） ip address # 退出容器test_busybox2 exit从现象可以得知容器test_busybox1,test_busybox2都没有IP地址。步骤4# 步骤3: 为容器test_busybox1设置IP地址为172.17.0.100 # 安装bridge-utils软件包 yum -y install bridge-utils # 创建veth对，并将veth0加入docker0网桥 ip link add veth0 type veth peer name veth1 # 虚拟网桥 peer的name brctl addif docker0 veth0 # 桥接管理器 添加 管理桥接 brctl show # 启动veth0，原神启动 (另外一个veth1也会自动启动) ip link set veth0 up # 获取容器test_busybox1的PID pid1=$(docker inspect -f '{{.State.Pid}}' test_busybox1) echo "容器test_busybox1的PID是：$pid1" #有两种途径索引network namespace：名字（例如netns1）或者属于该namespace的进程PID。 #使用命名（Name）：为网络命名空间分配可读的名称，然后使用该名称来引用和操作命名空间。这使得管理网络命名空间更加方便和直观。 #使用进程PID：每个网络命名空间都与一个进程相关联，通常是一个子进程。可以使用该进程的PID来访问和管理与之关联的网络命名空间。 # 创建network namespace软连接 mkdir -p /var/run/netns ln -s /proc/$pid1/ns/net /var/run/netns/$pid1 ip netns ls # 将veth1连接到容器test_busybox1的network namespace，并重命名为eth0 ip link set veth1 netns $pid1 ip netns exec $pid1 ip link set dev veth1 name eth0 # 启用eth0 ip netns exec $pid1 ip link set eth0 up # 分配IP地址和设置网关 ip netns exec $pid1 ip addr add 172.17.0.100/24 dev eth0 ip netns exec $pid1 ip route add default via 172.17.0.1安装包：网桥：veth0启动：PID:Docker State信息.Id: 容器的唯一标识符，通常是一个长字符串，也被称为容器ID。.Name: 容器的名称，通常是用户定义的名称，可以用来引用容器。.State.Status: 容器的状态，如运行中、停止等。.State.Running: 表示容器是否正在运行（布尔值）。.State.Pid: 容器内部主进程的PID。.Config.Image: 使用的容器镜像的名称。.Config.Cmd: 启动容器时使用的命令。.Config.Env: 容器的环境变量。.NetworkSettings.IPAddress: 容器的IP地址（如果有网络配置）。.HostConfig.Binds: 挂载到容器内部的卷或目录。.Mounts: 容器的挂载点信息。.Created: 容器创建的时间戳。.Ports: 容器的端口映射信息。.Labels: 用户定义的容器标签。.LogPath: 容器的日志文件路径。.HostConfig.NetworkMode: 容器的网络模式。netns:执行完之后，可以看到已经分配到网卡：目前这个namespace叫2133然后再命名空间里执行了一些命令。步骤5配置容器test_busybox2的网络# 创建一对虚拟以太网设备veth2和veth3，这两个设备是成对出现的，数据可以在两个设备之间传送 ip link add veth2 type veth peer name veth3 # 将veth2这端加入到docker0桥接器中，这样veth2就能和docker0桥接器上的其他网络设备进行通信了 brctl addif docker0 veth2 # 显示当前桥接器的信息，可以看到docker0桥接器及其所连接的网络接口 brctl show # 启用veth2网络接口，使其能够进行数据传输 ip link set veth2 up # 使用docker命令检查名为test_busybox2的容器，提取容器的进程ID docker inspect test_busybox2 | grep Pid # 用docker inspect命令获取名为test_busybox2的容器的PID，并将其存储在变量pid2中 pid2=$(docker inspect -f '{{.State.Pid}}' test_busybox2) # 输出容器test_busybox2的PID echo "容器test_busybox2的PID是：$pid2" # 为容器的网络命名空间创建一个软链接，方便后续的操作。/var/run/netns/目录通常用于存放网络命名空间 ln -s /proc/$pid2/ns/net /var/run/netns/$pid2 # 将veth3这端的网络接口移到容器test_busybox1的网络命名空间中 ip link set veth3 netns $pid2 # 在test_busybox1容器的网络命名空间内，将网络接口veth3重命名为eth0 ip netns exec $pid2 ip link set dev veth3 name eth0 # 启用容器内的eth0网络接口 ip netns exec $pid2 ip link set eth0 up # 为容器内的eth0接口分配IP地址172.17.0.200，并设置子网掩码为24位 ip netns exec $pid2 ip addr add 172.17.0.200/24 dev eth0 # 设置容器内的网络路由，使其默认网关为172.17.0.1，即docker0桥的IP地址 ip netns exec $pid2 ip route add default via 172.17.0.1 [root@node-a docker]# ip link add veth2 type veth peer name veth3 [root@node-a docker]# brctl addif docker0 veth2 [root@node-a docker]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.0242398240fb no veth0 veth2 veth271d838 veth588fc94 [root@node-a docker]# ip link set veth2 up [root@node-a docker]# docker inspect test_busybox2 | grep Pid "Pid": 2212, "PidMode": "", "PidsLimit": null, [root@node-a docker]# pid2=$(docker inspect -f '{{.State.Pid}}' test_busybox2) [root@node-a docker]# echo "容器test_busybox2的PID是：$pid2" 容器test_busybox2的PID是：2212 [root@node-a docker]# ln -s /proc/$pid2/ns/net /var/run/netns/$pid2 [root@node-a docker]# ip link set veth3 netns $pid2 [root@node-a docker]# ip netns exec $pid2 ip link set dev veth3 name eth0 [root@node-a docker]# ip netns exec $pid2 ip link set eth0 up [root@node-a docker]# ip netns exec $pid2 ip addr add 172.17.0.200/24 dev eth0 [root@node-a docker]# ip netns exec $pid2 ip route add default via 172.17.0.1 [root@node-a docker]# docker exec -it test_busybox2 ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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 12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue qlen 1000 link/ether 92:7f:5d:85:1e:69 brd ff:ff:ff:ff:ff:ff inet 172.17.0.200/24 scope global eth0 valid_lft forever preferred_lft forever [root@node-a docker]#步骤6：测试docker exec -it test_busybox2 ip addr docker exec -it test_busybox2 ping -c 4 172.17.0.100docker exec -it test_busybox1 ip addr docker exec -it test_busybox1 ping -c 4 172.17.0.200
云与虚拟化_实验9 Docker数据卷和数据卷容器应用
云与虚拟化_实验9 Docker数据卷和数据卷容器应用实验要求掌握Docker数据卷和数据卷容器使用方法。前置准备要求实验主机能够连接外网，已经正确安装Docker，并关闭防火墙和selinux。实验步骤步骤1：创建数据卷创建容器，为容器添加一个数据卷，容器名为mycentos，数据卷在容器里的目录是/opt/data。[root@master ~]# docker pull centos:7 // 拉取所需镜像 [root@master ~]# docker run -it --name mycentos -v /opt/data centos:7 /bin/bash [root@7908b98ed9e6 /]# cd /opt/data [root@7908b98ed9e6 data]# echo "centos1">centos1 [root@7908b98ed9e6 data]# echo "centos2">centos2 [root@7908b98ed9e6 data]# ls centos1 centos2新建一个终端，连接到主机，在主机上查看容器对应数据卷的目录路径。[root@master ~]# docker inspect mycentos|grep /var/lib/docker/volumes "Source": "/var/lib/docker/volumes/ceb3146a0edcbe16a525bff78f06ae9169b471459678695db8155a14e629cbb0/_data", [root@master ~]# ls /var/lib/docker/volumes/ceb3146a0edcbe16a525bff78f06ae9169b471459678695db8155a14e629cbb0/_data centos1 centos2 [root@master ~]# echo "123" >> /var/lib/docker/volumes/ceb3146a0edcbe16a525bff78f06ae9169b471459678695db8155a14e629cbb0/_data/123root@dayi-cloud-k8s-master:/home/dayi# docker inspect mycentos|grep /var/lib/docker/volumes "Source": "/var/lib/docker/volumes/bd8e9cc17f888e8ecda307c6040aae00a8b17b7de051ec4064e83477fe26d923/_data", root@dayi-cloud-k8s-master:/home/dayi# 在mycentos容器中再次查看目录信息。[root@6d7542b2ab36 data]# ls 123 centos1 centos2 [root@6d7542b2ab36 data]# cat 123 123 [root@6d7542b2ab36 data]# 步骤2：挂载宿主机文件或目录到容器数据卷建立容器，将容器里挂载的共享数据设置为只读，以使数据修改只能在宿主机上操作。[root@master ~]# docker run -it --name mynginx -v /root/index.html:/myweb/index.html:ro nginx /bin/bash root@3feb79f12fb3:/# cd /myweb/ root@3feb79f12fb3:/myweb# echo "123">index.html //写入数据 bash: index.html: Read-only file system //只读，无法写入噗，成目录了。这样就可以啦：echo " ovo " >>/opt/index.html && docker run -it --name mynginx2 -v /opt/index.html:/myweb/index.html:ro nginx /bin/bash root@registry:/home/dayi# echo " ovo " >>/opt/index.html && docker run -it --name mynginx2 -v /opt/index.html:/myweb/index.html:ro nginx /bin/bash root@2dc277e6ec93:/# cd m media/ mnt/ myweb/ root@2dc277e6ec93:/# cd myweb/ root@2dc277e6ec93:/myweb# ls index.html root@2dc277e6ec93:/myweb# cat index.html ovo ovo root@2dc277e6ec93:/myweb# echo ovo>>index.html bash: index.html: Read-only file system root@2dc277e6ec93:/myweb# 在宿主机上修改共享数据。[root@master ~]# echo "456">>index.html在容器中再次查看文件信息。root@3feb79f12fb3:~# cat /myweb/index.html welcome user nginx 456挂载宿主机目录到容器。[root@master ~]# mkdir /opt/data [root@master ~]# echo "data1">/opt/data/data1 [root@master ~]# echo "data2">/opt/data/data2 [root@master ~]# docker run -it --name mycentos3 -v /opt/data:/opt/mydata centos:7 /bin/bash [root@47ba22a96edd /]# cd /opt/mydata/ [root@47ba22a96edd mydata]# ls data1 data2 [root@47ba22a96edd mydata]# echo "data3">data3 [root@47ba22a96edd mydata]# ls data1 data2 data3在宿主机上查看目录信息。[root@master ~]# ls /opt/data data1 data2 data3OVOroot@registry:/home/dayi# mkdir /opt/data root@registry:/home/dayi# echo "data1">/opt/data/data1 root@registry:/home/dayi# echo "data2">/opt/data/data2 root@registry:/home/dayi# docker run -it --name mycentos3 -v /opt/data:/opt/mydata centos:7 /bin/bash [root@57ad12222cf8 /]# cd /opt/mydata/ [root@57ad12222cf8 mydata]# ls data1 data2 [root@57ad12222cf8 mydata]# echo "data3">data3 [root@57ad12222cf8 mydata]# ls data1 data2 data3 [root@57ad12222cf8 mydata]# exit exit root@registry:/home/dayi# ls /opt/data/ data1 data2 data3 root@registry:/home/dayi# 步骤3：创建数据卷容器启动名为myontainer的容器，要求此容器包含两个数据卷/opt/volume1和/opt/volume2。[root@master ~]# docker run -it -v /opt/volume1 -v /opt/volume2 --name my_container centos:7 /bin/bash [root@0cc99e4c61da /]# ls /opt/ volume1 volume2 // 容器内数据卷目录vloume1和volume2已经创建在宿主机上查看与数据卷对应的目录路径。[root@master ~]# docker inspect my_container|grep /var/lib/docker/volumes "Source": "/var/lib/docker/volumes/3f6cc0585bef55d1be35566aa79dbd21d17a4625b587bc883ab8c4fc159d562d/_data", "Source": "/var/lib/docker/volumes/4e606dc4e1dbe1a907970d59668681169debf4ece91fdf6c7a5f782d2955d05e/_data", // 从显示结果可以看到与数据卷对应的目录路径创建my_centos容器，挂载my_container容器中的数据卷。[root@master ~]# docker run -it --rm --volumes-from my_container --name my_centos centos:7 /bin/bash [root@50fe2adcdfa6 /]# ls /opt volume1 volume2 // 从显示结果上可以看到volume1和volume2数据卷都存在root@debian-crop:/home/dayi/docker# docker run -it -v /opt/volume1 -v /opt/volume2 --name my_container centos:7 /bin/bash Unable to find image 'centos:7' locally 7: Pulling from library/centos 2d473b07cdd5: Pull complete Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 Status: Downloaded newer image for centos:7 [root@003437a20dcd /]# ls /opt volume1 volume2 [root@003437a20dcd /]# root@debian-crop:/home/dayi# docker inspect my_container|grep /var/lib/docker/volumes "Source": "/var/lib/docker/volumes/c806dd7516316241a7ec23312ca49fdfe18026734bb4e2e4dd40ffcaf3112683/_data", "Source": "/var/lib/docker/volumes/4d26d6db345290ac0c6532631dcc2fe069ffc2d761732a30b44c015492d2577c/_data", root@debian-crop:/home/dayi# docker run -it --rm --volumes-from my_container --name my_centos centos:7 /bin/bash [root@4420a54f7387 /]# ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@4420a54f7387 /]# cd /opt [root@4420a54f7387 opt]# ls volume1 volume2 [root@4420a54f7387 opt]#
云与虚拟化_实验8 利用Docker实现容器互连
云与虚拟化_实验8 利用Docker实现容器互连主机名IP地址/子网掩码容器名容器IP地址node-a192.168.59.128/24node1_busybox172.100.0.10node-b192.168.59.130/24node2_busybox172.100.1.101. 需要两个虚拟机克隆一个OVO具体的机子：node-anode-b#修改克隆机子的主机名 vim /etc/hostname reboot查看IP地址ip addr我这里两个机子的IP：192.168.59.128 node-a192.168.59.130 node-b步骤1-4node-a上创建网桥和容器# 创建自定义网桥，并分配网段172.100.0.0/24 docker network create --subnet=172.100.0.0/24 node1-br0 # 查看网桥信息 docker network inspect node1-br0 # 启动一个容器，并设置IP地址为172.100.0.10/24 docker run -itd --net=node1-br0 --ip=172.100.0.10 --name node1_busybox busybox # 查看容器的IP docker exec -it node1_busybox ip addr # 测试容器和网桥是否能通 docker exec -it node1_busybox ping -c 4 172.100.0.1node-b 上创建网桥和容器# 创建自定义网桥，并分配网段172.100.1.0/24 docker network create --subnet=172.100.1.0/24 node2-br0 # 查看网桥信息 docker network inspect node2-br0 # 启动一个容器，并设置IP地址为172.100.1.10/24 docker run -itd --net=node2-br0 --ip=172.100.1.10 --name node2_busybox busybox # 查看容器的IP docker exec -it node2_busybox ip addr # 测试容器和网桥是否能通 docker exec -it node2_busybox ping -c 4 172.100.1.1测试两个容器之间是否可以相通结果应该是不通！node-adocker exec -it node1_busybox ping -c 4 172.100.1.10node-bdocker exec -it node2_busybox ping -c 4 172.100.0.10蚌埠，通了，真蚌埠住了（因为这个公网IP，主机开了tun的网卡，然后就通了）关掉tun网卡：这样是正常的:然后这个通了（但通的是一个美国的机子，这次实验的ip是公网IP，172的IP只有部分（0-31）是公网）：步骤5在node-a节点和node-b节点上配置路由表和iptable规则。把iptables全部清除了之后加了个路由表。node-a#添加路由表 ip route add 172.100.1.0/24 via 192.168.59.130 dev ens33 # 清除iptables规则，并设置默认策略为接受 iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -F iptables -L -nnode-bip route add 172.100.1.0/24 via 192.168.59.128 dev ens33 # 清除iptables规则，并设置默认策略为接受 iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -F iptables -L -n步骤6 再次测试node1节点上的node1-busybox容器与node2节点上的node2-busybox容器与是否可以连通。在node1节点上进行测试。ping -c 4 172.100.1.10 PING 172.100.1.10 (172.100.1.10): 56 data bytes 64 bytes from 172.100.1.10: seq=0 ttl=62 time=12.601 ms通啦
云与虚拟化_虚拟化和容器——6 Docker资源控制
云与虚拟化_虚拟化和容器——6 Docker资源控制基于cgroupshttps://tech.meituan.com/2015/03/31/cgroups.html1. 对CPU的控制步骤1：限制CPU命令速率。（1）利用centos:7镜像分别生成容器名为centos1和centos2的容器，其中centos1容器不限制cpu的使用率，centos2容器将cpu的使用率限制为20%。docker pull centos:7 docker run -dit --name centos1 centos:7 /bin/bash # 限制CPU百分之20000 docker run -dit --name centos2 --cpu-quota 20000 centos:7 /bin/bash100000微秒020000只能使用百分之20--cpu-quota 20000: --cpu-quota定义了容器每--cpu-period（默认为100000微秒，即100ms）可以获得的最大CPU时间，单位为微秒。--cpu-quota 20000意味着在默认的100ms周期中，容器最多只能使用20ms的CPU时间。（2）通过查看对应的cgroup配置文件 /sys/fs/cgroup/cpu/docker/容器编号/cpu.cfs_quota_us来查看各容器cpu的使用率。#这里是你的容器名 cat /sys/fs/cgroup/cpu/docker/86ff33795a71f86236011598c9cbaea55c4a319bd142e6ffb845e055d4db4fd9/cpu.cfs_quota_us没限制的（-1）（3）如需修改对应容器的cpu使用率，可以直接修改cgroup配置文件 /sys/fs/cgroup/cpu/docker/容器编号/cpu.cfs_quota_us的值来实现。修改占用为百分之40echo 40000 >> /sys/fs/cgroup/cpu/docker/9b19d5677dd03ad0f0f4bd3eee7659b7845fb34e78e42e819f21b6338fdc6342/cpu.cfs_quota_us cat /sys/fs/cgroup/cpu/docker/9b19d5677dd03ad0f0f4bd3eee7659b7845fb34e78e42e819f21b6338fdc6342/cpu.cfs_quota_us这里虽然是追加写，但是并不会追加，linux都是文件，估计是直接内核交互了。？删掉这俩实验容器docker rm -f centos1 centos2步骤2：多任务按比例分享CPU(1)利用centos:7镜像创建centos3和centos4容器。设置容器权重，使用centos3和centos4的cpu占比为33.3%和66.7% 。docker run -dit --name=centos3 --cpu-shares 512 centos:7 /bin/bash docker run -dit --name=centos4 --cpu-shares 1024 centos:7 /bin/bashdocker ps -a（2）打开另一个终端，使用docker stats命令查看状态。docker stats（3）分别打开两个终端，利用两个终端分别进行centos1和centos2容器，安装压力测试包stress。（4）分别在两个容器内启用4个线程。容器3#进入容器 docker exec -it centos3 /bin/bash yum -y install epel-release yum -y install stress stress -c 4容器4docker exec -it centos4 /bin/bash yum -y install epel-release yum -y install stress stress -c 4（5）再次利用docker stats命令查看。还是挺准的132/(66+132)=0.666666666667步骤3： 查看cpu内核使用通过查看Cgroup配置文件为/sys/fs/cgroup/cpuset/docker/容器编号/cpuset.cpus。1.CPU限制这个没有文件诶。应该是默认没有启用docker run -dit --cpuset-cpus="0,1" --name my_container nginx 这样就有啦[root@node-a cpuset]# cd docker/8aae2d1e6621d076570cb4e9fc10d099a46475f25add3440848ec1bd243a1f16/ [root@node-a 8aae2d1e6621d076570cb4e9fc10d099a46475f25add3440848ec1bd243a1f16]# cat cpuset.cpus 0-1删除容器docker rm -f my_container2. 对内存使用的限制docker run -dit --name=centos5 -m 512m centos:7 /bin/bash docker run -dit --name=centos6 centos:7 /bin/bash步骤2：打开一个终端，使用docker stats命令查看状态docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 8df1885fca97 centos6 0.00% 400KiB / 1.777GiB 0.02% 656B / 0B 0B / 0B 1 501410e22049 centos5 0.00% 1.965MiB / 512MiB 0.38% 656B / 0B 9.61MB / 0B 1步骤3：查看容器对应的Cgroup配置文件可查看容器内存限制。cat /sys/fs/cgroup/memory/docker/501410e22049929b244235423c19239b50cc61a082834dba85cb18c59e2f7470/memory.limit_in_bytes536870912 // 536870912=512×1024×1024