本节重点介绍 : ansible playbook编写
- rsyslog 和 logrotate
- service_deploy yaml的编写
配置机器直接的ssh免密码登录
节点主机名host解析
节点主机名写入hosts
master上生成ssh key 并拷贝到node上
master 上安装ansible
playbook执行时需要设置机器文件
设置syslog 和logrotate服务
编写yaml文件
cat <<EOF > init_syslog_logrotate.yaml
- name: init syslog logrotate
hosts: all
user: root
gather_facts: false
vars:
app_log_path: /opt/logs/
sc_path: /opt/tgzs/
syslog_conf: syslog_server.conf
logrotate_conf: logrotate.conf
tasks:
- name: mkdir
file: path={{ app_log_path }} state=directory
- name: copy files
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
owner: root
group: root
mode: 0644
force: true
with_items:
- { src: '{{ sc_path }}/{{ syslog_conf }}', dest: '/etc/rsyslog.d/{{ syslog_conf }}' }
- { src: '{{ sc_path }}/{{ logrotate_conf }}', dest: '/etc/logrotate.d/{{ logrotate_conf }}' }
register: result
- name: Show debug info
debug: var=result verbosity=0
- name: restart service
systemd:
name: "{{ item }}"
state: restarted
daemon_reload: yes
with_items:
- 'rsyslog'
register: result
- name: Show debug info
debug: var=result verbosity=0
EOF
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
准备 syslog_server.conf 和 logrotate.conf
cat <<EOF > syslog_server.conf
if $programname == 'alertmanager' then /opt/logs/alertmanager.log
if $programname == 'prometheus' then /opt/logs/prometheus.log
if $programname == 'node_exporter' then /opt/logs/node_exporter.log
if $programname == 'process_exporter' then /opt/logs/process_exporter.log
if $programname == 'mysql_exporter' then /opt/logs/mysql_exporter.log
if $programname == 'redis_exporter' then /opt/logs/redis_exporter.log
if $programname == 'blackbox_exporter' then /opt/logs/blackbox_exporter.log
if $programname == 'mysqld_exporter' then /opt/logs/mysqld_exporter.log
if $programname == 'process-exporter' then /opt/logs/process-exporter.log
if $programname == 'pushgateway' then /opt/logs/pushgateway.log
if $programname == 'm3coordinator' then /opt/logs/m3coordinator.log
if $programname == 'm3dbnode' then /opt/logs/m3dbnode.log
EOF
cat <<EOF > logrotate.conf
/opt/logs/*.log
{
daily
missingok
notifempty
dateext
compress
delaycompress
copytruncate
rotate 15
}
EOF
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
执行
编写ansible 发布服务脚本
cat <<EOF > service_deploy.yaml
- name: install
hosts: all
user: root
gather_facts: false
vars:
local_path: /opt/tgzs
app_dir: /opt/app
tasks:
- name: mkdir
file: path={{ app_dir }}/{{ app }} state=directory
- name: mkdir
file: path={{ local_path }} state=directory
- name: copy config and service
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
owner: root
group: root
mode: 0644
force: true
with_items:
- { src: '{{ local_path }}/{{ tgz }}', dest: '{{ local_path }}/{{ tgz }}' }
- { src: '{{ local_path }}/{{ app }}.service', dest: '/etc/systemd/system/{{ app }}.service' }
register: result
- name: Show debug info
debug: var=result verbosity=0
- name: tar gz
shell: rm -rf /root/{{ app }}* ; \
tar xf {{ local_path }}/{{ tgz }} -C /root/ ; \
/bin/cp -far /root/{{ app }}*/* {{ app_dir }}/{{ app }}/ \
register: result
- name: Show debug info
debug: var=result verbosity=0
- name: restart service
systemd:
name: "{{ item }}"
state: restarted
daemon_reload: yes
enabled: yes
with_items:
- '{{ app }}'
register: result
- name: Show debug info
debug: var=result verbosity=0
EOF
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
使用这个脚本安装node_exporter
下载node_exporter
准备service文件
cat <<EOF> node_exporter.service
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/opt/app/node_exporter/node_exporter
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node_exporter
[Install]
WantedBy=default.target
EOF
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
执行安装
本节重点总结 : ansible playbook编写
- rsyslog 和 logrotate
- service_deploy yaml的编写