ROLE, ENVIRONMENT TRONG CHEF LÀ GÌ VÀ CÁCH LÀM VIỆC CỦA CHÚNG.
Ví dụ trong
công ty bạn hạ tầng phát triển tới một mức có lưu lượng lớn, nhiều server và
chúng thực thi một công việc cơ bản lặp đi lặp lại, và chúng có cùng cấu hình
cơ bản và chúng ta gọi nó là “role”.
Environment
là gì:
Như bạn đã
biết trong quy trình làm phần mềm có nhiều
công đoạn như : dev, staging, QA, product, Environment là trạng thái môi
trường của product của bạn, (stage of production). Mặc định môi trường là “_default”.
Ví dụ: một môi trường có thể là “testing” có thể là “production” . Các môi trường
này có code khác nhau và có thể có số lượng server khác nhau, tùy thuộc vào môi
trường.
Cách dùng role:
1. Tạo role sử dụng ruby DSL.
cd ~/chef-repo/roles
vi
web_server.rb
có nội dung
sau:
name
"web_server"
description
"A role to configure our front-line web servers"
run_list
"recipe[apt]", "recipe[nginx]"
env_run_lists
"production" => ["recipe[nginx::config_prod]"],
"testing" => ["recipe[nginx::config_test]"]
trong file trên ta thấy: “config_prod” recipe chạy
trong cookbook nginx trong môi trường “production”. Tuy nhiên, nếu node đó là “testing”
thì recipe là config_test.
Bạn có thể dung attribute như ví dụ dưới:
name
"web_server"
description
"A role to configure our front-line web servers"
run_list
"recipe[apt]", "recipe[nginx]"
env_run_lists
"production" => ["recipe[nginx::config_prod]"],
"testing" => ["recipe[nginx::config_test]"]
default_attributes
"nginx" => { "log_location" =>
"/var/log/nginx.log" }
override_attributes
"nginx" => { "gzip" => "on" }
2.
Tạo role sử dụng JSON file(cách này
tôi hay dùng):
Knife role create test
{
"name":
"web_server",
"description": "A
role to configure our front-line web servers",
"json_class":
"Chef::Role",
"default_attributes":
{
"nginx": {
"log_location":
"/var/log/nginx.log"
}
},
"override_attributes":
{
"nginx": {
"gzip":
"on"
}
},
"chef_type":
"role",
"run_list": [
"recipe[apt]",
"recipe[nginx]"
],
"env_run_lists": {
"production": [
"recipe[nginx::config_prod]"
],
"testing": [
"recipe[nginx::config_test]"
]
}
}
Upload json file này lên chef
server dung lệnh sau:
knife role from file /path/file
Bạn muốn hiển thị role trên
server và lưu chúng về máy dạng json dung:
knife role show web_server -Fjson > path/to/save/to
hiển thị
các node :
knife node list
sau đó : knife
node edit node_name
{
"name": "client1",
"chef_environment": "_default",
"normal": {
"tags": [
]
},
"run_list": [
"role[web_server]"
]
}
Bạn them role
[web_server] vào.
Đến đây bạn
có thể bootstrap đệ tạo những server như bạn muốn.
Cách dùng environment:
Tương từ
như cách tạo role , tạo environment cũng có 2 cách tạo là:
1. Dùng ruby DSL (tạo trực tiếp file
environments.rb.)
cd ~/chef-repo/environments
vi development.rb
name
"development"
description "The master development
branch"
cookbook_versions({
"nginx" => "<= 1.1.0",
"apt" => "= 0.0.1"
})
override_attributes ({
"nginx" => {
"listen" => [ "80", "443" ]
},
"mysql" => {
"root_pass" => "root"
}
})
Cách tạo
theo dạng JSON :
Knife environment create development
Export
EDITOR=vi
{
"name": "development",
"description": "The master development branch",
"cookbook_versions": {
"nginx": "<= 1.1.0",
"apt": "= 0.0.1"
},
"json_class": "Cheff:Environment",
"chef_type": "environment",
"default_attributes": {
},
"override_attributes": {
"nginx": {
"listen": [
"80",
"443"
]
},
"mysql": {
"root_pass": "root"
}
}
}
Sau đó bạn
có thể upload lên chef server.
Set môi trường
vào node:
knife node edit client1
{
"name": "client1",
"chef_environment": "_default",
"normal": {
"tags": [
]
},
"run_list": [
"role[web_server]"
]
}
Sau đó bạn
thay đổi biến:chef_environment như bạn mong muốn.
Nhận xét
Đăng nhận xét