Skip to content

Instantly share code, notes, and snippets.

@ozgurakan
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozgurakan/10a563f8c81389f483e0 to your computer and use it in GitHub Desktop.
Save ozgurakan/10a563f8c81389f483e0 to your computer and use it in GitHub Desktop.
SaltStack MongoDB in 30 minutes

Install Salt and Configure MongoDB

Create two servers

We assume you already have two Ubuntu Linux 14.04 servers. salt-master has hostname master and salt-minion has hostname as minion

Configure passwordless ssh login

This is optional and if you like so just google to learn how to do it.

Configure hosts so master minion is easy to access

Edit /etc/hosts file in order to be able to login to master and minion servers

Install salt master

admin@master:~$ sudo add-apt-repository ppa:saltstack/salt -y
admin@master:~$ sudo apt-get update -y
admin@master:~$ sudo apt-get install salt-master -y

Check if master installed

admin@master:~$ ps -fe|grep salt-master
root      6088     1  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6102  6088  0 21:50 ?        00:00:02 /usr/bin/python /usr/bin/salt-master
root      6103  6088  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6104  6088  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6105  6088  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6110  6105  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6111  6105  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6113  6105  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6115  6105  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6116  6105  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master
root      6119  6105  0 21:50 ?        00:00:00 /usr/bin/python /usr/bin/salt-master

Install salt minion

admin@minion:~$ sudo add-apt-repository ppa:saltstack/salt -y
admin@minion:~$ sudo apt-get update -y
admin@minion:~$ sudo apt-get install salt-minion -y

Check if minion is installed

admin@minion:~$ ps -fe|grep salt-minion
root      4223     1  1 21:52 ?        00:00:00 /usr/bin/python /usr/bin/salt-minion

Configure minion

First get master's IP address.

admin@master:~$ ifconfig eth0|grep inet
        inet addr:192.99.70.40  Bcast:192.99.70.40  Mask:255.255.255.255

Then put master's IP address into minion's configuration file

admin@minion:~$ sudo emacs /etc/salt/minion
- #master: salt
+ master: 192.99.70.40

Restart minion

admin@minion:~$ sudo service salt-minion restart
salt-minion stop/waiting
salt-minion start/running, process 4341

Accept Minion on Master

Check if minion tried to connect to master

admin@master:~$ sudo salt-key -L
Accepted Keys:
Unaccepted Keys:
minion.localdomain
Rejected Keys:

Accept the key

admin@master:~$ sudo salt-key -a minion.localdomain
The following keys are going to be accepted:
Unaccepted Keys:
minion.localdomain
Proceed? [n/Y] Y
Key for minion minion.localdomain accepted.

Test master & minion communication

Ping all the servers registered

admin@master:~$ sudo salt \* test.ping
minion.localdomain:
    True

Or ping our only minion

admin@master:~$ sudo salt minion.localdomain test.ping
minion.localdomain:
    True        

Configure Master

Create default formula and pillar folders

admin@master:~$ sudo mkdir /srv/salt
admin@master:~$ sudo mkdir /srv/pillar

Create MongoDB formula

admin@master:~$ cd /srv/salt/

Create mongodb.sls with the code below

mongodb:
  pkg:
    - installed

Execute the command to install mongodb on minion

admin@master:/srv/salt$ sudo salt minion.localdomain state.sls mongodb
minion.localdomain:
----------
          ID: mongodb
    Function: pkg.installed
      Result: True
     Comment: The following packages were installed/updated: mongodb.
     Started: 22:33:18.877988
    Duration: 21619.969 ms
     Changes:   
              ----------
              libboost-filesystem1.55.0:
                  ----------
                  new:  
                      1.55.0+dfsg-1ubuntu3
                  old:  
...
...
... 
              mongodb:
                  ----------
                  new:
                      1:2.6.3-0ubuntu5
                  old:

              mongodb-clients:
                  ----------
                  new:
                      1:2.6.3-0ubuntu5
                  old:

              mongodb-server:
                  ----------
                  new:
                      1:2.6.3-0ubuntu5
                  old:


Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

Installation complete

Check on minion if mongodb is really installed

admin@minion:~$ dpkg --list|grep mongo
ii  mongodb                              1:2.6.3-0ubuntu5             amd64        object/document-oriented database (metapackage)
ii  mongodb-clients                      1:2.6.3-0ubuntu5             amd64        object/document-oriented database (client apps)
ii  mongodb-server                       1:2.6.3-0ubuntu5             amd64        object/document-oriented database (server package)

Let's run mongodb formula again

admin@master:/srv/salt$ sudo salt minion.localdomain state.sls mongodb
minion.localdomain:
----------
          ID: mongodb
    Function: pkg.installed
      Result: True
     Comment: Package mongodb is already installed.
     Started: 22:36:33.121874
    Duration: 1242.492 ms
     Changes:   

Summary
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1

It won't install as it is already installed.

Improve MongoDB formula

Improve MongoDB formula to ensure MongoDB is running. /srv/salt/mongodb.sls shall look like this;

mongodb:
  pkg:
    - installed
  service:
    - running
    - require:
       - pkg: mongodb

This SLS data will ensure that the package named MongoDB is installed, and that the MongoDB service is running.

The first line is the ID for a set of data, and it is called the ID Declaration. This ID sets the name of the thing that needs to be manipulated.

The second and fourth lines are the start of the State Declarations, so they are using the pkg and service states respectively. The pkg state manages a software package to be installed via the system's native package manager, and the service state manages a system daemon.

The third and fifth lines are the function to run. This function defines what state the named package and service should be in. Here, the package is to be installed, and the service should be running.

Finally, on line six, is the word require. This is called a Requisite Statement, and it makes sure that the Apache service is only started after a successful installation of the MongoDB package.

Let's run formula again

admin@master:/srv/salt$ sudo salt minion.localdomain state.sls mongodb
minion.localdomain:
----------
          ID: mongodb
    Function: pkg.installed
      Result: True
     Comment: Package mongodb is already installed.
     Started: 22:39:45.462946
    Duration: 447.777 ms
     Changes:   
----------
          ID: mongodb
    Function: service.running
      Result: True
     Comment: Started Service mongodb
     Started: 22:39:45.910868
    Duration: 51.021 ms
     Changes:   
              ----------
              mongodb:
                  True

Summary
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2

Check MongoDB on minion

admin@minion:~$ mongo
db.help()
...
exit

Other Simple Tasks Salt Can Do

Ip addresses

admin@master:/srv/salt$ sudo salt \* network.ip_addrs
minion.localdomain:
    - 192.99.67.144

Run bash commands

admin@master:/srv/salt$ sudo salt \* cmd.run 'w'
minion.localdomain:
     22:50:49 up 9 days,  2:14,  2 users,  load average: 0.36, 0.17, 0.15
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    admin    pts/0    192.99.65.236    09Dec14  9days  0.10s  0.10s -bash
    admin    pts/1    192.99.65.236    21:47    1:37   0.17s  0.17s -bash

Ping other hosts

admin@master:/srv/salt$ sudo salt \* network.ping 'www.google.com'
minion.localdomain:
    PING www.google.com (64.233.171.104) 56(84) bytes of data.
    64 bytes from 64.233.171.104: icmp_seq=1 ttl=45 time=40.1 ms
    64 bytes from 64.233.171.104: icmp_seq=2 ttl=45 time=40.3 ms
    64 bytes from 64.233.171.104: icmp_seq=3 ttl=45 time=40.0 ms
    64 bytes from 64.233.171.104: icmp_seq=4 ttl=45 time=40.0 ms
    
    --- www.google.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3003ms
    rtt min/avg/max/mdev = 40.004/40.135/40.327/0.123 ms

Check disk usage

admin@master:/srv/salt$ sudo salt \* status.diskusage /home
minion.localdomain:
    ----------
    /home:
        ----------
        available:
            5287759872
        total:
            10432602112        

Others

http://docs.saltstack.com/en/latest/salt-modindex.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment