平时使用的 vagrant 集群配置实践。一个配置文件管理一个集群,嗯,相对于以前想玩一个集群需要搞 三个机器,想想之前,都是泪。这里要区分 vagrant box 配置,和虚拟机配置,3 台集群的虚拟机都是用同一版本 box 镜像。
核心配置说明:

  1. config.vm.define :node1 do |node1|
  2. # 配置主机名
  3. node1.vm.hostname = "node1"
  4. # 配置私有 IP
  5. node1.vm.network "private_network", ip: "192.168.56.121"
  6. # 配置虚拟机提供者配置,我用的 virtualbox
  7. node1.vm.provider "virtualbox" do |v|
  8. # 配置在 virtualbox 中显示名字,不配置默认是 vagrant 自动生成的 id,
  9. # vagrant-globalstatus 可以查看 id
  10. v.name = "node1-121"
  11. # 配置内存大小
  12. v.memory = "1024"
  13. # 配置 CPU 核数
  14. v.cpus = "2"
  15. end
  16. end
  17. config.vm.define :node2 do |node2|
  18. node2.vm.hostname = "node2"
  19. node2.vm.network :private_network, ip: "192.168.56.122"
  20. node2.vm.provider "virtualbox" do |v|
  21. v.name = "node2-122"
  22. v.memory = "1024"
  23. v.cpus = "2"
  24. end
  25. end
  26. config.vm.define :node3 do |node3|
  27. node3.vm.hostname = "node3"
  28. node3.vm.network "private_network", ip: "192.168.56.123"
  29. node3.vm.provider "virtualbox" do |v|
  30. v.name = "node3-123"
  31. v.memory = "1024"
  32. v.cpus = "2"
  33. end
  34. end

配置好之后 vagrant up 启动集群。启动成功后, vagrant ssh node1 即可登录集群服务器 1。
完整详细参考配置如下:

  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. # All Vagrant configuration is done below. The "2" in Vagrant.configure
  4. # configures the configuration version (we support older styles for
  5. # backwards compatibility). Please don't change it unless you know what
  6. # you're doing.
  7. Vagrant.configure("2") do |config|
  8. # The most common configuration options are documented and commented below.
  9. # For a complete reference, please see the online documentation at
  10. # https://docs.vagrantup.com.
  11. # Every Vagrant development environment requires a box. You can search for
  12. # boxes at https://atlas.hashicorp.com/search.
  13. config.vm.box = "mycentos7"
  14. # Disable automatic box update checking. If you disable this, then
  15. # boxes will only be checked for updates when the user runs
  16. # `vagrant box outdated`. This is not recommended.
  17. # config.vm.box_check_update = false
  18. # Create a forwarded port mapping which allows access to a specific port
  19. # within the machine from a port on the host machine. In the example below,
  20. # accessing "localhost:8080" will access port 80 on the guest machine.
  21. # config.vm.network "forwarded_port", guest: 80, host: 8080
  22. # Create a private network, which allows host-only access to the machine
  23. # using a specific IP.
  24. # config.vm.network "private_network", ip: "192.168.56.155"
  25. # Create a public network, which generally matched to bridged network.
  26. # Bridged networks make the machine appear as another physical device on
  27. # your network.
  28. # config.vm.network "public_network"
  29. # Share an additional folder to the guest VM. The first argument is
  30. # the path on the host to the actual folder. The second argument is
  31. # the path on the guest to mount the folder. And the optional third
  32. # argument is a set of non-required options.
  33. # config.vm.synced_folder "../data", "/vagrant_data"
  34. # Provider-specific configuration so you can fine-tune various
  35. # backing providers for Vagrant. These expose provider-specific options.
  36. # Example for VirtualBox:
  37. #
  38. config.vm.define :web do |web|
  39. web.vm.hostname = "web"
  40. web.vm.network "private_network", ip: "192.168.33.10"
  41. config.vm.synced_folder "../php", "/usr/share/nginx/html"
  42. config.vm.synced_folder "nginx-conf", "/etc/nginx/conf.d"
  43. web.vm.network "forwarded_port", guest: 80, host: 80
  44. web.vm.provider "virtualbox" do |v|
  45. v.name = "web"
  46. v.memory = "2048"
  47. v.cpus = "1"
  48. end
  49. end
  50. config.vm.define :db1 do |db1|
  51. db1.vm.hostname = "db1"
  52. db1.vm.network :private_network, ip: "192.168.33.11"
  53. config.vm.synced_folder "../php", "/usr/share/nginx/html"
  54. config.vm.synced_folder "nginx-conf", "/etc/nginx/conf.d"
  55. db1.vm.network "forwarded_port", guest: 8081, host: 8081
  56. db1.vm.provider "virtualbox" do |v|
  57. v.name = "db1"
  58. v.memory = "1024"
  59. v.cpus = "1"
  60. end
  61. end
  62. config.vm.define :db2 do |db2|
  63. db2.vm.hostname = "db2"
  64. db2.vm.network :private_network, ip: "192.168.33.12"
  65. config.vm.synced_folder "../php", "/usr/share/nginx/html"
  66. config.vm.synced_folder "nginx-conf", "/etc/nginx/conf.d"
  67. db2.vm.network "forwarded_port", guest: 8082, host: 8082
  68. db2.vm.provider "virtualbox" do |v|
  69. v.name = "db2"
  70. v.memory = "1024"
  71. v.cpus = "1"
  72. end
  73. end
  74. config.vm.define :db3 do |db3|
  75. db3.vm.hostname = "db3"
  76. db3.vm.network :private_network, ip: "192.168.33.13"
  77. config.vm.synced_folder "../php", "/usr/share/nginx/html"
  78. config.vm.synced_folder "nginx-conf", "/etc/nginx/conf.d"
  79. db3.vm.network "forwarded_port", guest: 8083, host: 8083
  80. db3.vm.provider "virtualbox" do |v|
  81. v.name = "db3"
  82. v.memory = "1024"
  83. v.cpus = "1"
  84. end
  85. end
  86. #
  87. # View the documentation for the provider you are using for more
  88. # information on available options.
  89. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  90. # such as FTP and Heroku are also available. See the documentation at
  91. # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  92. # config.push.define "atlas" do |push|
  93. # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  94. # end
  95. # Enable provisioning with a shell script. Additional provisioners such as
  96. # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  97. # documentation for more information about their specific syntax and use.
  98. # config.vm.provision "shell", inline: <<-SHELL
  99. # apt-get update
  100. # apt-get upgrade
  101. # SHELL
  102. end

参考:https://favoorr.github.io/2017/01/06/vagrant-multiple-servers-cluster-config-best-practices/

分类: web

标签:   vagrant