编写Vagrantfile

创建3台4核4G虚拟机,IP从192.168.56.57开始

Vagrant.configure("2") do |config|
	(1..3).each do |i|
		config.vm.define "k8s-node#{i}" do |node|
			#设置虚拟机box
			node.vm.box ="centos/7"
			#设置虚拟机主机名
			node.vm.hostname="k8s-node#{i}"
			#设置虚拟IP
			node.vm.network "private_network", ip: "192.168.56.#{56+i}",netmask:"255.255.255.0"
			
			# VirtaulBox相关配置
			node.vm.provider "virtualbox" do |v|
				#设置虚拟的名称
				v.name="k8s-node#{i}"
				#设置虚拟的内存大小
				v.memory=4096
				#设置虚拟CPU个数
				v.cpus=4
			end
		end
	end
end


  • 放在D:\VirtualBox\k8s
    在这里插入图片描述

开始批量创建虚拟机

  • vagrant up 开始创建
D:\VirtualBox\k8s>vagrant up

在这里插入图片描述

  • 创建中
    在这里插入图片描述
  • 创建完成后效果
    在这里插入图片描述

开启root 账号密码访问

  • root 密码默认为vagrant

k8s-node1 到k8s-node3 依次操作

C:\Users\herion>vagrant ssh k8s-node1
[vagrant@k8s-node1 ~]$ su root
Password:
[root@k8s-node1 vagrant]# vi /etc/ssh/sshd_config
[root@k8s-node1 vagrant]# systemctl restart sshd
[vagrant@k8s-node1 ~]$ exit
logout Connection to 127.0.0.1 closed.
C:\Users\herion>vagrant ssh k8s-node2
Last login: Sun Jun 20 15:59:56 2021 from 10.0.2.2
[vagrant@k8s-node2 ~]$ su root
Password:
……
  • vi /etc/ssh/sshd_config 中下面配置改为yes
PasswordAuthentication yes

在这里插入图片描述

  • 重启sshd 服务
systemctl restart sshd

网络设置

需要设置网络原因

3台虚拟机默认网卡都是eth0 ,并且IP都是10.0.2.15

[root@k8s-node1 ~]# ip route show
default via 10.0.2.2 dev eth0 proto dhcp metric 100 
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100 
192.168.56.0/24 dev eth1 proto kernel scope link src 192.168.56.57 metric 101 
[root@k8s-node1 ~]# 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:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 84240sec preferred_lft 84240sec
    inet6 fe80::5054:ff:fe4d:77d3/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:d5:2f:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.57/24 brd 192.168.56.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fed5:2fb5/64 scope link 
       valid_lft forever preferred_lft forever
[root@k8s-node2 ~]#  ip route show
default via 10.0.2.2 dev eth0 proto dhcp metric 100 
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100 
192.168.56.0/24 dev eth1 proto kernel scope link src 192.168.56.58 metric 101 
[root@k8s-node2 ~]# 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:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 84293sec preferred_lft 84293sec
    inet6 fe80::5054:ff:fe4d:77d3/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:5b:17:0a brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.58/24 brd 192.168.56.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe5b:170a/64 scope link 
       valid_lft forever preferred_lft forever

  • 关闭虚拟机
    在这里插入图片描述
  • 管理->全局设定->网络 添加NatNetwork
    在这里插入图片描述
  • 选择虚拟机右键->设置->网络->连接方式->高级

注意此处需要刷新MAC地址,3台虚拟机都需要配置

在这里插入图片描述

关闭防火墙

#临时关闭防火墙
systemctl stop firewalld
#防火墙开机不启动
systemctl disable firewalld

关闭SELINUX

#全局关闭
sed  -i  s#SELINUX=enforcing#SELINUX=disabled#  
# 关闭当前会话
setenforce   0
[root@k8s-node1 ~]# sed  -i  s#SELINUX=enforcing#SELINUX=disabled#   /etc/selinux/config   
[root@k8s-node1 ~]# cat  /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@k8s-node1 ~]#  setenforce   0

关闭swap

#永久关闭swap分区
sed -ri 's/.*swap.*/#&/' /etc/fstab  
临时关闭swap分区,当前会话生效,重启失效
swapoff  -a
[root@k8s-node1 ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab  
[root@k8s-node1 ~]# swapoff  -a

配置host

  • vi /etc/hosts
    在这里插入图片描述

配置内核参数,将桥接的IPv4流量传递到iptables的链

cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐