使用Vagrant部署集群

一、运行多个虚拟机

我们通过配置Vagrantfile配置两个虚拟机——web服务器和数据库服务器。

Vagrant::configure("2") do |config|
config.vm.box = "precise64" config.vm.define "web" do |web|
web.vm.network "forwarded_port", guest:80,host:8080
web.vm.provision :shell,path: "provision.sh"
end config.vm.define "db" do |db| end
end

二、管理多个虚拟机

Most commands, such as up, destroy, and reload now take an argument with the name of the machine to affect. As an example, if you wanted to reload just the web machine, you could do this:

$vagrant reload web

By specifying no arguments, Vagrant assumes you want to take action on every machine. So vagrant reload alone would reload both the web and db machines.

For some commands, this default behavior of taking action on every machine doesn’t make sense. vagrant ssh can’t SSH you into every machine in one terminal! So some commands require a target machine.

The output of vagrant status changes, too. It now lists multiple machines and the state of each of them:

    $ vagrant status
Current machine states:
web running (virtualbox)
db 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`.
For detailed information on the state of a machine as well as helpful instructions on how to change the state of the machine, just call vagrant status with a target machine:
$ vagrant status web
Current machine states:
web running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
In cases where the environment has many machines, you can specify multiple targets in the same command:
$ vagrant reload node1 node2 node3 Or, if you’re managing many nodes, you can even use a regular expression. Vagrant assumes if the node name starts and ends with / that it is a regular expression. For example:
$ vagrant reload /node\d/
...

Although the command interface changes slightly, the behavior of commands upon individual machines is unchanged. Everything continues working as you’d expect, only now Vagrant manages multiple machines!

三、虚拟机机器之间进行通信

If multimachine environments were made to model service-oriented architectures, there needs to be a way for the machines to communicate with each other. By default, by simply defining the machines, there is no way for them to communicate.

3.1 private network

By defining a private network on the machines that exists on the same subnet, the machines are able to communicate with each other. When specifying a static IP address with host-only networks on Vagrant, Vagrant by default uses a subnet mask of 255.255.255.0. This means that as long as the first three parts (octets) of the IP address are the same, the machines will be placed on the same network.

Vagrant::configure("2") do |config|
config.vm.box = "precise64" config.vm.define "web" do |web|
web.vm.network "forwarded_port", guest:80,host:8080
web.vm.provision :shell,path: "provision.sh"
web.vm.network "private_network",ip: "192.168.33.10"
end config.vm.define "db" do |db|
db.vm.network "private_network",ip:"192.168.33.11"
end
end

四、实例 MySQL

Now that the basics have been covered, we’ll provision the second machine with the shell provisioner to install MySQL on the second machine, and talk to that MySQL instance from the web machine using the MySQL client.

First, create a new file db_provision.sh in your project root with the following contents:

export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y mysql-server
sed -i -e 's/127.0.0.1/0.0.0.0/' /etc/mysql/my.cnf
restart mysql
mysql -uroot mysql <<< "GRANT ALL ON *.* TO 'root'@'%'; FLUSH PRIVILEGES;"

This is the shell script that will provision the db machine. Let’s quickly explain a few parts because it looks considerably different than the script used to set up Apache.

  • First, we export an environmental variable DEBIAN_FRONTEND with the value “noninteractive.” This makes it so that when installing MySQL server, it does not ask us questions for a root password and all that.

  • Next, we use sed to replace the bind address from loopback to 0.0.0.0, which means all interfaces. This is necessary so that remote machines can connect to the server.

  • Then, we restart MySQL so that our configuration changes can take effect.

  • And finally, we tell MySQL to allow root to connect from any host. This is generally very unsafe, but for the purpose of this example, it will work well.

Let’s wire it all up in the Vagrantfile with networking, and also by provisioning the web machine with the MySQL client. The Vagrantfile should look like this:

Vagrant::Configure("2") do |config|
config.vm.box = "precise64"
config.vm.define "web" do |web|
web.vm.network "forwarded_port",guest: 80,host: 8080
web.vm.provision :shell, path: "provision.sh"
web.vm.provision :shell, inline: "apt-get install mysql-client"
web.vm.network "private_network", ip: "192.168.33.10"
end
config.vm.define "db" do |db|
db.vm.provision :shell, path: "db_provision.sh"
db.vm.network "private_network",ip: "192.168.33.11"
end
end

At this point, you should be able to fully understand the contents of the Vagrantfile. If there are any parts that are confusing or unclear, reread the previous sections until you fully understand them.

Run vagrant destroy to fully destroy both machines if they were running before. We want to start from a clean slate for this example.

After destroying the environment, run vagrant up to bring up both machines. This should take a few minutes, as both machines must fully provision.

Once the machines are running, log in to the web machine via SSH and use the MySQL client to access the db machine. The output should look similar to the following, but may be slightly different depending on versions and so on:

$ vagrant ssh web
vagrant@precise64:~$ mysql -uroot -h192.168.33.10
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

Success! The web machine is able to connect to a remote MySQL server, except that the remote MySQL server is actually just another virtual machine on the local system.

Given this example, it should be clear to see how these concepts can apply to larger or more complex architectures.

Vagrant 集群的部署的更多相关文章

  1. Storm集群安装部署步骤【详细版】

    作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2012/11/30/how ...

  2. JStorm集群的部署

    JStorm是一个类似Hadoop MapReduce的系统,不同的是JStorm是一套基于流水线的消息处理机制,是阿里基于Storm优化的版本,和Storm一样是一个分布式实时计算的系统,从开发角度 ...

  3. Redis集群的部署

    Redis集群分为主节点Master和从节点Slave,主节点只有1个,而从节点可以有多个,这样从节点和主节点可以进行数据的传输,Redis集群的性能将比单机环境更高,接下来是配置的过程 首先配置Ma ...

  4. Storm入门教程 第三章Storm集群安装部署步骤、storm开发环境

    一. Storm集群组件 Storm集群中包含两类节点:主控节点(Master Node)和工作节点(Work Node).其分别对应的角色如下: 主控节点(Master Node)上运行一个被称为N ...

  5. Storm集群安装部署步骤

    本文以Twitter Storm官方Wiki为基础,详细描述如何快速搭建一个Storm集群,其中,项目实践中遇到的问题及经验总结,在相应章节以"注意事项"的形式给出. 1. Sto ...

  6. HBase集群安装部署

    0x01 软件环境 OS: CentOS6.5 x64 java: jdk1.8.0_111 hadoop: hadoop-2.5.2 hbase: hbase-0.98.24 0x02 集群概况 I ...

  7. Tomcat集群应用部署的实现机制

    集群应用部署是一个很重要的应用场景,设想一下如果没有集群应用部署功能,每当我们发布应用时都要登陆每台机器对每个tomcat实例进行部署,这些工作量都是繁杂且重复的,而对于进步青年的程序员来说是不能容忍 ...

  8. flink部署操作-flink standalone集群安装部署

    flink集群安装部署 standalone集群模式 必须依赖 必须的软件 JAVA_HOME配置 flink安装 配置flink 启动flink 添加Jobmanager/taskmanager 实 ...

  9. kafka 基础知识梳理及集群环境部署记录

    一.kafka基础介绍 Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特 ...

随机推荐

  1. 读取xml时,遇到xmlns的问题

    1.读取xml的时候,由于xml里有xmlns的属性,导致了读xml无法正常读取.通过网上搜索,发现需要先注册命名空间.  xmlns是XML Namespaces的缩写,中文名称是XML(标准通用标 ...

  2. Eclipse代码自动填充.

    在默认情况下,Eclipse只在程序员输入“.”并用ALT+/组合键强制调用编码提示功能 我们可以通过少量配置,让Eclipse更聪明,实现完全自动编码提示:1.在你的“工作空间”下找到下在文件.me ...

  3. HTTP could not register URL http://+:8000/.... Your process does not have access rights to this namespace

    windows 7, Visual Studio 2013 在托管 Windows 服务中承载 WCF 服务时报错: HTTP could not register URL http://+:8000 ...

  4. index ffs、index fs原理考究-1109

    h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justi ...

  5. XE6 /XE8 & IOS开发之免证书真机调试三步走,生成iPA文件并安装到其它苹果设备上

    XE6 & IOS开发之免证书真机调试(1):颁发属于自己的App签名证书(有图有真相) XE6 & IOS开发之免证书真机调试(2):连接真机并运行App(有图有真相) XE6 &a ...

  6. iOS单例的两种实现

    单例模式算是开发中比较常见的一种模式了.在iOS中,单例有两种实现方式(至少我目前只发现两种).根据线程安全的实现来区分,一种是使用@synchronized,另一种是使用GCD的dispatch_o ...

  7. 图的遍历(bfs 和dfs)

    BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...

  8. 【BZOJ3295】【块状链表+树状数组】动态逆序对

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  9. 仿小米网jQuery全屏滚动插件fullPage.js

    演 示 下 载   简介 如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次.比如 iPhone 5C 的介绍页面,QQ浏 ...

  10. hibernate映射关系之多对多

    多对多: * 关系在第三张表中,和两张表本身没有关系 * 多对多谁维护关系:谁都能维护关系(效率是一样的),一般情况下可以通过页面 来体现 * 关系体现: 第三张表的维护:增加.删除 course类对 ...