简介:

SaltStack是 一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,一般可以理解为简化版的puppet和加强版的func。SaltStack基 于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、 python-msgpack和PyYAML等)构建。


最近工作比较闲,所以继续学习saltstack,今天就用源码来安装Mysql数据库,学saltstack还是得多实践,废话不多说,上干货。


实验环境

Master: www.master.com 10.0.4.52   Centos6.5
Slave:  www.slave.com  10.0.4.53   Centos6.5

开始部署:

# cat top.sls 
base:
  '*':
    - init.install
    - mysql.install

目录结构:

# tree init/
init/
└── install.sls                #主要安装mysql数据库需要的依赖库。
# tree mysql/                  #文件内容下面会给出。
mysql/
├── init.sh
├── install.sls
├── my.cnf                  #mysql的主配置文件,自己根据要求来配置。
├── mysql-5.6.14.tar.gz
└── mysqld                   #mysql的启动脚本,即是/etc/init.d/mysqld。
0 directories, 5 files


初始化配置文件:

# cat init/install.sls 
pkg-init:  
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      - glibc
      - make
      - autoconf
      - libjpeg-turbo
      - libjpeg-turbo-devel
      - libpng
      - libpng-devel
      - freetype
      - freetype-devel
      - libxml2
      - libxml2-devel
      - zlib
      - zlib-devel
      - libcurl
      - libcurl-devel
      - openssl
      - openssl-devel
      - swig
      - cmake
      - ncurses
      - ncurses-devel
      - libtool-ltdl-devel

安装mysql主配置文件:

# cat mysql/install.sls
include:
  - init.install

mysql-source-install:
  file.managed:
    - name: /usr/local/src/mysql-5.6.14.tar.gz
    - source: salt://mysql/mysql-5.6.14.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src/ && tar xf mysql-5.6.14.tar.gz && cd mysql-5.6.14 && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql5.6/ -DMYSQL_DATADIR=/usr/local/webserver/mysql5.6/data -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci && make && make install
    - require:
      - file: mysql-source-install
    - unless: test -d /usr/local/webserver/mysql5.6

mysql-init:
  file.managed:
    - name: /usr/local/src/init.sh
    - source: salt://mysql/init.sh
    - user: root
    - group: root
    - mode: 755
  cmd.script:
    - name: /usr/local/src/init.sh
    - require:
      - cmd: mysql-source-install

mysql-config:
  file.managed:
    - name: /usr/local/webserver/mysql5.6/my.cnf
    - source: salt://mysql/my.cnf
    - user: root
    - group: root
    - mode: 644
    - require:
      - file: mysql-init

mysql-service:
  file.managed:
    - name: /etc/init.d/mysqld
    - source: salt://mysql/mysqld
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: chkconfig --add mysqld
    - unless: chkconfig --list |grep mysqld
    - require:
      - file: mysql-service
  service.running:
    - name: mysqld
    - require:
      - cmd: mysql-service

Mysql初始化脚本:

# cat mysql/init.sh 
#!/bin/bash

groupadd mysql            #saltstack也可以创建mysql用户和组,这里就直接让它引用这个脚本。
useradd -r -g mysql mysql
ln -s /usr/local/webserver/mysql5.6/bin/mysql /usr/bin/
cd /usr/local/webserver/mysql5.6/scripts/ && ./mysql_install_db --basedir=/usr/local/webserver/mysql5.6/ --datadir=/usr/local/webserver/mysql5.6/data/
rm -rf /etc/my.cnf
cp -a /usr/local/webserver/mysql5.6/support-files/my-default.cnf /usr/local/webserver/mysql5.6/my.cnf
chown mysql:mysql /usr/local/webserver/mysql5.6/ -R

好激动,要执行安装了

#salt 'www.slave.com' state.highstate   #强烈建议执行这命令之前先执行salt 'www.slave.com' state.highstate test=True来测试实验是否成功。

..........................................................
----------
          ID: mysql-service
    Function: cmd.run
        Name: chkconfig --add mysqld
      Result: True
     Comment: unless execution succeeded
     Started: 11:01:55.890689
    Duration: 519.892 ms
     Changes:   
----------
          ID: mysql-service
    Function: service.running
        Name: mysqld
      Result: True
     Comment: Started Service mysqld
     Started: 11:01:56.414994
    Duration: 2598.256 ms
     Changes:   
              ----------
              mysqld:
                  True

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

minion端查看:

# netstat -anlp |grep mysqld
tcp        0      0 :::3306                     :::*                        LISTEN      29890/mysqld        
unix  2      [ ACC ]     STREAM     LISTENING     897638 29890/mysqld        /var/lib/mysql/mysql.sock

从上面看出mysql已经在minion端安装成功了,之前各种报错,各种调试。总算成功了。还是那句话,多动手。