saltstack部署lamp

salt简介

  • C/S模式、证书认证、批量管理主机,比puppet轻量
  • 集中化管理、分发文件、采集系统数据及软件包的安装与管理
  • 部署简单、管理方便
  • 支持大部分的操作系统
  • C/S管理模式,易于扩展
  • 配置简单、功能覆盖广
  • Master和Minion基于认证,确保安全
  • 支持API及自定义Pyhton模块,轻松实现功能扩展

salt工作原理

  • Minion启动时,会自动生成一套秘钥,将公钥发送给服务器端,服务器验证并接受公钥,以此建立可靠且加密的通信连接。同时通过消息队列ZeroMQ在客户端与服务器之间建立消息发布连接。
  • Minion是saltstack需要管理的客户端安装组件,会主动连接Master端,并从Master得到资源状态信息,同步资源管理信息。
  • Master负责salt命令运行和资源状态的管理
  • ZeroMQ消息队列软件,用于在Master和Minion建立系统通信桥梁。
  • Daemon运行于每个成员内的守护进程,承担着发布消息及通信端口监听的功能

实验环境

  • Operating System: CentOS Linux 7 (Core)
  • server5--->master
  • server2,server3--->minion

-

[root@server5 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.109.138 server5.example.com server5
192.168.109.131 server3.example.com server3
192.168.109.136 server2.example.com server2
[root@server5 ~]# hostname -i
192.168.109.138
[root@server2 ~]# hostname -i
192.168.109.136
[root@server3 ~]# hostname -i
192.168.109.131

-

 salt安装

- yum源

[root@server5 ~]# cat /etc/yum.repos.d/salt-latest.repo 
[salt-latest]
name=SaltStack Latest Release Channel for RHEL/Centos $releasever
baseurl=https://repo.saltstack.com/yum/redhat/7/$basearch/latest
failovermethod=priority
enabled=1
gpgcheck=0
[root@server5 ~]# scp /etc/yum.repos.d/salt-latest.repo server2:/etc/
[root@server5 ~]# scp /etc/yum.repos.d/salt-latest.repo server3:/etc/

- salt安装

[root@server5 ~]# yum clean all
[root@server5 ~]# yum install -y salt-master
[root@server2 ~]# yum clean all
[root@server2 ~]# yum install -y salt-minion
[root@server3 ~]# yum clean all
[root@server3 ~]# yum install -y salt-minion
[root@server5 ~]# vim /etc/salt/master
 15 interface: 0.0.0.0
 22 publish_port: 4505
 32 ret_port: 4506 
 254 worker_threads: 5

[root@server2 ~]# vim /etc/salt/minion
16 master: 192.168.109.138
103 id: 192.168.109.136
[root@server3 ~]# vim /etc/salt/minion
16 master: 192.168.109.138
103 id: 192.168.109.131

[root@server5 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
192.168.109.131
192.168.109.136
Rejected Keys:
[root@server5 ~]# salt-key -A
[root@server5 ~]# salt-key -L
Accepted Keys:
192.168.109.131
192.168.109.136
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@server5 ~]# salt '*' test.ping
192.168.109.131:
    True
192.168.109.136:
    True

-

- lamp搭建

[root@server5 ~]# vim /etc/salt/master
 599 file_roots:
 600   base:
 601     - /srv/salt/
 602   dev:
 603     - /srv/salt/dev
 604 #     - /srv/salt/dev/services
 605 #    - /srv/salt/dev/states
 606   test:
 607     - /srv/salt/test
 608   prod:
 609 #    - /srv/salt/prod/services
 610 #    - /srv/salt/prod/states
 611     - /srv/salt/prod
[root@server5 ~]# yum install -y tree
# 生成如下目录树
[root@server5 ~]# tree /srv/
/srv/
└── salt
    ├── dev
    │   ├── files
    │   │   ├── httpd.conf# apache配置文件
    │   │   └── my.cnf# mariadb数据库的主配置文件
    │   ├── init.sls
    │   └── lamp.sls#lamp文件
    ├── init
    │   ├── dns.sls#测试文件
    │   └── files
    │       └── resolv.conf
    ├── prod
    ├── test
    └── top.sls#顶层
[root@server5 dev]# cp /etc/httpd/conf/httpd.conf /srv/salt/dev/files/
[root@server5 dev]# cp /etc/my.cnf /srv/salt/dev/files/
[root@server5 dev]# cp /etc/resplv.conf /srv/salt/init/files/
[root@server5 dev]# cat lamp.sls 
lamp-pkg-install:
  pkg.installed:
    - names:
      - php
      - mariadb-server
      - php-cli
      - php-common
      - php-mysql
      - php-pdo
apache-service:
  pkg.installed:
    - name: httpd
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://dev/files/httpd.conf
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: apache-service
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
      - file: apache-service
mysql-service:
  pkg.installed:
    - name: mariadb-server
    - require_in: 
      - file: mysql-service
  file.managed:
    - name: /etc/my.cnf
    - source: salt://dev/files/my.cnf
    - user: root
    - group: root
    - mode: 644
    - watch_in:
      - service: mysql-service
  service.running:
    - name: mariad
[root@server5 dev]# cp lamp.sls init.sls #lamp和init文件内容相同,因为采取不同的试验方式
[root@server5 init]# cat dns.sls 
/etc/resolve.conf:
  file.managed:
    - source: salt://init/files/resolv.conf
    - user: root
    - group: root
    - mode: 644
[root@server5 ~]# cat /srv/salt/top.sls 
base:
  '192.168.109.136':
     - dev.lamp
###用top.sls###
[root@server5 dev]# salt '192.168.109.131' state.highstate
192.168.109.131:
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php
      Result: True
     Comment: Package php is already installed
     Started: 20:49:09.344369
    Duration: 1478.144 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: mariadb-server
      Result: True
     Comment: Package mariadb-server is already installed
     Started: 20:49:10.822741
    Duration: 0.627 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-cli
      Result: True
     Comment: Package php-cli is already installed
     Started: 20:49:10.823488
    Duration: 0.426 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-common
      Result: True
     Comment: Package php-common is already installed
     Started: 20:49:10.824014
    Duration: 0.453 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-mysql
      Result: True
     Comment: Package php-mysql is already installed
     Started: 20:49:10.824567
    Duration: 0.379 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-pdo
      Result: True
     Comment: Package php-pdo is already installed
     Started: 20:49:10.825040
    Duration: 0.438 ms
     Changes:   
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed
     Started: 20:49:10.825576
    Duration: 0.365 ms
     Changes:   
----------
          ID: apache-service
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 20:49:10.827981
    Duration: 16.337 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 20:49:10.845038
    Duration: 39.714 ms
     Changes:   
----------
          ID: mysql-service
    Function: pkg.installed
        Name: mariadb-server
      Result: True
     Comment: Package mariadb-server is already installed
     Started: 20:49:10.884946
    Duration: 0.648 ms
     Changes:   
----------
          ID: mysql-service
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 20:49:10.885966
    Duration: 15.648 ms
     Changes:   
----------
          ID: mysql-service
    Function: service.running
        Name: mariadb
      Result: True
     Comment: Service mariadb has been enabled, and is running
     Started: 20:49:10.901856
    Duration: 2679.212 ms
     Changes:   
              ----------
              mariadb:
                  True

Summary for 192.168.109.131
-------------
Succeeded: 12 (changed=1)
Failed:     0
-------------
Total states run:     12
Total run time:    4.232 s
[root@server5 dev]# salt '*' state.sls dev
192.168.109.131:
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php
      Result: True
     Comment: Package php is already installed
     Started: 00:27:42.978079
    Duration: 2064.483 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: mariadb-server
      Result: True
     Comment: Package mariadb-server is already installed
     Started: 00:27:45.043001
    Duration: 0.61 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-cli
      Result: True
     Comment: Package php-cli is already installed
     Started: 00:27:45.043729
    Duration: 0.489 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-common
      Result: True
     Comment: Package php-common is already installed
     Started: 00:27:45.044341
    Duration: 0.432 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-mysql
      Result: True
     Comment: Package php-mysql is already installed
     Started: 00:27:45.044913
    Duration: 0.434 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-pdo
      Result: True
     Comment: Package php-pdo is already installed
     Started: 00:27:45.045454
    Duration: 0.431 ms
     Changes:   
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed
     Started: 00:27:45.045986
    Duration: 0.418 ms
     Changes:   
----------
          ID: apache-service
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 00:27:45.052523
    Duration: 24.134 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 00:27:45.078938
    Duration: 68.561 ms
     Changes:   
----------
          ID: mysql-service
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 00:27:45.147728
    Duration: 12.236 ms
     Changes:   
----------
          ID: mysql-service
    Function: service.running
        Name: mariadb
      Result: True
     Comment: The service mariadb is already running
     Started: 00:27:45.160397
    Duration: 38.356 ms
     Changes:   

Summary for 192.168.109.131
-------------
Succeeded: 11
Failed:     0
-------------
Total states run:     11
Total run time:    2.211 s
192.168.109.136:
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php
      Result: True
     Comment: Package php is already installed
     Started: 15:27:43.063599
    Duration: 2221.375 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: mariadb-server
      Result: True
     Comment: Package mariadb-server is already installed
     Started: 15:27:45.285181
    Duration: 0.504 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-cli
      Result: True
     Comment: Package php-cli is already installed
     Started: 15:27:45.285779
    Duration: 0.417 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-common
      Result: True
     Comment: Package php-common is already installed
     Started: 15:27:45.286290
    Duration: 0.394 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-mysql
      Result: True
     Comment: Package php-mysql is already installed
     Started: 15:27:45.286773
    Duration: 0.399 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-pdo
      Result: True
     Comment: Package php-pdo is already installed
     Started: 15:27:45.287261
    Duration: 0.378 ms
     Changes:   
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed
     Started: 15:27:45.287741
    Duration: 0.428 ms
     Changes:   
----------
          ID: apache-service
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 15:27:45.292362
    Duration: 16.507 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 15:27:45.311330
    Duration: 40.625 ms
     Changes:   
----------
          ID: mysql-service
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 15:27:45.352200
    Duration: 10.358 ms
     Changes:   
----------
          ID: mysql-service
    Function: service.running
        Name: mariadb
      Result: True
     Comment: The service mariadb is already running
     Started: 15:27:45.362988
    Duration: 29.431 ms
     Changes:   

Summary for 192.168.109.136
-------------
Succeeded: 11
Failed:     0
-------------
Total states run:     11
Total run time:    2.321 s

[root@server2 ~]# netstat -antlpe
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      27         397375     42128/mysqld        
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      0          15433      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      0          23135      1372/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          20875      910/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      0          21348      904/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      0          23297      1379/master         
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      0          22167      905/php-fpm: master 
tcp        0      0 192.168.109.136:57840   192.168.109.138:4505    ESTABLISHED 0          341170     34615/python        
tcp        0     52 192.168.109.136:22      192.168.109.1:51172     ESTABLISHED 0          369579     37730/sshd: root@pt 
tcp6       0      0 :::111                  :::*                    LISTEN      0          15432      1/systemd           
tcp6       0      0 :::80                   :::*                    LISTEN      0          395922     41807/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      0          20877      910/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      0          21347      904/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      0          23298      1379/master   
[root@server3 ~]# netstat -antlpe
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      27         139645     16549/mysqld        
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      0          15418      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      0          22819      1415/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          21418      936/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      0          21600      922/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      0          22994      1417/master      
tcp6       0      0 :::80                   :::*                    LISTEN      0          137420     16087/httpd  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值