CentOS6.3下安装部署CHEF环境教程

   2015-07-29 0

一.前言:

初识Chef,我们可以先了解一下DevOps运动http://zh.wikipedia.org/wiki/DevOps,简单点说,就是传统的软件组织将开发、IT运营和质量保障设为各自分离的部门,而DevOps运动的出现是由于软件行业日益清晰地认识到:为了按时交付软件产品和服务,开发和运营工作必须紧密合作。

所以Chef简单点说,就是DevOps运动中的一项重要工具成员,是一个同时面向开发与运维的集中管理工具。

就服务器的集中管理工具而言,知名度与Chef平分天下的是叫“Puppet”的工具,它们是OSS知名度排名最前的2个。

想像一下我们现在需要搭建一台MySQL Database Slave服务器,安装过程我们手动操作了没过多久,又需要第二台,这时候我们会想,如果之后安装第一台的时候把操作过程执行的命令写成脚本,现在安装第二台,运行一下脚本就行了,节约时间而且不容易出错。

Chef就相当于这样的一个脚本管理工具,但功能要强大得多,可定制性强,Chef将脚本命令代码化,定制时只需要修改代码,安装的过程就是执行代码的过程。打个比方,Chef就像一个制作玩具的工厂,它可以把一些原材料做成漂亮的玩具,它有一些模板,你把原材料放进去,选择一个模板(比如怪物史莱克),它就会制造出这个玩具,服务器的配置也是这样,一台还没有配置的服务器,你给它指定一个模板(role或recipe),Chef就会把它配置成你想要的线上服务器。

Chef使用服务器—客户端模式管理所有需要配置的机器,使用Chef涉及至少三台机器:
一台开发机器(Workstation),在上面编写大餐的做法;

一台Chef服务器(server),管理所有要配置的Chef客户端,给它们下发配置信息;
多台Chef客户端(Node),就是我将要烹调出的大餐。

操作系统:CentOS-6.3-x86-64

CHEF:   chef-server-11.0.12-1.el6.x86_64
Server :     10.107.91.251 (chef.example.com)
Workstation:     10.107.91.251 (chef.example.com)
node:         10.107.91.252 (node1.example.com)   

二.安装前的准备:(chef.example.com,node1.example.com)

1.关闭iptables

复制代码
代码如下:

# service iptables stop

2.关闭SELINUX

复制代码
代码如下:

# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------

3.同步时间(重要)

复制代码
代码如下:

# ntpdate asia.pool.ntp.org
# hwclock -w

4.安装ruby环境:
详见: http://www.lexue001.com/article/54981.htm

三.chef-server安装:(chef.example.com)

1.下载chef-server安装包

进入页面http://www.opscode.com/chef/install,点击Chef Server标签,选择要下载的版本
或在终端下用以下命令下载11.0.12版本:

复制代码
代码如下:

# wget -c --no-check-certificate
<a href="https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.12-1.el6.x86_64.rpm">https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.12-1.el6.x86_64.rpm</a>


2.安装chef-server

终端内进入chef-server软件包所在目录,执行以下命令:

复制代码
代码如下:

# rpm -ivh chef-server-11.0.12-1.el6.x86_64.rpm

注:请使用下载的软件包名称替换上面命令中的软件包名称.

3.修改本地FQDN名:

1).首先修改主机的hostname

复制代码
代码如下:

# vi /etc/sysconfig/network
———————-———————-———————-———————-
HOSTNAME=chef.example.com
———————-———————-———————-———————-

2).修改本地host,添加server与node的FNDN


复制代码
代码如下:

# echo "10.107.91.251 chef.example.com" >> /etc/hosts
# echo "10.107.91.252 node1.example.com" >> /etc/hosts

重启系统. 登录后验证:

复制代码
代码如下:

# hostname -f
———————-———————-———————-———————-
chef.example.com
———————-———————-———————-———————-

3.配置chef-server

执行以下命令配置chef-server

复制代码
代码如下:

# chef-server-ctl reconfigure

注:chef-server 10.x版本默认监听4000端口,chef-server 11.x监听443端口
SO若线上开启防火墙,需执行以下命令防火墙开启443端口

复制代码
代码如下:

# iptables -I INPUT -p tcp --dport 443 -j ACCEPT
# service iptables save

现在浏览器打开https://10.107.91.251
输入:

复制代码
代码如下:

username: admin
password: p@ssw0rd1

即可访问chef-server web页面.


四.chef-workstation安装:(chef.example.com)


1.安装chef-Client
进入页面http://www.opscode.com/chef/install,点击Chef Client标签,选择要下载的版本.
本例使用11.12.4-1版本:

复制代码
代码如下:

# wget -c --no-check-certificate https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-11.12.4-1.el6.x86_64.rpm
# rpm -ivh chef-11.12.4-1.el6.x86_64.rpm


2.安装配置git

1).yum安装git:


复制代码
代码如下:

# yum -y install git


2).进入root主目录,git克隆chef repository


复制代码
代码如下:
# su -
# cd ~
# git clone git://github.com/opscode/chef-repo.git
————————————————————————————————————————————————————————————
Initialized empty Git repository in /root/chef-repo/.git/
remote: Reusing existing pack: 223, done.
remote: Total 223 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (223/223), 45.77 KiB | 37 KiB/s, done.
Resolving deltas: 100% (57/57), done.
————————————————————————————————————————————————————————————


2.配置chef-workstation

运行命令 knife configure -i ,配置过程如下所示(只需填写chef repository一项:/root/chef-repo,其他项使用默认值):

复制代码
代码如下:

# knife configure --initial
————————————————————————————————————————————————————————————
WARNING: No knife configuration file found
Where should I put the config file? [/root/.chef/knife.rb]
Please enter the chef server URL: [https://chef.example.com:443]
Please enter a name for the new user: [root]
Please enter the existing admin name: [admin]
Please enter the location of the existing admin's private key: [/etc/chef-server/admin.pem]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem]
Please enter the path to a chef repository (or leave blank): /root/chef-repo
Creating initial API user...
Please enter a password for the new user: 123456
注:这里输入API user密码(后面要使用)
Created user[root]
Configuration file written to /root/.chef/knife.rb
————————————————————————————————————————————————————————————

注: 没有出现Please enter a name for the new user: [root] 检查chef-server的443端口是否可以访问.

五.chef-workstation与chef-server不在同一服务器上的配置方法

(本例server与workstation在同一服务器,如无特殊需可略过这部分)

1.将chef-server的域名解析添加至chef-workstation的hosts文件


复制代码
代码如下:

# echo "10.107.91.251 chef.example.com" >>/etc/hosts


2.在chef-workstation先创建/root/.chef目录,并将chef服务器上的/etc/chef-server/admin.pem和/etc/chef-server/chef-validator.pem文件拷贝到此目录

复制代码
代码如下:

# mkdir ~/.chef
# scp chef.example.com:/etc/chef-server/admin.pem ~/.chef
# scp chef.example.com:/etc/chef-server/chef-validator.pem ~/.chef


3.执行knife configure -i命令进行初始化, 然后删除~/.chef/admin.pem

复制代码
代码如下:

# knife configure --initial
# rm ~/.chef/admin.pem


4.knife configure配置过程:
1).server URL修改为chef服务器的地址https://chef.example.com:443,
2).admin's private key路径改为/root/.chef/admin.pem
3).validation key路径改为/root/.chef/chef-validation.pem
4).chef repository地址输入/root/chef-repo,其余项保持默认值.

复制代码
代码如下:

# knife configure --initial
————————————————————————————————————————————————————————————
Overwrite /root/.chef/knife.rb? (Y/N) Y
Please enter the chef server URL: [https://workstation:443] https://chef.example.com:443
Please enter a name for the new user: [root]
Please enter the existing admin name: [admin]
Please enter the location of the existing admin's private key: [/etc/chef-server/admin.pem] /root/.chef/admin.pem
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] /root/.chef/chef-validator.pem
Please enter the path to a chef repository (or leave blank): /root/chef-repo
Creating initial API user...
Please enter a password for the new user: 123456
注:这里输入API user密码(后面要使用)
Created user[root]
Configuration file written to /root/.chef/knife.rb
————————————————————————————————————————————————————————————


5.配置ruby路径(之前已安装RUBY,这里可以略过)

chef默认集成了一个ruby的稳定版本,需修改PATH变量,保证chef集成的ruby被优先使用.

复制代码
代码如下:
# echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile

六.验证chef-workstation

执行knife client list命令返回client列表则配置成功.

复制代码
代码如下:

# knife client list
——————————————————
chef-validator
chef-webui
——————————————————


七.chef-node配置 (node1.example.com)

node即为被chef-server配置管理的服务器

1.安装chef-Client

进入页面http://www.opscode.com/chef/install,点击Chef Client标签,选择要下载的版本.
本例使用11.12.4-1版本:

复制代码
代码如下:

# wget -c --no-check-certificate https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-11.12.4-1.el6.x86_64.rpm
# rpm -ivh chef-11.12.4-1.el6.x86_64.rpm


2.修改本地FQDN名:

1).首先修改主机的hostname

复制代码
代码如下:

# vi /etc/sysconfig/network
———————-———————-———————-———————-
HOSTNAME=node1.example.com
———————-———————-———————-———————-


2).修改本地host,添加本机和server的FNDN


复制代码
代码如下:

# echo "10.107.91.251 chef.example.com" >> /etc/hosts
# echo "10.107.91.252 node1.example.com" >> /etc/hosts

重启系统. 登录后验证:

复制代码
代码如下:

# hostname -f
———————-———————-———————-———————-
node1.example.com
———————-———————-———————-———————-


3.在chef-worksation执行下面命令添加并配置node


复制代码
代码如下:

# knife bootstrap node1.example.com -x root -P 123456

注: 这里的密码是node1系统root账号的密码
chef-workstation通过ssh连接到node1(10.107.91.252)上执行bootstrap脚本(chef-workstation /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef11.6.0/lib/chef/knife/bootstrap/chef-full.erb)可以使用自定义的bootstrap脚本对node进行初始化配置.
node1会下载https://www.opscode.com/chef/install.sh脚本.脚本检查操作系统类型并在网络上下载符合此系统的chef版本进行安装(下载安装较慢).可以在node上预安装chef(见workstation安装chef部分)跳过此脚本.

复制代码
代码如下:

———————-———————-———————-———————-—-———————-—-———————-
Connecting to node1.example.com
node1.example.com Starting first Chef Client run...
node1.example.com [2014-05-08T15:53:22+08:00] WARN:
node1.example.com * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
node1.example.com SSL validation of HTTPS requests is disabled. HTTPS connections are still
node1.example.com encrypted, but chef is not able to detect forged replies or man in the middle
node1.example.com attacks.
node1.example.com
node1.example.com To fix this issue add an entry like this to your configuration file:
node1.example.com
node1.example.com ```
node1.example.com # Verify all HTTPS connections (recommended)
node1.example.com ssl_verify_mode :verify_peer
node1.example.com
node1.example.com # OR, Verify only connections to chef-server
node1.example.com verify_api_cert true
node1.example.com ```
node1.example.com
node1.example.com To check your SSL configuration, or troubleshoot errors, you can use the
node1.example.com `knife ssl check` command like so:
node1.example.com
node1.example.com ```
node1.example.com knife ssl check -c /etc/chef/client.rb
node1.example.com ```
node1.example.com
node1.example.com * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
node1.example.com
node1.example.com Starting Chef Client, version 11.12.4
node1.example.com Creating a new client identity for node1.example.com using the validator key.
node1.example.com resolving cookbooks for run list: []
node1.example.com Synchronizing Cookbooks:
node1.example.com Compiling Cookbooks...
node1.example.com [2014-05-08T15:53:25+08:00] WARN: Node node1.example.com has an empty run list.
node1.example.com Converging 0 resources
node1.example.com
node1.example.com Running handlers:
node1.example.com Running handlers complete
node1.example.com
node1.example.com Chef Client finished, 0/0 resources updated in 2.393659851 seconds
———————-———————-———————-———————-—-———————-—-———————-

查看是否连接node1成功:

复制代码
代码如下:

# knife node list
———————-———————-———————
node1
———————-———————-———————

从上面可以看出node1已经成功注册到了chef-server中,整个环境 chef-workstation => chef-server => chef-client-1 已经搭建成功。

八.登陆server web后台

1.修改浏览器本机HOST,这里笔者使用的是MAC系统

复制代码
代码如下:
# vi /etc/hosts
———————-———————-———————
10.107.91.251 chef.example.com
10.107.91.252 node1.example.com
———————-———————-———————

2.访问https://chef.example.com
如图:

CentOS6.3下安装部署CHEF环境教程

CentOS6.3下安装部署CHEF环境教程

CentOS6.3下安装部署CHEF环境教程

CentOS6.3下安装部署CHEF环境教程

九.创建一个cookbook实例

1.git克隆chef repository (chef.example.com)
注: chef repository 是一个存储cookbooks和其他文件的目录结构,初次使用需要从github克隆

复制代码
代码如下:

# su -
# cd ~
# git clone git://github.com/opscode/chef-repo.git
———————————————————————————————————
Initialized empty Git repository in /root/chef-repo/.git/
remote: Reusing existing pack: 223, done.
remote: Total 223 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (223/223), 45.77 KiB | 37 KiB/s, done.
Resolving deltas: 100% (57/57), done.
———————————————————————————————————

获取到的目录如下:

复制代码
代码如下:

# ls
——————————————————————————————————
Desktop Downloads Pictures Templates anaconda-ks.cfg install.log src
Documents Music Public Videos chef-repo install.log.syslog
——————————————————————————————————
# cd chef-repo/
# ls
——————————————————————————————————
LICENSE Rakefile chefignore cookbooks environments
README.md certificates config data_bags roles
——————————————————————————————————

2.创建一个cookbook,取名quick_start (chef.example.com)
注:需要切换到之前clone获取到的目录的cookbooks目录下

复制代码
代码如下:

# cd ~/chef-repo/cookbooks
# knife cookbook create quick_start -o ./
——————————————————————————————————
** Creating cookbook quick_start
** Creating README for cookbook: quick_start
** Creating CHANGELOG for cookbook: quick_start
** Creating metadata for cookbook: quick_start
——————————————————————————————————

创建的cookbooks目录如下

复制代码
代码如下:

# # ls -1p quick_start
—————————————
CHANGELOG.md
README.md
attributes/
definitions/
files/
libraries/
metadata.rb
providers/
recipes/
resources/
templates/
—————————————

注: cookbooks用来在chef中分布共享,大多数你创建基础实例都需要cookbooks.
此cookbook实例是创建一个简单的recipe,用来传递给node1一个简单的带有一些已经定义好的变量属性的文本.

3.创建一个属性文件,取名"quick_start.rb" (chef.example.com)

复制代码
代码如下:

# vi ~/chef-repo/cookbooks/quick_start/attributes/quick_start.rb
——————————————————————————
normal[:deep_thought] = "If a tree falls in the forest ..."
——————————————————————————

注: 在cookbook中属性文件用来在node中创建一些配置,从而你可以从recipe中调用这些属性.

4.对default recipe创建一个source template源模板 (chef.example.com)

复制代码
代码如下:

# vi ~/chef-repo/cookbooks/quick_start/recipes/default.rb
——————————————————————————
template "/tmp/deep_thought.txt" do
source "deep_thought.txt.erb"
variables :deep_thought => node[:deep_thought]
action :create
end
——————————————————————————

注: recipes允许你对具体的源进行管理,这个例子中,你创建了一个叫quick_start的recipe,内容包括一个单独的源模板名叫template "/tmp/deep_thought.txt"

5.创建一个template模板文件 (chef.example.com)

注:这个文件调用源模板的具体属性,而后被chef传送给具体的node客户端

复制代码
代码如下:
# vi ~/chef-repo/cookbooks/quick_start/templates/default/deep_thought.txt.erb
——————————————————————————
Today's deep thought: <%= @deep_thought %>
——————————————————————————

6.将cookbook上传到Server (chef.example.com)

复制代码
代码如下:

# cd ~/chef-repo/cookbooks/
# ls
——————————————————————————
README.md quick_start
——————————————————————————
# knife cookbook upload -a -o ./
——————————————————————————
Uploading quick_start [0.1.0]
Uploaded all cookbooks.
——————————————————————————

确认你刚上传的cookbook

复制代码
代码如下:

# knife cookbook list
——————————————————————————
quick_start 0.1.0
——————————————————————————

6.将quick_start recipe添加到你的node中 (chef.example.com)

复制代码
代码如下:

# knife node run_list add node1.example.com 'recipe[quick_start]'
——————————————————————————
node1.example.com:
run_list: recipe[quick_start]
——————————————————————————

查看添加好的recipe

复制代码
代码如下:

# knife node show node1.example.com -r
——————————————————————————
node1.example.com:
run_list: recipe[quick_start]
——————————————————————————

6.在node客户端注册,从而获取server上recipe的具体实例 (node1.example.com)
注:保证/etc/chef下有client.pem与validation.pem证书文件,如果没有检查之前的配置.

复制代码
代码如下:
# chef-client
————————————————————————————————————
[2014-05-08T23:55:33+08:00] WARN:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SSL validation of HTTPS requests is disabled. HTTPS connections are still
encrypted, but chef is not able to detect forged replies or man in the middle
attacks.
To fix this issue add an entry like this to your configuration file:
```
# Verify all HTTPS connections (recommended)
ssl_verify_mode :verify_peer
# OR, Verify only connections to chef-server
verify_api_cert true
```
To check your SSL configuration, or troubleshoot errors, you can use the
`knife ssl check` command like so:
```
knife ssl check -c /etc/chef/client.rb
```
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Starting Chef Client, version 11.12.4
resolving cookbooks for run list: ["quick_start"]
Synchronizing Cookbooks:
- quick_start
Compiling Cookbooks...
Converging 1 resources
Recipe: quick_start::default
* template[/tmp/deep_thought.txt] action create
- create new file /tmp/deep_thought.txt
- update content in file /tmp/deep_thought.txt from none to feb62f
--- /tmp/deep_thought.txt 2014-05-08 23:55:43.098408727 +0800
+++ /tmp/chef-rendered-template20140508-8171-11cxwpb 2014-05-08 23:55:43.099454345 +0800
@@ -1 +1,2 @@
+Today's deep thought: If a tree falls in the forest ...
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 9.915108372 seconds
————————————————————————————————————

最终执行后,创建 /tmp/deep_thought.txt文件,即实现了server向node的文件分发

复制代码
代码如下:
# vi /tmp/deep_thought.txt
————————————————————————————————————
Today's deep thought: If a tree falls in the forest ...
————————————————————————————————————

注: warning问题将在以后的文档中解决...
大功告成....

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 用Xshell设置CentOS密钥登录
    用Xshell设置CentOS密钥登录
    今天带来设置X shell 密钥登录  因为用的中文x shell 所以按照中文设置很简单1.点击工具(Tools)------新建用户密钥生成向导(New User key Wizard)(附图)2.然后出现下图时,记得密钥类型 选择RSA.其实选择DSA ,也能实现,二者其实是不同的算法。(我用的R
    03-08
  • centOS下安装Adobe Flash Player
    centOS下安装Adobe Flash Player
    centOS下安装Adobe Flash Player  2009-10-09 11:29:33|  分类:linux|  标签: |订阅 写出来就这么简单几步,当初刚接触Linux的时候搜索了很长时间,找了很多资料,总是出现这样那样的问题,最后终于摸索出最简单最有效的安装的方法。 随便打开一个带
    03-08
  • 部署Node.js项目(CentOS) node项目搭建
    部署Node.js项目(CentOS) node项目搭建
    操作步骤步骤 1:创建ECS实例选择操作系统为公共镜像CentOS7.2。使用root用户登录Linux实例。步骤2:部署Node.js环境—二进制文件安装该部署过程使用的安装包是已编译好的二进制文件,解压之后,在bin文件夹中就已存在node和npm,无需手工编译。安装步骤:1、
    02-10
  • centos安装python与jdk centos安装python3.7
    centos安装python与jdk centos安装python3.7
    安装python#压缩包安装[root@china ~]# yum -y install zlib*Loaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun
    02-10
  • centos6下同时安装python2和python3
    #build-essential compile packagesyum groupinstall "Development Tools"yum install openssl-develyum install zlib-develyum install make gcc gcc-c++ kernel-develhttp://unix.stackexchange.com/questions/291737/zipimport-zipimporterror-cant-decomp
    02-10
  • CentOS下查看文件和文件夹大小 linux查看文件夹
    当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择。  df可以查看一级文件夹大小、使用比例、档案系统及其挂入点,但对文件却无能为力。当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择。  df可以查
    02-10
  • centos7 离线升级/在线升级操作系统内核
    centos7 离线升级/在线升级操作系统内核
    centos7 离线升级/在线升级操作系统内核目录一.前言二.系统环境三.系统内核下载网址四.centos7离线升级系统内核1.先查看系统环境2.离线升级系统内核五.在线升级系统内核一.前言CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于
    02-10
  • centos7 rc.local脚本执行不成功
    腾讯云 centos7   配置文件/etc/rc.local的内容如下:#!/bin/sh#secu_agent init monitor, install at Thu Aug 3 11:19:41 CST 2017/usr/local/sa/agent/init_check.sh/dev/null 21/usr/sbin/ntpdate ntpupdate.tencentyun.com /dev/null 21 /usr/local/qclo
    02-10
  • 如何在centos7启动时自动挂载硬盘
    在/etc/rc.local中加入如下的语句,这样就不用每次重启后手动挂载了(后面挂载的目录根据自己的需求而定):mount  /dev/sdb1 /usr/sharedfiles/sdbmount  /dev/sdc1 /usr/sharedfiles/sdcmount  /dev/sdb1 /root/sdbmount  /dev/sdc1 /root/sdc注意:可以
    02-10
  • centos7.2 开启防火墙
    开启防火墙当我们修改了某些配置之后(尤其是配置文件的修改),firewall并不会立即生效。可以通过两种方式来激活最新配置 systemctl restart firewalld 和 firewall-cmd --reload 两种方式,前一种是重启firewalld服务,建议使用后一种“重载配置文件”
    02-10
点击排行