运维工作: 系统安装(物理机虚拟机)à 程序包安装配置服务启动 à 批量操作 à 程序发布 à 监控
OS Provisioning:
物理机: PXECobbler
虚拟机: Image Templates
- Configuration:
- Puppet(ruby)
- Saltstack(python)
- Chef
- Cfengine
- Command and Control:
- Fabric
预发布验证:
新版本的代码先发布到服务器(跟线上环境配置完全相同, 只是未接入到调度器)
程序发布:
不能影响用户体验;
系统不能停机;
不能导致系统故障或造成系统完全不可用;
灰度发布:
发布路径:
- /webapp/tuangou-1.1
- /webapp/tuangou
- /webapp/tuangou-1.2
/webapp/tuangou 是一个软连接, 本来指向 / webapp/tuangou-1.1, 发布时, 将 / webapp/tuangou 指向 / webapp/tuangou-1.2, 如发生错误, 会滚到 1.1. 版本
在调度器上下线一批主机(maintanance) --> 关闭服务 --> 部署新版本的应用程序 --> 启动服务 --> 在调度器上启用这一批服务器;
自动化灰度发布: 脚本发布平台;
运维工具的分类:
- Agent:puppet,func
- Agentless:ansible,fabric
- Ssh
Ansible: 自动化运维部署工具
Ansible 的特性:
模块化, 调用特定的模块, 完成特定的任务;
基于 python 语言实现, 由 ParamikopyYAML 和 Jinjia2 三个关键模块;
部署简单, agentless;
主从模式;
支持自定义模块;
支持 playbook;
幂等性;
一 Ansible 的配置使用:
ansible 通过 ssh 实现配置管理应用部署任务执行等功能, 因此, 需要事先配置 ansible 端能基于密钥认证的方式联系各被管理节点
配置文件:
/etc/ansible/ansible.cfg 核心配置
/etc/ansible/hosts 主机清单
/usr/share/ansible_plugins/ 插件目录
程序:
ansible
ansible-doc 可以获取帮助文档
ansible-playbook 剧本
基本用法:
- Ansible <host-pattern> [-f forks] [-m module_name] [-a args]
- args:
- key-value
Note:command 模块要执行命令无须为 key=value 格式, 而是直接给出要执行的命令即可;
常用模块:
- command
- -a COMMAND
- user
- -a name= state={present|absent} system= uid=
- group
- -a name= gid= state= system=
- cron
- -a name= minute= hour= day= month= weekday= job= user= state=
- copy
- -a dest= src= mode= owner= group=
- file
- -a path= mode= owner= group= state={directory|link|touch|absent|} src=
- ping
没有参数
- yum
- -a name state={present|latest|absent}
- service
- -a name= state={started|stopped|restarted} enabled=
- shell
- -a COMMAND
- script
- -a /path/to/script
- setup
用法演示:
环境: 四台虚拟机, 两台 centos7, 两台 centos6, 其中一台 centos7 作为管理节点, 另外三台作为被管理端
管理端: 192.168.19.203(centos7)
被管理端: 192.168.19.200(centos7),192.168.19.143(centos6),192.168.19.144(centos6)
1. 配置基于密钥认证连接被管控主机;(安全)
管理端:
在主机中 hosts 文件中添加映射:
在管理端测试:
2. 对 ansible 配置文件备份, 并编辑配置主机清单(/etc/ansible/hosts);
3. 可以获取使用帮助;
基于以上环境, 以下将对 ansible 的模块用法进行一一演示:
1. command 模块
1) 获取所有主机的 ip 地址:
2) 给 webserver 组的两台主机添加一个 centos 用户, 并其添加密码也为 centos;
验证: 可以发现密码添加失败;
Note:command 命令不支持管道传输, 所以可以用 shell 模块通过管道传输;
2. shell 模块
1) 给上面创建的 centos 用户设置密码为 centos(通过管道方式)
2) 验证: 密码添加成功
3. user 模块
1) 用 user 模块为所有主机创建 redhat 用户;
2) 也可以删除用户;
4. group 模块
5. cron 模块: 管理周期性任务
1) 创建自定义任务, 每个五分钟向控制主机同步一下时间;
2) 测试远程主机查看;
6. copy 模块
1) 复制管控主机上的 / etc/fstab 文件到被控主机的 / tmp 目录下, 文件名为 fstab.ansible
2) 测试;
7. file 模块
1) 改变文件属性;
2) 创建文件;
3) 创建链接文件;
测试:
4) 创建目录文件;
8. ping 模块
9. yum 模块
1) 为 webserver 主机安装 nginx:
2) 卸载 nginx
10. service 模块
开启 webserver 上的 nginx 服务;
11. script 模块
12. setup
二 YAML
1. YAML 介绍
YAML 是一个可读性高的用来表达资料序列的格式 YAML 参考了其它多种语言, 包括: XMLc 语言 PythonPerl 以及电子邮件格式 RFC2822 等
YAML Ain't Markup Language, 即 YAML 不是 XML 不过, 在开发的这种语言时, YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)其特性:
YAML 可读性好;
YAML 和脚本语言的交互性好;
YAML 使用实现语言的数据类型
YAML 有一个一致的信息模型
YAML 易于实现;
YAML 可以基于流来处理;
YAML 表达能力强, 扩展性好;
2. 语法
YAML 的语法和其他高阶语言类似, 并且可以简单表达清单散列表标量等数据结构其结构 (Structure) 通过空格来展示, 序列 (Sequence) 里的项用 "-" 来代表, Map 里的键值对用 ":" 分隔下面是一个示例
- name: John Smith
- age: 41
- gender: Male
- spouse:
- name: Jane Smith
- age: 37
- gender: Female
- children:
- - name: Jimmy Smith
- age: 17
- gender: Male
- - name: Jenny Smith
- age 13
- gender: Female
YAML 文件扩展名通常为. yaml, 如 example.yaml
1) list
列表的所有元素均使用 - 开头, 例如:
- #A list of tasty fruits
- - Apple
- - Orange
- - Strawberry
- - Mango
- 2) directory
字典通过 key 与 vale 进行标识, 例如:
- # An employee record
- name:Example Developer
- job:Develper
- skill:Elite
也可以将 key:value 放置于 {} 中进行表示, 例如:
- # An employee record
- {name: Example Developer, job: Developer, skill: Elite}
三 ansible 基础元素
1. 变量
1) 变量命名
变量名仅能由字母数字和下划线组成, 且只能以字母开头
2) facts
facts 是由正在通信的远程目标主机发回的信息, 这些信息被保存在 ansible 变量中要获取指定的远程主机所支持的所有 facts, 可使用如下命令进行:
- #ansible HOSTNAME -m setup
- 3) register
把任务的输出定义为变量, 然后用于其他任务, 示例如下:
- tasks:
- - shell: /usr/bin/foo
- register: foo_result
- ignore_errors: True
4) 通过命令行传递变量
在运行 playbook 的时候也可以传递一些变量供 playbook 使用, 示例如下:
- ansible-playbook test.yaml --extra-vars=hosts=www user=claude
- ansible-playbook test.yaml -e hosts=www user=claude
5) 通过 roles 传递变量
当给一个主机应用角色的时候可以传递变量, 然后在角色内使用这些变量, 示例如下:
- - hosts: webservers
- roles:
- - common
- - { role: foo_app_instance, dir: '/web/htdocs/a.com', port: 8080 }
- 2. Incentory
Ansible 的主要功能在于批量主机操作, 为了编辑地使用其中的部分主机, 可以在 inventory file 中将其分组命名默认的 inventory file 为 / etc/ansible/hosts
Inventory file 可以有多个, 且也可以通过 Dynamic Inventory 来动态生成
1) Inventory 文件格式
Inventory 文件遵循 INI 文件风格, 中括号中的字符为组名可以将同一个主机同时归并到多个不同的组中; 此外, 当如若目标主机使用了非默认的 SSH 端口, 还可以在主机名称之后使用冒号加端口号来标明
- ntp.magedu.com
- [webservers]
- www1.magedu.com:2222
- www2.magedu.com
- [dbservers]
- db1.magedu.com
- db2.magedu.com
- db3.magedu.com
如果主机名称遵循相似的命名模式, 还可以使用列表的方式标识各主机, 例如:
- [webservers]
- www[01:50].example.com
- [databases]
- db-[a:f].example.com
2) 主机变量
可以在 inventory 中定义主机时为其添加筑基变量以便于在 playbook 中使用例如:
- [webservers]
- www1.magedu.com http_port=80 maxRequestsPerChild=808
- www2.magedu.com http_port=8080 maxRequestsPerChild=909
3) 组变量
组变量是指赋予给指定组内所有主机上的 playbook 中可用的变量例如:
- [webservers]
- www1.magedu.com
- www2.magedu.com
- [webservers:vars]
- ntp_server=ntp.magedu.com
- nfs_server=nfs.magedu.com
4) 组嵌套
在 inventory 中, 组还可以包含其他的组, 并且也可以向组中的主机指定变量不过, 这些变量只能在 ansible-playbook 中使用, 而 ansible 不支持例如:
- [apache]
- httpd1.magedu.com
- httpd2.magedu.com
- [nginx]
- ngx1.magedu.com
- ngx2.magedu.com
- [webservers:children]
- apache
- nginx
- [webservers:vars]
- ntp_server=ntp.magedu.com
5) Inventory 参数
Ansible 基于 ssh 连接 inventory 中指定的远程主机时, 还可以通过参数指定其交互方式; 这些参数如下所示:
- ansible_ssh_host
- The name of the host to connect to, if different from the alias you wish to give to it.
- ansible_ssh_port
- The ssh port number, if not 22
- ansible_ssh_user
- The default ssh user name to use.
- ansible_ssh_pass
- The ssh password to use (this is insecure, we strongly recommend using --ask-pass or SSH keys)
- ansible_sudo_pass
- The sudo password to use (this is insecure, we strongly recommend using --ask-sudo-pass)
- ansible_connection
- Connection type of the host. Candidates are local, ssh or paramiko. The default is paramiko before Ansible 1.2, and 'smart' afterwards which detects whether usage of 'ssh' would be feasible based on whether ControlPersist is supported.
- ansible_ssh_private_key_file
- Private key file used by ssh. Useful if using multiple keys and you don't want to use SSH agent.
- ansible_shell_type
- The shell type of the target system. By default commands are formatted using 'sh'-style syntax by default. Setting this to 'csh' or 'fish' will cause commands executed on target systems to follow those shell's syntax instead.
- ansible_python_interpreter
- The target host python path. This is useful for systems with more than one Python or not located at "/usr/bin/python" such as \*BSD, or where /usr/bin/python is not a 2.X series Python. We do not use the "/usr/bin/env" mechanism as that requires the remote user's path to be set right and also assumes the"python" executable is named python, where the executable might
- be named something like "python26".
- ansible\_\*\_interpreter
- Works for anything such as ruby or perl and works just like ansible_python_interpreter. This replaces shebang of modules which will run on that host.
3. 条件测试
如果需要根据变量 facts 或此前任务的执行结果来作为某 task 执行与否的前提时要用到条件测试
1) When 语句
在 task 后添加 when 子句即可启用条件测试; when 语句支持 Jinja2 表达式语法例如:
- tasks:
- - name: "shutdown Debian flavored systems"
- command: /sbin/shutdown -h now
- when: ansible_os_family == "Debian"
when 语句中还可以使用 Jinja2 的大多 filter, 例如要忽略此前某语句的错误并基于其结果 (failed 或者 sucess) 运行后面指定的语句, 可使用类似如下形式:
- tasks:
- - command: /bin/false
- register: result
- ignore_errors: True
- - command: /bin/something
- when: result|failed
- - command: /bin/something_else
- when: result|success
- - command: /bin/still/something_else
- when: result|skipped
此外, when 语句中还可以使用 facts 或 playbook 中定义的变量
4. 迭代
当有需要重复性执行任务时, 可以使用迭代机制其使用格式为将需要迭代的内容定义为 item 变量引用, 并通过 with_items 语句来指明迭代的元素列表即可例如:
- - name: add several users
- user: name={{ item }} state=present groups=wheel
- with_items:
- - testuser1
- - testuser2
上面语句的功能等同于下面的语句:
- - name: add user testuser1
- user: name=testuser1 state=present groups=wheel
- - name: add user testuser2
- user: name=testuser2 state=present groups=wheel
事实上, with_items 中可以使用元素还可为 hashes, 例如:
- - name: add several users
- user: name={{ item.name }} state=present groups={{ item.groups }}
- with_items:
- - { name: 'testuser1', groups: 'wheel' }
- - { name: 'testuser2', groups: 'root' }
四 ansible playbooks
playbook 是由一个或多个 play 组成的列表 Play 的主要功能在于将事先归并为一组的主机装扮成事先通过 ansible 中的 task 定义好的角色从根本上来讲, 所谓 task 无非是调用 ansible 的一个 module 将多个 play 组织在一个 playbook 中, 即可以让它们联同起来按事先编排的机制同唱一台大戏下面是一个简单示例
- - hosts: webnodes
- vars:
- http_port: 80
- max_clients: 256
- remote_user: root
- tasks:
- - name: ensure apache is at the latest version
- yum: name=httpd state=latest
- - name: ensure apache is running
- service: name=httpd state=started
- handlers:
- - name: restart apache
- service: name=httpd state=restarted
1. playbook 基础组件
1) hosts 和 users
playbook 中的每一个 play 的目的都是为了让某个或某些主机以某个指定的用户身份执行任务 Hosts 用于指定要执行指定任务的主机, 其可以是一个或多个由冒号分隔主机组: remote_user 则用于指定远程主机上的执行任务的用户如上面示例中的
- -hosts: webnodes
- remote_user: root
不过, remote_user 也可用于各 task 中也可以通过指定其通过 sudo 的方式在远程主机上执行任务, 其可用于 play 全局或某任务; 此外, 是指可以在 sudo 时使用 sudo_user 时切换的用户
- - hosts: webnodes
- remote_user: mageedu
- tasks:
- - name: test connection
- ping:
- remote_user: mageedu
- sudo: yes
2) 任务列表和 action
Play 的主体部分是 task listTask list 中的各任务按次序逐个在 hosts 中指定的所有主机上执行, 即在所有主机上完成第一个任务后再开始第二个在运行自下而上 mouplaybook 时, 如果中途发生错误, 所有已执行任务都将回滚, 因此, 在更正 playbook 后重新执行一次即可
Task 的目的是使用指定的参数执行模块, 而在模块参数中可以使用变量模块执行时幂等的, 这意味着多次执行时安全的, 因为其结果均一致
每个 task 都应该有其 name, 用于 playbook 的执行结果输出, 建议其内容尽可能清晰地描述任务执行的步骤如果未提供 name, 则 action 的结果将用于输出
定义 task 的可以使用 action:module options 或 module:options 的格式, 推荐使用后者以实现向后兼容如果 action 一行的内容过多, 也中使用在行首使用几个空白字符进行换行
- tasks:
- - name: make sure apache is running
- service: name=httpd state=started
在众多模块中, 只有 command 和 shell 模块仅需要给定一个列表而无需使用 key=value 格式, 例如:
- tasks:
- - name: disable selinux
- command: /sbin/setenforce 0
如果命令或脚本的退出码不为零, 可以使用如下方式替代:
- tasks:
- - name: run this command and ignore the result
- shell: /usr/bin/somecommand || /bin/true
或者使用 ignore_errors 来忽略错误信息:
- -tasks:
- - name: run this command and ignore the result
- shell: /usr/bin/somecommand
- ignore_errors: True
- 3) handlers
用于当关注的资源发生变化时采取一定的操作
notify 这个 action 可用于在每一个 play 的最后被触发, 这样可以避免多次有改变发生时灭磁都执行指定的操作, 取而代之, 仅在所有的变化发生后一次性的执行指定操作在 notify 中列出的操作称为 handler, 也即 notify 中调用 handler 定义的操作
- - name: template configuration file
- template: src=template.j2 dest=/etc/foo.conf
- notify:
- - restart memcached
- - restart apache
handler 是 task 列表, 这些 task 与前述的 task 并没有本质上的不同
- handlers:
- - name: restart memcached
- service: name=memcached state=restarted
- - name: restart apache
- service: name=apache state=restarted
案例:
- heartbeat.yaml
- - hosts: hbhosts
- remote_user: root
- tasks:
- - name: ensure heartbeat latest version
- yum: name=heartbeat state=present
- - name: authkeys configure file
- copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys
- - name: authkeys mode 600
- file: path=/etc/ha.d/authkeys mode=600
- notify:
- - restart heartbeat
- - name: ha.cf configure file
- copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf
- notify:
- - restart heartbeat
- handlers:
- - name: restart heartbeat
- service: name=heartbeat state=restarted
五 roles
ansible 自 1.2 版本引入的新特性用于层次性结构化地组织 playbookRoles 能够根据层次型结构自动装载变量文件 tasks 以及 handlers 等要使用 roles 只需在 playbook 中使用 include 指令即可简单来讲, roles 就是通过分别将变量文件任务模板及处理器放置于单独的目录中, 并可以便捷地 include 它们的一种机制 roles 一般用于基于主机构建服务的场景中, 但也可以是用于构建守护进程等场景中
一个 roles 的案例如下所示:
- site.yml
- webservers.yml
- dbservers.yml
- roles/
- common/
- files/
- templates/
- tasks/
- handlers/
- vars/
- meta/
- webservers/
- files/
- templates/
- tasks/
- handlers/
- vars/
- meta/
而在 playbook 中, 可以这样使用 roles:
- ---
- - hosts: webservers
- roles:
- - common
- - webservers
也可以向 roles 传递参数, 例如:
- - hosts: webservers
- roles:
- - common
- - { role: foo_app_instance, dir: '/opt/a', port: 5000 }
- - { role: foo_app_instance, dir: '/opt/b', port: 5001 }
甚至也可以条件式地使用 roles, 例如:
- ---
- - hosts: webservers
- roles:
- - { role: some_role, when: "ansible_os_family =='RedHat'" }
1. 创建 role 的步骤
1) 创建以 roles 命名的目录;
2) 在 roles 目录中分别创建以各角色名称命名的目录, 如 webservers 等;
3) 在每个角色命名的目录中分别创建 fileshandlersmetataskstemplates 和 vars 目录; 用不到的目录可以创建为空目录, 也可以不创建;
4) 在 playbook 文件中, 调用各角色;
2. role 内个各目录中可用的文件
1) tasks 目录: 至少应该包含一个名为 main.yml 的文件, 其定义了此角色的任务列表; 此文件可以使用 include 包含其它的位于此目录中的 task 文件;
2) files 目录: 存放由 copy 或 script 等模块调用的文件;
3) templates 目录: template 模块会自动在此目录中寻找 Jinja2 模板文件;
4) handlers 目录: 此目录应当包含一个 main.yml 文件, 用于定义此角色用到的各 handler; 在 handler 中使用 include 包含的其它的 handler 文件也应该位于此目录中;
5) vars 目录: 应当包含一个 main.yml 文件, 用于定义此角色用到的变量;
6) meta 目录: 应当包含一个 main.yml 文件, 用于定义此角色的特殊设定及其依赖关系; ansible1.3 及其以后的版本才支持;
7) default 目录: 为当前角色设定默认变量时使用此目录; 应当包含一个 main.yml 文件;
六 tags
tags 用于让用户选择运行 playbook 中的部分代码 ansible 具有幂等性, 因此会自动跳过没有变化的部分, 即便如此, 有些代码为测试其确实没有发生变化的时间依然会非常地长此时, 如果确信其没有变化, 就可以通过 tags 跳过此些代码片断
Ansible-playbook -t TAGS(--tags=TAGS)
Ansible-playbook h 可以获取具体用法
演示 1: 编排剧本, 简单创建组和用户, 安装并开启服务;
1. 首先定义 yaml 格式的列表;
2. 可以看一下匹配的主机, 任务列表
3. 测试运行;
演示 2: 利用 handler, 当 httpd 的配置文件发生修改时, 重启 httpd 服务;
1. 准备 httpd 模板配置文件;
将端口改为 8080;
2. 编辑 yaml 列表文件, 定义 playbook;
3. 测试运行;
4. 再次执行一次; 配置文件没有发生改变, 所以不会执行 handler 任务;
演示 3: 给指定任务定义 tags;
1. 在以上文件的基础上编辑定义 playbook;
2. 当我们只是修改了配置文件, 确保服务已经安装并且启动时, 我们只需运行 tags 的任务即可;
以上可见, 只执行了标记 tags 的任务, 可以避免浪费很多资源
演示 4: 变量
方法一: 通过命令行传递
1. 配置 playbook;
2. 命令行传递安装程序包;
方法二: 在 playbook 中直接定义变量值;
1. 定义 playbook;
2. 执行命令即可;
方法三: 在 hosts Inventory 中为每个主机定义专用变量值;
方法四: 在 hosts Inventory 中定义组变量, 向组内所有的主机传递相同的变量;
演示 5:template, 基于模板复制配置文件; 以下定义一个完整的 nginx 的 playbook;
1. 准备模板文件, 将 worker_processes 的值修改为一个变量,;
其中 ansible_processor_vcpus 为 ansible 内置变量;
2. 定义一个完整的 nginx 的 playbook;
3. 测试运行;
演示 6: 角色 roles;
1. 进入 / etc/ansible/roles 目录, 在此目录中分别创建以各角色名称命名的目录, 并在每个角色命名的目录下分别创建 fileshandlersmetataskstemplates 和 vars 目录; 用不到的目录的可以创建为空目录, 也可以不创建;
2. 首先定义 nginx 角色的 tasks 目录, 如需调用一些模块, 它会自动到对应的目录中获取文件内容;
3. 定义 tasks 需要用到的模块目录的内容;
1) handlers 目录
2) 准备 templates 的模板文件, 并修改其进程数为系统变量, 并自定义监听端口变量;
3) 准备 vars 目录下的自定义变量;
4) 定义的文件树状图;
4. 定义 playbook 调用 roles;
5. 确保 / etc/ansible/ansible.cfg 定义的 roles 路径与刚才编辑的一致;
6. 测试运行;
来源: http://www.bubuko.com/infodetail-2503844.html