0%

金砖国赛样题三

2022金砖国家职业技能大赛样题

C 场次题目:企业级应用的自动化部署和运维

任务 1 企业级应用的自动化部署(15 分)

  1. ansible 自动化运维工具的安装【3 分】

请使用提供的软件包在 master 节点安装 ansible,安装完成后使用 ansible –version 命令验证是否安装成功。为所有节点添加 test 用户,设置用户密 码为 000000。为 test 用户设置免密 sudo,配置 ssh 免密登录,使 master 节点 能够免密登录所有节点的 test 用户。 将 ansible –version 命令和回显粘贴到答题框。

安装ansible

1
2
3
4
5
6
7
8
9
yum install  -y ansible
[root@master ~]# ansible --version
ansible 2.9.10
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

为所有节点添加 test 用户,设置用户密 码为 000000为所有节点添加 test 用户,设置用户密 码为 000000

1
2
[root@master ~]# useradd test
[root@master ~]# passwd test

为 test 用户设置免密 sudo

1
2
3
[root@master ~]# chmod u+w /etc/sudoers
[root@master ~]# vi /etc/sudoers
test ALL=(ALL) NOPASSWD: ALL #添加这一行

配置 ssh 免密登录

1
2
3
4
[root@master ~]# ssh-keygen
[root@master ~]# ssh-copy-id test@master
[root@master ~]# ssh-copy-id test@node1
[root@master ~]# ssh-copy-id test@node2

2.ansible 自动化运维工具的初始化【3 分】

创建 /root/ansible 目录作为工作目录,在该目录内创建 ansible.cfg 文 件并完成以下配置,清单文件位置为 /root/ansible/inventory,登录用户为 t est,登录时不需要输入密码。设置并行主机数量为 2,允许 test 用户免密提权 到 root。

1
2
mkdir ansible
cd ansible
1
2
3
4
5
6
7
8
9
10
11
cat ansible.cfg
[defaults]
inventory = /root/ansible/inventory #清单文件
forks = 2
ask_pass = False
remote_user = test #登录用户
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

3.主机清单的编写【2 分】

编写主机清单文件,创建 master 用户组,master 用户组内添加 master 主 机;创建 node 用户组,node 组内添加 node1 和 node2 主机,主机名不得使用 I P 地址。 完成后执行 ansible-inventory –list 、ansible all -m ping 和 ansib le all -a “id” 命令,将这三条命令返回结果粘贴到答题框。

1
2
3
4
5
6
7
[root@master ansible]# cat inventory 
[master]
master

[node]
node1
node2
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
56
[root@master ansible]# ansible-inventory --list
[WARNING]: Found both group and host with same name: master
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"master",
"node",
"ungrouped"
]
},
"master": {
"hosts": [
"master"
]
},
"node": {
"hosts": [
"node1",
"node2"
]
}
}
[root@master ansible]# ansible all -m ping
[WARNING]: Found both group and host with same name: master
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
node2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
master | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@master ansible]# ansible all -a "id"
[WARNING]: Found both group and host with same name: master
node1 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root)
node2 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root)
master | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root)

4.使用自动化工具对 master 节点进行初始化【2 分】

请编写 prometheus.yml 控制 master 主机组,使用对应模块将 SELinux 临时 状态和开机启动状态也设置为 disabled。请使用 ansible 对应模块安装时间同 步服务,使用文本编辑模块将该服务的作用域设置为 0.0.0.0/0,并设置状态为 启动和开机自动启动。首先将提供的 prometheus-2.37.0.linux-amd64.tar.gz 使用文件拷贝模块将该压缩包拷贝到目标主机的/usr/local/ 下,使用 shell 模 块解压该压缩包。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@master ansible]# cat prometheus.yml
---

- name: install prometheus
hosts: master
tasks:
- name: disable selinux
selinux:
state: disabled
- name: echo chronyd
shell: echo "allow 0.0.0.0/0" >> /etc/chrony.conf
- name: enable chronyd
service:
name: chronyd
state: started
enabled: true
- name: copy prometheus
copy:
src: /root/ansible/prometheus-2.37.0.linux-amd64.tar.gz
dest: /usr/local/
- name: tar prometheus
shell: tar -zxvf /usr/local/prometheus-2.37.0.linux-amd64.tar.gz -C /usr/local/
1
[root@master ansible]# ansible-playbook prometheus.yml

5.使用自动化运维工具完成企业级应用的部署【5 分】

编写prometheus.yml.j2模板文件,将所有node节点信息添加到该文件中, 但是被管节点的主机名信息必须使用变量 IP 地址可以手动输入。完成后请创建 node_exporter.yml 文件,编写第一个 play,将该 play 命名为 node,该 play 控制的主机组为 node。使用 ansible 模块将 node_exporter-1.3.1.linux-amd6 4.tar.gz 发送到 node 主机组的 /usr/local/ 下,使用一个 shell 模块解压该 压缩包,并启动该服务。随后编写第二个 play,将第二个 play 命名为 master, 第二个 play 控制 master 节点。首先使用 ansible 模块将 prometheus.yml.j2 文 件传输到 master 节点,然后使用 script 模块将 prometheus 启动。使用对应模 块将 grafana-8.1.2-1.x86_64.rpm 包发送到被控节点的 /mnt/ 目录下,然后使用对应模块将该软件包安装,安装完成后设置 grafana 服务启动并设置开机自动 启动。使用浏览器登录 prometheus 查看 prometheus 是否成功监控所有 node 节 点。 请将浏览器反馈的结果截图、prometheus.yml.j2 文件的内容、node_expor ter.yml 文件内容及运行结果提交到答题框。

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
[root@master ansible]# cat prometheus.yml.j2 

# my global config

global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.

# scrape_timeout is set to the global default (10s).

# Alertmanager configuration

alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.

rule_files:

# - "first_rules.yml"

# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:

# Here it's Prometheus itself.

scrape_configs:

# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.

- job_name: "prometheus"

# metrics_path defaults to '/metrics'

# scheme defaults to 'http'.

static_configs:

- targets: ["192.168.20.115:9090"]

- job_name: "node"
static_configs:
{% for node in groups['node'] %}
- targets: ["{{node}}:9100"]
{% endfor %}
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
[root@master ansible]# cat node_exporter.yml 
---


- name: node
hosts: node
tasks:
- name: copy node_exporter
copy:
src: /root//ansible/node_exporter-1.3.1.linux-amd64.tar.gz
dest: /usr/local/

- name: install node_exporter
shell: tar -zxvf /usr/local/node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
- name: service
template:
src: ./node_exporter.service.j2
dest: /usr/lib/systemd/system/node_exporter.service
- name: enable node_exporter
service:
name: node_exporter
state: started
enabled: true

- name: master
hosts: master
tasks:
- name: copy
template:
src: ./prometheus.yml.j2
dest: /usr/local/prometheus-2.37.0.linux-amd64/prometheus.yml
- name: service
template:
src: ./prometheus.service.j2
dest: /usr/lib/systemd/system/prometheus.service

- name: enable prometheus
service:
name: prometheus
state: started
enabled: true

- name: copy grafana-8.1.2-1.x86_64.rpm
copy:
src: /root/ansible/grafana-8.1.2-1.x86_64.rpm
dest: /mnt/
- name: install grafana-8.1.2-1.x86_64.rpm
shell: yum install -y /mnt/*.rpm
- name: enable grafana-8.1.2-1.x86_64.rpm
service:
name: grafana-server
state: started
enabled: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@master ansible]# cat prometheus.service.j2 

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/prometheus-2.37.0.linux-amd64/prometheus \
--config.file=/usr/local/prometheus-2.37.0.linux-amd64/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus-2.37.0.linux-amd64/data/ \
--storage.tsdb.retention=15d \
--web.enable-lifecycle

ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@master ansible]# cat node_exporter.service.j2 

[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/node_exporter-1.3.1.linux-amd64/node_exporter \
--collector.ntp \
--collector.mountstats \
--collector.systemd \
--collector.tcpstat

ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
1
[root@master ansible]# ansible-playbook node_exporter.yml 

任务 2 企业级应用的运维(12 分)

1.使用 prometheus 监控 mysqld 服务【3 分】

将提供的 mysqld_exporter-0.14.0.linux-amd64.tar.gz 发送到 agent 虚 拟机 /usr/local/ 目录下解压并安装 mariadb 服务。进入 mariadb 数据库中创 建 mysqld_monitor 用户并授权,然后创建 mariadb 配置文件,内容为数据库用 户名密码。启动 mysqld_exporter 组件确保 9104 端口启动。回到 prometheus 节 点修改 prometheus.yml 文件并添加 mysql 被监控信息。重启 prometheus,随后 web 界面刷新并查看 mysqld 被控信息。