saltstack 安装和基本配置使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack 安装和基本配置使用相关的知识,希望对你有一定的参考价值。

环境: rhel6.5 server1master
server2 minion server3 minion
配置yum安装包:rhel6
[[email protected] ~]# yum install salt-master
[[email protected] ~]# /etc/init.d/salt-master start
[[email protected] ~]# yum install salt-minion
[roo[email protected] ~]# vim /etc/salt/minion
master: 172.25.135.1
[[email protected] ~]# /etc/init.d/salt-minion start
[[email protected] ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
server2
Proceed? [n/Y] y
Key for minion server2 accepted.
[[email protected] ~]# salt-key -L
Accepted Keys:
server2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[[email protected] ~]# salt server2 test.ping
server2:
True
[[email protected] ~]# salt server2 cmd.run hostname
server2:
server2
[[email protected] ~]# salt server2 cmd.run df
server2:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_server0-lv_root 18102140 2078072 15104516 13% /
tmpfs 510200 16 510184 1% /dev/shm
/dev/vda1 495844 34532 435712 8% /boot
[[email protected] ~]# salt server2 cmd.run poweroff #测试关机
server2:
[[email protected] ~]# vim /etc/salt/master
技术分享图片
[[email protected] ~]# cd /srv/salt/
[[email protected] salt]# ls
[[email protected] salt]# mkdir apache
[[email protected] salt]# cd apache/
[[email protected] apache]# vim install.sls
httpd:
pkg.installed #写个简单的http安装
[[email protected] apache]# salt server2 state.sls apache.install
[[email protected] apache]# mkdir files
[[email protected] ~]# scp /etc/httpd/conf/httpd.conf server1:/srv/salt/apache/files
[[email protected] apache]# cd files/
[[email protected] files]# ls
httpd.conf
[[email protected] files]# vim httpd.conf #简单修改一下80端口为8080
[[email protected] apache]# ls
files install.sls
[[email protected] apache]# vim install.sls
apache-install:
pkg.installed:

  • pkgs:

    file.managed:

  • name: /etc/httpd/conf/httpd.conf
  • source: salt://apache/files/httpd.conf
  • mode: 644
  • user: root
  • group: root

    service.running:

  • name: httpd
  • enable: Ture
  • watch:
  • pkgs:
    • gcc
    • pcre-devel
    • openssl-devel

[[email protected] pkgs]# ls
make.sls
[[email protected] pkgs]# cd ..
[[email protected] salt]# ls
apache nginx pkgs
[[email protected] salt]# cd nginx/
[[email protected] nginx]# ls
files install.sls
[[email protected] nginx]# vim install.sls
include:

  • pkgs.make

nginx-install:
file.managed:

  • name: /mnt/nginx-1.14.0.tar.gz
  • source: salt://nginx/files/nginx-1.14.0.tar.gz

    cmd.run:

  • name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.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"/#CCFLAGS="$CFLAGS -g"/g‘ auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &>/dev/null && make &>/dev/null && make install &>/dev/null && cd .. && rm -fr nginx-1.14.0
  • creates: /usr/local/nginx
    [[email protected] nginx]# salt server3 state.sls nginx.install
    写启动脚本修改一些
    [[email protected] nginx]# vim service.sls
    include:
    • nginx.install

/usr/local/nginx/conf/nginx.conf:
file.managed:

  • source: salt://nginx/files/nginx.conf

/etc/init.d/nginx:
file.managed:

  • source: salt://nginx/files/nginx
  • mode: 755

nginx:
service.running:

haproxy-install:
file.managed:

  • name: /mnt/haproxy-1.6.11.tar.gz
  • source: salt://haproxy/files/haproxy-1.6.11.tar.gz
    cmd.run:
  • name: cd /mnt && tar zxf haproxy-1.6.11.tar.gz && cd haproxy-1.6.11 && make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy &> /dev/null && make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy install
  • creates: /usr/local/haproxy

/etc/haproxy:
file.directory:

  • mode: 755

/usr/sbin/haproxy:
file.symlink:

  • target: /usr/local/haproxy/sbin/haproxy
    [[email protected] haproxy]# vim service.sls
    include:
    • haproxy.install
    • users.haproxy

/etc/haproxy/haproxy.cfg:
file.managed:

  • source: salt://haproxy/files/haproxy.cfg

haproxy-service:
file.managed:

  • name: /etc/init.d/haproxy
  • source: salt://haproxy/files/haproxy.init
  • mode: 755
    service.running:
  • name: haproxy
  • relpad: True
  • watch:
    • file: /etc/haproxy/haproxy.cfg

[[email protected] salt]# mkdir users
[[email protected] users]# vim haproxy.sls
haproxy-group:
group.present:

  • name: haproxy
  • gid: 200

haproxy-user:
user.present:

  • name: haproxy
  • uid: 200
  • gid: 200
  • shell: /sbin/nologin
  • home: /usr/local/haproxy
  • createhome: False
    [[email protected] salt]# vim top.sls
    base:
    "server1":
  • apache

[[email protected] ~]# /etc/init.d/salt-minion restart
[[email protected] ~]# cd /etc/salt/
[[email protected] salt]# vim grains
roles: nginx
[[email protected] salt]# salt server2 grains.item roles
server2:

roles:
    - apache

[[email protected] salt]# salt server3 grains.item roles
server3:

roles:
    nginx

[[email protected] salt]# vim top.sls
base:
"server1":

  • haproxy.service
    "roles:apache":
  • match: grain
  • apache.service
    "roles:nginx":
  • match: grain
  • nginx.service
    [[email protected] salt]# salt ‘*‘ state.highstate

[[email protected] salt]# mkdir _grains
[[email protected] salt]# cd _grains/
[[email protected] _grains]# vim my_grains.py
#! /usr/bin/env python
def my_grains():
grains = {};
grains[‘hello‘] = ‘world‘
grains[‘salt‘] = ‘stack‘
return grains
[[email protected] _grains]# salt server2 saltutil.sync_grains
[[email protected] _grains]# salt server2 grains.item hello
server2:

hello:
    world

[[email protected] salt]# cd /etc/salt
[[email protected] salt]# mkdir /srv/pillar/
[[email protected] pillar]# /etc/init.d/salt-master restart
[[email protected] pillar]# mkdir web
[[email protected] pillar]# cd web/
[[email protected] web]# ls
[[email protected] web]# vim install.sls
{% if grains[‘fqdn‘] == ‘server2‘ %}
webserver: httpd
{% elif grains[‘fqdn‘] == ‘server3‘%}
webserver: nginx
{% endif %}

[[email protected] web]# cd ..
[[email protected] pillar]# ls
web
[[email protected] pillar]# vim top.sls
base:
‘*‘:

  • web.install
    [[email protected] pillar]# salt ‘*‘ pillar.items
    server2:

    webserver:
    httpd
    server1:

    server3:

    webserver:
    nginx
    [[email protected] pillar]# salt ‘‘ saltutil.refresh_pillar
    server2:
    True
    server3:
    True
    server1:
    True
    [[email protected] pillar]# salt ‘
    ‘ pillar.items webserver
    server3:

    webserver:
    nginx
    server1:

    webserver:
    server2:

    webserver:
    httpd
    [[email protected] pillar]# salt -I ‘webserver:nginx‘ test.ping
    server3:
    True
    [[email protected] pillar]# salt -S 172.25.135.0/24 test.ping
    server3:
    True
    server2:
    True
    server1:
    True
    金佳模版:
    [[email protected] salt]# cd apache/
    [[email protected] apache]# vim install.sls
    apache-install:
    pkg.installed:

  • pkgs:

    • httpd
    • php
    • php-mysql

    file.managed:

  • name: /etc/httpd/conf/httpd.conf
  • source: salt://apache/files/httpd.conf
  • mode: 644
  • user: root
  • group: root
  • template: jinja
  • context:
    port: 80
    bind: {{ grains[‘ipv4‘][1] }}
    [[email protected] apache]# vim files/httpd.conf #修改监听端口
    Listen {{ bind }}:{{ port }}
    [[email protected] apache]# salt server2 state.sls apache.install

以上是关于saltstack 安装和基本配置使用的主要内容,如果未能解决你的问题,请参考以下文章

saltstack的安装及配置

2.2-saltstack安装配置认证

saltstack安装配置使用

SaltStack安装配置与远程执行测试

saltstack 安装

SaltStack 使用pillar安装配置管理zabbix