[Chef] nginx recipe
앞선 포스팅을 통해 Chef Server와 Workstation, Client 설치 및 테스트를 진행하였다. 이번 Chapter에서는 Chef 이외의 방법으로 패키지 조작이나 설정 파일 변경 등의 작업을 일절 하지 않은 상태에서 Chef만으로 웹 서버를 가동하는 작업을 수행할 예정이다. 진행 과정은 다음과 같다.
- 웹 서버 nginx 설치
- 서비스 가동
- Chef를 통한 설정파일 배포
- nginx Cookbook 신규 생성
[root@ChefWorkstation] $ knife cookbook create nginx
** Creating cookbook nginx in /root/chef-repo/cookbooks
** Creating README for cookbook: nginx
** Creating CHANGELOG for cookbook: nginx
** Creating metadata for cookbook: nginx
- recipe 작성
[root@ChefWorkstation] $ vi ~/chef-repo/cookbooks/nginx/recipes/default.rb
%w{epel-release nginx}.each do |pkg|
package pkg do
action :install
end
end
service "nginx" do
supports :status => true, :restart => true, :reload => true
action [ :enable, :start ]
end
template "nginx.conf" do
path "/etc/nginx/nginx.conf"
source "nginx.conf.erb"
owner "root"
group "root"
mode 0644
notifies :reload, 'service[nginx]'
end
bash "iptables_http" do
not_if 'cat /etc/sysconfig/iptables | grep 80'
code <<-EOC
iptables -F
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save
EOC
end
service "iptables" do
action [:restart ]
end
:wq
CentOS 표준에는 nginx 패키지가 존재하지 않기 때문에 EPEL 패키지 설치를 선행한다. EPEL (Extra Packages for Enterprise Linux)은 Fedora Project에서 제공하는 저장소로 각종 패키지의 최신 버전을 제공하는 community 기반의 저장소이다. 또한 셸 스크립트를 사용하여 iptables에 http 허용 규칙을 추가한다. 셸 스크립트는 chef 명령과는 다르게 중복 수행하므로, not_if를 사용하여 규칙이 존재하지 않을 경우에만 추가하도록 작성한다.
Template 내용에 notifies :reload, ‘service[nginx]’가 존재한다. 이는 Notification으로 다른 Resource에서 특정 Resource에 Notification(통지)을 보내어 임의의 액션을 트리거(trigger)하는 것이 가능하다. Notifies는 첫 번째 파라미터로 action을, 두 번째 파라미터로 resource_type[resource_name]이라는 형태로 적혀있다. resource_type에는 service와 template이라는 Resource의 종류, resource_name은 자신이 recipe 내에 정의한 Resource이다. 예를 들어, service[nginx]나 template[nginx.conf]라는 형태가 된다.
- Notification 시점
Notification 실행은 기본적으로 지연된다. notifies 행이 실행된 시점으로 바로 통지 내용이 실행되지 않고 일단 큐에 쌓인다. Chef 전체 실행과 상관없이 마지막에 실행된다. 그래서 통지를 보내는 쪽은 순서에 상관없이 그냥 통지를 보내면 되는 그러한 구조로 되어 있다.
notifies의 세 번째 파라미터에 :immediately를 추가하면 통지 시점을 바로 변경할 수 있지만, 일반적으로 많이 사용하지는 않는다.
notifies :reload, 'service[nginx]', :immediately
- 템플릿 파일 작성
[root@ChefWorkstation] $ vi /root/chef-repo/cookbooks/nginx/templates/default/nginx.conf.erb
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
:wq
- Chef Server에 Cookbook 업로드
[root@ChefWorkstation] $ knife cookbook upload nginx
Uploading nginx [0.1.0]
Uploaded 1 cookbook.
- Node Object의 run_list에 recipe 등록
[root@ChefWorkstation] $ knife node run_list set 172.16.1.148 recipe[nginx]
ChefClient:
run_list:
recipe[nginx]
- Chef Client에 적용
[root@ChefWorkstation] $ knife ssh 'name:172.16.1.148' 'sudo chef-client'
172.16.1.148 Starting Chef Client, version 12.4.1
172.16.1.148 resolving cookbooks for run list: ["nginx"]
172.16.1.148 Synchronizing Cookbooks:
172.16.1.148 - nginx
…
172.16.1.148 * service[nginx] action reload
172.16.1.148 - reload service service[nginx]
172.16.1.148 Running handlers:
172.16.1.148 Running handlers complete
172.16.1.148 Chef Client finished, 6/6 resources updated in 4.514535 seconds
- nginx 서비스 동작 확인
참 고 문 헌
[1] 이토 나오야, 박상욱, “인프라스트럭처 자동화 프레임워크 Chef Solo 입문”, 제이펍, 2014.