-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchef.txt
More file actions
441 lines (302 loc) · 10.6 KB
/
Copy pathchef.txt
File metadata and controls
441 lines (302 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
--::~CHEF~::--
- Configuration management tool.
- Infrastucture as code.(with help of code we build infrastucture)
- CMT are two types:
- Push Based - Pull Based
- Push configuration : Server push configuration to the nodes.(Ancible,Salt Stack)
Pull configuration : nodes check with server and fetch all configuration from it(Chef,Puppet).
*********
- Chef is written in Ruby and Erlang.
- Its is created by Adam Jacobs.
- Chef is an adminstration tool.
-> Advantages::
- Increase Uptime.
- Improve Performance.
- Ensure compliance.
- Prevent Errors.
- Reduce Cost.
+ Chef Architecture::
+WorkStation -----------knife----------- +Chef Server---------------Knife------ +Nodes *(Ohio(node database/current updated conf.),Chef Client)
[code:(Recipe)] (upload CookBook) (BOOTSTRAP)
[CookBook(collection of Recipe)]
[Code written in Ruby]
-*Idempotency(which file not there that will only update not all).
(Mirror Example::- node checks itself and update missing files)
Components of Chef::
-> Workstation: workstation are personal computer/virtual servers where all configuration code is created and changed.
- Devops engg. actually sits here and wrote code, and code is called recipe.
- with knife workstation communicate with chef server.
- KNIFE is a command line tool that uploads the cook book to the server.
-> Chef Sever: It is a middle man between chef work station and node.
- All cook books are stored here.
- server may be hosted locally or remote.
-> Node: It is system that requires configurations.
- Ohio fetches current state of node.
- node communicate with chef-server using chef client
- Each node may have different conf.
- chef client installed on every node.
* Chef Work Station where you write code.
* Chef Server where you upload code.
* chef node where you apply code.
Chef Client tools runs on every chef node to pull code from chef server.
- gathers current system conf. from ohio.
- download desired conf. from chef server.
- configure node that adhere to policy.
Ohio
- maintains current state information of chef node.
Chef Supermarket:
- where you get all Recipe and cook book.
Idempotency
- tracking the state of system resource to ensure that changes should not reapply repeatdly.
-----------------------------------@Lecture2-------------------------
Linux M/C in AWS --Chef WorkStation-->Make Dir cookbooks--> Make another cook book-->Make Recipe
www.chef.io(download chef-workstation)
wget https://packages.chef.io/files/stable/chef-workstation/21.4.365/amazon/2/chef-workstation-21.4.365-1.el7.x86_64.rpm
ls
.rpm (chef package)
yum install -y <chef-workstation>
which chef
chef --version
[chefignore,kitchen.yml,metadata.rb,Readme,Recipe,Spec,text]
mkdir cookbooks
ls
cd cookbooks/
chef generate cookbook test-cookbook
yum install tree -y
tree
cd test-cookbook
chef generate recipe test-recipe
tree
cd..
vi test-cookbook/recipe/test-recipe.rb
file '/myfile' do
content 'Welcome to My Chef World'
action :create
end
chef spec ruby -c test-cookbook/recipe/test-recipe.rb //output= Syntax OK
chef-client -zr "recipe [test-cookboook::test-recipe]"
.............
cd test-cookbook
chef generate recipe test-recipe2.rb
cd ..
vi test-cookbook/recipe/test-recipe2.rb
package 'tree' do
action :install
end
File '/myFile' do
content 'second File Created'
action :create
owner 'root'
group 'root'
end
chef exec ruby -c test-cookbook/recipe/test-recipe2.rb //output= Syntax OK
chef-client -zr "recipe [test-cookboook::test-recipe2]"
cat /myfile2
yum remove tree -y
chef-client -zr "recipe [test-cookboook::test-recipe2]"
.......................
ls
chef generate cookbook apache-cookbook
cd apache-cookbook/
chef generate recipe apache-recipe
tree
cd ..
ls
vi apache-cookbook/apache-recipe.rb
package "httpd" do
action :install
end
file "/var/www/html/index.html" do
content "Welcome to this apache Recipe"
action :create
end
service "httpd" do
action [:enable,:start]
end
chef exec ruby -c apache-cookbook/recipes/apache-recipe.rb
chef-client -zr "recipe [test-cookbook::apache-recipe]"
................................................
Resource::
It is a basic compoenet of recipe used to manage different kind of state.There can be multiple resources
in recipe, which helps in configuring and managing infrastucture.
Package: managing package on node.
service: manage the service on node.
user: manage user on the node
group: manage group on the node
template: manage the files with embedded ruby template.
cookbook-file: transfers the files from file subdirectory in the cookbook to location on the node.
File: manage the content of file on the node.
execute: execute the command on the node.
cron : edit the existing cron file on the node.
directory : manage the directory on the node.
--------------------------Lecture 3------------------------------------------------
Chef Attribute:->
- Attribute: An attribute is an key-value pair which represents specific details about a node.
- types: +default,+force_default,+normal,+override,+force_overide,+automatic.(Lowest priority to Highest).
- who uses it: chef client.
- who defines it: Node, cookbook(attribute files), Recipes, Roles, Enviroments.
- Why we use::-
- TO convert state of node.
- what was state of node at end of the previous chef-client run.
- what the state of node at end of the current chef-client run.
*Note--
- Attributes define bt Ohai have the highest priority, followed by attributes defines in Recipe, then
attribute define by attribute file.
Lab--
sudo su
ohai
ohai ipaddress
ohai memory/total
ohai cpu/0/mhz
ls
cd cookbooks/
ls
cd apache-cookbook
tree
chef generate recipe recipe3
cd ..
vi apache-cookbook/recipes/recipe3.rb
file '/basicinfo' do
content "This is to get Attributes"
Hostname: #{node['hostname']}
IPAddress: #{node['ipaddress']}
CPU: #{node['cpu']['0']['mhz']}
MEMORY: #{node['memory']}
owner 'root'
group 'root'
action :create
end
chef client -zr "recipe[apache-cookbook::recipe3]"
----------------LECTURE 4---------------------------------------
sudo su
cd cookbooks
ls
//how to run linux code inside recipe
vi test-cookbook/recipes/test-recipe.rb
execute 'run a script' do
command <<-EOH
MKDIR /mydir
touch /myfile
EOH
end
chef-client -zr "recipe[test-cookbook::test-recipe]"
ls
//create user
vi test-cookbook/recipes/test-recipe.rb
user 'myuser' do
action :create
end
chef-client -zr "recipe[test-cookbook::test-recipe]"
//create group
group'mygroup' do
action :create
member 'myuser'
append true
end
cat /etc/group
Convergence: we run chef-client to apply recipe to bring node into desired state. i.e, chef client zr cmd.
what is Runlist??
- To run a recipe in sequence order that mentioned in list.
- with this process we can run multiple recipes, but the condition is there must be only one recipe from one cookbook.
- chef-client -zr "recipe[test-cookbook::test-recipe]","recipe[apache-cookbook::apache-recipe]"
include-recipe::-[to run multiple recipe all must be same cookbook]
- cook-book: default recipe,test-recipe,apache-recipe.
- include recipes in default recipe.
ex: include_recipe "test-cookbook::test-recipe"
include_recipe "test-cookbook::test-recipe2"
chef-client -zr "recipe[test-cookbook::default]"
- Combine above two to run multiple cook-book , multiple recipes.
chef-client -zr "recipe[test-cookbook::default]","recipe[apache-cookbook::default]"
chef-client -zr "recipe[test-cookbo.ok]","recipe[apache-cookbook]"
---------------Lecture 5-----------------------------------------------------
- CHEF Server is going to be mediator for the code or cookbooks.
- First create an account in chef-server.
- Then attach your workstation to chef-server.
- Now, upload your cookbook work station to chef server.
- now attach nodes to chef server via bootstrap.
- Apply cookbook from chef server to node.
LAb::
sudo su
ls
cd cookbooks/
ls
open google chrome::search manage.chef.io
create account
go chef account->click on org->starter kit->download starter kit
open download content->unzip->chef-repo, Now download 'WinSCP' ->login with ec2 credentials-->
drag and drop 'chef-repo' folder to linux.
ls
cd ..
ls
cd chef-repo/
ls -a
cd .chef/
ls
cat config.rb
knife ssl check //workstation successfully connected or not
Bootstraping a node:
- Attaching a node to chef server is called bootstraping.(Both workstation and node should be in same AZ)
- Now you have to be inside chef-repo directory to run ant command.
Two action will be done while bootstraping::
- Adding node to chef-server.
- Installing chef package
Create a linux Machine(Node1),bunch in same AZ(availability zone)
Advance details
#!bin/bash
sudo su
yum update -y
Now go to chef-workstation
- knife bootstrap 'privateip' --ssh-user ec2-user --sudo -i node-key.pem -N node1 [transfer node-key.pem using winscp in chef-repo]
- knife node list [to see bootstrapped node]
mv cookbooks/test-cookbooks chef-repo/cookbooks
mv cookbooks/apache-cookbooks chef-repo/cookbooks
rm -rf cookbooks/
cd chef-repo
ls
cd cookbooks/
ls
knife cookbook upload apache-cookbook //workstation to chefserver
knife cookbook list
knife node run_list set node1 "recipe[apache-cookbook::apache-recipe]"
knife node run_list set node1 "recipe[my_demo_cookbook::my_demo]"
knife node show node1
Go to node1::
[ec-2 user] sudo su
[ec-2 user] chef-client
Go to node1:
vi /etc/crontab
* * * * * root chef-client
.......................................................................................
------------------------Lecture 6---------------------------------------------------------
knife cookbook list
knife cookbook delete <cookbookname> -y
knife node list
knife node delete <nodename> -y
knife client-list (client present in chef server)
knife client delete <clientname> -y
knife role list
knife role delete <rolename> -y
=======
cd chef-repo
ls
cd role
ls
vi devops.rb
name "devops"
description "Bla Bla.."
run_list "recipe[apache-cookbook::apache-recipe]"
cd ..
[chef-repo]#
knife role from file role/devops.rb
knife bootstrap <private-ip> --ssh-user ec2-user --sudo -i node-key.pem -N node1
knife bootstrap <private-ip> --ssh-user ec2-user --sudo -i node-key.pem -N node2
knife node list
knife node run_list set node1 "role[Demo_role]"
knife node run_list set node2 "role[devops]"
knife node show node1
[chef-repo] vi codebooks/test-cookbook/recipes/test-recipe.rb
%w(httpd mariadb-server unzip git vim).each do |p|
package p do
action :install
end
knife cookbook upload test-cookbook