a user's perspective on saltstack and other configuration management tools

46
An introduction to infrastructure management with SaltStack Aurélien Géron - 06/2013

Upload: saltstack

Post on 11-May-2015

12.773 views

Category:

Technology


4 download

DESCRIPTION

Aurelien Geron uses SaltStack to manage a few VMs running Django web apps based on a sharded mongodb cluster. He had struggled with another configuration management tool for months but then read about Saltstack and decided to try it out. For Aurelien SaltStack just works, it's plain and simple, powerful, configurable and ultra-fast. This is his presentation.

TRANSCRIPT

Page 1: A user's perspective on SaltStack and other configuration management tools

An introduction to infrastructure management with SaltStack

Aurélien Géron - 06/2013

Page 2: A user's perspective on SaltStack and other configuration management tools

Overview

Page 3: A user's perspective on SaltStack and other configuration management tools

• Hardware & network

• Configure cloud & spawn VMs

• O.S. & softwares (install, config, updates)

• Scheduled tasks (backups, clean logs...)

• Manual tasks (deploy app, reboot...)

• Monitoring

• Graphs

• ...

Infrastructure management is...

Page 4: A user's perspective on SaltStack and other configuration management tools

Config management tools• Hardware & network

• Configure cloud & spawn VMs

• O.S. & softwares (install, config, updates)

• Scheduled tasks (backups, clean logs...)

• Manual tasks (deploy app, reboot...)

• Monitoring

• Graphs

• ...

Page 5: A user's perspective on SaltStack and other configuration management tools

• Hardware & network

• Configure cloud & spawn VMs

• O.S. & softwares (install, config, updates)

• Scheduled tasks (backups, clean logs...)

• Manual tasks (deploy app, reboot...)

• Monitoring

• Graphs

• ...

Remote control tools

rake

Page 6: A user's perspective on SaltStack and other configuration management tools

• Hardware & network

• Configure cloud & spawn VMs

• O.S. & softwares (install, config, updates)

• Scheduled tasks (backups, clean logs...)

• Manual tasks (deploy app, reboot...)

• Monitoring

• Graphs

• ...

All-in-one tools

Page 7: A user's perspective on SaltStack and other configuration management tools

• Hardware & network

• Configure cloud & spawn VMs

• O.S. & softwares (install, config, updates)

• Scheduled tasks (backups, clean logs...)

• Manual tasks (deploy app, reboot...)

• Monitoring

• Graphs

• ...

A full stack example

statsd

salt-cloud

Page 8: A user's perspective on SaltStack and other configuration management tools

Change configExecute script SSH

For example, with:

Control strategies

+ Simple+ No daemon- Slow- No CMDB

Page 9: A user's perspective on SaltStack and other configuration management tools

Scheduled updates

CMDB

Upload config & scripts

For example, with:

Control strategies

+ Centralized- Super slow

Page 10: A user's perspective on SaltStack and other configuration management tools

Manual update

CMDB

Go !

Control strategiesUpload config

& scripts

+ Centralized- Slow- Complicated

For example, with:

Page 11: A user's perspective on SaltStack and other configuration management tools

CMDB

Upload config & scripts

Go !

SSH

For example, with:

Control strategies

+ Simple+ No daemon+ Centralized- Slow

Page 12: A user's perspective on SaltStack and other configuration management tools

Control strategies

Permanent encrypted connection (AES/

ØMQ)

CMDB

Upload config & scripts

For example, with:

+ Simple+ Centralized+ Fast

Page 13: A user's perspective on SaltStack and other configuration management tools

Control strategies

Permanent encrypted connection (AES/

ØMQ)

CMDB

Go !

For example, with:

+ Simple+ Centralized+ Fast

Page 14: A user's perspective on SaltStack and other configuration management tools

Scalable topology

Master

MinionSyndic

MinionMinion

Page 15: A user's perspective on SaltStack and other configuration management tools

Enough with the overview, let’s get our

hands dirty now!

Page 16: A user's perspective on SaltStack and other configuration management tools

Installation : salt-minion

• Same one-liner on all platforms:wget -O - http://bootstrap.saltstack.org | sudo sh

• On Debian / Ubuntu, this script will add the appropriate apt repo and install the latest package

Page 17: A user's perspective on SaltStack and other configuration management tools

Installation : salt-master

• For the master, it’s the same one-liner as for the minions, plus (on Debian/Ubuntu):

apt-get install salt-master

Page 18: A user's perspective on SaltStack and other configuration management tools

Minion config• Config is in /etc/salt/minion

• By default, the minion connects to the master with hostname salt

• Edit config to change the master hostname or add the appropriate DNS entry (or add a salt entry to /etc/hosts)

• Restart minion :

service salt-minion restart

Page 19: A user's perspective on SaltStack and other configuration management tools

Master config

• Edit /etc/salt/master

• By default, it looks for minion config in:

/srv/salt/

• Default options are fine, actually

• Restart the master if you changed something:

service salt-master restart

Page 20: A user's perspective on SaltStack and other configuration management tools

Authorize minions• Minions generate their own key-pair upon

first startup, and send the public key to the master

• On the master, list the keys with:

salt-key -L (or -P for details)

•Keys are pending for authorization. Check them, then accept them with:

salt-key -A

•That’s it! We’re up and running. :-)

Page 21: A user's perspective on SaltStack and other configuration management tools

Remote control

• Let’s try executing a remote command

• Connect to the master and type:

salt '*' test.ping

•First argument = target minions

•Second argument = function to execute

•Other arguments = params for the function

Page 22: A user's perspective on SaltStack and other configuration management tools

Predefined modules• There are a bunch of predefined «execution

modules»

• List them with: salt '*' sys.doc

• For example, executing a shell command:

salt '*' cmd.run 'ls /'

• Python-style kwargs are supported, and arguments are parsed as YAML:

salt '*' cmd.run 'echo "Hello $CITY"' \

env='{CITY: "Salt Lake City"}' runas=joe

Page 23: A user's perspective on SaltStack and other configuration management tools

Running a script

• Put your script on the master in /srv/salt/

• Then run it!salt '*' cmd.script salt://myscript.sh

• Boy, that was a no-brainer, wasn’t it?

• Salt includes a simple file-server (it’s meant to sync configuration files, not terabytes)

Page 24: A user's perspective on SaltStack and other configuration management tools

Specifying targets

• Target is interpreted as a minion id glob:salt 'app_server_*' test.ping

• Minion id defaults to the minion’s FQDN, but you can change it in the minion’s config

• SaltStack also gives access to some of the minion’s attributes (CPU type, OS...), and you can target them. These attributes are called «grains»:salt -G 'os:Ubuntu' test.ping

Page 25: A user's perspective on SaltStack and other configuration management tools

Specifying targets• You can define groups in the master’s config (called

«nodegroups») and target them:salt -N app_servers test.ping

• You can target IPs and subnets:salt -S '10.1.2.0/24' test.ping

• You can target «pillars»: those are key/value pairs defined on the master and associated to minions.

• And finally you can mix all of the above using an «and/or» expression (this is called a «compound target»)

Page 26: A user's perspective on SaltStack and other configuration management tools

Home-made modules• A salt module is just a regular python module:# mathmagic.pydef pow(x, exp = 2): return x**exp

• Put it in /srv/salt/_modules/

• Synchronize the modules on the minions:'salt '*' saltutil.sync_modules

• Then run!salt '*' mathmagic.pow 5 exp=3

• Arguments are parsed as YAML, so the function receives integer arguments, not strings :-)

Page 27: A user's perspective on SaltStack and other configuration management tools

Salt states

Page 28: A user's perspective on SaltStack and other configuration management tools

SLS files• SaLt State files are an extension of the

modules system, designed to bring minions into a predefined state

• You define the desired states in SLS files. These are simple YAML files, such as:

vim: pkg.installednginx: pkg: - latest service.running: - watch: - file: /etc/nginx.conf

Page 29: A user's perspective on SaltStack and other configuration management tools

SLS syntax

• The following SLS fragment results in a call to the latest() function in the pkg state module, with "vim" passed as the first argument (the name argument):nginx: pkg.latest

• This is equivalent to:nginx: pkg: - latest

Page 30: A user's perspective on SaltStack and other configuration management tools

Postfix SLS examplepostfix: pkg: - installed service.running: - require: - pkg: postfix - watch: - file: /etc/postfix/main.cf

/etc/postfix/main.cf: file.managed: - source: salt://postfix/main.cf - require: - pkg: postfix

Page 31: A user's perspective on SaltStack and other configuration management tools

Postfix SLS examplepostfix: pkg: - installed service.running: - require: - pkg: postfix - watch: - file: /etc/postfix/main.cf

/etc/postfix/main.cf: file.managed: - source: salt://postfix/main.cf - require: - pkg: postfix

Calls pkg.installed("postfix")Calls service.running("postfix")...

...but only after postfix is installed

watch = require + if the state of the watched resource has changed (main.cf in this example) then calls the watching module’s mod_watch() function (in this example, service.mod_watch("postfix"), which will restart the postfix service).

Calls file.managed("/etc/postfix/main.cf", source="salt://postfix/main.cf")only after the postfix package is installed

Page 32: A user's perspective on SaltStack and other configuration management tools

Postfix SLS examplepostfix: pkg: - installed service.running: - require: - pkg: postfix - watch: - file: postfix_main_cf

postfix_main_cf: file.managed: - name: /etc/postfix/main.cf - source: salt://postfix/main.cf - require: - pkg: postfix

You may pass the name argument explicitely rather than defaulting to the parent key.

Page 33: A user's perspective on SaltStack and other configuration management tools

SLS templates• The SLS files go through a (configurable)

template engine, by default jinja

• This gives SLS files a lot of flexibility, for example:

{% set motd = ['/etc/motd'] %}{% if grains['os'] == 'Debian' %} {% set motd = ['/etc/motd.tail', '/var/run/motd'] %}{% endif %}

{% for motdfile in motd %}{{ motdfile }}: file.managed: - source: salt://motd{% endfor %}

Page 34: A user's perspective on SaltStack and other configuration management tools

Config files templates• The configuration files themselves can be

rendered through a template engine:

/etc/motd: file.managed: - source: salt://motd - template: jinja - defaults: message: 'Foo' {% if grains['os'] == 'FreeBSD' %} - context: message: 'Bar' {% endif %}

The motd file is actually a jinja template. In this example, it is passed the message variable and it can render it using the jinja syntax: {{ message }}

file.managed allows two dictionaries to be passed as arguments to the template: defaults and context. Values in context override those in defaults.

Page 35: A user's perspective on SaltStack and other configuration management tools

Applying an SLS file

• SLS files must be placed in /srv/salt/ or subdirectories

• You can apply an individual SLS formula like this:salt '*' state.sls myproject.mystate

The name of the SLS formula is the path of the SLS file (relative to /srv/salt/), without the .sls suffix, and with slashes replaced by dots.If the file is named init.sls, then .init can be omitted, for example the munin.node formula can be stored either in /srv/salt/munin/node.sls or in/srv/salt/munin/node/init.sls.

Page 36: A user's perspective on SaltStack and other configuration management tools

The «top» file• Instead of manually applying SLS files to minions,

you can define the special top.sls file

• It defines the list of SLS files that must be applied to each minion, for example:base: '*': - users - users.admin 'app_servers': - match: nodegroup - nginx.server

Apply the users and users.admin formulas to all minions

Apply the nginx.server formula to all minions that belong to the app_servers nodegroup

Page 37: A user's perspective on SaltStack and other configuration management tools

The highstate

• Simply put top.sls in /srv/salt/

• Then run:salt '*' state.highstate

Page 38: A user's perspective on SaltStack and other configuration management tools

Wait! There’s more!

Page 39: A user's perspective on SaltStack and other configuration management tools

Wait! There’s more!

• You can schedule commands to be executed at regular intervals

• The master can be configured to store the results of specific commands in a local database called the «salt mine». Minions can query data from the salt mine.

For example the master can store the IP address of all web servers, and the load balancers can query this information for their configuration.

Page 40: A user's perspective on SaltStack and other configuration management tools

And more!

• You can store arbitrary values, such as passwords and secrets, in «pillars». They are configured much like SLS files, and they allow you to set key/value pairs for minions in a very flexible way.

• You can authorize specific minions to send specific commands to any minion. This is called «peer communication».

But be aware that commands and results still pass through the master, though.

Page 41: A user's perspective on SaltStack and other configuration management tools

• You can specify a «returner» when sending a command: instead of returning the result to the master, the returner will save it to redis, mongo, etc.

• You can configure the «outputter» to format the result of a command the way you want it: json, pprint, raw, txt, yaml...

And much much more!

Page 42: A user's perspective on SaltStack and other configuration management tools

And much much more!

• There’s an API so you can do everything programmatically.

• There’s an event framework that allows you to trigger events: you define reactors as SLS files that define how each minion should react.

Page 43: A user's perspective on SaltStack and other configuration management tools

And lots more!

• SLS files go through a configurable renderer which applies Jinja / YAML by default, but you can use any other renderer, not just in python.

• SLS declarations can include or extend other SLS declarations.

Page 44: A user's perspective on SaltStack and other configuration management tools

Some links

• saltstack.org☞ official website, excellent documentation.

• github.com/saltstack☞ source code

• https://github.com/saltstack/salt-cloud☞ salt plugin to spawn and manage VMs

Page 46: A user's perspective on SaltStack and other configuration management tools

Questions ?