Virtualbox搭建K8S集群NAT网络+Host-Only 03

2022年3月18日 1164点热度 0人点赞 0条评论

1 配置网络

1 设置全局网络和host-only网络d88a6414775fc1a406829b2bd56aa5b7

 

db8007ad8b864335814a366ab63347102 配置NAT网络+Host-Only

1c515bd898d197130ee4e86f31dde6fe

c97af92a8dd94d1fa17b9a5b660f4233

3 配置网卡地址

从上图看到:

NAT网络的mac:0800277798C9

Host-Only的mac:080027E12706

2c8a0f2d6c0f61e9717030c7fc397708

从上图中看到:

网卡enp0s3 对应的是NAT网络,不配置固定IP。

网卡enp0s8 对应的是Host-Only网络,配置为固定IP。

我们分别这2个网络的IP地址:

设置enp0s3网卡

命令:vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

修改:ONBOOT=yes

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=6e09cc57-b477-444c-815e-5aa38b2baba4
DEVICE=enp0s3
ONBOOT=yes

设置enp0s8网卡

命令:vi /etc/sysconfig/network-scrirpts/ifcfg-enp0s8

修改如下:

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s8
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s8
UUID=df800aa7-41bc-4bd3-be84-cfe772b11375
DEVICE=enp0s8
ONBOOT=yes
IPADDR=192.168.56.101
GETEWAY=192.168.56.1
NETMASK=255.255.255.0
DNS1=192.168.56.1
DNS2=8.8.8.8

配置后的结果:

[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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:77:98:c9 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.4/24 brd 10.0.2.255 scope global noprefixroute dynamic enp0s3
       valid_lft 586sec preferred_lft 586sec
    inet6 fe80::66cb:6cd7:3089:be3c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:e1:27:06 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global noprefixroute enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::e0dc:4bc8:4f66:4057/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

验证网络

1 虚拟机验证

#ping宿主机
[root@localhost ~]# ping 172.18.50.90

PING 172.18.50.90 (172.18.50.90) 56(84) bytes of data.
64 bytes from 172.18.50.90: icmp_seq=1 ttl=63 time=0.862 ms
64 bytes from 172.18.50.90: icmp_seq=2 ttl=63 time=0.593 ms
64 bytes from 172.18.50.90: icmp_seq=3 ttl=63 time=0.627 ms
64 bytes from 172.18.50.90: icmp_seq=4 ttl=63 time=0.573 ms
64 bytes from 172.18.50.90: icmp_seq=5 ttl=63 time=0.630 ms

--- 172.18.50.90 ping statistics ---

5 packets transmitted, 5 received, 0% packet loss, time 4006ms

rtt min/avg/max/mdev = 0.573/0.657/0.862/0.104 ms

#ping外网
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=50 time=20.3 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=50 time=21.8 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=50 time=18.5 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=50 time=17.5 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms

rtt min/avg/max/mdev = 17.519/19.562/21.821/1.663 ms

2 从宿主机器验证

#ping Host-only 网络IP
ping 192.168.56.101
PING 192.168.56.101 (192.168.56.101): 56 data bytes
64 bytes from 192.168.56.101: icmp_seq=0 ttl=64 time=0.326 ms
64 bytes from 192.168.56.101: icmp_seq=1 ttl=64 time=0.512 ms
64 bytes from 192.168.56.101: icmp_seq=2 ttl=64 time=0.686 ms
64 bytes from 192.168.56.101: icmp_seq=3 ttl=64 time=0.548 ms


--- 192.168.56.101 ping statistics ---

4 packets transmitted, 4 packets received, 0.0% packet loss

round-trip min/avg/max/stddev = 0.326/0.518/0.686/0.128 ms

#ping 虚拟机 IP ,这里是ping不通的。
ping 10.0.2.4
PING 10.0.2.4 (10.0.2.4): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2


--- 10.0.2.4 ping statistics ---

4 packets transmitted, 0 packets received, 100.0% packet loss

2 复制虚拟机

复制虚拟机,并且修改IP,

 

25b0d8550fad41f5bd70f8e4e90208c2复制后启动,验证网络是否互通:

#第一台机器
ssh root@192.168.56.101
root@192.168.56.101's password: 
Last login: Fri Mar 18 16:08:33 2022 from 192.168.56.1
[root@localhost ~]# ping 192.168.56.103
PING 192.168.56.103 (192.168.56.103) 56(84) bytes of data.
64 bytes from 192.168.56.103: icmp_seq=1 ttl=64 time=1.02 ms
64 bytes from 192.168.56.103: icmp_seq=2 ttl=64 time=0.536 ms
64 bytes from 192.168.56.103: icmp_seq=4 ttl=64 time=0.763 ms
--- 192.168.56.103 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3004ms

rtt min/avg/max/mdev = 0.536/0.745/1.025/0.183 ms

#NAT网络互相拼接,说明网络是通的。
[root@localhost ~]# ping 10.0.2.6
PING 10.0.2.6 (10.0.2.6) 56(84) bytes of data.
64 bytes from 10.0.2.6: icmp_seq=1 ttl=64 time=1.05 ms
64 bytes from 10.0.2.6: icmp_seq=2 ttl=64 time=0.346 ms
64 bytes from 10.0.2.6: icmp_seq=3 ttl=64 time=0.457 ms

--- 10.0.2.6 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.346/0.618/1.052/0.310 ms

修改后的网络结构如下:

b01ed9c7c1a17f4160b16e23606f7593

初步网络结构搭建完成.

分别设置hostname.

192.168.56.101 k8s-master

192.168.56.102 k8s-node1

192.168.56.103 k8s-node2

并且在各机器写入 /etc/hosts

sudo vi /etc/hosts
192.168.56.101 k8s-master
192.168.56.102 k8s-node1
192.168.56.103 k8s-node2

设置免登录:

#生成密钥
[root@k8s-master ~]# ssh-keygen -t rsa 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Ca10nCMlDI+3/Jb1JKDIkq0XcAmrXQXa7ixN15ak/pg root@k8s-master
The key's randomart image is:
+---[RSA 2048]----+
|  . o+o .        |
|   = =.= .       |
|  + * * O        |
| o O = @ =       |
|. + B B S o .    |
|   B + o o +     |
|  o = . +   .    |
|   o   =         |
|      E .        |
+----[SHA256]-----+
#拷贝
[root@k8s-master ~]# ssh-copy-id -i .ssh/id_rsa.pub k8s-node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host 'k8s-node1 (192.168.56.102)' can't be established.
ECDSA key fingerprint is SHA256:WHxCR14RPL2NpzKGuXCOV2qXXZ2ie80LeRxeVskWI28.
ECDSA key fingerprint is MD5:c2:8c:50:63:94:13:21:d0:d0:95:2e:bd:41:00:70:3f.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@k8s-node1's password: 
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'k8s-node1'"
and check to make sure that only the key(s) you wanted were added.
#拷贝
[root@k8s-master ~]# ssh-copy-id -i .ssh/id_rsa.pub k8s-node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host 'k8s-node2 (192.168.56.103)' can't be established.
ECDSA key fingerprint is SHA256:WHxCR14RPL2NpzKGuXCOV2qXXZ2ie80LeRxeVskWI28.
ECDSA key fingerprint is MD5:c2:8c:50:63:94:13:21:d0:d0:95:2e:bd:41:00:70:3f.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@k8s-node2's password: 
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'k8s-node2'"
and check to make sure that only the key(s) you wanted were added.

 

如果觉得有用,请点击广告作为支持。

管理员

这个人很懒,什么都没留下

文章评论