Saltstack系统初始化企业生产案例

1.Saltstack系统初始化之需求梳理

1.1需求分析

1.系统初始化(前提是系统已经安装好)2.服务部署 3.监控 4.业务关联 5.其它

1.系统初始化

1.关闭SELinux   2.关闭默认iptables  3.时间同步  4.文件描述符  5.内核优化  6.SSH服务优化  7.精简开机系统服务(只开启SSHD服务 8.DNS解析 9.字符集  10.host文件统一  11.历史纪录优化(记录时间,用户) 12.设置终端超时时间 13.配置yum源 14.安装各种agent 15基础用户,用户审计,sudo权限设置 16.常用基础命令,命令别名(screen tree lrzsz openssl telnet iftop iotop sysstat wget ntpdate dos2unix lsof net-tools mtr) 17.用户登录提示,PS1的修改  18.tcpwrapper修改

2.Saltstack生产系统初始化之模块识别

2.1系统优化

1.关闭SELinux(file.managed)/etc/selinux/config

2.关闭默认iptables (service.disabled)

3.时间同步(pkg.installed) cron 任务计划

4.文件描述符 (file.managed)/etc/security/limits.conf

 5.内核优化(tcp 内存) sysctl

6.SSH服务优化(关闭DNS解析,修改端口)file.managed

7.精简开机系统服务(只开启SSHD服务)service.disabled

8.DNS解析 file.managed /etc/resolv.conf

9.历史纪录优化histroy(记录时间,用户) /etc/profile

10.设置终端超时时间(安全考虑) file.managed /etc/profile

11.配置yum源 file.managed

12.安装各种agent  pkg(安装) file(配置) service(启动) jinja

13.基础用户(应用用户user group),用户登录提醒,sudo权限设置

14.常用基础命令,命令别名(screen tree lrzsz openssl telnet iftop iotop sysstat wget ntpdate dos2unix lsof net-tools mtr)pkg.installed pkgs

17.用户登录提示,PS1的修改 file.managed

3.Saltstack系统初始化之实现案例

3.1关闭selinux

#存放到init目录下
[root@linux-node1 ~]# cd /srv/salt/base/
[root@linux-node1 base]# mkdir init
[root@linux-node1 base]# cd init/
[root@linux-node1 init]# mkdir files
[root@linux-node1 init]# vim selinux.sls
close-selinux:
  file.managed:
    - name: /etc/selinux/config
    - source: salt://init/files/selinux_config
    - user: root
    - group: root
    - mode: 644
  cmd.run:
    - name: setenforce 0 
##拷贝selinux-config文件到files目录下 
[root@linux-node1 ~]# cp /etc/selinux/config /srv/salt/base/init/files/selinux_config

3.2关闭iptables

##关闭防火墙
[root@linux-node1 init]# vim firewalld.sls
firewalld-stop:
  service.dead:
    - name: firewalld.service
    - enable: False

3.3时间同步

[root@linux-node1 init]# vim ntp-client.sls
install-ntpdate:
  pkg.installed:
    - name: ntpdate

cron-ntpdate:
  cron.present:
    - name: ntpdate ntp1.aliyun.com
    - user: root
    - minute: '*/5'

3.4文件描述符

[root@linux-node1 init]# vim limits.sls 
limits-config:
  file.managed:
    - name: /etc/security/limits.conf
    - source: salt://init/files/limits.conf
    - user: root
    - group: root
    - mode: 644
  cmd.run:
    - name: ulimit -SHn 65535 && ulimit -n
###拷贝limits.conf文件到files目录下
[root@linux-node1 ~]# cp /etc/security/limits.conf /srv/salt/base/init/files/
###修改配置文件
[root@linux-node1 files]# vim limits.conf
*       -       nofile       65535

3.5优化系统内核

[root@linux-node1 init]# vim sysctl.sls
#设置本地TCP可以使用的端口范围
net.ipv4.ip_local_port_range:
  sysctl.present:
    - value: 10000 65000
#设置可以打开的最大文件数
fs.file-max:
  sysctl.present:
    - value: 2000000

#开启ip转发
net.ipv4.ip_forward:
  sysctl.present:
    - value: 1

#尽量不使用交换分区
vm.swappiness:
  sysctl.present:
    - value: 0

3.6SSH服务优化

[root@linux-node1 init]# vim sshd.sls
sshd-config:
  file.managed:
    - name: /etc/ssh/sshd_config
    - source: salt://init/files/sshd_config
    - user: root
    - group: root
    - mode: 600
  service.running:
    - name: sshd
    - enable: True
    - reload: True
    - watch:
      - file: sshd-config
###修改sshd_config文件
sed -i.bak 's@#UseDNS yes@UseDNS no@g;s@^GSSAPIAuthentication yes@GSSAPIAuthentication no@g' /etc/ssh/sshd_config
###拷贝sshd_config文件到files目录下
[root@linux-node1 ~]# cp /etc/ssh/sshd_config /srv/salt/base/init/files/

3.7精简开机启动项

[root@linux-node1 init]# vim thin.sls
postfix:
  service.dead:
    - enable: False

3.8DNS解析

[root@linux-node1 init]# cat dns.sls 
dns-config:
  file.managed:
    - name: /etc/resolv.conf
    - source: salt://init/files/resolv.conf
    - user: root
    - group: root
    - mode: 644
#dns-scripts:
#  cmd.run:
#    - name: /bin/bash /srv/salt/base/init/files/networking_conf.sh
#    - unless: test -f /etc/sysconfig/network-scripts/network.lock
[root@linux-node1 init]# cat files/networking_conf.sh 
#!/bin/bash
networkpath=/etc/sysconfig/network-scripts/ifcfg-eth0
resolvpath=/etc/resolv.conf

if [ -f "$networkpath" ];then
 IPADDR=`grep -i "GATEWAY" $networkpath |awk -F '[="]+' '{print $2}'`
 echo "nameserver $IPADDR" >"$resolvpath"
 touch /etc/sysconfig/network-scripts/network.lock
 else
 echo "no open networking_file"
fi

3.9历史纪录优化histroy(记录时间,用户)

[root@linux-node1 ~]# cat /srv/salt/base/init/history.sls 
history-init:
  file.append:
    - name: /etc/profile
    - text:
      - export HISTTIMEFORMAT="%F %T `whoami` "

3.10设置终端超时时间

[root@linux-node1 ~]# cat /srv/salt/base/init/tty-timeout.sls 
tty-timeout:
  file.append:
    - name: /etc/profile
    - text:
      - export TMOUT=300

3.11优化yum源

[root@linux-node1 init]# vim yum-repo.sls 
/etc/yum.repos.d/epel.repo:
  file.managed:
    - source: salt://init/files/epel-7.repo
    - user: root
    - group: root
    - mode: 644

3.12安装agent

[root@linux-node1 init]# mkdir zabbix
[root@linux-node1 init]# cd zabbix/
[root@linux-node1 zabbix]# mkdir files
[root@linux-node1 zabbix]# yum install -y zabbix22-agent
[root@linux-node1 zabbix]# cp /etc/zabbix_agentd.conf files/
[root@linux-node1 zabbix]# vim files/zabbix_agentd.conf
Hostname= {{ HOSTNAME }}
Server= {{ ZABBIX-SERVER }}
[root@linux-node1 zabbix]# vim zabbix_agent.sls
zabbix-agent:
  pkg.installed:
    - name: zabbix22-agent
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://init/files/zabbix_agentd.conf
    - template: jinja
    - defaults:
      ZABBIX-SERVER: {{ pillar['zabbix-agent']['Zabbix_Server'] }}
      HOSTNAME: {{ grains['fqdn'][0] }} 
    - require:
      - pkg: zabbix-agent
  service.running:
    - enable: True
    - watch:
      - pkg: zabbix-agent
      - file: zabbix-agent
zabbix_agentd.conf.d:
  file.directory:
    - name: /etc/zabbix_agentd.conf.d
    - watch_in:
      - service: zabbix-agent
    - require:
      - pkg: zabbix-agent
      - file: zabbix-agent
##创建pillar
[root@linux-node1 base]# pwd
/srv/pillar/base
[root@linux-node1 base]# vim zabbix.sls
zabbix-agent:
  Zabbix_Server: 118.190.201.11

2.13基础用户

[root@linux-node1 init]# vim user-www.sls
www-user-group:
  group.present:
    - name: www
    - gid: 1000

  user.present:
    - name: www
    - fullname: www
    - shell: /sbin/bash
    - uid: 1000
    - gid: 1000

2.14常用基础命令

[root@linux-node1 init]# vim pkg-base.sls
include:
  - init.yum-repo

base-install:
  pkg.installed:
    - pkgs:
      - screen
      - lrzsz
      - tree
      - openssl
      - telnet
      - iftop
      - iotop
      - sysstat
      - wget
      - dos2unix
      - lsof
      - net-tools
      - mtr
      - unzip
      - zip
      - vim
    - require:
      - file: /etc/yum.repos.d/epel.repo

2.15用户登录提示

[root@linux-node1 init]# vim tty-style.sls
/etc/bashrc:
  file.append:
    - text:
      - export PS1=' [\u@\h \w]\$ '

4.include系统状态

[root@linux-node1 init]# vim init-all.sls
include:
  - init.dns
  - init.firewalld
  - init.history
  - init.limits
  - init.ntp-client
  - init.pkg-base
  - init.selinux
  - init.sshd
  - init.sysctl
  - init.thin
  - init.tty-style
  - init.tty-timeout
  - init.user-www
  - init.yum-repo
####top_file文件内容
[root@linux-node1 base]# pwd
/srv/salt/base
[root@linux-node1 base]# cat top.sls 
base:
  '*':
    - init.init-all
0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!
发表我的评论

Hi,请填写昵称和邮箱!

取消评论
代码 贴图 加粗 链接 删除线 签到