vagrant管理虚拟机集群:脚本化管理
1, vagrant网络配置类型配置方法dhcpconfig.vm.network “private_network”, type: “dhcp”staticconfig.vm.network “private_network”, ip: “192.168.x.x”bridgeconfig.vm.network “public_network”, bridg......
·
vagrant官方文档:https://www.vagrantup.com/intro/getting-started/
vagrant安装(win, tar包,rpm, deb):https://www.vagrantup.com/downloads.html
vagrant 的box 镜像下载官网:
(国内源) https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/
(下载Vagrantfile文件)https://app.vagrantup.com/boxes/search,
(下载.box文件)http://www.vagrantbox.es/
文章目录
1, 创建单个虚拟机
a, 使用默认配置
- vagrant init; vagrant up; vagrant ssh
#方式1, 离线下载镜像,手动导入box
$ vagrant.exe box add su12 .\opensuse-12.2-64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'su12' (v0) for provider:
box: Unpacking necessary files from: file://D:/VirtualBox%20VMs/opensuse-12.2-64.box
box:
==> box: Successfully added box 'su12' (v0) for 'virtualbox'!
#方式2, 自动拉取镜像box
[root@test ~]# vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
Enter your choice: 3
==> box: Adding box 'centos/7' (v1905.1) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
box: Download redirected to host: cloud.centos.org
[root@test ~] vagrant init ; vagrant up; vagrant ssh
## init命令: 会创建一个Vagrantfile文件,里面有基本配置,包括主机名,内存等
## up 命令 : 会通过Vagrantfile文件的配置,在virtualbox上创建一个虚拟机
## ssh 命令: 会免密码登录到虚拟机里面
b, 自定义虚拟机配置: ip,hostname
vagrant网络配置
类型 | 配置方法 |
---|---|
dhcp | config.vm.network “private_network”, type: “dhcp” |
static | config.vm.network “private_network”, ip: “192.168.x.x” |
bridge | config.vm.network “public_network”, bridge: “en1” |
注: 默认情况下,vagrant会给每个虚拟机添加一个NAT网卡,只有在自定义网卡超过两块时才不会默认添加
#1, 查看主机的网卡:(设置虚拟机桥接到某网卡上,使其ip和该网卡同一网段)
####1, 默认配置下: 只有NAT网卡,vbox分配的ip是:10.0.20.15
wang@wang-pc:~/vagrant/u14$ ip addr |grep "^[0-9]" -A 2
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 1c:6f:65:28:7c:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.1.111/24 brd 192.168.1.255 scope global dynamic enp2s0
5: vboxnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 0a:00:27:00:00:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.1/24 brd 192.168.56.255 scope global vboxnet0
#### 所以需要添加需要的网卡ip: hostonly网卡段(私网), bridge网段(公网)
wang@wang-pc:~/vagrant/u14$ cat Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box= "u14"
config.vm.hostname= "ubuntu14"
config.vm.network "private_network", ip: "192.168.56.150"
config.vm.network "public_network",bridge: "enp2s0"
config.vm.provider "virtualbox" do | m|
m.memory=3072
m.cpus=3
end
end
####2, 重新加载配置
wang@wang-pc:~/vagrant/u14$ vagrant reload
==> default: Attempting graceful shutdown of VM...
....
####3, 登陆虚拟机,查看ip
wang@wang-pc:~/vagrant/u14$ vagrant ssh
vagrant@ubuntu14:~$ ip addr |grep "^[0-9]" -A 2
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
--
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:5f:bb:e6 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
--
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:7a:47:51 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.150/24 brd 192.168.56.255 scope global eth1
--
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:06:de:cb brd ff:ff:ff:ff:ff:ff
inet 192.168.1.103/24 brd 192.168.1.255 scope global eth2
2, 创建多个虚拟机
通过配置Vagrantfile,来创建多个虚拟机: vagrant up 启动多个虚拟机
Vagrant.configure("2") do |config|
config.vm.box = "c6"
config.vm.provision "shell", inline: "echo ok!========"
config.vm.define "node1" do |node1|
node1.vm.hostname = "cdh-node1"
node1.vm.network "private_network", ip: "192.168.56.161"
node1.vm.network "public_network",bridge:"wlp0s20f3"
#执行脚本,放在vagrantfile同目录下, node1.vm.provision "shell",path:"/a/b.sh"
node1.vm.provision "shell", inline: "echo Hello,I am node1 !!!"
node1.vm.provider "virtualbox" do |m|
m.memory = 5024
m.cpus = 2
#m.name="cdh-node1"
end
end
config.vm.define "node2" do |node2|
node2.vm.hostname = "cdh-node2"
node2.vm.network "private_network", ip: "192.168.56.162"
node2.vm.network "public_network",bridge:"wlp0s20f3"
node2.vm.provider "virtualbox" do |m|
m.memory = 2048
m.cpus = 1
#m.name="cdh-node2"
end
end
end
a, 查看虚拟机状态
wang@wang-T58-V:~/vagrant/cdhs-c6$ vagrant status
Current machine states:
node1 running (virtualbox)
node2 running (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
- 在virtualbox中:查看创建的虚拟机
b, 登录虚拟机
查看网络连接 ( vagrant ssh 节点名)
wang@wang-T58-V:~/vagrant/cdhs-c6$ vagrant ssh node1
Last login: Sun Jan 19 06:32:09 2020 from 10.0.2.2
[vagrant@cdh-node1 ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
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 qlen 1000
link/ether 52:54:00:e0:48:c2 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
inet6 fe80::5054:ff:fee0:48c2/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:d9:3d:d9 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.161/24 brd 192.168.56.255 scope global eth1
inet6 fe80::a00:27ff:fed9:3dd9/64 scope link
valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:48:8a:91 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.69/24 brd 192.168.1.255 scope global eth2
inet6 fe80::a00:27ff:fe48:8a91/64 scope link
valid_lft forever preferred_lft forever
c, 上传文件
wang@wang-T58-V:~/vagrant/cdhs-c6$ vagrant upload Vagrantfile
This command requires a specific VM name to target in a multi-VM environment.
wang@wang-T58-V:~/vagrant/cdhs-c6$ vagrant upload Vagrantfile node1
Uploading Vagrantfile to Vagrantfile
Upload has completed successfully!
Source: Vagrantfile
Destination: Vagrantfile
wang@wang-T58-V:~/vagrant/cdhs-c6$ vagrant ssh
This command requires a specific VM name to target in a multi-VM environment.
wang@wang-T58-V:~/vagrant/cdhs-c6$ ls
Vagrantfile
wang@wang-T58-V:~/vagrant/cdhs-c6$ vagrant ssh node1
Last login: Sun Jan 19 06:37:05 2020 from 10.0.2.2
[vagrant@cdh-node1 ~]$ ls
Vagrantfile
[vagrant@cdh-node1 ~]$
3,导入导出box
a, 查看下载的box
- vagrant box list
### 默认的环境变量: VAGRANT_HOME=~/.vagrant.d/, 里面保存vagrant相关的数据(要想修改vagrant的工作目录,改变这个环境变量即可)
[root@test ubuntu16_3]# ls ~/.vagrant.d/
boxes/ gems/ rgloader/ tmp/
data/ insecure_private_key setup_version
### 如果使用命令导入了一个名为u16的box, 则可以看到有如下文件
[root@test ubuntu16_3]# ls ~/.vagrant.d/boxes/u16/0/virtualbox/
box-disk001.vmdk box-disk002.vmdk box.ovf metadata.json Vagrantfile
[root@test ubuntu16_3]# cat /home/vagrant/boxes/u2/0/virtualbox/Vagrantfile
Vagrant::Config.run do |config|
# This Vagrantfile is auto-generated by `vagrant package` to contain
# the MAC address of the box. Custom configuration should be placed in
# the actual `Vagrantfile` in this box.
config.vm.base_mac = "02C3B4E73126"
end
# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)
b, 导入导出box
#1,查看运行的虚拟机
[root@test ubuntu16_4]# VBoxManage list runningvms
"ubuntu16_4_default_1565175806323_25314" {30ef1adb-e85d-4c5a-85f2-7e90e8ed721e}
#2,追加vagrant的原始ssh私钥: 避免因为私钥的变化而使得导入该box时出现ssh 登录不了
vagrant ssh
cat >> ~/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
EOF
#3,导出虚拟机 (会强制关闭虚拟机): 在当前目录生成一个.box文件
vagrant package --base ubuntu16_4_default_1565175806323_25314 --output my_ubuntu16.box
#4,在目标端导入此.box文件: 使用vagrant box list查看结果
vagrant box add 自定义的name my_ubuntu16.box
4, vagrant虚拟机:解决密钥丢失,不能免密登录
#1, 查看原有的密钥信息
[vagrant@c7 ~]$ cat .ssh/authorized_keys.bak
ssh-rsa AAAAB3NzaC1yc2EAAAADAQA...N4ET9dL8Wl3R vagrant
#2,假定删除了该密钥,退出重新登录:遇到不能免密登录的问题
[vagrant@c7 ~]$ mv ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak
[vagrant@c7 ~]$ logout
Connection to 127.0.0.1 closed.
D:\software\Vagrant\virtual-machines\c7
λ vagrant.exe ssh
vagrant@127.0.0.1's password:
#3, 解决办法: 把磁盘里面的私钥文件,拷贝到另一台正常的linux服务器,使用命令查看里面的公钥信息
D:\software\Vagrant\virtual-machines\test-c72
λ vagrant.exe upload ..\c7\.vagrant\machines\default\virtualbox\private_key
Uploading ..\c7\.vagrant\machines\default\virtualbox\private_key to private_key
Upload has completed successfully!
Source: ..\c7\.vagrant\machines\default\virtualbox\private_key
Destination: private_key
λ vagrant.exe ssh
[vagrant@test-c72 ~]$ sudo su
[root@test-c72 vagrant]# ls
private_key
[root@test-c72 vagrant]# ssh-agent bash
[root@test-c72 vagrant]# ssh-add private_key
Identity added: private_key (private_key)
[root@test-c72 vagrant]# ssh-add -L
ssh-rsa AAAAB3NzaC1yc2EAAAADAQA...N4ET9dL8Wl3R private_key
更多推荐
已为社区贡献8条内容
所有评论(0)