Someone is cooking in the kitchen....



Part of what makes chef tooling so powerful is its ability to test your product quickly and easily over a variety of different platforms. Using recipes and test-kitchen, a chef user can call on a variety of different drivers to push their cookbooks to EC2 instances, docker containers, VSphere and Azure instances in a matter of moments. If a driver exists for the platform, test-kitchen can be used for "local" deployment.

This article will talk about the deployment of infrastructure to EC2 instances through test-kitchen. Note however that EC2 is just an example case to get one started. Several other driver platforms can be found including ::

kitchen-allA driver for everything, or “all the drivers in a single Ruby gem”.
kitchen-blueboxA driver for Blue Box.
kitchen-cloudstackA driver for CloudStack.
kitchen-digitaloceanA driver for DigitalOcean.
kitchen-dockerA driver for Docker.
kitchen-dscA driver for Windows PowerShell Desired State Configuration (DSC).
kitchen-ec2A driver for Amazon EC2.
kitchen-fogA driver for Fog, a Ruby gem for interacting with various cloud providers.
kitchen-googleA driver for Google Compute Engine.
kitchen-hypervA driver for Hyper-V Server.
kitchen-joyentA driver for Joyent.
kitchen-linodeA driver for Linode.
kitchen-opennebulaA driver for OpenNebula.
kitchen-openstackA driver for OpenStack.
kitchen-pesterA driver for Pester, a testing framework for Microsoft Windows.
kitchen-rackspaceA driver for Rackspace.
kitchen-terraformA driver for Terraform.
kitchen-vagrantA driver for Vagrant. The default driver packaged with the Chef development kit.

This list is pulled directly from Chef's Kitchen docs page. Other drivers can also be found in the open community including kitchen-cloudformation which was used to some success for a recent project that my company worked on. 

In order to begin using test-kitchen you must install chefdk. This will not be covered here but I discussed these steps earlier in a previous blog post here. The kitchen-ec2 driver is installed with ChefDK by default and so no further work will be needed to setup the ec2 kitchen driver.

An AWS account will also be required to launch instances. 

REQUIREMENTS: 

  • AWS Account
  • ChefDK installed on your local computer 


First start by opening your Chef Development tools by double clicking on the link. 


Next, we will need to create a cookbook to run test kitchen on. We can do this by running the command chef generate cookbook [cookbook_name] . I will name this cookbook test_cookbook to keep it descriptive. 


When this command runs successfully you will see a number of things returned, including a section which says "Your cookbook is ready. Type 'cd [cookbook_name]' to enter it" 


Since actually designing these cookbooks is out of scope for now all we are really interested in is the kitchen.yml file. This file is what will give our cookbook the information needed to spin up our instance through test-kitchen. Using your favorite editor (we will be using visual studio code for this demo, which can be downloaded here)  open the new cookbook that you just created. 



As we can see in the explorer window there are a great number of files and directories from which to choose from. We will discuss these in a later blog post. What we are most worried about now is the kitchen.yml file. 


The kitchen.yml contains a number of different components and will provide a great deal of control when spinning up our instances. We are currently using the EC2 driver. It's full documentation can be found here. Since this is just a guide to get us started lets look at four specific locations; driver, transport, platform and suites. 






Lets walk through the components.

Driver 

name is the name of the driver that we are going to use to spin up our instance. In this case, as explained earlier, we will use the ec2 kitchen driver. This driver uses the aws sdk gem to provision and destroy EC2 instances.

Instance_type is the EC2 instance type (also known as size) to use. The default is t2.micro or t1.micro, depending on whether the image is hvm or paravirtual. (paravirtual images are incompatible with t2.micro.)

aws_ssh_key_id is The ID of the AWS key pair you want to use. The default will be read from the AWS_SSH_KEY_ID environment variable if set, or nil otherwise. If aws_ssh_key_id is specified, it must be one of the KeyName values shown by the AWS CLI: aws ec2 describe-key-pairs. Otherwise, if not specified, you must either have a user pre-provisioned on the AMI, or provision the user using user_data. This all gets very technical but luckily there are some good instructions on how to pull down these keys here.

security_group_ids these are an Array of EC2 security groups which will be applied to the instance. The default is ["default"].

Transport

ssh_key is The private key file for the AWS key pair you want to use. This will allow you to ssh into your instance to run your recipe during the kitchen converge stage and using the kitchen login

Platforms

name is the way to specify the image you are wanting to run on your instance. 

Suites

suits a collection of test suites, with each suite_namegrouping defining an aspect of a cookbook to be tested. Each suite_name must specify a run-list, for example

name the name of the suite that we are going to run. 

run_list what we want run on the instance when it is up and running. 


All together our kitchen.yml will look something like this. 



This .yml says we want to provision a ubuntu-16.04 server on a t2.medium instance. We will use the key my_key as our aws ssh key to provision the instance. This key will also be used to ssh into the instance once it is provisioned. The instance will have two security groups included (As part of an array), thus the [], and will run the default recipe for our cookbook. 

Since our cookbook default recipe is completely empty this will be a relatively boring test but we will run it anyway just to get a feel for what test-kitchen will do. 

First we will launch chefDK again if we have closed it by going to the icon. 


Navigate to the root folder of our project. For this case it was test_cookbook, yours may differ. 



Next we will run berks install this will download any cookbook dependencies that we might need to get our current cookbook to run. While we are not layering or wrapping any cookbooks currently, this is a good habit to get into so you don't run into problems in the future. NOTE: If you have already run berks install on a cookbook you will run berks upgrade from that point on. Berks install is only for the initial run.


Unfortunately, my connection was not being friendly when this was written, but berks did complete. Next we will actually run test-kitchen. From your command line run kitchen converge. This command will spin up the instance and then install your cookbook on top of it.


If everything is setup appropriately you should start to see movement in your command prompt


After everything is deployed and setup you should get the return "Kitchen is finished" letting you know that everything deployed successfully.


in order to log in to our instance we will run kitchen login



Once in our instance we can make any changes necessary and verify our instance. Type exit to get back out.


Finally, to destroy the instance we will type kitchen destroy. 


This is just the very beginning steps of what can be done with kitchen. Test kitchen is a powerful tool for testing cookbooks locally before pushing into staging or production boxes. 
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment