puppetDB with PostgreSQL


Saat ini puppetDB sebenarnya mensupport dua database untuk dijadikan tempat penyimpanan datanya, pertama adalah HSQLDB dan yang kedua adalah PostgreSQL. HSQLDB dijadikan default dari puppetDB tetapi tidak disarankan untuk penggunaan puppet agent yang berjumlah ratusan, berhubung kantor penulis memiliki ratusan host, maka penulis memilih untuk menggunakan PostgreSQL. Nah kali ini penulis akan men-share mengenai setup puppetDB menggunakan PostgreSQL. Ini adalah kelanjutan dari artikel penulis sebelumnya mengenai setup puppetDB.


Berikut adalah langkah-langkah yang akan kita lakuan:
1. Instalasi dan konfigurasi PostgreSQL
2. Konfigurasi user dan database untuk puppetDB
3. Konfigurasi PuppetDB dan puppetmaster
4. Testing

Hal yang perlu kita perhatikan sebelum melanjutkan:
1. versi puppet untuk puppetDB adalah minimal versi 2.7.12-1.
2. Instalasi puppetDB silahkan menuju http://unixhat.blogspot.com/2012/10/setup-puppetdb.html

1. Instalasi dan konfigurasi& PostgreSQL
Sub ini bertujuan bagi para pembaca yang belum memiliki PostgreSQL server, jadi dibuat se-simple mungkin untuk memudahkan dalam aplikasi. Disini PuppetDB dan PostgreSQL server dalam satu host. jika sudah memiliki silahkan setup sesuai kebutuhan puppetDB.
a. Install PostgreSQL server.
[root@LAB01-01 ~]# yum install postgresql-server
Installing:
 postgresql-server             x86_64            8.1.23-6.el5_8
Installing for dependencies:
 postgresql                    x86_64            8.1.23-6.el5_8
Rubah autentikasi dari PostgreSQL pada file /var/lib/pgsql/data/pg_hba.conf pada baris "host all all 127.0.0.1/32 ident sameuser" menjadi "host all all 127.0.0.1/32 md5". Berikut konfigurasi penulis:
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

local   all         all                               ident sameuser
host    all         all         127.0.0.1/32          md5
host    all         all         ::1/128               ident sameuser

2. Konfigurasi user dan database untuk puppetDB PostgreSQL memiliki system keamanan yang cukup bagus, jadi untuk melakukan konfigurasi kita tidak di perbolehkan menggunakan user root:
[root@LAB01-01 ~]# su - postgres
-bash-3.2$ createuser -DRSP puppetdb
Enter password for new role: 
Enter it again: 
CREATE ROLE
-bash-3.2$ -bash-3.2$ psql -d template1 -c "ALTER USER puppetdb WITH PASSWORD 'sib0mbom';"
ALTER ROLE
-bash-3.2$ psql -h 127.0.0.1 puppetdb puppetdb
Password for user puppetdb: 
Welcome to psql 8.1.23, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

puppetdb=> 
3. Konfigurasi PuppetDB dan puppetmaster

a. PuppetDB
Tunjukan puppetDB server:
Set pada /etc/puppet/puppetdb.conf:
[root@LAB01-01 ~]# cat /etc/puppet/puppetdb.conf 
[main]
server = lab01-01.cloud.net
port = 8081

b. Tambahkan block config [master] agar menggunakan puppetDB untuk "storeconfig" backend dan activated "storeconfig", pada file puppet.conf:
[root@LAB01-01 ~]# cat /etc/puppet/puppet.conf 
[main]
    # The Puppet log directory.
    # The default value is '$vardir/log'.
    logdir = /var/log/puppet

    # Where Puppet PID files are kept.
    # The default value is '$vardir/run'.
    rundir = /var/run/puppet

    # Where SSL certificates are kept.
    # The default value is '$confdir/ssl'.
    ssldir = $vardir/ssl

[agent]
    # The file in which puppetd stores a list of the classes
    # associated with the retrieved configuratiion.  Can be loaded in
    # the separate ``puppet`` executable using the ``--loadclasses``
    # option.
    # The default value is '$confdir/classes.txt'.
    classfile = $vardir/classes.txt

    # Where puppetd caches the local configuration.  An
    # extension indicating the cache format is added automatically.
    # The default value is '$confdir/localconfig'.
    localconfig = $vardir/localconfig
server = LAB01-01.cloud.net
[master]
storeconfigs = true
storeconfigs_backend = puppetdb

c. Buat file routes.yaml di folder /etc/puppet.
[root@LAB01-01 ~]# cat /etc/puppet/routes.yaml
---
master:
  facts:
    terminus: puppetdb
    cache: yaml

d. Setup puppetDB agar menggunakan driver postgresql, jangan lupa untuk meng-comment hal yang berhubungan dengan hsqldb, karena secara default menggunakan database tersebut:
[root@LAB01-01 ~]# cat /etc/puppetdb/conf.d/database.ini 
[database]
# For the embedded DB: org.hsqldb.jdbcDriver
# For PostgreSQL: org.postgresql.Driver
# Defaults to embedded DB
#classname = org.hsqldb.jdbcDriver

# For the embedded DB: hsqldb
# For PostgreSQL: postgresql
# Defaults to embedded DB
#subprotocol = hsqldb

# For the embedded DB: file:/path/to/database;hsqldb.tx=mvcc;sql.syntax_pgs=true
# For PostgreSQL: //host:port/databaseName
# Defaults to embedded DB located in /db
#subname = file:/var/lib/puppetdb/db/db;hsqldb.tx=mvcc;sql.syntax_pgs=true

# Connect as a specific user
# username = foobar

# Use a specific password
# password = foobar

# How often (in minutes) to compact the database
# gc-interval = 60

# Number of seconds before any SQL query is considered 'slow'; offending
# queries will not be interrupted, but will be logged at the WARN log level.
log-slow-statements = 10

classname = org.postgresql.Driver
subprotocol = postgresql
subname = //localhost:5432/puppetdb
username = puppetdb
password = sib0mbom
Silahkan restart puppetmaster dan puppetDB:
[root@LAB01-01 ~]# /etc/init.d/puppetmaster restart
Stopping puppetmaster:                                     [  OK  ]
Starting puppetmaster:                                     [  OK  ]
[root@LAB01-01 ~]# /etc/init.d/puppetdb restart
Stopping puppetdb:                                         [  OK  ]
Starting puppetdb:                                         [  OK  ]

4. Testing
Untuk mengetahui jalan atau tidaknya koneksi puppetDB kita ke PostgreSQL lihat di pada lognya:
[root@LAB01-01 ~]# tail /var/log/puppetdb/puppetdb.log
2012-11-09 01:37:40,340 INFO  [main] [cli.services] PuppetDB version 1.0.2
2012-11-09 01:37:40,708 INFO  [main] [cli.services] Starting broker
2012-11-09 01:37:43,168 INFO  [main] [journal.Journal] ignoring zero length, partially initialised journal data file: db-1.log number = 1 , length = 0
2012-11-09 01:37:43,392 INFO  [main] [cli.services] Starting 1 command processor threads
2012-11-09 01:37:43,640 INFO  [main] [cli.services] Starting query server
2012-11-09 01:37:43,641 INFO  [main] [cli.services] Starting database sweeper (60 minute interval)
2012-11-09 01:37:43,740 INFO  [clojure-agent-send-off-pool-2] [server.Server] jetty-7.x.y-SNAPSHOT
2012-11-09 01:37:43,742 INFO  [clojure-agent-send-off-pool-3] [cli.services] Starting database garbage collection
2012-11-09 01:37:43,883 INFO  [clojure-agent-send-off-pool-3] [cli.services] Finished database garbage collection
2012-11-09 01:37:43,969 INFO  [clojure-agent-send-off-pool-2] [server.AbstractConnector] Started SelectChannelConnector@localhost:8080
2012-11-09 01:37:44,578 INFO  [clojure-agent-send-off-pool-2] [ssl.SslContextFactory] Enabled Protocols [SSLv2Hello, SSLv3, TLSv1] of [SSLv2Hello, SSLv3, TLSv1]
2012-11-09 01:37:44,582 INFO  [clojure-agent-send-off-pool-2] [server.AbstractConnector] Started SslSelectChannelConnector@LAB01-01.cloud.net:8081
Jalankan puppet, pada host yang berperan sebagai puppet agent:
[root@LAB01-02 ~]# puppet agent --test
info: Caching catalog for lab01-02.cloud.net
info: Applying configuration version '1352443469'
notice: Finished catalog run in 0.03 seconds

Sekarang kita lihat hasilnya apakah tabel dari si puppetDB sudah terbentuk:
[root@LAB01-01 ~]# su - postgres
-bash-3.2$ psql -h 127.0.0.1 puppetdb puppetdb
Password for user puppetdb: 
Welcome to psql 8.1.23, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

puppetdb=> \d
                  List of relations
 Schema |          Name           | Type  |  Owner   
--------+-------------------------+-------+----------
 public | catalog_resources       | table | puppetdb
 public | catalogs                | table | puppetdb
 public | certname_catalogs       | table | puppetdb
 public | certname_facts          | table | puppetdb
 public | certname_facts_metadata | table | puppetdb
 public | certnames               | table | puppetdb
 public | classes                 | table | puppetdb
 public | edges                   | table | puppetdb
 public | resource_params         | table | puppetdb
 public | schema_migrations       | table | puppetdb
 public | tags                    | table | puppetdb
(11 rows)

Ya alhamduillah sudah terbentuk sebelas baris baru dari database puppetDB. Moga bisa menjadi amal ibadah penulis. Amiin.