25 August 2015

Setting up Wildfly 9.0 using Chef on Centos



Introduction

The aim of this article is to provide a dive into Chef for the impatient. It takes you through the setup of the various chef components and how to use it to install and configure WildFly 9.0.1 in standalone mode.

Subsequent posts will address:
  • How to use Chef to install and configure WildFly 9.0.1 in domain mode.
  • Chef Best practices
  • Using Ansible to accomplish the above tasks


Required Environment:


Server /
Virtual Machine
Hostname
IP
OS
Chef server
**chefserver.c2b2.net
192.168.33.70
Centos-6.6-x86_64 miminal
 (Chef Management console did not install properly on Centos 7)
Chef workstation
**chefworkstation.c2b2.net
192.168.33.71
Centos-7.1.1503-x86_64 miminal
Chef nodes
**wildfly.c2b2.net
192.168.33.72
Centos-7.1.1503-x86_64 miminal




**Any hostname of your choice



Minimum requirements for a standalone deployment of chef server:

i. 4 total cores, 2.0 GHz AMD 41xx/61xx or Intel Xeon 5000/E5 CPUs

ii. 4 GB of RAM; 8 GB of RAM for Chef analytics

iii. 5 GB of free disk space in /opt

iv. 5 GB of free disk space in /var  


Our Goal:

To install

i. Chef server: chef-server-core-12.1.2-1.el6.x86_64.rpm

ii. Chef workstation: chefdk-0.6.2-1.el7.x86_64.rpm

iii. Chef Node: chef-12.4.1-1.el7.x86_64.rpm

iv. Standalone wildfly-9.0.1.Final on a node

Assumptions:

i. We are installing on a server within an environment with direct access to the internet.

ii. All the installation is done by a user with sudo access.

iii. The above servers are able to communicate with each other


Install and Configure Chef server


Target machine:
Chef server
**chefserver.c2b2.net
192.168.33.70

1.      Update the OS
sudo yum update
sudo yum install epel-release

2.      Download the package related to your OS from https://downloads.chef.io/chef-client/redhat/#/.


3.      Install the downloaded rpm
sudo rpm -Uvh chef-server-core-12.1.0-1.el7.x86_64.rpm
The Chef server will be installed at /opt/opscode/.
4.      Start all of the services:
sudo chef-server-ctl reconfigure

For a successful installation, you should have a text similar to the following in the console:
Recipe: <Dynamically Defined Resource>
  * service[nginx] action restart
    - restart service service[nginx]
Recipe: private-chef::nginx
  * execute[restart_nginx_log_service] action run
    - execute /opt/opscode/embedded/bin/sv restart /opt/opscode/sv/nginx/log
 
Running handlers:
Running handlers complete
Chef Client finished, 411/476 resources updated in 194.465004981 seconds
opscode Reconfigured!

5.      Create an administrator. :
Syntax: “chef-server-ctl user-create user_name first_name last_name email password --filename FILE_NAME”
cd ~/.ssh
sudo chef-server-ctl user-create admin Kanmi Durotoye admin@test.c2b2.net default --filename admin.pem

6.      Create an organization:
Syntax: “chef-server-ctl org-create short_name "full_organization_name" --association_user user_name --filename ORGANIZATION-validator.pem
sudo chef-server-ctl org-create c2b2 "C2B2, Inc." --association_user admin --filename c2b2-validator.pem

7.      Install the management console (Chef Manage). This is used to manage data bags, attributes, run-lists, roles, environments, and cookbooks from a web user interface.

Ensure that port 443 is open
sudo iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

sudo chef-server-ctl install opscode-manage
sudo chef-server-ctl reconfigure
sudo opscode-manage-ctl reconfigure

8.      Test your installation
sudo chef-server-ctl test

You can also test your installation by browsing to:


Install and Configure Chef Workstation


Target machine:
Chef workstation
**chefworkstation.c2b2.net
192.168.33.71
To install the Chef development kit:
1.      Update the os
sudo yum update
sudo yum install epel-release
2.      Download the sdk for your platform at http://downloads.chef.io/chef-dk/.
3.      Install the downloaded rpm by following the command:
sudo rpm -Uvh /tmp/chefdk-0.6.2-1.el7.x86_64.rpm 
4.      The Chef development kit is installed to /opt/chefdk/ on UNIX and Linux systems.
5.      Verify your installation
 [root@chefworkstation ~]# chef verify

Setup System Ruby
$ which ruby
which will return something like /usr/bin/ruby.
echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile
The following worked for me. I appended the text below at the end of my ~/.bash_profile file
export PATH="/opt/chefdk/bin:/root/.chefdk/gem/ruby/2.1.0/bin:/opt/chefdk/embedded/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
export GEM_ROOT="/opt/chefdk/embedded/lib/ruby/gems/2.1.0"
export GEM_HOME="/root/.chefdk/gem/ruby/2.1.0"
export GEM_PATH="/root/.chefdk/gem/ruby/2.1.0:/opt/chefdk/embedded/lib/ruby/gems/2.1.0"

source ~/.bash_profile
6.      Install git
sudo yum install git
Set the name and email that git will use to tag any commits you make. This is a requirement for git to accept commits. We set this globally so that any git repo we create will use these values:
git config --global user.name "Kanmi Durotoye"
git config --global user.email admin@test.c2b2.net
7.      Set up the chef-repo
cd ~
git clone git://github.com/chef/chef-repo.git
and then clone the chef-repo:
git clone git://github.com/chef/chef-repo.git
8.      Create .chef Directory
mkdir -p ~/chef-repo/.chef
Add .chef to the .gitignore file to prevent uploading the contents of the .chef folder to GitHub. For example:
$ echo '.chef' >> ~/chef-repo/.gitignore
Add all of the modified files to the current staging area:
cd ~/chef-repo
git add .
Commit the changes. We will use the -m flag to specify an in-line commit message describing the changes we are making:
git commit -m "Excluding the ./.chef directory from version control"
Update /etc/hosts with the details below:
192.168.33.70 chefserver.c2b2.net
9.      Create the knife.rb File
touch ~/chef-repo/.chef/knife.rb
Insert the following text
current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "admin"
client_key               "#{current_dir}/admin.pem"
validation_client_name   "c2b2-validator"
validation_key           "#{current_dir}/c2b2-validator.pem"
chef_server_url          "https://chefserver.c2b2.net/organizations/c2b2/"
syntax_check_cache_path  "#{ENV['HOME']}/.chef/syntaxcache"
cookbook_path            ["#{current_dir}/../cookbooks"]

copy admin.pem and c2b2-validator.pem from the Chefserver to ~/chef-repo/.chef on the chefworkstation
scp admin.pem username@192.168.33.71:~/chef-repo/.chef
scp c2b2-validator.pem  username@192.168.33.71:~/chef-repo/.chef
10.   Add Ruby to $PATH
echo 'export PATH="/opt/chefdk/embedded/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile
11.   Get SSL Certificates
knife ssl fetch
12.   Verify Install
cd ~/chef-repo
In a command window, enter the following:
knife client list
13.   Set up the Cookbook
We are going to reuse the following cookbooks from the Chef supermarket: apt, yum. Java and WildFly.
       i.          Still on the workstation, get required cookbooks from chef market:
cd ~/chef-repo/cookbooks
knife cookbook site download apt
knife cookbook site download yum
knife cookbook site download java
knife cookbook site download wildfly
     ii.          Extract the cookbooks:
tar xvf apt-2.8.1.tar.gz
tar xvf yum-3.6.3.tar.gz
tar xvf java-1.35.0.tar.gz
tar xvf wildfly-0.1.19.tar.gz
    iii.          Upload Cookbooks to chefserver
knife upload apt
knife upload yum
knife upload java
knife upload wildfly
14.   Create wrapper cookbook for WildFly,
i.                 Create the wrapper cookbook called custom-wildfly (or any other name of your choice).
cd ~/chef-repo/cookbooks
knife cookbook create custom-wildfly
cd ~/chef-repo/cookbooks/custom-wildfly/attributes
touch default.rb
touch java.rb

ii.               Edit java.rb
vi ~/chef-repo/cookbooks/custom-wildfly/attributes/java.rb 

insert the following text
# => Java Configuration
default['java']['install_flavor'] = 'oracle'
default['java']['oracle']['accept_oracle_download_terms'] = true
default['java']['jdk_version'] = '8'
 
default['java']['jdk']['8']['x86_64']['url'] = 'http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.tar.gz'
default['java']['jdk']['8']['x86_64']['checksum'] = 'd7149012cc51c2cdb8d3a1767825e0dfc36ca0419c3dae56d993cb2732cbeff1' #sha256
default['java']['java_home'] = "/usr/bin/java/latest"
iii.             Edit default.rb
vi ~/chef-repo/cookbooks/custom-wildfly/attributes/default.rb

insert the following text
# => Wildfly Configuration
# => Source
default['wildfly']['version'] = '9.0.1'
default['wildfly']['url'] = 'http://download.jboss.org/wildfly/9.0.1.Final/wildfly-9.0.1.Final.tar.gz'
default['wildfly']['checksum'] = '8f570f978dff65a006c1c6cb58c26a3856b19f01dad5b2a3ef9e463c8dee2a54'
 
# => Base Directory
default['wildfly']['base'] = '/opt/wildfly'
 
# => Wildfly Deployment Type (standalone or domain)
default['wildfly']['mode'] = 'standalone'
 
# => Interface Configuration
# => Should probably put a proxy in front of these... Maybe NginX?
default['wildfly']['int']['mgmt']['bind'] = '192.168.33.72'
default['wildfly']['int']['mgmt']['http_port'] = '9990'
default['wildfly']['int']['mgmt']['https_port'] = '9993'
 
default['wildfly']['int']['pub']['bind'] = '192.168.33.72'
default['wildfly']['int']['pub']['http_port'] = '8080'
default['wildfly']['int']['pub']['https_port'] = '8443'
 
default['wildfly']['int']['wsdl']['bind'] = '192.168.33.72'
default['wildfly']['int']['ajp']['port'] = '8009'
iv.             Edit the recipe default.rb
vi ~/chef-repo/cookbooks/custom-wildfly/recipes/default.rb

include text
include_recipe "wildfly"
v.               Edit metadata.rb
vi ~/chef-repo/cookbooks/custom-wildfly/metadata.rb

include text similar to the following:
name             'custom-wildfly'
maintainer       'Kanmi Durotoye'
maintainer_email 'kanmi@durotoye.com'
license          'All rights reserved'
description      'Installs/Configures my_wildfly'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version          '0.1.0'
 
depends "java"
depends "wildfly"
vi.             Edit knife.rb
vi ~/chef-repo/.chef/knife.rb

Ensure that your content is similar to:
current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "admin"
client_key               "#{current_dir}/admin.pem"
validation_client_name   "c2b2-validator"
validation_key           "#{current_dir}/c2b2-validator.pem"
chef_server_url          "https://chefserver.c2b2.net/organizations/c2b2/"
syntax_check_cache_path  "#{ENV['HOME']}/.chef/syntaxcache"
cookbook_path            ["#{current_dir}/../cookbooks"]
knife[:editor]="vi"
vii.           Compose your runlist by adding java and custom-wildfly.
knife node edit wildfly.c2b2.net

Include the following:
 {
  "name": "wildfly.c2b2.net",
  "chef_environment": "_default",
  "normal": {
    "tags": [
 
    ]
  },
  "run_list": [
  "recipe[java]",
  "recipe[custom-wildfly]"
]
 
}
viii.         Save all the above updates in custom-wildfly unto the chefserver
~/chef-repo/cookbooks
knife upload custom-wildfly


Install and configure Chef Node


Target machine:
Chef nodes
**wildfly.c2b2.net
192.168.33.72

1.      Include the following in /etc/hosts:
192.168.33.70 chefserver.c2b2.net

2.      Switch back to Chef workstation
cd ~/chef-repo
knife bootstrap 192.168.33.72 -x username -P password --sudo 

3.      Switch back to the chef node
4.      Execute the runlist
sudo chef-client

5.      Test your installation








References:


https://nylas.com/blog/graduating-past-playbooks


Kanmi Durotoye
Senior Consultant