[译注]本文翻译自Cloud Foundry英文博客站点,原文题为“Deploying to AWS Using Cloud Foundry BOSH”,文章发表时间是 2012 年 09 月 06 日。
设计和构建 Cloud Foundry 的目的是为了支持可在多个云之间移动的分布式应用程序,包括那些运行在不同的 IaaS 基础架构(vSphere、vCloud、OpenStack 和 Amazon Web Services)上的应用程序。BOSH 是为多个云提供支持的关键所在,这是一个今年年初公布的云部署和生命周期管理工具。
这篇博客文章将介绍要开始在 AWS 上使用 BOSH 所需执行的一系列步骤,还会说明如何部署一个三层应用程序示例。
BOSH
要开始使用 BOSH,您需要对您的环境进行引导。Cloud Foundry 的工程师们创建了 BOSH 用于部署和管理分布式应用程序(如 Cloud Foundry),我们希望能够从头开始创建一个可通过受控制的方式进行管理和升级的工作环境。BOSH 是一个分布式应用程序,因此我们很自然地会使用 BOSH 来引导和部署 BOSH 本身。对此,我们有一个 BOSH 内核的概念 – 一个运行所有 BOSH 组件的单一系统,称为 micro BOSH。虽然这足以管理简单的应用程序,但对于生产级系统,还是需要一个完整的 BOSH 环境。不过,为了便于介绍 AWS 上的 BOSH,我们将直接从 micro BOSH 实例部署一个示例应用程序。
先决条件
要开始在 AWS 上使用 BOSH,您需要拥有以下三项:
- 一个 AWS 帐户
- AWS 凭据,即
access_key_id和secret_access_key - 一台 Mac 或 *NIX 计算机
安装 BOSH 部署程序
假设您已安装了 Ruby (1.9.2) 和 rubygems (1.8)。要安装 BOSH 部署程序 gem(它包括 BOSH cli):
gem install bosh_deployer
Micro BOSH AMI
我们在每个 AWS 区域都发布了 micro BOSH AMI。如果您已准备好使用 BOSH 部署程序,则可使用下表中与您所用的 EC2 区域对应的 AMI。
| 区域 | AMI |
| ap-northeast-1 | ami-7656eb77 |
| ap-southeast-1 | ami-64d59436 |
| eu-west-1 | ami-874c4af3 |
| sa-east-1 | ami-6280597f |
| us-east-1 | ami-69dd6900 |
| us-west-1 | ami-4f3e1a0a |
| us-west-2 | ami-7ac7494a |
部署 Micro BOSH
首先,需要在 AWS 上部署一个 micro BOSH 实例。由于您无法设置 AWS 实例的 IP,因此需要创建一个与 micro BOSH 配合使用的弹性 IP,因为 AWS 实例将要部署的实例需要与 micro BOSH 实例进行通信,而 AWS 公共 IP 会发生变化。
创建弹性 IP
- 转到 AWS Dashboard(AWS 仪表板),并找到右上角的 Elastic IP(弹性 IP)链接
- 在“分配新地址”( Allocate New Address) 弹出窗口中,保留默认值 EC2 并单击“分配”(Allocate)。
- 现在,已经分配了一个 IP,请重复这些步骤再创建三个(一个用于 micro BOSH,另外三个用于我们将要部署的 WordPress 演示应用程序)。
创建目录结构
BOSH 部署程序将基于预期目录位置中的文件部署应用程序:
mkdir ~/deployments cd ~/deployments mkdir aws
创建 Micro BOSH 配置文件
Micro BOSH 配置在 micro_bosh.yml(需创建此文件)中进行设置。
- 使用下面显示的模板创建
~/deployments/aws/micro_bosh.yml。
--- name: aws logging: level: DEBUG network: type: dynamic vip:x.x.x.xresources: persistent_disk: 20000 cloud_properties: instance_type: m1.small availability_zone:sa-east-1acloud: plugin:aws properties: aws: access_key_id:AKIAIYJWVDUP4KRWBESQsecret_access_key:EVGFswlmOvA33ZrU1ViFEtXC5Sugc19yPzokeWRfdefault_key_name: bosh default_security_groups: ["bosh"] ec2_private_key: ~/.ssh/bosh ec2_endpoint: ec2.sa-east-1.amazonaws.com apply_spec: agent: blobstore: address:x.x.x.xnats: address:x.x.x.xproperties: aws_registry: address:x.x.x.x
- 使用您所创建的其中一个弹性 IP 以及您要部署到的区域更新出现的所有
x.x.x.x。在 aws 部分添加您的 AWS 凭据。 - 更新可用性区域和端点设置,使之与您的可用性区域匹配。例如,如果部署到
us-east-1,应将可用性区域设置为us-east-1a或us-east-1b。 - 保存此文件
创建密钥对
如果已有 EC2 的密钥对,可以跳过这部分。
- 转到 AWS 仪表板 (AWS Dashboard),并单击“密钥对”( Key Pair)
- 单击“创建密钥对”(Create Key Pair)按钮
- 将密钥对命名为“bosh”
- 您的浏览器应自动下载名为
bosh.pem的私钥 - 将私钥文件重命名为
bosh并将其保存在~/.ssh目录中
创建安全组
- 在 AWS Dashboard(AWS 仪表板)中,创建名为
bosh的安全组
- 添加
0.0.0.0/0(可使用更具限制性的地址)的以下入站规则:- 端口
25555(BOSH 控制器 REST API) - 端口
6868(BOSH 代理 HTTP 接口) - 端口
22(ssh) 用于故障排除以及访问 micro BOSH 实例
- 端口
- 单击“应用规则更改”(Apply Rule Changes) 使规则生效
部署 Micro BOSH
现在可以从您的部署目录部署 Micro BOSH。
- 确保您正位于您的部署目录中:
cd ~/deployments
- 选择您所创建的部署:
bosh micro deployment aws
注意:不要为出现的似乎不正确的消息 WARNING! Your target has been changed to `http://aws:25555(警告!您的目标已更改为 `http://aws:25555) 而担心!
- 使用上文 BOSH AMI 部分中的 AMI 启动部署:
bosh micro deploy ami-xxxxxxxx
- 您的 micro BOSH 实例将在 20 分钟内完成部署。出现“完成”(Done) 消息后,您的 micro BOSH 实例就已经在运行了。
Done 11/11 00:07:10 WARNING!Your target has been changed to `http://x.x.x.x.:25555'! Deployment set to '/Users/bosh/deployments/aws/micro_bosh.yml' Deployed `aws/micro_bosh.yml' to `http://aws:25555', took 00:07:10 to complete
- 如果您的部署由于某种原因未能成功完成,请使用以下命令进行清理:
bosh micro delete
- 登录到 Micro BOSH:
bosh login
- 键入默认帐户名称
admin和密码admin - 使用以下命令更改帐户名称和密码。请更改帐户名称和密码,以防其他人删除您的部署!
bosh create user <username> <password>
使用 BOSH 部署应用程序
我们已经创建了一个三层应用程序示例(Nginx、Apache + PHP 以及 WordPress 和 MySQL)用以演示如何使用 BOSH,下一步将使用新创建的 micro BOSH 实例部署此应用程序。
上传示例版本
示例版本位于 Github 上,以方便您执行克隆:
- 首先,对示例应用程序版本库执行 git 克隆:
cd ~ git clone git://github.com/cloudfoundry/bosh-sample-release.git cd bosh-sample-release
- 将版本上传到 micro BOSH:
bosh upload release releases/wordpress-1.yml
上传最新的 stemcell
接下来,我们下载最新的 stemcell,以便上传到 micro BOSH 实例。
- 下载 AWS 的最新 BOSH stemcell:
bosh download public stemcell bosh-stemcell-aws-0.6.4.tgz
- 将其上传到您的 micro BOSH 实例:
bosh upload stemcell bosh-stemcell-aws-0.6.4.tgz
创建部署清单
- 创建一个名为“open”的允许相互安全组通信的安全组、端口 80(以便任何位置都可以通过 Internet 访问 Web 服务器)以及端口 22(如果希望在任何位置都可以使用 ssh 进行登录)。
- 使用以下命令获得控制器 UUID:
bosh status
- 在
bosh-same-release目录中创建wordpress-aws.yml的副本并更新红色部分。
--- name: wordpress director_uuid:718f5344-c8e4-4266-ab10-3a75e1638e25release: name: wordpress version: latest compilation: workers: 3 # 仅提供必要的数量 network: default reuse_compilation_vms: true cloud_properties: instance_type: m1.small # 这部分介绍如何进行更新 update: canaries: 1 canary_watch_time: 3000-120000 update_watch_time: 3000-120000 max_in_flight: 1 max_errors: 1 networks: - name: elastic type: vip cloud_properties: {} - name: default type: dynamic cloud_properties: security_groups: - open resource_pools: - name: common network: default size: 4 stemcell: name: bosh-stemcell version: 0.6.4 cloud_properties: instance_type: m1.small availability_zone: key_name: jobs: - name: mysql template: mysql instances: 1 resource_pool: common networks: - name: default default: [dns, gateway] - name: elastic static_ips: -107.21.118.59- name: nfs template: debian_nfs_server instances: 1 resource_pool: common networks: - name: default default: [dns, gateway] - name: elastic static_ips: -107.21.118.229- name: wordpress template: wordpress instances: 1 resource_pool: common networks: - name: default default: [dns, gateway] - name: elastic static_ips: -107.21.118.223- name: nginx template: nginx instances: 1 resource_pool: common networks: - name: default default: [dns, gateway] - name: elastic static_ips: -107.21.118.225properties: nginx: workers: 1 wordpress: admin: foo@bar.com port: 8008 servers: -107.21.118.223servername:107.21.118.225db: name: wp user: wordpress pass: w0rdpr3ss auth_key: random key secure_auth_key: random key logged_in_key: random key nonce_key: random key auth_salt: random key secure_auth_salt: random key logged_in_salt: random key nonce_salt: random key mysql: address:107.21.118.59port: 3306 password: rootpass nfs_server: address:107.21.118.229network:107.21.118.223/255.255.255.255debian_nfs_server: no_root_squash: true
部署
- 选择您刚才创建的部署清单:
bosh deployment ~/wordpress-aws.yml
- 启动部署:
bosh deploy
- 现在可以休息一下,等待部署成功完成吧!
总结
祝贺您。您刚刚已经使用 BOSH 向 AWS 部署了一个应用程序!现在,刚开始时最好使用示例应用程序。我们很快会发布相似的说明,用于部署 Cloud Foundry。同时,我们推荐您参阅 Nic 博士为 BOSH 编写的许多入门指南。
–Cloud Foundry BOSH 团队的Martin Englund





