代码改变世界

4 - Configure network connectivity for Virtual machine

2021-06-05 08:56  WayneWei  阅读(75)  评论(0编辑  收藏  举报

Prerequisites:

  • CentOS7 virtual machine on Hyper-V

Now we have a working CentOS7 virtual machine on Hyper-V. Let's configure the network for it, then we can access CentOS from host system (Windows 10) with xshell or MobaXterm, and enable CentOS access to Internet.

 

Setup Internal network:

  1. Sharing Physical network switch with Vitual switch. 
    Windows Home Button -> Control Panel -> Network and sharing center -> Change adapter settings
    Right click on your Physical network switch which can access to Internet, then select on tab Sharing, share the connection with Hyper-V Default virtual switch
     

  2. Configure Hyper-V default switch
    Right click on vEthernet (Default Switch) -> Internet protocal v4(TCP/IPv4) -> Set IP and Subnet mask as below
  3. Configure k8s-master network adapter
    Go back to Hyper-V - > right click on k8s-master -> settings -> Network adapter -> Virtual switch select to Defalut switch
  4. Configure CentOS network adapter
    Start and connect to k8s-master -> enter command: vi /etc/sysconfig/network-scripts/ifcfg-eth0 -> update fields as below

    To summarize the update, below fields will be updated or introduced:

            BOOTPROTO=static
            ONBOOT=yes
            IPADDR=192.168.137.161
            NETMASK=255.255.255.0
            NM_CONTROLLED=no
            GATEWAY=192.168.137.1
            DNS1=192.168.137.1

  5. Restart k8s-master (Here restart network service may not take effect for the change with: systemctl restart network)
  6. After restart k8s-master, check ip details. You will find the static IP is take effected on adapter eth0
    ip addr
  7. Try to access internet from k8s-master, and good it's working as expected

     

  8. Go back to host Windows 10, try ping to k8s-master, working as well.

     

  9. Now try remote to k8s-master from Host with xshell or MobaXterm. And able to remote no issues.
  10. Now we have a virtual machine which can access from host, and it can access to Internet !
    Remember to apply the same for another machine k8s-node1 from step 3 to step 9
  11. Trouble shooting IMPORTANT !
    Once the network setup correctly at first place, and network working as expected ! However based on my experience, after some network change or HOST machine restart or shutdown, it will cause VM not able to connect internet ! Even you try to restart HOST machine or restart VM, but it's won't fix it. And you verify IP tables on Win 10 Host by running command: route print, you even can see the vEthernet IP 192.168.137.1 onlink without any issue, on the other hand your VM network setting also correctly. What we should do to get this fixed ? Well, easy:
    From your Win 10 Host, go to Control Panel -> Change adapter settings (as step 1), disable the sharing, then enable the sharing again !