流利说 Android 部门管理着多台服务器支撑 日常 APK 打包和一些公共基础服务。
所有设备和服务统一采用 Ansible 管理部署,cockpit 作为日常监控,极大的简化了运维工作量。下文将描述 Ansible 配置过程,以 cockpit 和 gitlab-runner 为例,落地服务。
Ansible 安装配置
Ansible 是基于 Python 开发的自动化运维工具,实现批量系统配置、批量程序部署、批量运行命令等功能。由于 Ansible 是基于 SSH 和远程主机通讯,所以不需要事先在主机上安装 client/agnets。
组成部分:Ansible、Modules、Plugins、Playbookds、Host Inventory。
工作机制:通过 Ansible 程序查看 Host Inventory 里被控的主机,由 Playbookds 命令完任务操作,通过 Modules 来完成 ping、copy 等指令,再通过 Plugins 连接被控主机。
下面介绍 Ansible 项目通用配置流程:
1. 本机安装 Ansible
通过 pip 安装 ansible
sudo pip3 install ansibleyum install -y ansible
查看安装是否成功
ansible --version
2.新建一个项目,文件组织结构并没有严格要求,下面是流利说配置项目
2.1 hosts 文件定义了所管理的机器节点与通用的配置,比如节点、SSH 账号、服务安装目录定义等
all:
vars:
ansible_connection: ssh
ansible_ssh_user: ******
ansible_ssh_pass: ******
ansible_become: yes
ansible_become_password: ****
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
ansible_python_interpreter: /usr/bin/python3
ansible_dir: /opt/ansible
nginx_config_dir: /etc/nginx/sites-enabled
nfs_share_dir: /opt/runner-share-files
nfs_share_backup_dir: /opt/runner-share-files-bak
children:
master:
hosts:
node2:
ansible_host: node1.xxx.com
slave:
hosts:
node1:
ansible_host: node2.xxx.com
2.2 main.yml 为 playbook 需要执行的所有配置服务
- import_playbook: config-service.yml
- import_playbook: config-cockpit.yml
- import_playbook: config-gitlab-runner.yml
- import_playbook: config-nfs.yml
2.3 tasks/ 与 handlers/ 中包含了单独服务的配置方法,比如将 gitlab-runner 配置文件复制到节点机器中。其中 hosts,限定了需要执行任务的主机。
2.4 config-xxx 负责串联分发执行 task
2.5 files/conf 目录下包含一系列服务的的配置文件,比如配置某个 nginx 服务。
由于具体需求差异较大,服务各有不同,下文会介绍 cockpit 配置和 gitlab-runner 配置。
3. 执行配置
ansible-playbook -i hosts main.yml --limit node4
注:limit 参数可以限定配置特定节点,不加此参数可以配置全部节点。
Cockpit 监控服务
Cockpit 是红帽开发的网页版图像化服务管理工具,优点是无需中间层,且可以管理多种服务。以下通过 Ansible 配置安装说明。
Cockpit 在 Ansible 中的配置文件 config-cockpityml, name 中定义了所有需要安装的cockpit 插件。
- name: config cockpit
hosts: all
vars:
tasks:
- name: install cockpit
apt:
name:
- cockpit
- cockpit-docker
- cockpit-pcp
- cockpit-ws
state: present
- name: ensure Cockpit is started
systemd:
name: "cockpit.socket"
state: "started"
enabled: true
daemon_reload: true
- name: enable port 9090 for cockpit
ufw:
rule: allow
port: 9090
Ansible 部署完成后,打开启动任意一个节点的 url 就可以配置管理,并且能无缝切换各个节点。此后所有机器设备状态、 Docker 服务、网络、账号等都可以通过 Web 一站式监控管理。
Gitlab Runner
1. 在目标主机直接安装 Runner。
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-runner
2. Ansible 中设置配置文件包括:runner 并发数、gitlab 地址、gitlab token 路径等。
concurrent = 2
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "xxx"
url = "https://xxx/"
token = "xxxx"
executor = "docker"
builds_dir = "/opt/builds"
cache_dir = "/opt/runner-share-files/runner-cache"
pre_clone_script = "source /opt/scripts/pre_clone_script.sh"
pre_build_script = "source /opt/scripts/pre_build_script.sh"
post_build_script = "source /opt/scripts/post_build_script.sh"
3. 安装 Runner 需要的 Docker 镜像,只需维护 playbook 剧本。
- import_playbook: tasks/copy_gitlab_config.yml
- hosts: all
vars:
dest_path: /opt/scripts
tasks:
- name: create runner script dir
file:
path: "{{ dest_path }}"
state: directory
- name: copy runner script
copy:
src: scripts/
dest: "{{ dest_path }}"
owner: root
group: root
mode: preserve
- import_tasks: tasks/docker.yml
- import_tasks: tasks/androidrunner.yml
handlers:
- import_tasks: handlers/androidrunner.yml
比如需要安装 flutter 指定版本镜像,在上述 androidrunner.yml 设定版本号等信息
- name: copy flutter dockerFile
copy:
src: docker/flutter
dest: "{{ ansible_dir }}/docker"
owner: root
group: root
mode: preserve
notify:
- rebuild flutter docker image
- name: build flutter docker image
docker_image:
build:
path: "{{ ansible_dir }}/docker/flutter"
args:
flutter_version: "{{ item.git }}"
name: "lls_flutter:{{ item.image }}-jdk11"
source: build
loop:
- { git: 'v1.12.13+hotfix.9', image: 'v1.12.13-hotfix.9' }
- { git: '3.0.5', image: '3.0.5' }
尾声
通过 Ansible 工具,运维门槛和工作量大福下降,Android 工程师可以很好的维护 CI/CD 服务。 此外标准化部署服务器也大大降低了团队变动的抗风险能力,服务和设备变动只需要维护 ansbile 脚本即可,不需要复杂的文档资料和上下文知识也可以让服务持续运行。
原文始发于微信公众号(流利说技术团队):Ansible 和 Cockpit 管理 Android 服务器
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论