SaltStackのデータシステムコンポーネント: GrainsとPillar

目次- 1. SaltStackのデータシステムコンポーネント: GrainsとPillar

    1. Grains
  • 2.1 Grainsデータの確認
  • 2.2 ターゲットマッチング
  • 2.3 カスタムGrainsの構築:
    1. Pillar
  • 3.1 Pillarカスタムデータ:
    1. GrainsとPillarの違い
  1. SaltStackのデータシステムコンポーネント: GrainsとPillar ==================================

SaltStackには2つの主要なデータシステムがあり、それぞれGrainsとPillarです。

GrainsとPillarは、minionでユーザー定義変数を使用できるようにするための仕組みです。テンプレートはこれらの変数を用いて、minion上でのファイル作成をより高度に扱うことができます。

Grainsは特定のminionに定義され、Pillarはmasterに定義されます。これらは静的または動的に定義できますが、Grainsは通常変更が少ないデータ、つまり静的なデータに使用され、Pillarは動的なデータに適しています。

  1. Grains =========

Grainsの概要とテンプレートでのターゲットマッチングについては、https://docs.saltstack.com/en/latest/topics/grains/ を参照してください。

GrainsSaltStackのコンポーネントであり、minion起動時に収集される情報を格納します。

GrainsSaltStackにおいて非常に重要なコンポーネントの一つであり、設定配布を行う際に頻繁に使用されます。Grainsminionの静的な情報、例えばCPU、メモリ、ディスク、ネットワーク情報などを記録しています。grains.itemsを使って、特定のminionのすべてのGrains情報を確認できます。

Grainsの機能:

  • 資産情報の収集

Grainsの用途:

  • 情報の照会
  • コマンドラインでのターゲットマッチング
  • top fileでのターゲットマッチング
  • テンプレートでのターゲットマッチング

Grainsはminionプロセスの起動時に読み込まれ、メモリにキャッシュされます。これにより、salt-minionプロセスは毎回システムを検索しなくてもよく、minionのパフォーマンスが大幅に向上します。これはSaltにとって必要不可欠な設計です。なぜなら、Saltは最初から迅速なタスク実行を目的として設計されているからです。

2.1 Grainsデータの確認

grains.itemsメソッドを使用して、minionにどのようなGrainsデータがあるかを確認できます:

salt myminion grains.items

[root@master ~]# salt '*' grains.items
minion:
    ----------
    SSDs:
    biosreleasedate:
        07/29/2019
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
......省略.....
    uid:
        0
    username:
        root
    uuid:
        64cb4d56-25df-192a-2e7e-dea64f261efc
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.1.4

すべてのGrainsのキーを取得する:

[root@master ~]# salt '*' grains.ls
minion:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - cwd
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - fqdns
    - gid

特定のキーの値を取得する:

[root@master ~]# salt '*' grains.get fqdn_ip4
minion:
    - 192.168.32.135
[root@master ~]# salt '*' grains.get os_family
minion:
    RedHat
[root@master ~]# salt '*' grains.get ip4_interfaces
minion:
    ----------
    ens33:
        - 192.168.32.135
    lo:
        - 127.0.0.1

2.2 ターゲットマッチング

Grainsを使ってminionをマッチングします:

すべてのCentOSシステムでコマンドを実行
[root@master ~]# salt -G 'os:CentOS' cmd.run 'uptime'
minion-2:
     22:24:25 up  1:58,  1 user,  load average: 0.00, 0.01, 0.05
minion-1:
     10:24:25 up  1:58,  1 user,  load average: 0.26, 0.12, 0.07

top file内でGrainsを使用する:

[root@master ~]# vim /srv/salt/base/top.sls
base:
  'os:CentOS':
    - match: grain
    - web.apache.install

2.3 カスタムGrainsの構築:

  • minionの設定ファイルでgrainsを検索します。
  • /etc/saltにgrainsファイルを作成し、ここに定義します(推奨方法)。
[root@minion ~]# vim /etc/salt/minion
grains:
  foo:
    - bus
    
[root@master ~]# salt '*' saltutil.sync_grains
minion-1:
minion-2:
[root@master ~]# salt '*' grains.get foo
minion-1:
minion-2:
    - bus    

この方法は古くから存在していますが、現在でも有効ですが、推奨されません。

現在では、カスタムの静的Grainsを/etc/salt/grainsという名前のファイルに保存するのが一般的です。その利点は:

  • Grainsは独立して保存されており、ローカルで見つけやすい
  • GrainsはGrain実行モジュールによって変更可能
[root@master ~]# vim /etc/salt/grains
[root@master ~]# cat /etc/salt/grains
number: 114
[root@master ~]# salt '*' saltutil.sync_grains
minion-1:
minion-2:
[root@master ~]# salt '*' grains.get number
minion-1:
    114
minion-2:

  1. Pillar =========

PillarもSaltStackの重要なコンポーネントの一つであり、データ管理センターとして、大規模な構成管理作業でよく使われます。Pillarは構成管理に必要なデータ、例えばソフトウェアバージョンやユーザー名、パスワードなどを格納および定義するためのものです。その定義形式はGrainsと同様にYAML形式です。

Masterの設定ファイルにはPillarに関する設定項目があります:

#pillar_roots:
#  base:
#    - /srv/pillar

デフォルトのBase環境では、Pillarの作業ディレクトリは/srv/pillarディレクトリにあります。異なる環境のPillarの作業ディレクトリを定義したい場合は、この設定ファイルを編集すればよいです。

Pillarの特徴:

  • 特定のminionに必要なデータを定義できます
  • 定義されたデータは指定された人だけが見ることができます
  • masterの設定ファイルで設定します
//Pillarの情報を確認
[root@master ~]# salt '*' pillar.items
minion-2:
    ----------
minion-1:
    ----------

デフォルトではpillarには何も情報がありません。情報を見たい場合は、masterの設定ファイルでpillar_optsのコメントを外し、値をTrueに設定します。

[root@master ~]# vim /etc/salt/master
pillar_opts: Ture

#masterを再起動してPillar情報を確認
[root@master ~]# systemctl restart salt-master
[root@master ~]# salt '*' pillar.items
.......
        winrepo_user:
        worker_threads:
            5
        zmq_backlog:
            1000
        zmq_filtering:
            False
        zmq_monitor:
            False

3.1 Pillarカスタムデータ:

masterの設定ファイル内のpillar_rootsでPillarの場所を確認できます。

[root@master ~]# vim /etc/salt/master
......
#####         Pillar settings        #####
##########################################
# Salt Pillars allow for the building of global data that can be made selectively
# available to different minions based on minion grain filtering. The Salt
# Pillar is laid out in the same fashion as the file server, with environments,
# a top file and sls files. However, pillar data does not need to be in the
# highstate format, and is generally just key/value pairs.
pillar_roots:
base:
- /srv/pillar/base
prod:
- /srv/pillar/prod

[root@localhost ~]# mkdir -p /srv/pillar/{base,prod}
[root@localhost pillar]# tree
.
├── base
└── prod

2 directories, 0 files


[root@master ~]# salt '*' grains.get fqdn
minion-1:
minion-1
minion-2:
minion-2



[root@master ~]# systemctl restart salt-master
[root@master pillar]# vim /srv/pillar/base/apache.sls
[root@master pillar]# cat /srv/pillar/base/apache.sls
{% if grains['fqdn'] == 'minion-1' %}
apache: httpd
{% elif grains['fqdn'] == 'minion-2' %}
apache: nginx
{% endif %}

[root@master salt]# salt '*' saltutil.refresh_pillar
minion-1:
True
minion-2:
True
[root@master salt]# salt '*' pillar.item apache
minion-1:
----------
apache:
httpd
minion-2:
----------
apache:
nginx






#top fileのエントリーポイントを定義
[root@master ~]# vim /srv/pillar/base/top.sls
base:
'*':
- apache

#このtop.slsファイルはすべてのホストがbase環境でapacheというPillarにアクセスできることを意味します
[root@master ~]# salt '*' pillar.items
minion-1:
----------
apache:
httpd
minion-2:
----------
apache:
nginx

#Saltでapacheの状態ファイルを修正し、Pillarのデータを参照
[root@master ~]# vim /srv/salt/base/web/apache/apache.sls
apache-install:
pkg.installed:
- name: {{ pillar['apache'] }}

apache-service:
service.running:
- name: {{ pillar['apache'] }}
- enable: True






[root@localhost ~]# tree /srv/
/srv/
├── pillar
│   ├── base
│   │   ├── apache.sls
│   │   └── top.sls
│   └── prod
└── salt
└── base
├── top.sls
└── web
└── apache
└── install.sls

7 directories, 4 files
[root@localhost ~]# cat /srv/salt/base/top.sls
base:
'*':
- web.apache.install



#高次の状態ファイルを実行
[root@master base]# salt '*' state.highstate
minion-1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 22:31:57.697039
Duration: 7968.647 ms
Changes:
----------
apr:
----------
new:
1.4.8-5.el7
old:
apr-util:
----------
new:
1.5.2-6.el7
old:
httpd:
----------
new:
2.4.6-93.el7.centos
old:
httpd-tools:
----------
new:
2.4.6-93.el7.centos
old:
mailcap:
----------
new:
2.1.41-2.el7
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 22:32:05.692740
Duration: 399.347 ms
Changes:
----------
httpd:
True

Summary for minion-1
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:   8.368 s
minion-2:
----------
ID: apache-install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 10:31:57.006091
Duration: 8868.752 ms
Changes:
----------
centos-indexhtml:
----------
new:
7-9.el7.centos
old:
dejavu-fonts-common:
----------
new:
2.33-6.el7
old:
dejavu-sans-fonts:
----------
new:
2.33-6.el7
old:
fontconfig:
----------
new:
2.13.0-4.3.el7
old:
fontpackages-filesystem:
----------
new:
1.44-8.el7
old:
gd:
----------
new:
2.0.35-26.el7
old:
gperftools-libs:
----------
new:
2.6.1-1.el7
old:
libX11:
----------
new:
1.6.7-2.el7
old:
libX11-common:
----------
new:
1.6.7-2.el7
old:
libXau:
----------
new:
1.0.8-2.1.el7
old:
libXpm:
----------
new:
3.5.12-1.el7
old:
libjpeg-turbo:
----------
new:
1.2.90-8.el7
old:
libxcb:
----------
new:
1.13-1.el7
old:
nginx:
----------
new:
1:1.16.1-1.el7
old:
nginx-all-modules:
----------
new:
1:1.16.1-1.el7
old:
nginx-filesystem:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-http-image-filter:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-http-perl:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-http-xslt-filter:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-mail:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-stream:
----------
new:
1:1.16.1-1.el7
old:
----------
ID: apache-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx has been enabled, and is running
Started: 10:32:05.884002
Duration: 254.402 ms
Changes:
----------
nginx:
True

Summary for minion-2
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:   9.123 s





[root@nimion-1 ~]# rpm -qa | egrep "nginx|httpd"
httpd-tools-2.4.6-93.el7.centos.x86_64
httpd-2.4.6-93.el7.centos.x86_64

[root@nimion-2 ~]# rpm -qa | egrep "nginx|httpd"
httpd-tools-2.4.6-93.el7.centos.x86_64
nginx-mod-mail-1.16.1-1.el7.x86_64
nginx-all-modules-1.16.1-1.el7.noarch
nginx-filesystem-1.16.1-1.el7.noarch
nginx-mod-http-perl-1.16.1-1.el7.x86_64
nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64
nginx-mod-http-image-filter-1.16.1-1.el7.x86_64
nginx-mod-stream-1.16.1-1.el7.x86_64
nginx-1.16.1-1.el7.x86_64

タグ: saltstack Grains Pillar YAML Configuration Management

9月13日 08:31 投稿