vagrant+virtual box 安装 centos7 集群


====== 采用vagrant+virtual box ====== 单机
1、安装Vagrant
  访问Vagrant官网:【https://www.vagrantup.com/】,下载对应版本
  
2、下载安装virtual box
  访问VirtualBox官网:【https://www.virtualbox.org/】,下载对应版本
  
2、将 virtualbox.box 文件添加到vagrant管理的镜像中
  1)、下载网盘中的镜像文件 virtualbox.box 文件,在官网可以下载
  2)、保存到磁盘的某个目录,比如【C:\SoftInstalled\Vagrant\virtualbox.box】
  3)、添加镜像并起名叫 centos/7,执行命令:【vagrant box add centos/7 C:\SoftInstalled\Vagrant\virtualbox.box】
  4)、vagrant box list 查看本地的box[这时候可以看到centos/7]
  
3、安装centos7:
  1)、创建centos7文件夹,并进入其中[目录全路径不要有中文字符]
  2)、在此目录下打开cmd,运行 【vagrant init centos/7】
  3)、此时会在当前目录下生成 Vagrantfile,进入Vagrantfile文件,修改信息,如下:
    config.vm.box = "centos/7"
    config.vm.network "public_network"
    config.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
      vb.name= "tiger-centos7"
      vb.cpus= 2
    
4、centos/7镜像有了,根据Vagrantfile文件启动创建虚拟机,来到centos7文件夹,在此目录打开cmd窗口,执行【vagrant up】[打开virtual box观察,可以发现centos7创建成功]

至此,使用vagrant+virtualbox搭建centos7完成,后面可以修改Vagrantfile对虚拟机进行相应配置`

5、以后大家操作虚拟机,还是要在centos文件夹打开cmd窗口操作
    vagrant halt   优雅关闭
    vagrant up     正常启动
  
6、vagrant常用命令
  1)、vagrant ssh        进入刚才创建的centos7中
  2)、vagrant status     查看centos7的状态
  3)、vagrant halt       停止/关闭centos7
  4)、vagrant destroy     删除centos7
  5)、vagrant status    查看当前vagrant创建的虚拟机
  6)、Vagrantfile中也可以写脚本命令,使得centos7更加丰富
  但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload
  
7、查看虚拟机网卡:【ip a】192.168.3.11/24 ,本地网络需要通:验证是否能通,查看本地网络地址:【ipconfig】,拼网络【ping 192.168.3.11】

====== 通过Xshell连接centos7 ======

01 使用centos7的默认账号连接
    在centos文件夹下执行vagrant ssh-config
    关注:Hostname  Port  IdentityFile
    IP:127.0.0.1
    port:2222
    用户名:vagrant
    密码:vagrant
    文件:Identityfile指向的文件private-key
    
02 使用root账户登录
    vagrant ssh   进入到虚拟机中
    sudo -i
    vi /etc/ssh/sshd_config
    修改PasswordAuthentication yes
    passwd修改密码,比如abc123
    systemctl restart sshd
    使用账号root,密码abc123进行登录
    
    
====== 搭建多机 ======
1、只需要修改 Vagrantfile 文件如下
boxes = [
    {
        :name => "manager-node",
        :eth1 => "192.168.3.11",
        :mem => "1024",
        :cpu => "1"
    },
    {
        :name => "worker01-node",
        :eth1 => "192.168.3.12",
        :mem => "1024",
        :cpu => "1"
    },
    {
        :name => "worker02-node",
        :eth1 => "192.168.3.13",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "centos/7"
  
   boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]
        config.vm.provider "vmware_fusion" do |v|
          v.vmx["memsize"] = opts[:mem]
          v.vmx["numvcpus"] = opts[:cpu]
        end

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
          v.customize ["modifyvm", :id, "--name", opts[:name]]
        end

        config.vm.network :public_network, ip: opts[:eth1]
      end
  end

end

猜你喜欢

转载自blog.csdn.net/qq_36336332/article/details/103534746
今日推荐