SaltStack實踐案例二

5、Memcached配置管理php

    Memcached是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它經過在內存中緩存數據和對象來減小讀取數據庫的次數,從而提升動態數據庫驅動網站的訪問速度。mysql

    常常會遇到在負載均衡的環境下session問題,通常的解決辦法有三種:nginx

    Session保持;redis

    Session複製;sql

    Session共享;shell

    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

6、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模塊

相關文章
相關標籤/搜索