您好,登錄后才能下訂單哦!
這篇文章主要講解了“macvtap的使用教程”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“macvtap的使用教程”吧!
kubernetes一鍵安裝
macvtap是虛擬機(jī)網(wǎng)絡(luò)虛擬化常用的一種技術(shù),當(dāng)然容器也可以用. MACVTAP 的實(shí)現(xiàn)基于傳統(tǒng)的 MACVLAN. 和 TAP 設(shè)備一樣,每一個(gè) MACVTAP 設(shè)備擁有一個(gè)對(duì)應(yīng)的 Linux 字符設(shè)備,并擁有和 TAP 設(shè)備一樣的 IOCTL 接口,因此能直接被 KVM/Qemu使用,方便地完成網(wǎng)絡(luò)數(shù)據(jù)交換工作. 引入 MACVTAP 設(shè)備的目標(biāo)是:簡(jiǎn)化虛擬化環(huán)境中的交換網(wǎng)絡(luò),代替?zhèn)鹘y(tǒng)的 Linux TAP 設(shè)備加 Bridge 設(shè)備組合,同時(shí)支持新的虛擬化網(wǎng)絡(luò)技術(shù),如 802.1 Qbg.
如kata的虛擬化網(wǎng)絡(luò)就用了這個(gè)技術(shù),以下實(shí)踐完就會(huì)對(duì)kata的網(wǎng)絡(luò)原理比較清楚了,建議對(duì)照教程動(dòng)手實(shí)踐.
我已經(jīng)做好了qemu libvirt的鏡像,大家可以直接使用: 在容器中有非常多的好處,環(huán)境如果亂了可以快速恢復(fù)干凈的環(huán)境。 使用設(shè)備對(duì)也可減少對(duì)宿主機(jī)網(wǎng)絡(luò)的影響。
docker run -d --privileged -v /dev:/dev -v /home/fanux:/root --name qemu-vm fanux/libvirt:latest init
注意:
網(wǎng)絡(luò)等操作需要容器有特權(quán)模式
tap網(wǎng)絡(luò)需要掛載/dev目錄
/home/fanux可以作為工作目錄,鏡像自己編寫的libvirt配置等放在里面防止刪除容器后丟失
由于libvirt需要systemd所以我們?cè)谌萜髦袉?dòng)init進(jìn)程
也可自己構(gòu)建鏡像,我提供了一個(gè)Dockerfile, -j參數(shù)根據(jù)你機(jī)器CPU來(lái)設(shè)置編譯時(shí)的線程數(shù):
FROM centos:7.6.1810 RUN yum install -y wget && wget https://download.qemu.org/qemu-4.0.0.tar.xz && \ tar xvJf qemu-4.0.0.tar.xz \ && yum install -y automake gcc-c++ gcc make autoconf libtool gtk2-devel \ && cd qemu-4.0.0 \ && ./configure \ && make -j 72 && make install \ && yum install -y bridge-utils && yum install -y net-tools tunctl iproute && yum -y install openssh-clients \ cd .. && rm qemu-4.0.0.tar.xz && rm -rf qemu-4.0.0 RUN yum install -y libvirt virt-manager gustfish openssh-clients
進(jìn)入容器
[root@compute84 libvirt]# docker exec -it qemu-vm bash bash-4.2# cd bash-4.2# ls CentOS-7-x86_64-GenericCloud.qcow2 centos.qcow2 image nohup.out start.sh vm3.xml CentOS-7-x86_64-Minimal-1810.iso cloud-init-start.sh kernel qemu vm.xml Fedora-Cloud-Base-30-1.2.x86_64.qcow2 destroy.sh libvirt run.sh vm2.xml
下載虛擬機(jī)鏡像:
openstack已經(jīng)提供很多已經(jīng)裝過(guò)cloud-init的鏡像,地址: https://docs.openstack.org/image-guide/obtain-images.html
我用的一個(gè)比較新的centos的qcow2格式鏡像:
wget http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1905.qcow2
修改虛擬機(jī)root密碼:
virt-customize -a CentOS-7-x86_64-GenericCloud.qcow2 --root-password password:coolpass
查看容器網(wǎng)絡(luò)信息:
bash-4.2# systemctl start libvirtd bash-4.2# ip addr 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: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:c6:59:47 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 3: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000 link/ether 52:54:00:c6:59:47 brd ff:ff:ff:ff:ff:ff 1310: eth0@if1311: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.2/16 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::42:acff:fe11:2/64 scope link valid_lft forever preferred_lft forever
1,2,3是libvirt創(chuàng)建的可以忽略,最主要是eth0
vm3.xml:
<domain type='kvm'> <name>vm3</name> <memory unit='MiB'>2048</memory> <currentMemory unit='MiB'>2048</currentMemory> <os> <type arch='x86_64'>hvm</type> <boot dev='hd'/> </os> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/local/bin/qemu-system-x86_64</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/root/CentOS-7-x86_64-GenericCloud.qcow2'/> <target dev='vda' bus='virtio'/> </disk> <interface type='direct'> <source dev='eth0' mode='bridge' /> <model type='virtio' /> <driver name='vhost' /> </interface> <serial type='pty'> <target port='0'/> </serial> <console type='pty'> <target type='serial' port='0'/> </console> </devices> </domain>
這里配置正確鏡像地址,interface的地方是macvtap相關(guān)的配置。
bash-4.2# virsh define vm3.xml Domain vm3 defined from vm3.xml bash-4.2# virsh start vm3 Domain vm3 started
啟動(dòng)完后就可以看到macvtap設(shè)備被創(chuàng)建出來(lái)了
bash-4.2# ip addr 7: macvtap0@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 500 link/ether 52:54:00:56:e4:20 brd ff:ff:ff:ff:ff:ff inet6 fe80::5054:ff:fe56:e420/64 scope link valid_lft forever preferred_lft forever
進(jìn)入到虛擬機(jī):
virsh console vm3
如果卡在這一步:
A start job is running for LSB: Bri... networking cloud-init[2253]: 2019-06-27 08:37:09,971 - url_helper.py[WARNING]: Calling 'http://192.168.122.1/latest/meta-data/instance-id' failed [87/120s]: request error
等它超時(shí)就好,因?yàn)閙acvtap時(shí)我們需要進(jìn)入虛擬機(jī)去配置網(wǎng)絡(luò)。 然后就可以進(jìn)入虛擬機(jī)了:
CentOS Linux 7 (Core) Kernel 3.10.0-957.1.3.el7.x86_64 on an x86_64 localhost login: root Password: Last login: Thu Jun 27 07:19:32 from gateway
密碼是我們上面設(shè)置的鏡像密碼:coolpass
[root@localhost ~]# ip addr 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:56:e4:20 brd ff:ff:ff:ff:ff:ff inet6 fe80::5054:ff:fe56:e420/64 scope link valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 172.17.0.2/16 dev eth0 [root@localhost ~]# ip route add default via 172.17.0.1 dev eth0 [root@localhost ~]# ip addr 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:56:e4:20 brd ff:ff:ff:ff:ff:ff inet 172.17.0.2/16 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe56:e420/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]# ip route default via 172.17.0.1 dev eth0 172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2 [root@localhost ~]# ping 172.17.0.1 PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data. 64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.622 ms 64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.194 ms
配置完后就可以ping通網(wǎng)關(guān)了。
這個(gè)不改可能會(huì)導(dǎo)致ssh時(shí)非常慢:
[root@localhost ~]# cat /etc/resolv.conf ; Created by cloud-init on instance boot automatically, do not edit. ; ; generated by /usr/sbin/dhclient-script nameserver 114.114.114.114
修改/etc/ssh/sshd-config文件,將其中的PermitRootLogin no修改為yes,PubkeyAuthentication yes修改為no,AuthorizedKeysFile .ssh/authorized_keys前面加上#屏蔽掉,PasswordAuthentication no修改為yes就可以了。
docker run --rm -it fanux/libvirt bash [root@ee18547e9ed2 /]# ssh root@172.17.0.2 ssh: connect to host 172.17.0.2 port 22: Connection refused
會(huì)發(fā)現(xiàn)不通, 這是因?yàn)槿萜骼锏膃th0和虛擬機(jī)里的eth0都配置了相同的地址導(dǎo)致,只需要把容器里的eth0地址刪掉即可:
bash-4.2# ip addr del 172.17.0.2/16 dev eth0
再次ssh即可進(jìn)入虛擬機(jī):
[root@ee18547e9ed2 /]# ssh root@172.17.0.2 The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established. ECDSA key fingerprint is SHA256:kTk3yy8588WQHNtwpzS+h7u0W3RELWC8hJQwIwLOkdc. ECDSA key fingerprint is MD5:0c:f3:b5:69:c6:08:05:14:f8:da:42:2f:85:29:51:d0. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.17.0.2' (ECDSA) to the list of known hosts. root@172.17.0.2's password: Last login: Thu Jun 27 08:38:00 2019 [root@localhost ~]# ip addr 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:56:e4:20 brd ff:ff:ff:ff:ff:ff inet 172.17.0.2/16 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe56:e420/64 scope link valid_lft forever preferred_lft forever
[root@localhost ~]# ip link set eth0 address 52:54:00:56:e4:23
會(huì)發(fā)現(xiàn)就連不上虛擬機(jī)了
改回:
[root@localhost ~]# ip link set eth0 address 52:54:00:56:e4:20
又可正常連接了,為啥?
這是因?yàn)樘摂M機(jī)的eth0的mac地址是必須與macvtap0的mac地址保持一樣,原理很簡(jiǎn)單
ARP時(shí)問IP地址是172.17.0.2的機(jī)器mac地址是什么
虛擬機(jī)回了一個(gè)52:54:00:56:e4:20
macvtap0是可以理解成掛在網(wǎng)橋端口上的,這樣就把包發(fā)給macvtap0了(因?yàn)閙ac地址一樣,不一樣就不會(huì)發(fā)給macvtap了)
macvtap0就把包丟給qemu應(yīng)用進(jìn)程(最終到虛擬機(jī)eth0)
以上是通過(guò)libvirt進(jìn)行使用的,這樣屏蔽了很多底層的細(xì)節(jié),如果是直接使用qemu命令需要如下操作:
創(chuàng)建macvtap設(shè)備:
ip link add link eth0 name macvtap0 type macvtap mode bridge ip link set macvtap0 address 1a:46:0b:ca:bc:7b up bash-4.2# cat /sys/class/net/macvtap0/ifindex # 對(duì)應(yīng)下面命令的/dev/tap2 2 bash-4.2# cat /sys/class/net/macvtap0/address # 與qemu mac地址配置一致 1a:46:0b:ca:bc:7b
啟動(dòng)qemu,然后虛擬機(jī)里面的地址配置同libvirt,可以通過(guò)vnc客戶端(vnc viewer)進(jìn)入虛擬機(jī)配置,不在贅述:
bash-4.2# qemu-system-x86_64 -enable-kvm /root/CentOS-7-x86_64-GenericCloud.qcow2\ -netdev tap,fd=30,id=hostnet0,vhost=on,vhostfd=4 30<>/dev/tap2 4<>/dev/vhost-net \ -device virtio-net-pci,netdev=hostnet0,id=net0,mac=1a:46:0b:ca:bc:7b \ -monitor telnet:127.0.0.1:5801,server,nowait VNC server running on ::1:5900
上文提到,通過(guò)macvtap技術(shù)配置虛擬機(jī)網(wǎng)卡地址是需要進(jìn)入虛擬機(jī)配置的, 然而我們實(shí)現(xiàn)一個(gè)虛擬機(jī)管理系統(tǒng)時(shí)顯然不會(huì)手動(dòng)進(jìn)入去配置,這就需要cloud-init了
它可以幫助我們?cè)谔摂M機(jī)啟動(dòng)時(shí)配置虛擬機(jī),如配置密碼,配置網(wǎng)絡(luò),執(zhí)行命令和寫一些文件等。 先創(chuàng)建一個(gè)user-data文件,里面內(nèi)容如下:
#cloud-config write_files: - content: | DEVICE=eth0 ONBOOT=yes TYPE=Ethernet USERCTL=no IPADDR=172.17.0.2 NETMASK=255.255.0.0 GATEWAY=172.17.0.1 BOOTPROTO=static DNS1=172.17.0.1 ONBOOT=yes path: /etc/sysconfig/network-scripts/ifcfg-eth0 runcmd: - systemctl start network groups: - centos: [root] - cloud-users ssh_pwauth: yes chpasswd: expire: false list: | user1:222222 root:333333
創(chuàng)建cloud-init鏡像文件
yum install -y cloud-utils cloud-localds my-seed.img user-data
libvirt中使用該鏡像
<disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/root/my-seed.img'/> <target dev='vdb' bus='virtio'/> </disk>
為了防止cloud-init走網(wǎng)絡(luò)獲取metadata,因?yàn)榫W(wǎng)卡沒設(shè)置好所以會(huì)卡住五分鐘,我們直接把網(wǎng)絡(luò)獲取metadata禁止掉:
vi /etc/cloud/cloud.cfg.d/05_logging.cfg
加上:
network: config: disabled
然后啟動(dòng)虛擬機(jī)即可
Inappropriate ioctl for device
qemu-system-x86_64: -net tap,fd=5: TUNGETIFF ioctl() failed: Inappropriate ioctl for device TUNSETOFFLOAD ioctl() failed: Inappropriate ioctl for device
因?yàn)槿萜鳑]有掛載/dev目錄
KVM bios被禁
[root@helix105 ~]# docker run busybox uname -a Could not access KVM kernel module: No such file or directory qemu-lite-system-x86_64: failed to initialize KVM: No such file or directory /usr/bin/docker-current: Error response from daemon: oci runtime error: Unable to launch /usr/bin/qemu-lite-system-x86_64: exit status 1. ERRO[0001] error getting events from daemon: net/http: request canceled [root@helix105 ~]# lsmod |grep kvm kvm 598016 0 irqbypass 16384 1 kvm [root@helix105 ~]# modprobe kvm-intel modprobe: ERROR: could not insert 'kvm_intel': Operation not supported You have mail in /var/spool/mail/root [root@helix105 ~]# dmesg |grep kvm [ 8.239309] kvm: disabled by bios
這個(gè)要進(jìn)bios打開
KVM: Permission denied
bash-4.2# virsh start vm_name1 error: Failed to start domain vm_name1 error: internal error: qemu unexpectedly closed the monitor: Could not access KVM kernel module: Permission denied 2019-06-20T07:26:33.304320Z qemu-system-x86_64: failed to initialize KVM: Permission denied
解決辦法:
#chown root:kvm /dev/kvm 修改/etc/libvirt/qemu.conf, #user="root" user="root" #group="root" group="root" 重啟服務(wù) #service libvirtd restart,問題解決了
感謝各位的閱讀,以上就是“macvtap的使用教程”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)macvtap的使用教程這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。