[Ubuntu] Use Chef-solo to Launch and Deploy to AWS EC2

[Ubuntu] Use Chef-solo to Launch and Deploy to AWS EC2

What’s Chef?

According to here, Chef is a systems and cloud infrastructure automation framework that makes it easy to deploy servers and applications to any physical, virtual, or cloud location, no matter the size of the infrastructure.

chef_overview


If I want to use chef to deploy something, I need to set up my workstation. A workstation is a computer that is configured to run Knife, to synchronize with the chef-repo, and interact with a single Chef server. The workstation is the location from which most users will do most of their work, including:

  • Developing cookbooks and recipes (and authoring them using Ruby)
  • Keeping the chef-repo synchronized with version source control
  • Using Knife to upload items from the chef-repo to the Chef server
  • Configuring organizational policy, including defining roles and environments and ensuring that critical data is stored in data bags
  • Interacting with nodes, as (or when) required, such as performing a bootstrap operation

What’s Knife?

Knife is a command-line tool that provides an interface between a local chef-repo and the Chef server. But today I just want to launch and deploy something on AWS EC2, if I use Chef-Solo, then there is no need to set up a Chef server.

I will step by step to teach you how to use Chef-Solo to launch AWS EC2 and deploy, I assume you’ve already run Ruby on your Ubuntu and prepare your AWS EC2 KEY.

Install the required tools

$ gem install knife-solo knife-ec2

$ knife --version

Chef: 11.12.0.rc.1

Create and initial a project is named “chef-project”

$ knife solo init chef-project

WARNING: No knife configuration file found
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
Setting up Berkshelf...

$ cd chef-project
$ tree
.
├── Berksfile
├── cookbooks
├── data_bags
├── environments
├── nodes
├── roles
└── site-cookbooks

Create a Knife configuration file

$ cd chef-project/
$ knife configure -r . --defaults

Configure EC2 security

$ vim .chef/knife.rb

#AWS config
knife[:aws_ssh_key_id]        = 'AWS_KEY_ID'
knife[:aws_access_key_id]     = 'ACCESS_KEY_ID'
knife[:aws_secret_access_key] = 'SECRET_ACCESS_KEY'
knife[:chef_mode]             = 'solo'
knife[:flavor]                = 'm1.small'
knife[:region]                = 'AWS_REGION'
knife[:availability_zone]     = 'AWS_AVAILABILITY_ZONE'
knife[:ssh_user]              = 'ubuntu'
knife[:groups]                = 'default'
knife[:image]                 = 'AMI'

Modify the Knife EC2 plugin
Because the EC2 plugin for Knife is maintained by Opscode and is designed to be used with Chef Server. This means that once a new server has been launched on Amazon, Knife will then attempt to register it with Chef Server and bootstrap it with Chef.[2][3]

$ mkdir -p ~/.chef/plugins/knife/
$ git clone https://gist.github.com/2049991.git

Then copy ec2_server_create.rb into ~/.chef/plugins/knife/

You can use command line to check ec2 server.

$ knife ec2 server list

Instance ID  Name  Public IP      Private IP     Flavor     Image        SSH Key  Security Groups  IAM Profile  State  
i-XXXXXXXX   Chef  XXX.XXX.XX.XX  XX.XXX.XXX.XX  m1.medium  ami-XXXXXXX  knife    default                     running

Lanuch EC2 instance

$ knife ec2 server create --identity-file ~/.ssh/AWS_KEY_ID.pem

Instance ID: i-XXXXXXXX
Flavor: m1.small
Image: ami-XXXXXXX
Region: us-west-1
Availability Zone: us-west-1a
Security Groups: default
SSH Key: AWS_KEY_ID

Waiting for server............
Public DNS Name: ec2-xx-xx-xx-xx.us-west-1.compute.amazonaws.com
Public IP Address: XXX.XXX.XX.XX
Private DNS Name: ip-xx-xxx-xxx-xxx.us-west-1.compute.internal
Private IP Address: XXX.XXX.XX.XX

Waiting for sshd.done

Instance ID: i-XXXXXXXX
Flavor: m1.small
Image: ami-XXXXXXX
Region: us-west-1
Availability Zone: us-west-1a
Security Groups: default
SSH Key: AWS_KEY_ID
Root Device Type: ebs
Root Volume ID: vol-bd5289bd
Root Device Name: /dev/sda1
Root Device Delete on Terminate: true
Public DNS Name: ec2-xx-xx-xx-xx.us-west-1.compute.amazonaws.com
Public IP Address: XXX.XXX.XX.XX
Private DNS Name: ip-xx-xxx-xxx-xxx.us-west-1.compute.internal
Private IP Address: XXX.XXX.XX.XX
Environment: _default

Deploy something to EC2
Prepare *.json, this *.json must be named as same as your EC2 instance public DNS name, such as ec2-xx-xx-xx-xx.us-west-1.compute.amazonaws.com.json, and put it in node directory. Of course you need to prepare appropriate recipe in cookbook.

$ vim nodes/ec2-xx-xx-xx-xx.us-west-1.compute.amazonaws.com.json

{
  "run_list":[
    "recipe[COOKBOOK]"
  ]
}

Bootstrap Chef-client on EC2 server, and use chef-client to run recipe

$ knife solo bootstrap ubuntu@ec2-xx-xx-xx-xx.us-west-1.compute.amazonaws.com --identity-file ~/.ssh/AWS_KEY_ID.pem

Bootstrapping Chef...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14401  100 14401    0     0  46382      0 --:--:-- --:--:-- --:--:-- 58779
Downloading Chef 11.10.4 for ubuntu...
...
...
Generating solo config...
Running Chef...
Starting Chef Client, version 11.10.4
Compiling Cookbooks...
...
...

If you have any revision on the cookbook, just cook, cook, and cook on EC2 instance.

$ knife solo cook ubuntu@ec2-xx-xx-xx-xx.compute.amazonaws.com --identity-file ~/.ssh/AWS_KEY_ID.pem 

Reference:
[1]http://docs.opscode.com/plugin_knife_ec2.html
[2]http://markbirbeck.com/blog/2012/03/16/using-knife-to-launch-ec2-instances-without-a-chef-server/
[3]https://gist.github.com/markbirbeck

(Visited 70 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail
Comments are closed.