Before you start

  1. Complete the first sections of the walkthrough, Getting started with the Chef cookbooks for Liberty profile up to the end of the section called “Set up git, and install the cookbooks and their dependencies”.
  2. Download Java 7 and the Liberty profile runtime jar, as specified in the section “Bootstrap the VM so it can poll the Chef server for configuration updates”

Prepare your chef-repo

  1. Commit the previous work into git. Run the following commands:
    $ git add .
    $ git commit -m "Completed Getting started with the Chef cookbooks for Liberty profile"
    
  2. Download an additional cookbook, this one is not available through knife but you can download it from github at https://github.com/WASdev/ci.chef.wlp.samples.
  3. Place the cookbook into the cookbooks folder in your chef-repo then upload it with the command:
    $ knife cookbook upload ci.chef.wlp.samples
    
  4. If you have any of your Vagrant-managed VMs running shut them down now.

Configure and bring up VirtualBox using Vagrant

In this section we will create three virtual machines: Two web servers that will run Liberty profile and an apache2 server that will load balance between them.

  1. Edit the Vagrantfile with the following contents:
    Vagrant.configure("2") do |config|
      config.vm.box = "opscode-ubuntu-12.04-i386"
      config.vm.box_url = "https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04-i386_provisionerless.box"
      config.ssh.forward_agent = true
    
    config.vm.define "webserver1" do |webserver1|
          config.vm.hostname = "webserver1"
          config.vm.network "private_network", ip: "192.168.50.4",
          virtualbox__intnet: "intnet"
      end
    config.vm.define "webserver2" do |webserver2|
          config.vm.hostname = "webserver2"
          config.vm.network "private_network", ip: "192.168.50.5",
          virtualbox__intnet: "intnet"
      end
    
    config.vm.define "balancer" do |balancer|
          config.vm.hostname = "balancer"
          config.vm.network :forwarded_port, guest: 80, host: 9090
          config.vm.network "private_network", ip: "192.168.50.6",
          virtualbox__intnet: "intnet"
      end
    end

    You will notice that only balancer has a forwarded port. The only way to access Liberty profile on webserver1 and webserver2 is by using the proxy we will install on balancer to access the webservers on the vm’s private network. We will not add an entry in Vagrantfile for the VM you created in Getting started with the Chef cookbooks for Liberty profile. at this time.

  2. Now bring up all three machines:
    vagrant up webserver1 webserver2 balancer

Create a Chef role and run it on the web servers

A Chef role is a configuration designed to be used across multiple machines.

  1. Create a Chef role for our web servers by running the following command:
    $ knife role create BalancedWebcluster1
  2. The configuration for your new role will open in your editor of choice. Replace the contents with the following:
    {
      "name": "BalancedWebcluster1",
      "description": "",
      "json_class": "Chef::Role",
      "default_attributes": {
        "wlp": {
          "archive": {
            "accept_license": "true",
            "base_url": "file:///vagrant/"
          }
        },
        "java": {
          "install_flavor": "ibm",
          "ibm": {
            "url": "file:///vagrant/ibm-java-i386-sdk-7.1-0.0.bin",
            "accept_ibm_download_terms": "true"
          }
        }
      },
      "override_attributes": {
      },
      "chef_type": "role",
      "run_list": [
        "recipe[apt]",
        "recipe[wlp]",
        "recipe[ci.chef.wlp.samples-master::proxyTest]"
      ],
      "env_run_lists": {
      }
    }
  3. Run Chef to set up both web servers by running the following commands (as they might take a while you can run them at the same time):
    $ knife bootstrap localhost  --ssh-user vagrant --ssh-password vagrant --sudo --run-list "role[BalancedWebcluster1]" --ssh-port <SSH port of webserver1>
    $ knife bootstrap localhost  --ssh-user vagrant --ssh-password vagrant --sudo --run-list "role[BalancedWebcluster1]" --ssh-port <SSH port of webserver2>
    

    (To get the port number, run vagrant ssh-config .)

Configure the Apache load balancer

The load balancer needs a node configuration:

  1. Install Chef on the VM:
    $ knife bootstrap localhost  --ssh-user vagrant --ssh-password vagrant --sudo --run-list "recipe[apt]" --ssh-port <SSH port of balancer>
  2. When the installation is finished run:
    $ knife node edit balancer
  3. Enter the following configuration:
    {
      "name": "balancer",
      "chef_environment": "_default",
      "normal": {
        "wlp-samples": {
          "proxy": {
            "clusterName": "BalancedWebcluster1",
            "clusterNetworkInterface": "eth1"
          }
        },
        "tags": [
        ]
      },
      "run_list": [
        "recipe[apt]",
        "recipe[ci.chef.wlp.samples::proxy]"
      ]
    }
  4. Run Chef to apply your configuration:
    $ vagrant ssh balancer
    $ sudo chef-client

Try it out

Open a web browser and go to http://127.0.0.1:9090/iptest/showip.jsp. Refresh the page to see the load balancing in action.

We’d love your feedback. Post your questions on Stack Overflow, or open issues in our GitHub repositories: WASdev/ci.chef.wlp and WASdev/ci.chef.wlp.application.

To learn more about and experiment with Chef take a look at the Opscode #learnchef page.

Join The Discussion

Your email address will not be published. Required fields are marked *