五、Memcached配置管理
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态数据库驱动网站的访问速度。
经常会遇到在负载均衡的环境下session问题,一般的解决办法有三种:
Session保持;
Session复制;
Session共享;
PHP可以很容易在php.ini配置中将Session存储在Memcached中,来实现Session共享,这样后端服务器有节点宕机,用户的访问请求被调度到集群中的其他节点时,用户的会话也不会丢失。
Memcached的安装比较简单。首先安装Memcached依赖于libevent,所以要先编译安装libevent,然后编译memcached。后面我们在PHP配置管理中编写php-memcache的模块。同时还需要创建一个管理用户的配置,Memcached包括后面要配置的Nginx和PHP,都要使用www用户来进行管理。
[root@salt-master-1 ~]# mkdir -p /srv/salt/prod/libevent/files [root@salt-master-1 ~]# mkdir -p /srv/salt/prod/memcached/files [root@salt-master-1 ~]# mkdir /srv/salt/prod/user
1.www用户配置
启动Memcached需要使用www用户,包括后面我们部署Nginx和PHP都需要使用到www用户。所以我们把www用户的配置单独放置在user目录下
[root@salt-master-1 ~]# cat /srv/salt/prod/user/www.sls www-user-group: group.present: - name: www - gid: 1000 user.present: - name: www - fullname: www - shell: /sbin/nologin - uid: 1000 - gid: 1000
2.Libevent配置
[root@salt-master-1 ~]# cd /srv/salt/prod/libevent/files/ && wget https://sourceforge.net/projects/levent/files/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz [root@salt-master-1 files]# cat /srv/salt/prod/libevent/install.sls libevent-source-install: file.managed: - name: /usr/local/src/libevent-2.0.22-stable.tar.gz - source: salt://libevent/files/libevent-2.0.22-stable.tar.gz - user: root - group: root - mode: 644 cmd.run: - name: cd /usr/local/src/ && tar zxf libevent-2.0.22-stable.tar.gz && cd libevent-2.0.22-stable && ./configure --prefix=/usr/local/libevent && make && make install - unless: test -d /usr/local/libevent - require: - file: libevent-source-install
3.Memcached配置
[root@salt-master-1 files]# cd /usr/local/src/ && wget && cp memcached-1.4.25.tar.gz /srv/salt/prod/memcached/files/ [root@salt-master-1 src]# cat /srv/salt/prod/memcached/install.sls include: - libevent.install memcached-source-install: file.managed: - name: /usr/local/src/memcached-1.4.25.tar.gz - source: salt://memcached/files/memcached-1.4.25.tar.gz - user: root - group: root - mode: 644 cmd.run: - name: cd /usr/local/src/ && tar zxf memcached-1.4.25.tar.gz && cd memcached-1.4.25 && ./configure --prefix=/usr/local/memcached --enable-64bit --with-libevent=/usr/local/libevent && make && make install - unless: test -d /usr/local/memcached - require: - cmd: libevent-source-install - file: memcached-source-install
4.Memcached服务
[root@salt-master-1 src]# cat /srv/salt/prod/memcached/service.sls include: - memcached.install - user.www # 生产环境建议使用supervisor来管理memcached进程 memcached-service: cmd.run: - name: /usr/local/memcached/bin/memcached -d -m 128 -p 11211 -c 8096 -u www - unless: netstat -nulp | grep 11211 - require: - cmd: memcached-source-install - user: www-user-group
5.执行Memcached状态
[root@salt-master-1 src]# cat /srv/salt/base/top.sls base: '*': - init.env_init - pkg-init prod: '*': - cluster.haproxy-outside - cluster.haproxy-outside-keepalived 'salt-minion-2.example.com': - memcached.service
六、Nginx配置管理
在开始编辑写一个稍复杂的状态功能时,首先应该警醒规划。包括应该如何设计目录结构,需要应用到哪些状态模块和状态间关系,是否需要使用Grains和Pillar等
Nginx+PHP(FastCGI)需要安装得到包首先有Nginx和PHP,需要进行编译安装,步骤:
1).所有源码包的编译安装需要依赖一些基础软件包,像gcc、make这样的,这些在我们的环境做初始化配置的时候已经编写了pkg-init.sls.
2).源码编译安装Nginx时需要依赖PCRE,所以需要有一个PCRE模块用来安装PCRE,然后Nginx进行include即可。
3).需要编译安装PHP,同时除了PHP常用的模块外,还应该支持Memcached和Redis这样的生产常用的第三方模块,我们需要一个机制把它加进去。
需要用到的功能如下:
使用状态模块:file、cmd、service
使用状态间关系:require、unless
SLS之间的include
首先创建目录如下:
[root@salt-master-1 ~]# mkdir -p /srv/salt/prod/pcre/files [root@salt-master-1 ~]# mkdir -p /srv/salt/prod/nginx/files [root@salt-master-1 ~]# mkdir -p /srv/salt/prod/php/files # 创建完,把对应的源码包放到对应的目录下
1.PCRE模块
PCRE模块里面主要是pcre的安装,使用基本的file、cmd和状态之间的关系即可。
[root@salt-master-1 ~]# cd /srv/salt/prod/pcre/files/ [root@salt-master-1 files]# wget [root@salt-master-1 files]# cat /srv/salt/prod/pcre/install.sls pcre-source-install: file.managed: - name: /usr/local/src/pcre-8.37.tar.gz - source: salt://pcre/files/pcre-8.37.tar.gz - user: root - group: root - mode: 755 cmd.run: - name: cd /usr/local/src/ && tar zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install - unless: test -d /usr/local/pcre - require: - file: pcre-source-install
2.Nginx模块
[root@salt-master-1 files]# cd /srv/salt/prod/nginx/files/ [root@salt-master-1 files]# wget [root@salt-master-1 files]# cat /srv/salt/prod/nginx/install.sls include: - pcre.install - user.www nginx-source-install: file.managed: - name: /usr/local/src/nginx-1.9.9.tar.gz - source: salt://nginx/files/nginx-1.9.9.tar.gz - user: root - group: root - mode: 755 cmd.run: - name: cd /usr/local/src && tar zxf nginx-1.9.9.tar.gz && cd nginx-1.9.9 && ./configure --prefix=/usr/local/nginx/ --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --with-pcre=/usr/local/src/pcre-8.37 && make && make install && chown -R www:www /usr/local/nginx - unless: test -d /usr/local/nginx - require: - user: www-user-group - pkg: pkg-init - cmd: pcre-source-install
3.Nginx服务SLS
[root@salt-master-1 files]# cat /srv/salt/prod/nginx/service.sls include: - nginx.install nginx-init: file.managed: - name: /etc/init.d/nginx - source: salt://nginx/files/nginx-init - mode: 755 - user: root - group: root cmd.run: - name: chkconfig --add nginx - unless: chkconfig --list | grep nginx - require: - file: nginx-init /usr/local/nginx/conf/nginx.conf: file.managed: - source: salt://nginx/files/nginx.conf - user: www - group: www - mode: 644 nginx-service: file.directory: - name: /usr/local/nginx/conf/vhost - require: - cmd: nginx-source-install service.running: - name: nginx - enable: True - reload: True - require: - cmd: nginx-init - watch: - file: /usr/local/nginx/conf/nginx.conf
4.PHP(FastCGI)配置管理
对于PHP的源码编译安装,我们使用FastCGI模式,FastCGI模式涉及三个文件:php.ini、php-fpm.conf和fastcgi的启动脚本
[root@salt-master-1 files]# cat /srv/salt/prod/php/install.sls pkg-php: pkg.installed: - names: - mysql-devel - openssl-devel - swig - libjpeg-turto - libjpeg-turto-devel - libpng - libpng-devel - freetype - freetype-devel - libxml2 - libxml2-devel - zlib - zlib-devel - libcurl - libcurl-devel php-source-install: file.managed: - name: /usr/local/src/php-5.6.9.tar.gz - source: salt://php/files/php-5.6.9.tar.gz - user: root - group: root - mode: 755 cmd.run: - name: cd /usr/local/src/ && tar zxf php-5.6.9.tar.gz && cd php-5.6.9 && ./configure --prefix=/usr/local/php-fastcgi --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-libxml-dir --with-curl --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --with-openssl --enable-mbstring --with-gd --enable-gd-native-ttf --with-freetype-dir=/usr/lib64 --with-gettext=/usr/lib64 --enable-sockets --with-xmlrpc --enable-zip --enable-soap --disable-debug --enable-opcache --enable-zip --enable-soap --disable-debug --enable-opcache --enable-zip --with-config-file-path=/usr/local/php-fastcgi/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www && make && make install - require: - file: php-source-install - user: www-user-group - unless: test -d /usr/local/php-fastcgi pdo-plugin: cmd.run: - name: cd /usr/local/src/php-5.6.9/ext/pdo_mysql && /usr/local/php-fastcgi/bin/phpize && ./configure --with-php-config=/usr/local/php-fastcgi/bin/php-config && make && make install - unless: test -f /usr/local/php-fastcgi/lib/php/extensions/*/pdo_mysql.so - require: - cmd: php-source-install php-ini: file.managed: - name: /usr/local/php-fastcgi-etc/php.ini - source: salt://php/files/php.ini-production - user: root - group: root - mode: 644 php-fpm: file.managed: - name: /usr/local/php-fastcgi/etc/php-fpm.conf - source: salt://php/files/php-fpm.conf.default - user: root - group: root - mode: 644 php-fastcgi-service: file.managed: - name: /etc/init.d/php-fpm - source: salt://php/files/init.d/php-fpm - user: root - group: root - mode: 755 cmd.run: - name: chkconfig --add php-fpm - unless: chkconfig --list | grep php-fpm - require: - file: php-fastcgi-service service.running: - name: php-fpm - enable: True - require: - cmd: php-fastcgi-service - watch: - file: php-ini - file: php-fpm
上面已经安装了PHP FastCGI的安装,服务也能正常启动,下面来看redis和memcache模块