1.Vagrant
1.Install
sudo apt-get install vagrant
2.Complete log
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant$ mkdir centosCentOS Default username/passwd is root/vagrant
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant$ cd centos/
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant/centos$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
Enter your choice: 3
==> box: Adding box 'centos/7' (v1804.02) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1804.02/providers/virtualbox.box
==> box: Successfully added box 'centos/7' (v1804.02) for 'virtualbox'!
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant/centos$ ls
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant/centos$ vagrant init centos/7A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant/centos$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos_default_1537980466397_98016
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Remote connection disconnect. Retrying...
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /home/satya/Desktop/DevOps/vagrant/centos/ => /vagrant
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant/centos$
satya@satya-Aspire-E5-523:~/Desktop/DevOps/vagrant/centos$ vagrant ssh
How to Download Vagrant Box Manually
Save: box.sh, run on terminal
/* this is the box (and the version) that we want to download from: https://app.vagrantup.com/debian/boxes/jessie64 */
wget https://app.vagrantup.com/debian/boxes/jessie64/versions/8.9.0/providers/virtualbox.box -O debian-jessie64-8.9.0.box
/* add the box to vagrant */
vagrant box add debian/jessie64 debian-jessie64-8.9.0.box
/* update box version */
cd ~/.vagrant.d/boxes/debian-VAGRANTSLASH-jessie64/
mv 0 8.9.0
/* create metadata_url file */
echo -n "https://app.vagrantup.com/debian/boxes/jessie64" > metadata_url
/* show vagrant boxes */
vagrant box list
2.Chef
1.Downloaded chefdk in Normal ubuntu System & installed
2.Check chef -v version details
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef$ chef -v
Chef Development Kit Version: 3.2.30
chef-client version: 14.4.56
delivery version: master (6862f27aba89109a9630f0b6c6798efec56b4efe)
berks version: 7.0.6
kitchen version: 1.23.2
inspec version: 2.2.70
1.Creating Recipe in Chef
Chef writen in Ruby. Now we are going to create a recipe “hello.rb” , here .rb is Ruby extensionCollection of Recipes are called CookBoook
1.Create chef_repo folder, go into it
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$
2.Create a resource in System using Chef
create hello.rb file inside chef_repofile 'motd' do
content 'Hello, World'
end
3.Apply recipe to Current System.
The above Code means, create a resorce(fille) with name “motd” with content as “hello world”
By executing chef-apply hello.rd, Chef will create a new file in our current infrasrure system
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ chef-apply hello.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* file[motd] action create
- create new file motd
- update content in file motd from none to 03675a
--- motd 2018-09-26 22:42:58.885793277 +0530
+++ ./.chef-motd20180926-23053-ysyfqd 2018-09-26 22:42:58.885793277 +0530
@@ -1 +1,2 @@
+Hello, World
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ ls
hello.rb motd
4.Here chef is idempotent, that means multiple changes won’t change the result.
It we change the hello.rd file, chef wont consider the changes until we apply changes
5.Delete file, using action
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ gedit hello.rb
file 'motd' do
action:delete
end
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ chef-apply hello.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* file[motd] action delete
- delete file motd
Complete Log
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ gedit hello.rb
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ cat hello.rb
file 'motd' do
content 'Hello, World'
end
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ ls
hello.rb
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ chef-apply hello.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* file[motd] action create
- create new file motd
- update content in file motd from none to 03675a
--- motd 2018-09-26 22:42:58.885793277 +0530
+++ ./.chef-motd20180926-23053-ysyfqd 2018-09-26 22:42:58.885793277 +0530
@@ -1 +1,2 @@
+Hello, World
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ ls
hello.rb motd
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ gedit hello.rb
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ chef-apply hello.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* file[motd] action delete
- delete file motd
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$
2. Chef : Recipes
1.Create a Resouce : Install a Software in Host Machine
In above ex, we just created a file as a resorce in host mechine. Now go to more advance install Softtware as a pkg in host machine- Apache2 pkg should install in host mechine
- Apache2 Should enable & Auto Start
- Create index.html, & make it as apache Homepage
1.edit hello.rd to perform above 3 steps on host system
package 'apache2'
service 'apache2' do
action[:enable, :start]
end
file '/var/www/html/index.html' do
content '<h1>Hello, Chef!!</h1>'
end
2.Do chef-apply
Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ sudo chef-apply hello.rbCross Checking
[sudo] password for satya:
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* apt_package[apache2] action install
- install version 2.4.29-1ubuntu4.1 of package apache2
* service[apache2] action enable (up to date)
* service[apache2] action start (up to date)
* file[/var/www/html/index.html] action create
- update content in file /var/www/html/index.html from b66332 to c0086c
--- /var/www/html/index.html 2018-09-26 23:24:20.577635660 +0530
+++ /var/www/html/.chef-index20180926-27935-qxmg32.html 2018-09-26 23:24:28.713710303 +0530
@@ -1,376 +1,2 @@
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ sudo chef-apply hello.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* apt_package[apache2] action install (up to date)
* service[apache2] action enable (up to date)
* service[apache2] action start (up to date)
* file[/var/www/html/index.html] action create (up to date)
3.Staring apache Server
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ service apache2 startsatya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$
4.Test by going : http://127.0.0.1/
Or using Curl
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef_repo$ curl localhost
<h1>Hello, Chef!!</h1>
3. Chef : Cookbooks
We are creating cookbooks folder, for working
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/cookbooks$ PS1=’\W>’;
shorten file path in terminalPS1='\W
>
'
To generate CookBook Syntax is
chef generate cookbook <Cookbook_Name>
1.Createing Apache Cookbook
cookbooks> chef generate cookbook chef_apache2
Here a folder is created inside /cookbooks withthe name chef_apache2
cookbooks> ls
chef_apache2
cookbooks> cd chef_apache2/
chef_apache2> tree
.
├── Berksfile
├── CHANGELOG.md
├── chefignore
├── LICENSE
├── metadata.rb
├── README.md
├── recipes
│ └── default.rb
├── spec
│ ├── spec_helper.rb
│ └── unit
│ └── recipes
│ └── default_spec.rb
└── test
└── integration
└── default
└── default_test.rb
7 directories, 10 files
2.Create a Template called index.html in chef_apache2 cookbook
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/cookbooks$here new folder (template) inside chef_apache2 and places index.html inside template folder
chef generate template chef_apache2 index.html
Recipe: code_generator::template
* directory[./chef_apache2/templates] action create
- create new directory ./chef_apache2/templates
* template[./chef_apache2/templates/index.html.erb] action create
- create new file ./chef_apache2/templates/index.html.erb
- update content in file ./chef_apache2/templates/index.html.erb from none to e3b0c4
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/cookbooks/chef_apache2$ tree
.
├── Berksfile
├── CHANGELOG.md
├── chefignore
├── LICENSE
├── metadata.rb
├── README.md
├── recipes
│ └── default.rb
├── spec
│ ├── spec_helper.rb
│ └── unit
│ └── recipes
│ └── default_spec.rb
├── templates
│ └── index.html.erb
└── test
└── integration
└── default
└── default_test.rb
3.Write “hello world” inside index.html
chef_apache2$ gedit templates/index.html.erb
4.edit recipe file recipes/default.rb to perform below 3 steps on host system
- Apache2 pkg should install in host mechine
- Apache2 Should enable & Auto Start
- Create index.html, & make it as apache Homepage
chef_apache2$ gedit recipes/default.rb
package 'apache2'
service 'apache2' do
action [:enable, :start]
end
template '/var/www/html/index.html' do
source 'index.html.erb'
end
5.Applay Cookbook to local System
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef$Check Apache Homepage
sudo chef-client --local-mode --runlist ‘recipe[chef_apache2]’;
[sudo] password for satya:
[2018-09-27T20:53:43+05:30] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 14.4.56
resolving cookbooks for run list: ["chef_apache2"]
Synchronizing Cookbooks:
- chef_apache2 (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 3 resources
Recipe: chef_apache2::default
* apt_package[apache2] action install (up to date)
* service[apache2] action enable (up to date)
* service[apache2] action start (up to date)
* template[/var/www/html/index.html] action create
- update content in file /var/www/html/index.html from c0086c to 95dfba
--- /var/www/html/index.html 2018-09-26 23:24:28.713710303 +0530
+++ /var/www/html/.chef-index20180927-5228-95sw5q.html 2018-09-27 20:53:46.556287854 +0530
@@ -1,2 +1,2 @@
-<h1>Hello, Chef!!</h1>
+Hello Satya
Running handlers:
Running handlers complete
Chef Client finished, 1/4 resources updated in 03 seconds
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef$ curl localhost
Hello Satya
4. CookBooks + Chef Server + Nodes
So far we did changes on local system only.
Now we are going to execute our cookbooks in Hosted Nodes through Chef server
-----------------
| Chef | ---------- Node 1
[CookBooks] ----> | Server | ---------- Node 2
(Programmer Mechine) | | --------- Node 3------------------
Chef Server : Create Online Chef server
1.Create Chef server by going Hosted Chef Server - Manage Chef
2.Login to Chef Server : https://manage.chef.io/
3.Administration > Oraganization >smlcodes > Actions > Starter Kit > Download Starter Kit
4.Extract Downloaded zip (Desktop/DevOps/chef/chef-starter)
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef-starter$ tree
.
└── chef-repo
├── cookbooks
│ ├── chefignore
│ └── starter
│ ├── attributes
│ │ └── default.rb
│ ├── files
│ │ └── default
│ │ └── sample.txt
│ ├── metadata.rb
│ ├── recipes
│ │ └── default.rb
│ └── templates
│ └── default
│ └── sample.erb
├── README.md
└── roles
└── starter.rb
10 directories, 8 files
5.https://supermarket.chef.io/where all the user created cookbooks are stored.
For example Search for learn_chef_apache2,
we are going to use above cookbook in this example
6. Download cookbook using Knife
i. Go to /DevOps/chef/chef-starter/chef-repo & run knife command to download cookbook.satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef-starter/chef-repo$ii.Exatract tar file to cookbooks folder
knife cookbook site download learn_chef_apache2
Downloading learn_chef_apache2 from Supermarket at version 0.3.0 to /home/satya/Desktop/DevOps/chef/chef-starter/chef-repo/learn_chef_apache2-0.3.0.tar.gz
Cookbook saved: /home/satya/Desktop/DevOps/chef/chef-starter/chef-repo/learn_chef_apache2-0.3.0.tar.gz
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef-starter/chef-repo$ lsAfter Extraction remove .tar file
cookbooks learn_chef_apache2-0.3.0.tar.gz README.md roles
satya@satya-Aspire-E5-523:~/Desktop/DevOps/chef/chef-starter/chef-repo$
tar -zxxvf learn_chef_apache2-0.3.0.tar.gz -C cookbooks
learn_chef_apache2/
learn_chef_apache2/.kitchen.yml
learn_chef_apache2/Berksfile
learn_chef_apache2/Berksfile.lock
learn_chef_apache2/chefignore
learn_chef_apache2/metadata.json
learn_chef_apache2/metadata.rb
learn_chef_apache2/README.md
learn_chef_apache2/recipes/
learn_chef_apache2/templates/
learn_chef_apache2/templates/default/
learn_chef_apache2/templates/default/index.html.erb
learn_chef_apache2/recipes/default.rb
rm -rf learn_chef_apache2-0.3.0.tar.gz
Check learn_chef_apache2/recipes/default.rb it contains same code we configue before
apt_update 'Update the apt cache daily' do
frequency 86_400
action :periodic
end
package 'apache2'
service 'apache2' do
supports :status => true
action [:enable, :start]
end
template '/var/www/html/index.html' do
source 'index.html.erb'
end
7.Upload CookBook to Chef Server
go to Desktop/DevOps/chef/chef-starter/chef-repo folder & upload cookbook to chef server
chef-repo> knife cookbook upload learn_chef_apache2
Uploading learn_chef_apache2 [0.3.0]
Uploaded 1 cookbook.
8.Check Uploaded CookBook in server > policy tab
Now we are going to manage our nodes throgh Chef Server
Nodes : Manage Nodes using Chef server
We have two nodes created using vagrant, up them[vagrant up, vagrant ssh]
- Ubuntu : vagrant@vagrant-ubuntu-trusty-64:
- CentOS : [vagrant@localhost ~]$
change the password using
sudo passwd ubuntu
(by default ubuntu
user has sudo-permissions with NOPASSWD
set)Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 80, host: 5555
config.vm.network "public_network"
end
1) wlp3s0 --> choose this
2) enp2s0f1
- IpAddrees - 192.168.0.105
- Username/pwd - ubuntu/ ubuntu
- IpAddrees - inet 192.168.0.107
- Username/pwd - root / vagrant
1.Go to Clinet workstation coomandline
/Desktop/DevOps/chef/chef-starter/chef-repo
chef-repo> knife bootstrap 192.168.0.105 --ssh-user ubuntu --ssh-password ubuntu --sudo --use-sudo-password --node-name cnode1 --run-list 'recipe[learn_chef_apache2]'
Node cnode1 exists, overwrite it? (Y/N) y
Client cnode1 exists, overwrite it? (Y/N) y
Creating new client for cnode1
Creating new node for cnode1
Connecting to 192.168.0.105
192.168.0.105 -----> Existing Chef installation detected
192.168.0.105 Starting the first Chef Client run...
192.168.0.105 Starting Chef Client, version 11.8.2
192.168.0.105 resolving cookbooks for run list: ["learn_chef_apache2"]
192.168.0.105 Synchronizing Cookbooks:
192.168.0.105 - learn_chef_apache2
192.168.0.105 Compiling Cookbooks...
192.168.0.105 Converging 3 resources
192.168.0.105 Recipe: learn_chef_apache2::default
192.168.0.105 * package[apache2] action install
192.168.0.105 - install version 2.4.7-1ubuntu4.20 of package apache2
192.168.0.105
192.168.0.105 * service[apache2] action enable
192.168.0.105 - enable service service[apache2]
192.168.0.105
192.168.0.105 * service[apache2] action start (up to date)
192.168.0.105 * template[/var/www/html/index.html] action create
192.168.0.105 - update content in file /var/www/html/index.html from 538f31 to ef4ffd
192.168.0.105 --- /var/www/html/index.html 2018-09-27 18:46:21.787423744 +0000
192.168.0.105 +++ /tmp/chef-rendered-template20180927-2085-27sx61 2018-09-27 18:46:35.617936153 +0000
192.168.0.105 @@ -1,379 +1,6 @@
vagrant@vagrant-ubuntu-trusty-64:~$ curl localhost2.Working with Node 2 - CentOS (192.168.0.107)
<html>
<body>
<h1>hello world</h1>
</body>
</html>
1.Go to Clinet workstation coomandline
/Desktop/DevOps/chef/chef-starter/chef-repo
chef-repo> knife bootstrap 192.168.0.107 --ssh-user vagrant --ssh-password vagrant --sudo --use-sudo-password --node-name cnode2 --run-list 'recipe[learn_chef_apache2]'
`rescue in new_session': Authentication failed for user vagrant@192.168.0.107@192.168.0.107 (Net::SSH::AuthenticationFailed)
ERROR: Net::SSH::AuthenticationFailed: Authentication failed for user vagrant@192.168.0.107@192.168.0.107
Fixed it!
So when you are using hosted chef you need to pass in a private key with the bootstrap and have the public key in your autherized_keys file....
- install the ChefSDK
- SCP your starter kit from hosted Chef
- extract the starter kit to
~/chef-repo
- generate a new keypair:
ssh-keygen
- add the public key to your autherized_keys file:
$ cat id_rsa.pub >> authorized_keys
- run the knife bootstrap with the following:
sudo knife bootstrap {{server-ip}} --ssh-user {{your-server-user}} -i ~/.ssh/id_rsa --sudo --node-name web1
That should work!
I would also suggest that the user you pass as the --ssh-user has passwordless sudo access.
4.Go To Node2-CentOS Terminal, Check the Home page
More on Chef
To get no.of Nodes in Chef server
chef-repo> knife node list
cnode1
Get more Node Info
chef-repo> knife node show cnode1
Node Name: cnode1
Environment: _default
FQDN: vagrant-ubuntu-trusty-64
IP: 10.0.2.15
Run List: recipe[learn_chef_apache2]
Roles:
Recipes: learn_chef_apache2
Platform: ubuntu 14.04
Tags: