Vagrant安装Centos7虚拟机

1.安装Vagrant

https://developer.hashicorp.com/vagrant/downloads
命令行输入vagrant,如下图显示,说明安装成功

Usage: vagrant [options] <command> [<args>]

    -h, --help                       Print this help.

Common commands:
     autocomplete    manages autocomplete installation on host
     box             manages boxes: installation, removal, etc.
     cloud           manages everything related to Vagrant Cloud
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     upload          upload to machine via communicator
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version
     winrm           executes commands on a machine via WinRM
     winrm-config    outputs WinRM configuration to connect to the machine

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.
        --[no-]color                 Enable or disable color output
        --machine-readable           Enable machine readable output
    -v, --version                    Display Vagrant version
        --debug                      Enable debug output
        --timestamp                  Enable timestamps on log output
        --debug-timestamp            Enable debug output with timestamps
        --no-tty                     Enable non-interactive output
2.下载box镜像

https://app.vagrantup.com/centos/boxes/7
https://cloud.centos.org/centos/7/vagrant/x86_64/images/
选择:CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box下载

3.初始化
# 查询vagrant 已经管理的 Box
vagrant box list
# 添加下载的box给vagrant管理
vagrant box add box的文件路径及文件名 --name boxname
# 加上boxname 表示使用哪个box 创建虚拟机,并生成Vagrantfile配置文件
vagrant init [boxname]

Vagrantfile 是Ruby 语法的文件

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  # # 必填的配置项
  config.vm.box = "centos7"
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # 配置端口转发,把虚机的8080,映射到宿主机的端口上。这样就能在宿主机上访问到虚拟机中的服务
  # 127.0.0.1:8080 => 虚拟机的 80端口
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
  
  # 配置3台虚拟机
  # 私有网络= HostOnly 网络
  config.vm.define "k8s-master" do |vb|
	config.vm.provider "virtualbox" do |v|
      v.memory = 2048
      v.cpus = 2
      v.name = "k8s-master" # 虚拟机名称 
    end
  vb.vm.host_name = "k8s-master"
  vb.vm.box = "centos7"
  vb.vm.network "private_network", ip: "192.168.56.101"
  end
  
  config.vm.define "k8s-node1" do |vb|
	config.vm.provider "virtualbox" do |v|
      v.memory = 2048
      v.cpus = 2
      v.name = "k8s-node1" # 虚拟机名称
    end
  vb.vm.host_name = "k8s-node1"
  vb.vm.box = "centos7"
  vb.vm.network "private_network", ip: "192.168.56.102"
  end
  
  config.vm.define "k8s-node2" do |vb|
	config.vm.provider "virtualbox" do |v|
      v.memory = 2048
      v.cpus = 2
      v.name = "k8s-node2" # 虚拟机名称
    end
  vb.vm.host_name = "k8s-node2"
  vb.vm.box = "centos7"
  vb.vm.network "private_network", ip: "192.168.56.103"
  end

end

3.1 启动
# 启动虚拟机,vagrant需要在 Vagrantfile 所在的目录下执行:
 vagrant up
#查看虚拟机状态,running 表示虚拟机启动成功
 vagrant status
4.连接

默认的登录用户名和密码:
root : vagrant
vagrant : vagrant

vagrant ssh
5.开启密码登录
vi /etc/ssh/sshd_config
# 将PasswordAuthentication 改为 yes
# 重启sshd
systemctl restart sshd
6.Vagrant相关命令

# 删除box
vagrant box remove boxname
# 停止虚拟机
vagrant halt
# 暂停虚拟机
vagrant suspend
# 恢复虚拟机
vagrant resume
# 删除虚拟机
vagrant destroy
# 重建虚拟机
vagrant reload

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值