Bootstrapping one more node
- Lets create a centos 7 machine in azure and bootstrap the node into chef server
- In yesterday’s class we have downloaded chef starter kit, whenever starter kit is downloaded a new key pair is generated for authentication. so if you try running commands from the chef-repo extracted from older download the commands will fail.
- As of now our chef configurations are as shown below
- Since nodes are bootstrapped, nodes will communicate to the server periodically by default for every 30 mins to ask about what has to be executed. This communication is referred as Convergance
- In Chef when we upload cookbooks to chef server and configure it run on nodes. Chef nodes will not run them immedietly, they will automatically run during next convergance. So this might lead to situations where your desired state is not the actual state till the convergance is executed. This state is called as Configuration Drift
Chef Cookbook Development and Node Configuration workflow
- Lets assume we want to install apache server on nodes using chef
- So to do this in Chef we need to generate a cookbook
- Navigate to chef repo and cookbooks folder
- In this folder we need to generate chef cookbook and when we install chefdk we get generators installed
chef generate --help chef generate cookbook --help
- Now lets generate cookbook using the following command
chef generate cookbook -b my_first_cookbook
- In the recipes folder there will be a file called as default.rb which is a default recipe.
- Now in chef the file extensions for recipe’s will be .rb i.e. this is a ruby file.
- Chef is developed using ruby, To work with chef , chef has its own DSL or you can directly write ruby.
- Now lets create a recipe called as centos_apache
- Now lets create a recipe called as ubuntu_apache
- In this recipes we need to write something to express our desired state. To express this desired state we need to understand Recipe DSL (Domain Specific Language ) Refer Here
- In chef recipes we will have to use chef resources. Refer Here
- Resource will be in the following format
resourcetype '<name of the resource>' do attribute1 'value1' attributen 'valuen' action :type_of_action end
- To write these resources effectively, we need to understand the manual configurations steps of your desired state
- ubuntu to install apache
sudo apt-get update sudo apt-get install apache2 -y
- centos to install apache
sudo yum install httpd sudo systemctl enable httpd.service sudo systemctl start httpd.service
- Now the next step is to convert each command in the installation/configuration step to equivalent chef resource.
- In todays class lets use chef resource to create an empty file in /tmp/myfirstfile.txt
- Refer Here for all the resources
- In recipes\default.rb write the following content
file '/tmp/myfirstfile.txt' do action :create end
- now in the commmand line of cookbook folder execute two simple commands
berks install berks upload
- Now navigate to manage.chef.io
- Next Steps:
- How to add the recipes to execution during convergance.
- Chef developer environment setup
- Chef cookbook for apache installations