Chef 서버 설치하고 테스트해보기
vagrant + virtualbox로 5대의 멀티 인스턴스 올리기
Chef 서버의 환경을 이해해 보려면 3개이상 가상머신을 설치해서 해보는게 좋다.
Vagrant로 5대의 가상머신을 올려보자.
Vagrant를 설치하고 VagrantFile에 아래와 같이 설정한 후
vagrant up
을 실행하면 된다.
vagrant 사용법은 아래의 링크를 참고 하자.
Chef의 테스트 환경을 만드는데 매우 적합한 툴 – Vagrant (http://gyus.me/?p=326)
VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.define :chef_server do |cfg| cfg.vm.box = "base" cfg.vm.network :private_network, ip: "192.168.30.10" cfg.vm.host_name = "chef-server" end config.vm.define :workstation do |cfg| cfg.vm.box = "base" cfg.vm.network :private_network, ip: "192.168.30.20" cfg.vm.host_name = "workstation" end config.vm.define :node01 do |cfg| cfg.vm.box = "base" cfg.vm.network :private_network, ip: "192.168.30.21" cfg.vm.host_name = "node01" end config.vm.define :node02 do |cfg| cfg.vm.box = "base" cfg.vm.network :private_network, ip: "192.168.30.22" cfg.vm.host_name = "node02" end config.vm.define :node03 do |cfg| cfg.vm.box = "base" cfg.vm.network :private_network, ip: "192.168.30.23" cfg.vm.host_name = "node03" end end
chef server 설치
아래 사이트에서 패키지를 다운 받아서 설치
http://www.getchef.com/chef/install/
[code lang=text]
wget https://opscode-omnitruck-release.s3.amazonaws.com/ubuntu/12.04/x86_64/chef-server_11.0.6-1.ubuntu.12.04_amd64.deb
[/code]
[code lang=text]
sudo dpkg -i chef-server.deb
[/code]
설치 완료후 chef-server-ctl로 Chef Server를 설정함
[code lang=text]
sudo chef-server-ctl reconfigure
[/code]
위 커맨드를 실행하면 서버를 기동시키고, nginx가 HTTPS 443포트를 바인드한다.
Chef의 각종 파일은 /opt 디렉토리에 인스톨된다.
knife 설정
[code lang=text]
knife configure
[/code]
실행하면 아래와 같은 설정 항목들이 나옴 걍 엔터.
[code lang=text]
Where should I put the config file? [/home/vagrant/.chef/knife.rb]
Please enter the chef server URL: [https://chef-server:443]
Please enter an existing username or clientname for the API: [vagrant]
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):
[/code]
임의의 클라이언트가 처음 Chef Server와 통신을 할경우, 디지털서명이 없기 때문에,
최초 한번, chef-validator라는 Client등록작업만을 전문으로 하는 클라이언트 에서 인증서를 빌려서 Chef Client의 등록작업을 하게됨. 이 작업이 완료되면 이후는 클라이언트 쪽에서 작성한 디지털서명에 의해 통신이 이루어지도록 된다.
dep패키지로 Chef Server를 설치하면 /etc/chef-server 디렉토리에 아래와 같은 파일이 있음. knife configure로 knife에게 이러한 인증서의 패스를 정확히 설정할 필요가 있다.
우선은 서버와 같은 호스트의 knife가 잘동작하도록 하고, 후에 workstation에 인증서를 전송하는 순서로 설치를 하겠음.
일단은 /etc/chef-server이하의 pem파일을 홈디렉토리로 카피함.
- chef-validator.pem : chef-validator용의 디지털 인증서 파일
- admin.pem : 관리자용의 클라이언트 인증서 파일(이 것을 사용하는 클라이언트 만 chef-validator를 통하지 않고 미리 작성되어 있음)
[code lang=text]
vagrant@chef-server:/$ sudo knife configure
Please enter the chef server URL: [https://chef-server:443] https://192.168.30.10
Please enter an existing username or clientname for the API: [vagrant] admin
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] ~/.chef/validation.pem
Please enter the path to a chef repository (or leave blank):
[/code]
위의 설정에서
* server URL : https://192.168.30.10
* clientname : admin
* validation key : /etc/chef-server/chef-validation.pem
을 설정함. 이것으로 해당 서버의 knife는 클라이언트의 인증서가 있는 ~/.chef/admin.pem을 이용해서 통신을 하게끔 된다. 한번 시험해봅시다.
[code lang=text]
$ knife client list
[/code]
Work Station의 셋업
새로운 Chef Client를 셋업 할 경우, 인증서의 생성과 클라이언트를 서버에 등록하는 것이 필요하다. 이것도 knife로 하게됨. 서버에 설치한 knfie를 사용해 보도록 하자.
knife client create를 실행하면 에디터에서 메타데이터의 편집을 요구하기때문에, EDITOR의 환경변수를 미리 설정해둠. knife client create 옵션의 -a 는 관리권한을 가진 클라이언트의 작성, -f는 인증파일의 저장소 지정. Vagrant를 사용하는 경우는 가상머신의 /vagrant
는 공유 디렉토리로 마운트되어 있어서 다른 가상서버로의 파일전송이 편리하다. 거기에 저장을 하도록 해보자.
[code lang=text]
$ export EDITOR=vi
$ knife client create workstation -a -f /vagrant/workstation.pem
[/code]
Work Station의 knife에도 validation.pem을 저장해두면 나중에 편하니 거기에도 카피해두자.
그리고 여기서 부터는 Work Station에서 작업해야한다. 우선은 Work Station에 Chef Solo를 설치할때 사용한 Opscode Omnibug Packaging의 인스톨스크립트를 실행해서 Chef를 설치한다.
[code lang=text]
$ vagrant ssh workstation
$ curl -L https://www.opscode.com/chef/install.sh | sudo bash
$ chef-client -v
Chef: 11.8.2
[/code]
이 다음으로는 서버에서 한것과 동일하게 knife를 설정해야한다.
[code lang=text]
$ cp /vagrant/workstation.pem ~/.chef/
$ cp /vagrant/validation.pem ~/.chef/
$ knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/home/vagrant/.chef/knife.rb]
Please enter the chef server URL: [https://workstation:443] https://192.168.30.10
Please enter an existing username or clientname for the API: [vagrant] workstation
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] ~/.chef/validation.pem
Please enter the path to a chef repository (or leave blank):
[/code]
서버의 URL과 방금 설정한 인승키의 패스를 설정.
제대로 설정을 했다면 knife client list로 출력이 되고 새로운 Client로 workstation이 등록된다.
[code lang=text]
### Chef Server에서 작업
$ knife client list
chef-validator
chef-webui
workstation
[/code]
<h3>Chef Client의 설치</h3>
Chef Server와 Work Station이 완료된 후에는 Client를 작업하자.
관리 대상이 되는 노드를 Chef Client로 셋업하자.
클라이언트측의 Chef인스톨도 Opscode Omnibus Packaging의 인스톨 스크립트를 실행하면 된다.
우분투를 클린상태로 설치했다면, 몇가지 패키지를 설치해야하니 다음 순서로 설치해보자.
[code lang=text]
### Chef Client에서 실행함.
$ sudo apt-get update
$ sudo apt-get install curl vim -y
$ curl -L https://www.opscode.com/chef/install.sh | sudo bash
$ chef-client -v
$ sudo mkdir -p /etc/chef
$ sudo cp /vagrant/validation.pem /etc/chef/validation.pem
[/code]
위의 스크립트를 한줄 한줄 실행해도 되고 귀찮은 사람은 파일로 만들어서 한방에 실행해도 된다.
노드에서 chef-client를 실행해서 해당 노드를 서버에 등록하자. –server로 서버의 URL을 지정하고, -N으로 노드명을 지정할 수 있다.(생략하려면 /etc/chef/client.rb를 설정하면 된다. 자세히 알고 싶으면 매뉴얼을 보도록 하자.)
[code lang=text]
$ sudo chef-client –server https://192.168.30.10 -N node01
[/code]
최초의 Client실행 때에 /etc/chef/client.pem에 클라이언트용 인증서가 생성되고, 이후의 통신은 validation.pem이 아닌 클라이언트용 인증서를 사용하게 된다.
Chef Server에 노드가 등록되었는지 Work Station의 knife에서 확인가능하다.
[code lang=text]
### Work Station에서 실행
$ knife client list
chef-validator
chef-webui
node01
workstation
[/code]
node02, node03에는 추후에 따로 세팅을 할테니 우선 두자.
헉헉헉…길다…
<h3>레시피 실행하기</h3>
잘동작하는지 테스트용 레시피를 작성후 실행해보자.
테스트로는 해당노드에 대한 각종 정보를 가지고 있는 ohai를 확인해 보는 레시피를 작성하도록 하자.
Work Station에서 쿡북을 작성하고 Server에 업로드 한 뒤 클라이언트에 적용해보자.
<h4>쿡북의 작성</h4>
[code lang=text]
$ knife cookbook create sample -o ~/chef-repo/cookbooks
[/code]
<h4>
레시피의 작성</h4>
[code lang=text]
$ vi ~/chef-repo/cookbooks/recipes/default.rb
bash "print ohai info" do
user 'vagrant'
group 'vagrant'
cwd '/home/vagrant'
environment "HOME" => '/home/vagrant'
code < /tmp/ohai.txt
EOC
creates "/tmp/ohai.txt"
end
[/code]
<h4>쿡북의 업로드</h4>
[code lang=text]
$ knife cookbook upload -a -o ~/chef-repo/cookbooks
[/code]
<h4>Node Object의 run_list 레시피 추가</h4>
[code lang=text]
$ knife node run_list add node01 'recipe[sample]'
[/code]
<h4>Node Object를 에디터에서 편집하고 싶은 경우</h4>
[code lang=text]
$ knife node edit node01
[/code]
이제 서버에 필요한 정보의 등록은 완료됐다.
노드 쪽에서 chef-client를 실행하고 생성된 파일을 확인해 보자.
[code lang=text]
$ sudo chef-client –server https://192.168.30.10 -N node01
$ cat /tmp/ohai.txt | head
{
"languages": {
"ruby": {
"platform": "x86_64-linux",
"version": "1.8.7",
"release_date": "2012-02-08",
"target": "x86_64-unknown-linux-gnu",
"target_cpu": "x86_64",
"target_vendor": "unknown",
"target_os": "linux",
…
[/code]
knife bootstrap 으로 노드 설정하기
가상머신으로 실행중인 경우 Work Station에서 knife bootstrap 명령을 원격으로 실행 가능하도록, 몇가지 선행되어야하는 작업이 있는데 다음과 같다.
- 가상서버에 ssh로그인 가능하도록 호스트 OS의 ~/.vagrant.d/insecure_private_key를 Work Station의 ~/.ssh/id_rsa에 카피
[code lang=text]
### 호스트 OS에서 작업
$ scp ~/.vagrant.d/insecure_private_key worksstation:~/.ssh/id_rsa
[/code]
- 가상서버에 설치되어 있는 옛날 버젼의 Chef가 설치시 에러를 일으킬 수 있으므로 제거. /usr/bin 디렉토리의 chef-* 파일을 어딘가로 이동해두면 된다.
[code lang=text]
### Work Station에서 작업
$ mv /usr/bin/chef-* /tmp/
[/code]
- Work Station의 /etc/hosts에 각 노드의 IP주소를 등록해둔다.
[code lang=text]
# /etc/hosts
…
192.168.30.21 node01
192.168.30.22 node02
192.168.30.23 node03
[/code]
위의 설정은 knife ssh가 호스트로의 통신을 FQDN(Fully Qualified Domain Name)으로 실행하기 위한 것이다.
정리
설치와 실행이 매우 길고 좀 복잡해 보이는데, 차근차근 따라해보면 그렇지도 않다. 관리하는 서버가 10대를 넘어가면 Chef Server를 고려해 보는 것이 좋은 것 같다.