saltstack源码安装nginx和负载均衡

nginx源码安装部署的步骤分为

1.依赖性的安装
2.源码安装nginx
3.启动nginx

环境

server5:salt-master 172.25.24.5
server7:salt-minion 172.25.24.7
需要打开master端配置文件的模块

1.依赖性的安装
[root@server5 nginx]# pwd
/srv/salt/nginx

[root@server5 nginx]# cat make.sls    ##安装依赖性的包
pkg-init:
  pkg.installed:
    - pkgs:
      - gcc-c++
      - zlib-devel
      - openssl-devel
      - pcre-devel

测试依赖性是否安装成功:
Server5:


[root@server5 nginx]# salt server7 state.sls nginx.make
server7:
----------
          ID: pkg-init
    Function: pkg.installed
      Result: True
     Comment: 4 targeted packages were installed/updated.
     Started: 12:59:35.810182
    Duration: 20630.922 ms
     Changes:   
              ----------
              cloog-ppl:
                  ----------
                  new:
                      0.15.7-1.2.el6
                  old:
              cpp:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              gcc:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              gcc-c++:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              glibc-devel:
                  ----------
                  new:
                      2.12-1.132.el6
                  old:
              glibc-headers:
                  ----------
                  new:
                      2.12-1.132.el6
                  old:
              kernel-headers:
                  ----------
                  new:
                      2.6.32-431.el6
                  old:
              keyutils-libs-devel:
                  ----------
                  new:
                      1.4-4.el6
                  old:
              krb5-devel:
                  ----------
                  new:
                      1.10.3-10.el6_4.6
                  old:
              libcom_err-devel:
                  ----------
                  new:
                      1.41.12-18.el6
                  old:
              libgomp:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              libselinux-devel:
                  ----------
                  new:
                      2.0.94-5.3.el6_4.1
                  old:
              libsepol-devel:
                  ----------
                  new:
                      2.0.41-4.el6
                  old:
              libstdc++-devel:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              mpfr:
                  ----------
                  new:
                      2.4.1-6.el6
                  old:
              openssl-devel:
                  ----------
                  new:
                      1.0.1e-15.el6
                  old:
              pcre-devel:
                  ----------
                  new:
                      7.8-6.el6
                  old:
              ppl:
                  ----------
                  new:
                      0.10.2-11.el6
                  old:
              zlib-devel:
                  ----------
                  new:
                      1.2.3-29.el6
                  old:

Summary for server7
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  20.631 s

在minion端查看能否进行源码编译,如果可以,那么成功
这里写图片描述

[root@server7 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx    --with-http_ssl_module --with-http_stub_status_module   ##也可以进行源码编译,所以依赖性的包是成功的
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found

上边进行的源码安装过程只是为了测试,终止了

2.源码编译模块的编写:
[root@server5 nginx]# ls
files  make.sls  nginx-still.sls
[root@server5 nginx]# cat nginx-still.sls    ##模块


nginx-source-install:
  file.managed:
    - name: /mnt/nginx-1.12.0.tar.gz
    - source: salt://nginx/files/nginx-1.12.0.tar.gz
  cmd.run:
    - name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h  &&  sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx    --with-http_ssl_module --with-http_stub_status_module  &> /dev/null && make &>/dev/null  && make install &> /dev/null

测试:

[root@server5 nginx]# salt server7 state.sls nginx.nginx-still
server7:
----------
          ID: nginx-source-install
    Function: file.managed
        Name: /mnt/nginx-1.12.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.12.0.tar.gz is in the correct state
     Started: 13:29:41.395887
    Duration: 133.28 ms
     Changes:   
----------
          ID: nginx-source-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h  &&  sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx    --with-http_ssl_module --with-http_stub_status_module  &> /dev/null && make &>/dev/null  && make install &> /dev/null
      Result: True
     Comment: Command "cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h  &&  sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx    --with-http_ssl_module --with-http_stub_status_module  &> /dev/null && make &>/dev/null  && make install &> /dev/null" run
     Started: 13:29:41.529917
    Duration: 23746.466 ms
     Changes:   
              ----------
              pid:
                  3328
              retcode:
                  0
              stderr:


 stdout:

Summary for server7
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:  23.880 s

Server7: ##原码编译成功

[root@server7 mnt]# ls
nginx-1.12.0  nginx-1.12.0.tar.gz
[root@server7 nginx-1.12.0]# /usr/local/nginx/sbin/nginx -t   ##检验成功
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3.启动模块的编译

因为需要将配置文件一起推过去,所以就需要将配置文件拿过来,进行配置,在进行传输

[root@server7 conf]# scp nginx.conf server5:/srv/salt/nginx/files

Server5:

[root@server5 nginx]# cd files/
[root@server5 files]# ls
nginx-1.12.0.tar.gz  nginx.conf
[root@server5 files]# pwd
/srv/salt/nginx/files


[root@server5 files]# ls
nginx  nginx-1.12.0.tar.gz  nginx.conf

[root@server5 nginx]# cat nginx-server.sls 
/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/files/nginx.conf


nginx-server:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx
    - mode: 755
  service.running:
    - name: nginx
    - reload: True
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf

测试:

[root@server5 nginx]# salt server7 state.sls nginx.nginx-server
server7:
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 13:56:36.473833
    Duration: 48.51 ms
     Changes:   
----------
          ID: nginx-server
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx is in the correct state
     Started: 13:56:36.522477
    Duration: 30.943 ms
     Changes:   
----------
          ID: nginx-server
    Function: service.running
        Name: nginx
      Result: True
     Comment: Started Service nginx
     Started: 13:56:36.559913
    Duration: 437.725 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for server7
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time: 517.178 ms
[root@server5 files]# vim nginx.conf   ##将进程数改为2

saltstack源码安装nginx和负载均衡_第1张图片

[root@server5 files]# salt server7 state.sls nginx.nginx-server

Server7:
这里写图片描述

4.用户和组模块的编译
[root@server5 files]# vim nginx.conf   ##添加用户和组

saltstack源码安装nginx和负载均衡_第2张图片

[root@server5 nginx]# cat nginx-group-user.sls 
nginx-group:
  Group.present:
    - name: nginx
    - gid: 800
nginx-user:
  user.present:
    - name: nginx
    - uid: 800
    - gid: 800
    - shell: /sbin/nologin
    - createhome: False
- home: /usr/local/nginx
root@server5 nginx]# cat nginx-server.sls    ##在启动项前面加上用户和组的建立
include:
  - nginx.nginx-group-user

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/files/nginx.conf


nginx-server:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx
    - mode: 755
  service.running:
    - name: nginx
    - reload: True
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf
      - 
[root@server5 nginx]# salt server7 state.sls nginx.nginx-serve

测试:

[root@server7 init.d]# /etc/init.d/nginx status
nginx (pid 8617 8616 8613) is running...


[root@server7 init.d]# id nginx
uid=800(nginx) gid=800(nginx) groups=800(nginx)

[root@server7 init.d]# vim /usr/local/nginx/conf/nginx.conf  ##已经改变

saltstack源码安装nginx和负载均衡_第3张图片

以上就是nginx的源码安装和部署

负载均衡的部署

环境

server5: salt-maste和salt-minion r: 172.25.24.5
server6: salt-minion’ 172.25.24.6
server7: salt-minion 172.25.24.7

角色:

server5:haproxy
server6:httpd
server7:nginx

由于server5是haproxy反向代理器,所以需要配置高可用yum源,server6的httpd是前面部署好的,再次不再声明

server5:

[root@server5 salt]# yum install haproxy -y
一键部署haproxy
[root@server5 haproxy]# ls
haproxy.sls
[root@server5 haproxy]# mkdir files
[root@server5 haproxy]# ls
files  haproxy.sls
[root@server5 haproxy]# cd files/
[root@server5 files]# cp /etc/haproxy/haproxy.cfg .
[root@server5 files]# ls
haproxy.cfg
[root@server5 files]# vim haproxy.cfg

saltstack源码安装nginx和负载均衡_第4张图片

编写haproxy安装模块

[root@server5 haproxy]# cat haproxy-install.sls 
haproxy-install:
  pkg.installed:
    - pkgs:
      - haproxy

  file.managed:
    - name: /etc/haproxy/haproxy.cfg
    - source: salt://haproxy/files/haproxy.cfg

  service.running:
    - name: haproxy
    - reload: True
    - watch:
      - file: haproxy-install

haproxy的安装到此结束

部署负载均衡集群

top.sls模块里面是每个虚拟机应该分配的角色

[root@server5 salt]# cat top.sls 
base:
  'server5':
    - haproxy.haproxy-install    ##/srv/salt/haproxy/haproxy-install.sls模块
  'server6':
    - httpd.install
  'server7':
    - nginx.nginx-server

测试:

[root@server5 salt]# salt server? test.ping   ##测试各个主机连接是否正常
server7:
    True
server6:
    True
server5:
    True

当集群中的某一环节出错误之后,可使用这个命令进行刷新之后会还原部署

[root@server5 files]# salt '*' state.highstate  ##将top.sls模块推倒所有连接的主机
server7:
----------
          ID: nginx-group
    Function: group.present
        Name: nginx
      Result: True
     Comment: Group nginx is present and up to date
     Started: 15:04:35.774074
    Duration: 5.12 ms
     Changes:   
----------
          ID: nginx-user
    Function: user.present
        Name: nginx
      Result: True
     Comment: User nginx is present and up to date
     Started: 15:04:35.780253
    Duration: 78.423 ms
     Changes:   
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 15:04:35.861290
    Duration: 158.538 ms
     Changes:   
----------
          ID: nginx-server
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx is in the correct state
     Started: 15:04:36.020030
    Duration: 129.252 ms
     Changes:   
----------
          ID: nginx-server
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 15:04:36.169504
    Duration: 140.196 ms
     Changes:   

Summary for server7
------------
Succeeded: 5
Failed:    0
------------
Total states run:     5
Total run time: 511.529 ms
server5:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:04:35.820744
    Duration: 429.173 ms
     Changes:   
----------
          ID: haproxy-install
    Function: file.managed
        Name: /etc/haproxy/haproxy.cfg
      Result: True
     Comment: File /etc/haproxy/haproxy.cfg is in the correct state
     Started: 15:04:36.252486
    Duration: 46.804 ms
     Changes:   
----------
          ID: haproxy-install
    Function: service.running
        Name: haproxy
      Result: True
     Comment: The service haproxy is already running
     Started: 15:04:36.300356
    Duration: 41.241 ms
     Changes:   

Summary for server5
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 517.218 ms
server6:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:04:37.183317
    Duration: 382.894 ms
     Changes:   
----------
          ID: apache-install
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf updated
     Started: 15:04:37.568795
    Duration: 69.306 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -133,7 +133,7 @@
                   # prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
                   #
                   #Listen 12.34.56.78:80
                  -Listen 8080
                  +Listen 80

                   #
                   # Dynamic Shared Object (DSO) Support
----------
          ID: apache-install
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service reloaded
     Started: 15:04:37.670248
    Duration: 77.244 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for server6
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
Total run time: 529.444 ms

查看负载均衡是否部署成功:
saltstack源码安装nginx和负载均衡_第5张图片
saltstack源码安装nginx和负载均衡_第6张图片

你可能感兴趣的:(个人作业)