It happens — you’re on a server that just can’t be upgraded any further, and you need more resources. Or, you need to backup a Chef server. Or, you need to setup a QA instance. Or, you need to finally migrate from Chef 10 to Chef 11. Or, you have one of many other possible reasons, but you need to be able to stand up a new Chef instance, and not have to do a ton of work. If any of that applies to you, then this post is for you.
In the case where you’re migrating from one Chef server to another (i.e., the old one is going bye-bye), it would be very helpful to have your Chef server be CNAMEd (e.g. chef.company.com -> vm101.iad.company.com) or behind a load balancer/proxy where you can change targets easily. That way, you won’t need to update the client configs, and it’ll be an easy swap. Everything should “just work” ™.
First, we’ll make a copy of your knife.rb:
|
cp -a ~/.chef/knife{,-orig}.rb |
Now, we’ll need to get access to your new Chef server via knife. You can do so by logging in as admin, and regenerating and saving a new private key. You can also create a new user here instead of using admin, but I advise against this, as any user you create will conflict with users of the same name from the old server. Yes, that means that if you’ve been using ‘admin’ as the main user, you may run into problems (but let’s just hope that you’ve been using per-person accounts).
Now, we’ll update your current knife.rb to reflect the new node information in it:
|
... node_name 'admin' client_key '/Users/user/.chef/new-server-admin.pem' chef_server_url 'https://vm102.iad.company.com' ... |
It wouldn’t hurt to check that you have access to the new node by doing a
knife user list .
Now, we’ll need to download all of the data from the “old” Chef server. To do so, we’ll be using the nifty ‘knife backup‘ plugin. To get it installed on OS X, I did:
|
sudo gem install knife-backup |
Now, to finally back things up, we’ll do:
|
knife backup export -D ~/chef-backup/ -c ~/.chef/knife-orig.rb |
Note that the argument after -D is the destination directory where all of the Chef data will go; this directory will automatically be created for you. The argument of -c tells knife which config file to use; we’ll, of course, be using the “old” server here. Also, if you only need to backup a certain set of data from your Chef server (e.g. only users and environments), you can specify that. See the knife backup documentation for details.
Now that we have all the data we need, we’ll need to push it up to the new server. This works much the same as the export:
|
knife backup restore -D ~/chef-backup |
I left off the -c here because knife.rb is the default config file.
Once everything has been restored, your original user in Chef will now be available (you can verify this via the Chef Server UI). The amazing thing is that your keys have not changed, and can be used as-is. Chef Server keeps track of your public keys, so all of your private keys for all nodes/clients are still good.
This, now, is where you update your knife.rb to reflect your original user settings. If you’re running behind a load balancer/proxy, you can simply use your original config as-is after replacing the old server with the new one. If you’re doing the CNAME/A record route, you can do the same once DNS has propagated. Otherwise, you can overwrite your new config with your old one, and edit it to reflect the new server’s URL.
If your nodes are pointing to the wrong server in their client.rb, you can use knife ssh with sed to find/replace the server URLs.
If you’ll be accessing multiple Chef servers frequently enough, I highly recommend looking at the knife block plugin. That way, you can switch between different configurations with ease, including those for Berkshelf.
Recent Comments