免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9109 | 回复: 3
打印 上一主题 下一主题

应用chef构建服务器集群自动化部署与管理 [复制链接]

论坛徽章:
6
CU大牛徽章
日期:2013-03-14 14:14:08CU大牛徽章
日期:2013-03-14 14:14:26CU大牛徽章
日期:2013-03-14 14:14:29处女座
日期:2014-04-21 11:51:59辰龙
日期:2014-05-12 09:15:10NBA常规赛纪念章
日期:2015-05-04 22:32:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-11 09:17 |只看该作者 |倒序浏览
为了将rails程序部署到服务器上,需要做的工作有

           
  • 安装服务器各组件,如libcurl等
           
  • 配置服务器,如用户管理、iptable等
           
  • 安装数据库
           
  • 安装缓存服务
           
  • 安装配置前端应用服务器
           
  • 安装rvm
           
  • 安装项目所需gems
以上工作属于新服务器到位后的一次性工作,而项目更新升级的工作属于不定期重复性工作,大致如下:

           
  • 下载最新源代码
           
  • 运行数据库脚本
           
  • 重新启动应用服务器
此外,日常工作还包括服务器的监控、维护等
当项目规模不大,服务器只有一两台时,可以手动或者应用 Capistranodeprec 自动处理
capistrano的缺点在应用capify安装某些组件的过程中,如果出现问题,只能进入源代码检查相应的recipe,因为capistrano的recipe相对来说是隐性的
比如,安装之前,我们不知道mysql的安装会是通过package直接安装还是通过下载source本地编译后安装,也无法获知其版本
而当recipe不符合你的要求时,需要对其进行改造的成本比较大
随着项目规模的不断扩大,比如,有了专门的数据库服务器,甚至是数据库服务器集群,这时候capistrano就会显示力不从心了
我们需要有专业的服务器管理配置工具来统一管理所有的服务器,这类工具不少,这里只介绍chef
ChefChef是一个什么样的工具想像一下我们现在需要搭建一台mysql database slave服务器,安装过程我们手动操作了
没过多久,我们需要第二台,这时候我们会想,如果之后安装第一台的时候把操作过程执行的命令写成脚本
现在安装第二台,运行一下脚本就行了,节约时间而且不容易出错
chef就相当于这样的一个脚本管理工具,但功能要强大得多,可定制性强
chef将脚本命令代码化,定制时只需要修改代码,安装的过程就是执行代码的过程
架构图

Chef的三种管理模式1. Chef-Solo
由一台普通电脑控制所有的服务器,不需要专设一台chef-server
2. Client-Server
所有的服务器作为chef-client,统一由chef-server进行管理,管理包括安装、配置等工作
chef-server可以自建,但安装的东西较多,由于使用solr作为全文搜索引擎,还需要安装java
3. Opscode Platform
类似于Client-Server,只是Server端不需要自建,而是采用 http://www.opscode.com 提供的chef-server服务,本文描述以此方式为主,免费帐号可以管理5个服务器

Library程序库程序库是定义的module方法,可以在chef中任何地方被调用,在方法体内可以执行与数据库等资源的交互动作
详见 Libraries
操作对象chef可以通过recipe指定新建目录、生成配置文件、安装gems等操作,可控性非常强
新建目录
新建目录/data,owner为node[:user]指定的内容,权限代码为0755
directory “/data” do
 owner node[:user]
 mode 0755
end
生成配置文件
以someservice.conf.erb为模板,生成/data/someservice.conf配置文件,生成时会传递参数applications给模板
template “/data/someservice.conf” do
 owner node[:user]
 mode 0644
 source “someservice.conf.erb”
 variables({
  :applications => node[:apps]
 })
end
把一台新的vps纳入chef管理假设vps ip为188.188.188.188
ssh密码为123456
注册https://cookbooks.opscode.com/users/new 注册帐号
通过邮件认证后,进入控制台 https://manage.opscode.com 创建organization
按提示下载两个链接文件至~/Downloads: Download validation key | Generate knife config
点击右上角的用户名,下载链接文件至~/Download: Get a new private key
下载后的knife.txt要改名为knife.rb
以上三个文件请妥善保管,下载之后opscode平台不再保存这些文件
本地安装gem install chef net-ssh-multi --no-ri --no-rdoccd ~/Documentsgit clone git://github.com/opscode/chef-repo.gitcd chef-repomkdir -p .chef# USERNAME和ORGANIZATION改为实际的文件名称cd ~/Downloadscp USERNAME.pem ~/Documents/chef-repo/.chefcp ORGANIZATION-validator.pem ~/Documents/chef-repo/.chefcp knife.rb ~/Documents/chef-repo/.chef#测试连接opscode platformknife client list#成功连接则显示validator的数组,如#[ "shopqi-validator" ]为vps安装ruby及chef客户端环境knife bootstrap 188.188.188.188 -u root -P 123456#ssh登录服务器不再需要输入密码ssh root@188.188.188.188 'sh -c "mkdir ~/.ssh"'scp ~/.ssh/id_rsa.pub root@188.188.188.188:/root/.ssh/authorized_keys远程测试是否安装正常ssh root@188.188.188.188chef-client#如果出现错误,可使用调试模式chef-client -l debug让vps运行第一个cookbook安装完服务器操作系统后,第一件事情就是更新软件
一般情况下,我们会在服务器上运行命令
sudo apt-get update现在我们要通过chef来指定vps运行此命令
本地安装apt cookbook#此命令最常用,会从opscode官网 "http://cookbooks.opscode.com/":http://cookbooks.opscode.com/ 下载cookbookknife cookbook site vendor apt为node加入recipe
knife node list#以上查看node id,假如为201166,现在查看node的run_listknife node show 201166 -r#加入apt recipe到vps的run list中knife node run_list add 201166 "recipe[apt]"knife node show 201166 -rknife cookbook upload -a远程登录vps,开始按run_list运行指定recipessh root@188.188.188.188chef-client#每次本地更新后都要在远程运行chef-client比较麻烦,可以使用以下命令,设置为后台程序运行chef-client -i 3600 -s 600安装ruby-on-rails3下载cookbook#参数-d要求下载依赖的cookbookknife cookbook site vendor rvm -d#此cookbook调用了chef内置的deploy resource,实现类似于capistrano的部署功能#但此cookbook还依赖了java cookbook等,忽略它们即可knife cookbook site vendor application -d新增apps data bagdata bag相当于全局变量,application cookcook需要根据apps指定role,recipe等参数
export EDITOR=vim#参考 "https://github.com/opscode/cookbooks/tree/master/application":https://github.com/opscode/cookbooks/tree/master/application 将json粘贴进来#退出vim时,data bag会被自动上传至chef server,本地不保存knife data bag create apps 55true#保存至本地mkdir data_bags/appsknife data bag show apps 55true > data_bags/apps/55true.jsonknife data bag from file apps 55true.json新增role railsexport EDITOR=vimknife role create railsTip服务器集群中可能会区分出production, staging环境,也可能同一应用部署在多台服务器中,但只需由其中的一台服务器运行数据库迁徙脚本

           
  • 新增production, staging等role,设置app_environment attribute,分配至相应的node,这样可以重复使用deploy cookbook
           
  • 新增app_name_run_migrations role,设置run_migrations attribute
常用cookbook以下列出在 https://github.com/opscode/cookbooks 中常用的cookbook
apt此recipt会运行apt-get update,用于更新操作系统
runit用于保证unicorn等服务时刻处于运行状态
runsvdir服务会不停地监测/etc/sv目录下的服务目录,每个目录对应一个runsv服务
如果某个runsv服务出现故障停止了,runsvdir会自动重新启动它
#sv服务不启动时查看日志ps -ef | grep runsvdir#查看服务的状态sv stat unicorn_server#查看服务的日志tail -f /etc/sv/unicorn_server/log/main/currentbluepillrunit保证了服务的运行,bluepill保证进程的cpu、memory占用率处于正常水平
users新增用户,用户定义在users data bag中
mongodb安装1.6.5版本的mongo server
https://github.com/papercavalier/mongodb-cookbook
unicorn经过对比,还是选择37signal的unicorn cookbook,因为支持多app部署,而unicornapp不支持(opscode官方已经删除此cookbook)。为了对unicorn子线程进行监控,两者都使用了bluepill取代God进行监控。
注意:在production环境下,rails3默认不会处理public目录下静态文件的请求,所以不能像dev环境下直接通过指定端口访问某个unicorn服务
application这个包含太多的依赖cookbook了,经过以上的实践,还是觉得不用它了,因为里面对rails项目有用的就是deploy部分,自己重写一个也很容易
注意事项在service或者definition中传递参数时,如果将父params需要级联传递下去,得手动为params赋值至其他变量,因为在service或者definition块中,params参数已经被当前块覆盖了
详情参考:"http://tickets.opscode.com/browse/CHEF-422"
参考资源Infrastructure Automation with Chef
Getting started with Chef tutorial
Getting Started With The Opscode Chef Platform – Configuration Management In The Cloud
How to Deploy Ruby on Rails With The Opscode Chef Application Cookbook
Cooking with Chef 101
Why Startups Need Automated Infrastructures
Deploy Resource
application cookbook
runit

论坛徽章:
0
2 [报告]
发表于 2012-05-11 09:59 |只看该作者
回复 1# wang290


    不错的分享,如果能再完善些就更好了

论坛徽章:
0
3
发表于 2012-05-13 22:35
回复 1# wang290

公司名称:  思科(Cisco)上海研发中心
公司行业:  电信/互联网系统及设备
公司性质:  外商独资.外企办事处
公司规模:  1000人以上
公司简介:
思科中国研发中心于2005年10月12日在上海市漕河泾经济技术开发区正式成立。自成立之后,思科中国研发中心快速成长。已成为思科海外第二大战略研发中心,总体投资达到1亿美金。期间,思科在全球并购了数十家在独自的市场和技术领域内遥遥领先的成长型公司,包括科学亚特兰大(Scientific Atlanta),网讯(WebEX)等,并首次在中国进行了并购(DVN 机顶盒业务)。2011年11月,思科中国研发中心在杭州、苏州、合肥成立分公司,标志着作为公司重点战略投资之一的网讯公司整合的顺利完成,至此思科中国研发中心拓展到了6个城市(上海、杭州、苏州、合肥、北京和深圳),思科中国研发中心聚集了来自中国及全球著名大学和优秀企业。

请有意向的朋友准备好中英文简历, 投递至 crdc-mars-hiring@external.cisco.com

工作地点: 上海徐汇漕河泾开发区

邮件标题:姓名+学校+工作年限+职位标题
-------------------------------------------------------------------------------------------------------------------
Job Title: Senior Cloud Service Reliability Engineer
Job Description:
Cisco is Seeking a capable Service Reliability Engineer with experience deploying and managing web services and applications in cloud based hosting environments to join our DevOps/Service Excellence team.

Join the Enterprise Networking Group's OnPlus Services team,  A dynamic, growth organization with responsibility for building, deploying and running cloud-based network management for a growing $20B USD networking business. OnPlus is positioned to gain preference and loyalty among Cisco's enterprise, mid-market and small business partners and customers worldwide.

Responsibilities:
- Work closely with marketing, development and test organizations to gain an understanding of our market, customer and product requirements and use cases and determine critical network, system and application thresholds
- Design, implement and maintain web service deployment and management  infrastructure for a geographically disperse, global Cloud Computing Environment
- Develop and deploy tools for monitoring service health status and analysis of performance metrics
- Work closely with management to develop, implement and document operational procedures for OnPlus Cloud Services
- Develop guidance that ensures scalability and high availability while minimizing performance issues
- Work closely with development engineering to evaluate and determine production readiness of software releases using Agile methods
- Analyze complex system behavior, performance and application issues
- Provide technical mentoring to Cloud Service Operations team members
- Become active member of a level 3 Subject Matter Expert team and part of a 24x7 on call rotation
- Educate, lead and assist Cisco Small Business Support Center level 1 and level 2 engineers in handling customer cases

Education and Experience Requirements:
- BSEE/CS plus 6 years related experience, or MSEE/CS plus 4 years related experience
- 4 years Unix/Linux experience, including kernel configuration, high availability and scalability design, performance analysis and tuning and automation scripting.

Preferred Qualifications:
- Flexible and adaptable with a strong work ethic and the ability to work independently as well as within a team environment
- Ability to take ownership and to lead, influence and mentor other DevOps engineers
- Strong problem solving and debugging skills in complex situations
- Proven ability to manage high priority tasks with competing priorities and with meticulous attention to detail
- Work effectively in a fast-paced and constantly changing growth environment
- Excellent skills in written and verbal communication
- Ability to collaborate with cross functional teams globally
- Proven ability to work effectively in a UNIX environment.
- Demonstrable programming skills with shell scripting, Perl, PHP, and Java
- Experience with  server config/deployment automation tools (Puppet, Chef etc) a plus
- Experience with Business Continuity and Disaster Recovery systems. Symantec NetBackup experience a plus
- Experience with IP networking, including familiarity with the functionality, operating, and failure modes of networks.
- Experience designing first and mid mile monitoring and proactively isolating worldwide internet issues
- Experience with server management and monitoring tools, Nagios experience a plus
- Experience with OS Virtualization, VMWare and Amazon AWS experience a plus
- ITIL v3 Foundation level certification a plus


   

论坛徽章:
0
4 [报告]
发表于 2012-05-14 19:21 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP