自動化運維工具之Puppet master/agent模型、站點清單和puppet多環境設定

1874發表於2020-12-07

  前文我們瞭解了puppe中模組的使用,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/14086315.html;今天我來了解下puppet的master/agent模型以及站點清單的相關話題;

  在說puppet的master/agent模型前,我們要先來回顧下master/agent的工作過程

  如上圖所示,puppet的master/agent模型的工作過程;首先agent向master傳送自己的主機名和facts資訊,然後對應的master收到資訊後,會根據對應的主機名來查詢和agent相關的配置,然後把對應的配置(主要是類和模組中的資源清單內容)編譯好,master把編譯好的catalog傳送給agent;agent收到master傳送到catalog後,首先要在本機查詢相關資源的狀態,如果對應資源的狀態和編譯好的catalog中的狀態一樣,則不改變其狀態或者跳過;如果對應的資源狀態和catalog中的狀態不一樣,此時agent就會應用catalog,把對應資源強制更改為和catalog中的狀態一樣;最後agent把應用報告傳送給master;此時agent就變成了我們在資源清單中定義的系統狀態;這裡需要注意一點catalog是一個二進位制檔案,不能直接檢視,這個二進位制檔案是通過master上定義的模組,或類等資源清單中的內容編譯而成;

  master和agent的通訊

  在puppet的master/agent模型中,master和agent通訊是以https協議通訊;使用https通訊就意味著要有證照驗證,有證照就會有ca;在puppet的master/agent模型中,它內建了ca,意思就是我們不需要再手動搭建ca;對應master的證照,私鑰檔案以及ca的證照私鑰檔案,puppet master都會自動生成;對於agent的私鑰和證照籤署檔案也會由puppet agent自動生成;並且在第一次啟動agent時,它預設會把生成的證照籤發檔案傳送給master,等待master簽發證照;在master上籤發證照,這一步需要人工手動干預;證照籤發好以後,master和agent才可以正常通訊;預設情況下agent是每30分鐘到master上拉取一次相關配置,這也就意味著我們在master端上的資源清單內容最多不超過30分鐘就能在對應的agent上得到應用;

  站點清單

  在上述master/agent模型的工作過程中,agent向master傳送主機名和facts,然後對應master把對應主機名的配置進行編譯然後傳送給對應agent;這裡master根據什麼來查詢對應主機名的對應配置呢?通常情況下如果我們在master端上不定義站點清單,對應的agent就無法獲取到對應的配置;也就說站點清單是用來定義哪些agent上,應該跑哪些資源或模組;這個邏輯和我們在使用ansible一樣,定義好角色以後,還需要額外的playbook來定義那些主機上應該應用那些角色;對於puppet也是一樣,我們定義好模組以後,至於哪些主機應用哪些模組這個是需要我們定義站點清單;

  puppet的master/agent模型的搭建

  部署master端

  配置主機名解析

[root@master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.16.151 master.test.org master
192.168.16.152 node01.test.org node01
192.168.16.153 node02.test.org node02
[root@master ~]# 

  提示:除了各伺服器之間的主機名解析,我們還需要注意確保各伺服器時間同步,關閉selinux,確保iptables是關閉狀態;

  安裝puppet-server包

[root@master ~]# yum install -y puppet-server

  提示:在master端安裝puppet-server這個包預設會把puppet當作依賴包一併安裝;

  啟動服務

[root@master ~]# systemctl start puppetmaster.service 
[root@master ~]# ss -tnl
State       Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              
LISTEN      0      128                         *:22                                      *:*                  
LISTEN      0      100                 127.0.0.1:25                                      *:*                  
LISTEN      0      128                         *:8140                                    *:*                  
LISTEN      0      128                      [::]:22                                   [::]:*                  
LISTEN      0      100                     [::1]:25                                   [::]:*                  
[root@master ~]# 

  提示:puppet master預設會監聽在tcp的8140埠,用於接收agent來master獲取配置;所以請確保master端的8140埠能夠正常處於監聽狀態;

  部署agent端

  安裝puppet

[root@node01 ~]# yum install -y  puppet 

  編輯配置檔案,配置puppet server的地址

  提示:編輯/etc/puppet/puppet.conf檔案,將server=puppetmaster主機的主機名配置上,如上;如果使用主機名的形式,請確保對應agent能夠正常解析;

  啟動服務

[root@node01 ~]# systemctl start puppet
[root@node01 ~]# ss -tnl
State       Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              
LISTEN      0      128                         *:22                                      *:*                  
LISTEN      0      100                 127.0.0.1:25                                      *:*                  
LISTEN      0      128                      [::]:22                                   [::]:*                  
LISTEN      0      100                     [::1]:25                                   [::]:*                  
[root@node01 ~]# ps aux |grep puppet
root      1653  0.0  0.0 115404  1436 ?        Ss   15:27   0:00 /bin/sh /usr/bin/start-puppet-agent agent  --no-daemonize
root      1654 26.7  2.2 317640 41888 ?        Sl   15:27   0:02 /usr/bin/ruby /usr/bin/puppet agent --no-daemonize
root      1680  0.0  0.0 112808   968 pts/0    S+   15:28   0:00 grep --color=auto puppet
[root@node01 ~]# 

  提示:puppet agent雖然也是一個守護程式工作,但它主要不是對外提供服務,它之所以要工作為守護程式,是因為它要週期性的到master端拉取和自己相關的配置;所以它不會監聽任何埠;

  在master端檢視未簽發證照列表

[root@master ~]# puppet cert list
  "node01.test.org" (SHA256) AD:01:59:E7:6C:97:E7:5E:67:09:B9:52:94:0D:37:89:82:8B:EE:49:BB:4D:FC:E1:51:64:BE:EF:71:47:15:11
  "node02.test.org" (SHA256) E1:EC:5B:0C:BF:B7:4C:B9:4F:10:A9:12:34:8B:7A:36:E3:A1:D4:EC:DD:DD:DC:F4:05:48:0B:85:B5:70:AC:28
[root@master ~]# 

  提示:puppet預設會自己維護一個ca,我們只需要用對應的子命令進行管理即可;以上命令表示列出未簽發的證照列表(即收到的agent證照籤發檔案列表);從上面的資訊可以看到現在node01和node02都沒有簽發證照;

  在master端簽發證照

[root@master ~]# puppet cert list
  "node01.test.org" (SHA256) AD:01:59:E7:6C:97:E7:5E:67:09:B9:52:94:0D:37:89:82:8B:EE:49:BB:4D:FC:E1:51:64:BE:EF:71:47:15:11
  "node02.test.org" (SHA256) E1:EC:5B:0C:BF:B7:4C:B9:4F:10:A9:12:34:8B:7A:36:E3:A1:D4:EC:DD:DD:DC:F4:05:48:0B:85:B5:70:AC:28
[root@master ~]# puppet cert sign node01.test.org
Notice: Signed certificate request for node01.test.org
Notice: Removing file Puppet::SSL::CertificateRequest node01.test.org at '/var/lib/puppet/ssl/ca/requests/node01.test.org.pem'
[root@master ~]# puppet cert list
  "node02.test.org" (SHA256) E1:EC:5B:0C:BF:B7:4C:B9:4F:10:A9:12:34:8B:7A:36:E3:A1:D4:EC:DD:DD:DC:F4:05:48:0B:85:B5:70:AC:28
[root@master ~]# puppet cert sign --all
Notice: Signed certificate request for node02.test.org
Notice: Removing file Puppet::SSL::CertificateRequest node02.test.org at '/var/lib/puppet/ssl/ca/requests/node02.test.org.pem'
[root@master ~]# puppet cert list
[root@master ~]# puppet cert list --all
+ "master.test.org" (SHA256) 0C:CC:20:EE:F5:FC:73:21:0B:15:73:EF:A5:0B:3A:F8:01:DB:F7:07:7C:DB:78:87:80:87:FC:F2:BF:E7:2F:30 (alt names: "DNS:master.test.org", "DNS:puppet", "DNS:puppet.test.org")
+ "node01.test.org" (SHA256) 34:BE:E1:1E:26:15:56:56:C3:A0:0D:FB:7F:01:B1:80:35:EC:1D:07:26:C7:05:CA:6E:19:8C:75:9A:A4:67:4E
+ "node02.test.org" (SHA256) E7:B6:B0:FD:04:61:A8:87:D9:E5:DA:51:8B:1D:E0:AD:11:F0:A2:65:43:6D:C4:8D:54:C8:75:8B:DF:CC:51:93
[root@master ~]# 

  提示:簽發證照需要用cert sign+對應要簽發的證照的主機名;如果不想一個一個指定來簽發,也可以使用--all選項來簽發所有未簽發的證照;到此puppet的master/agent模型就搭建好了;後續我們只需要在master端上定義模組和站點清單即可;

  示例:在master上建立redis模組,主要功能是安裝啟動redis,並讓其配置為主從複製模式;

  在master端上建立模組目錄結構

[root@master ~]# mkdir -pv /etc/puppet/modules/redis/{manifests,files,templates,lib,spec,tests}
mkdir: created directory ‘/etc/puppet/modules/redis’
mkdir: created directory ‘/etc/puppet/modules/redis/manifests’
mkdir: created directory ‘/etc/puppet/modules/redis/files’
mkdir: created directory ‘/etc/puppet/modules/redis/templates’
mkdir: created directory ‘/etc/puppet/modules/redis/lib’
mkdir: created directory ‘/etc/puppet/modules/redis/spec’
mkdir: created directory ‘/etc/puppet/modules/redis/tests’
[root@master ~]# tree /etc/puppet/modules/redis/
/etc/puppet/modules/redis/
├── files
├── lib
├── manifests
├── spec
├── templates
└── tests

6 directories, 0 files
[root@master ~]# 

  在/etc/puppet/modules/redis/manifests/目錄下建立資源清單

[root@master ~]# cat /etc/puppet/modules/redis/manifests/init.pp 
class redis{
        package{"redis":
                ensure  => installed,
        }
        service{"redis":
                ensure  => running,
                enable  => true,
                hasrestart      => true,
                restart => 'service redis restart',
        }
}
[root@master ~]# cat /etc/puppet/modules/redis/manifests/master.pp 
class redis::master($masterport='6379',$masterpass='admin') inherits redis {
        file{"/etc/redis.conf":
                ensure  => file,
                content => template('redis/redis-master.conf.erb'),
                owner   => 'redis',
                group   => 'root',
                mode    => '0644',
        }
        Service["redis"]{
                subscribe       => File["/etc/redis.conf"],
                restart => 'systemctl restart redis'
        }
}
[root@master ~]# cat /etc/puppet/modules/redis/manifests/slave.pp 
class redis::slave($masterip,$masterport='6379',$masterpass='admin') inherits redis {
        file{"/etc/redis.conf":
                ensure  => file,
                content => template('redis/redis-slave.conf.erb'),
                owner   => 'redis',
                group   => 'root',
                mode    => '0644',
        }
        Service["redis"]{
                subscribe       => File["/etc/redis.conf"],
                restart => 'systemctl restart redis'
        }
}
[root@master ~]# 

  在templates目錄建立對應的模版檔案

  redis-master.conf.erb檔案內容

自動化運維工具之Puppet master/agent模型、站點清單和puppet多環境設定
[root@master ~]# cat /etc/puppet/modules/redis/templates/redis-master.conf.erb
bind 0.0.0.0
protected-mode yes
port <%= @masterport %>
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
requirepass <%= @masterpass %>
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
[root@master ~]# 
View Code

  redis-slave.conf.erb檔案內容

自動化運維工具之Puppet master/agent模型、站點清單和puppet多環境設定
[root@master ~]# cat /etc/puppet/modules/redis/templates/redis-slave.conf.erb
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
slaveof <%= @masterip %> <%= @masterport %>
masterauth <%= @masterpass %>
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
[root@master ~]# 
View Code

  redis模組目錄和檔案存放情況,以及模版檔案中需要傳遞引數

[root@master ~]# tree /etc/puppet/modules/redis/
/etc/puppet/modules/redis/
├── files
├── lib
├── manifests
│   ├── init.pp
│   ├── master.pp
│   └── slave.pp
├── spec
├── templates
│   ├── redis-master.conf.erb
│   └── redis-slave.conf.erb
└── tests

6 directories, 5 files
[root@master ~]# grep -Ei "^port|requirepass|masterauth|slaveof" /etc/puppet/modules/redis/templates/redis-master.conf.erb 
port <%= @masterport %>
requirepass <%= @masterpass %>
[root@master ~]# grep -Ei "^port|requirepass|masterauth|slaveof" /etc/puppet/modules/redis/templates/redis-slave.conf.erb 
port 6379
slaveof <%= @masterip %> <%= @masterport %>
masterauth <%= @masterpass %>
[root@master ~]# 

  提示:到此redis模組就準備好了;

  定義站點清單檔案

[root@master ~]# cat /etc/puppet/manifests/site.pp
node 'node01.test.org'{
	class{"redis::master":
		masterport	=> "6379",
		masterpass	=> "admin123.com"
	}
}
node 'node02.test.org'{
	class{"redis::slave":
		masterip	=> '192.168.16.152',
		masterport	=> '6379',
		masterpass	=> 'admin123.com'
	}
}
[root@master ~]# 

  提示:站點清單必須是在/etc/puppet/manifests/目錄下,名字必須為site.pp,其內容必須由關鍵字node來定義對應一個主機或一類主機要應用的資源;站點清單也可以使用類的繼承和我們在定義類時的使用差不多;

  在node01上獲取自己相關的配置

  提示:可以看到在node01上手動拉取配置能夠獲取到對應到配置;

  取消--noop 選項跑一遍,看看對應到redis是否啟動?配置檔案是否上我們指定到就配置?

[root@node01 ~]# puppet agent -v --no-daemonize 
Notice: Starting Puppet client version 3.6.2
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for node01.test.org
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1607328529'
Info: /Stage[main]/Redis::Master/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d43fae161baaf47638d0bb571785974d
Notice: /Stage[main]/Redis::Master/File[/etc/redis.conf]/content: content changed '{md5}d43fae161baaf47638d0bb571785974d' to '{md5}32a39b519eb7a9f70a4bdf4705a7afc0'
Notice: /Stage[main]/Redis::Master/File[/etc/redis.conf]/mode: mode changed '0640' to '0644'
Info: /Stage[main]/Redis::Master/File[/etc/redis.conf]: Scheduling refresh of Service[redis]
Info: /Stage[main]/Redis::Master/File[/etc/redis.conf]: Scheduling refresh of Service[redis]
Notice: /Stage[main]/Redis/Service[redis]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Redis/Service[redis]: Unscheduling refresh on Service[redis]
Notice: Finished catalog run in 0.32 seconds
^CNotice: Caught INT; calling stop
[root@node01 ~]# ss -tnl
State      Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port              
LISTEN     0      128                                   *:22                                                *:*                  
LISTEN     0      100                           127.0.0.1:25                                                *:*                  
LISTEN     0      128                                   *:6379                                              *:*                  
LISTEN     0      128                                [::]:22                                             [::]:*                  
LISTEN     0      100                               [::1]:25                                             [::]:*                  
[root@node01 ~]# grep -Ei "^port|bind|requirepass" /etc/redis.conf 
bind 0.0.0.0
port 6379
requirepass admin123.com
[root@node01 ~]# 

  提示:可以看到node01上的reids已經正常啟動,並且配置檔案中的內容也是我們傳遞引數的內容;

  在node02上手動拉取配置並執行,看看對應redis是否正常執行?配置檔案下是否是我們指定傳遞的引數的配置檔案?

[root@node02 ~]# puppet agent -v --no-daemonize 
Notice: Starting Puppet client version 3.6.2
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for node02.test.org
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1607328529'
Notice: /Stage[main]/Redis/Package[redis]/ensure: created
Info: FileBucket got a duplicate file {md5}d98629fded012cd2a25b9db0599a9251
Info: /Stage[main]/Redis::Slave/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d98629fded012cd2a25b9db0599a9251
Notice: /Stage[main]/Redis::Slave/File[/etc/redis.conf]/content: content changed '{md5}d98629fded012cd2a25b9db0599a9251' to '{md5}d1f0efeaee785f0d26eb2cd82acaf1f9'
Notice: /Stage[main]/Redis::Slave/File[/etc/redis.conf]/mode: mode changed '0640' to '0644'
Info: /Stage[main]/Redis::Slave/File[/etc/redis.conf]: Scheduling refresh of Service[redis]
Info: /Stage[main]/Redis::Slave/File[/etc/redis.conf]: Scheduling refresh of Service[redis]
Notice: /Stage[main]/Redis/Service[redis]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Redis/Service[redis]: Unscheduling refresh on Service[redis]
Notice: Finished catalog run in 10.62 seconds
^CNotice: Caught INT; calling stop
[root@node02 ~]# ss -tnl
State      Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port              
LISTEN     0      128                                   *:22                                                *:*                  
LISTEN     0      100                           127.0.0.1:25                                                *:*                  
LISTEN     0      128                                   *:6379                                              *:*                  
LISTEN     0      128                                [::]:22                                             [::]:*                  
LISTEN     0      100                               [::1]:25                                             [::]:*                  
[root@node02 ~]# grep -Ei "^port|requirepass|slaveof|masterauth" /etc/redis.conf
port 6379
slaveof 192.168.16.152 6379
masterauth admin123.com
[root@node02 ~]# 

  提示:可以看到node02上的redis已經正常跑起來了,並且配置檔案中的對應引數也是為指定傳入的引數;

  驗證:在node01上登陸redis,建立一個key,看看對應node02上的redis是否正常將node01上建立的key同步到node02上的redis上?

[root@node01 ~]# redis-cli 
127.0.0.1:6379> AUTH admin123.com
OK
127.0.0.1:6379> set test test
OK
127.0.0.1:6379> get test
"test"
127.0.0.1:6379> exit
[root@node01 ~]# redis-cli  -h node02.test.org -a admin123.com 
node02.test.org:6379> get test
"test"
node02.test.org:6379> 

  提示:可以看到在node01上的redis上寫入一個key在node02上能夠看到對應key的值,說明redis的主從複製正常;以上就是puppet master/agent模型上安裝啟動redis主從的簡單示例;

  puppet多環境設定

  所謂多環境指一個agent通過指定不同的環境名稱,可以在master上獲取不同環境的配置清單,從而實現在不同的環境中,對應的agent拉取不同的配置到本地應用;預設不配置多環境,在agent來master拉取配置都是拉取的生產環境的配置(production);

  示例:在master端上配置多個環境,對應節點使用傳遞不同環境來獲取不同環境的配置

  提示:在master端端配置檔案中新整以上master配置段,並指明environmentpath位置;這裡需要注意,更改了配置檔案需要重啟服務才可生效;

  檢視confdir的位置

[root@master ~]# puppet config print |grep confdir
confdir = /etc/puppet
[root@master ~]# 

  在/etc/puppet目錄下建立environments目錄,並在其下建立對應環境名的目錄以及子目錄

[root@master ~]# mkdir -pv /etc/puppet/environments/{testing,development,production}/{manifests,modules}
mkdir: created directory ‘/etc/puppet/environments’
mkdir: created directory ‘/etc/puppet/environments/testing’
mkdir: created directory ‘/etc/puppet/environments/testing/manifests’
mkdir: created directory ‘/etc/puppet/environments/testing/modules’
mkdir: created directory ‘/etc/puppet/environments/development’
mkdir: created directory ‘/etc/puppet/environments/development/manifests’
mkdir: created directory ‘/etc/puppet/environments/development/modules’
mkdir: created directory ‘/etc/puppet/environments/production’
mkdir: created directory ‘/etc/puppet/environments/production/manifests’
mkdir: created directory ‘/etc/puppet/environments/production/modules’
[root@master ~]# tree /etc/puppet/environments/
/etc/puppet/environments/
├── development
│   ├── manifests
│   └── modules
├── production
│   ├── manifests
│   └── modules
└── testing
    ├── manifests
    └── modules

9 directories, 0 files
[root@master ~]# 

  提示:在每個環境目錄下必須建立manifests和modules目錄,manifests目錄用於存放站點清單,modules用於存放模組;

  在testing環境下建立安裝memcached,並指定監聽在11211埠的模組和站點清單,配置development環境下的memcached監聽在11212埠,配置prediction環境的memcached監聽在11213埠

  建立memcached模組目錄結構

[root@master ~]# mkdir -pv /etc/puppet/environments/testing/modules/memcached/{manifests,templates,files,lib,spec,tests}
mkdir: created directory ‘/etc/puppet/environments/testing/modules/memcached’
mkdir: created directory ‘/etc/puppet/environments/testing/modules/memcached/manifests’
mkdir: created directory ‘/etc/puppet/environments/testing/modules/memcached/templates’
mkdir: created directory ‘/etc/puppet/environments/testing/modules/memcached/files’
mkdir: created directory ‘/etc/puppet/environments/testing/modules/memcached/lib’
mkdir: created directory ‘/etc/puppet/environments/testing/modules/memcached/spec’
mkdir: created directory ‘/etc/puppet/environments/testing/modules/memcached/tests’
[root@master ~]# tree /etc/puppet/environments/testing/modules/memcached/
/etc/puppet/environments/testing/modules/memcached/
├── files
├── lib
├── manifests
├── spec
├── templates
└── tests

6 directories, 0 files
[root@master ~]# 

  在testing環境下的manifests目錄下建立init.pp檔案

[root@master ~]# cat /etc/puppet/environments/testing/modules/memcached/manifests/init.pp
class memcached{
	package{"memcached":
		ensure	=> installed,
	} ->
	file{"/etc/sysconfig/memcached":
		ensure	=> file,
		source	=> 'puppet:///modules/memcached/memcached',
		owner	=> 'root',
		group	=> 'root',
		mode	=> '0644',
	} ~>
	service{"memcached":
		ensure	=> running,
		enable	=> true
	}
}
[root@master ~]# 

  在testing環境下的files目錄提供memcached配置檔案

[root@master ~]# cat /etc/puppet/environments/testing/modules/memcached/files/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@master ~]# 

  在testing環境下的manifests目錄下建立站點檔案site.pp

[root@master ~]# cat /etc/puppet/environments/testing/manifests/site.pp 
node 'node02.test.org'{
	include memcached
}
[root@master ~]# 

  提示:到此testing環境的memcached模組的配置和站點清單就配置好了;

  配置development環境下的memcached模組和站點清單

  複製testing環境下的模組到development

[root@master ~]# cp -a /etc/puppet/environments/testing/modules/memcached/ /etc/puppet/environments/development/modules/
[root@master ~]# tree /etc/puppet/environments/development/modules/
/etc/puppet/environments/development/modules/
└── memcached
    ├── files
    │   └── memcached
    ├── lib
    ├── manifests
    │   └── init.pp
    ├── spec
    ├── templates
    └── tests

7 directories, 2 files
[root@master ~]# 

  編輯files目錄下的memcached配置檔案,修改監聽埠為11212

[root@master ~]# cat /etc/puppet/environments/development/modules/memcached/files/memcached
PORT="11212"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@master ~]# 

  提示:testing和development環境下的memcached除了配置檔案不同,站點清單一樣;

  複製testing環境下的站點清單到development環境下

[root@master ~]# cp /etc/puppet/environments/testing/manifests/site.pp /etc/puppet/environments/development/manifests/
[root@master ~]# cat /etc/puppet/environments/development/manifests/site.pp 
node 'node02.test.org'{
	include memcached
}
[root@master ~]# 

  提示:到此development環境下到memcached模組和站點清單檔案就配置好了;

  配置production環境下的memcached模組和站點清單 

  複製testing環境下到memcached模組

[root@master ~]# cp -a /etc/puppet/environments/testing/modules/memcached/ /etc/puppet/environments/production/modules/
[root@master ~]# tree /etc/puppet/environments/production/modules/
/etc/puppet/environments/production/modules/
└── memcached
    ├── files
    │   └── memcached
    ├── lib
    ├── manifests
    │   └── init.pp
    ├── spec
    ├── templates
    └── tests

7 directories, 2 files
[root@master ~]# 

  編輯files目錄下的memcached配置檔案,修改監聽埠為11213

[root@master ~]# cat /etc/puppet/environments/production/modules/memcached/files/memcached
PORT="11213"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@master ~]# 

  複製站點檔案到production環境下

[root@master ~]# cp /etc/puppet/environments/testing/manifests/site.pp /etc/puppet/environments/production/manifests/
[root@master ~]# cat /etc/puppet/environments/production/manifests/site.pp 
node 'node02.test.org'{
	include memcached
}
[root@master ~]# 

  提示:到此三個環境到memcached模組和站點清單都準備好了;

  在node02上使用不同的環境來啟動監聽不同的埠的memcached

  使用testing環境

[root@node02 ~]# puppet agent -v --no-daemonize --environment=testing 
Notice: Starting Puppet client version 3.6.2
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for node02.test.org
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1607334372'
Notice: /Stage[main]/Memcached/Service[memcached]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Memcached/Service[memcached]: Unscheduling refresh on Service[memcached]
Notice: Finished catalog run in 0.29 seconds
^CNotice: Caught INT; calling stop
[root@node02 ~]# ss -tnl
State      Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port              
LISTEN     0      128                                   *:22                                                *:*                  
LISTEN     0      100                           127.0.0.1:25                                                *:*                  
LISTEN     0      128                                   *:11211                                             *:*                  
LISTEN     0      128                                   *:6379                                              *:*                  
LISTEN     0      128                                [::]:22                                             [::]:*                  
LISTEN     0      100                               [::1]:25                                             [::]:*                  
LISTEN     0      128                                [::]:11211                                          [::]:*                  
[root@node02 ~]# 

  提示:可以看到在node02上使用--envrionment選項指定為testing環境,對應啟動的memcached的埠為11211;

  使用development環境

[root@node02 ~]# puppet agent -v --no-daemonize --environment=development
Notice: Starting Puppet client version 3.6.2
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for node02.test.org
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1607334662'
Info: /Stage[main]/Memcached/File[/etc/sysconfig/memcached]: Filebucketed /etc/sysconfig/memcached to puppet with sum 05503957e3796fbe6fddd756a7a102a0
Notice: /Stage[main]/Memcached/File[/etc/sysconfig/memcached]/content: content changed '{md5}05503957e3796fbe6fddd756a7a102a0' to '{md5}b69eb8ec579bb28f4140f7debf17f281'
Info: /Stage[main]/Memcached/File[/etc/sysconfig/memcached]: Scheduling refresh of Service[memcached]
Notice: /Stage[main]/Memcached/Service[memcached]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.34 seconds
^CNotice: Caught INT; calling stop
[root@node02 ~]# ss -tnl
State      Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port              
LISTEN     0      128                                   *:22                                                *:*                  
LISTEN     0      100                           127.0.0.1:25                                                *:*                  
LISTEN     0      128                                   *:6379                                              *:*                  
LISTEN     0      128                                   *:11212                                             *:*                  
LISTEN     0      128                                [::]:22                                             [::]:*                  
LISTEN     0      100                               [::1]:25                                             [::]:*                  
LISTEN     0      128                                [::]:11212                                          [::]:*                  
[root@node02 ~]# 

  提示:可以看到使用development環境,對應啟動的memcached的埠就是11212;

  使用production環境

[root@node02 ~]# puppet agent -v --no-daemonize --environment=production
Notice: Starting Puppet client version 3.6.2
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for node02.test.org
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1607334761'
Info: /Stage[main]/Memcached/File[/etc/sysconfig/memcached]: Filebucketed /etc/sysconfig/memcached to puppet with sum b69eb8ec579bb28f4140f7debf17f281
Notice: /Stage[main]/Memcached/File[/etc/sysconfig/memcached]/content: content changed '{md5}b69eb8ec579bb28f4140f7debf17f281' to '{md5}f7cdb226870b0164bbdb8671eb11e433'
Info: /Stage[main]/Memcached/File[/etc/sysconfig/memcached]: Scheduling refresh of Service[memcached]
Notice: /Stage[main]/Memcached/Service[memcached]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.33 seconds
^CNotice: Caught INT; calling stop
[root@node02 ~]# ss -tnl
State      Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port              
LISTEN     0      128                                   *:22                                                *:*                  
LISTEN     0      100                           127.0.0.1:25                                                *:*                  
LISTEN     0      128                                   *:6379                                              *:*                  
LISTEN     0      128                                   *:11213                                             *:*                  
LISTEN     0      128                                [::]:22                                             [::]:*                  
LISTEN     0      100                               [::1]:25                                             [::]:*                  
LISTEN     0      128                                [::]:11213                                          [::]:*                  
[root@node02 ~]# 

  提示:可以看到memcached監聽地址就變為了11213;如以上測試沒有任何問題,接下來就可以確定對應agent的環境,配置agent的環境;

  配置agent端的環境

  提示:在agent的配置檔案中使用environment指定對應的環境名稱,儲存退出重啟puppetagent服務,現在這臺agent就會週期性的到master端拉取testing環境的相關配置;

  以上就是puppet的master/agent模型以及多環境相關配置的使用和測試;

相關文章