在当今IT环境中,自动化已成为高效运维的核心。CentOS作为企业级Linux发行版,拥有丰富的自动化工具生态:
#!/bin/bash
# 自动备份脚本示例
BACKUP_DIR="/backups"
DATE=$(date +%Y%m%d)
tar -czf $BACKUP_DIR/web_$DATE.tar.gz /var/www/html
find $BACKUP_DIR -type f -mtime +7 -exec rm {} \;
# 每天凌晨3点执行备份
0 3 * * * /root/scripts/backup.sh
bash
pssh -h hosts.txt -l root -A -i "yum update -y"
bash
ansible all -i inventory.ini -m yum -a "name=httpd state=latest"
特点:无代理、基于SSH、YAML语法
安装:
yum install epel-release -y
yum install ansible -y
Inventory文件示例:
[webservers]
web1.example.com
web2.example.com ansible_port=2222
[dbservers]
db[1:3].example.com
Playbook示例:
---
- hosts: webservers
become: yes
tasks:
- name: Install Apache
yum:
name: httpd
state: latest
- name: Start Apache service
service:
name: httpd
state: started
enabled: yes
特点:高性能、基于消息队列、支持大规模环境
安装:
yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest.el7.noarch.rpm
yum install salt-master salt-minion -y
状态文件示例:
apache:
pkg.installed:
- name: httpd
service.running:
- name: httpd
- enable: True
- require:
- pkg: apache
特点:声明式语言、成熟的生态
安装:
yum install https://yum.puppet.com/puppet6-release-el-7.noarch.rpm
yum install puppetserver -y
Manifest示例:
package { 'httpd':
ensure => 'latest',
}
service { 'httpd':
ensure => 'running',
enable => true,
require => Package['httpd'],
}
安装Prometheus:
wget https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
./prometheus --config.file=prometheus.yml
Node Exporter安装:
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*
./node_exporter
Filebeat配置示例:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["elasticsearch:9200"]
Dockerfile示例:
FROM centos:7
RUN yum install -y httpd && yum clean all
COPY index.html /var/www/html/
EXPOSE 80
CMD ["httpd", "-DFOREGROUND"]
使用kubeadm安装:
yum install -y kubelet kubeadm kubectl
systemctl enable --now kubelet
kubeadm init --pod-network-cidr=10.244.0.0/16
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
ansiblePlaybook(
playbook: 'deploy.yml',
inventory: 'inventory.ini'
)
}
}
}
}
-vvv
参数获取详细输出salt-call --local test.ping -l debug
puppet agent --test --debug
通过合理选择和组合这些自动化工具,可以构建高效、可靠的CentOS运维自动化体系,显著提升运维效率和质量。