【DevOps】DevOps 实践之路(二):使用 Ansible 批量安装与配置 Docker

前言


本篇开始我们正式进入实战阶段,实战的目标就是把业务迁移到 K8s 集群中去管理,从而 达到 具备 CI/CD 、自动修复错误、一键扩容缩容、监测与预警等功能,我们使用的技术栈就是上文提到的 :

Ansible + Gitlab + Jenkins Pipeline + Docker+k8s + Helm

本篇我们开始实战 ansible 的使用,并用这个工具 多机安装并配置 Docker。

认识 Ansible


关于 Ansible,我们只需要知道一下几点就OK:

1、Ansible 是一款自动化运维工具,通过 SSH 协议 的方式对远程服务器进行集中化的配置管理、应用部署等,常结合Jenkins来实现自动化部署;

2、Ansible 基于python语言,由 Paramiko 和 PyYAML 两个关键模块构建;

用大白话说,就是我们把要管理的机器的信息 写在 Inventory 中,把对机器执行的操作 记录在 playbook(剧本) 中,就可以实现对多服务器的批量操作,从而还能 记录 与复现 操作过程,这也是目前非常流行的 基础设施即代码 (Infrastructure As Code)概念的体现,Tmuxp、Ansible、Dockerfile 等 都体现了这一理念;

https://www.ansible.com/
http://www.ansible.com.cn/

Ansible 批量安装 Docker


安装 Ansible

一般有两种安装方法,通过 sudo apt install ansible 命令 或者 手动下载 tar 安装包安装,为了保证安装最新版本,这里使用 tar 安装包的方式。

wget https://files.pythonhosted.org/packages/32/62/eec759cd8ac89a866df1aba91abf785486fed7774188a41f42f5c7326dcb/ansible-2.9.13.tar.gz

tar -zxvf tar zxvf ansible-2.9.13.tar.gz

cd ansible-2.9.13

python setup.py build

python setup.py install

mkdir /etc/ansible

cp  examples/ansible.cfg  examples/hosts  /etc/ansible/

ansible --version

配置要安装 Docker 的 hosts

1、编辑 hosts

vim /etc/ansible/hosts

[k8s_nodes1]
100.000.00.[164:166]
100.000.00.[168:170]

[k8s_nodes1:vars]
ansible_port=11022
ansible_user=rdadmin
ansible_sudo_pass="123456"

2、配置 被管理主机 的 免密登录

ssh-keygen

ssh-copy-id -i ~/.ssh/id_rsa.pub rdadmin@xxx.xxx.xx.168

3、测试能否 通过 ansible 连接到 被管理的 主机

ansible k8s_nodes1 -m  ping

发现其中有几台机器失败,首先通过ssh 调试命令查看

ssh -vvv rdadmin@120.133.xx.xxx -p 11022

调试发现,和可以 ping 通的机器比,没有 Authentication succeeded ,再查看失败机器的 /var/log/secure,可以

最后执行如下命令即可:

cd /home
chmod 700 rdadmin/

PS:.
.ssh 目录的权限必须是 700
.ssh/authorized_keys 文件权限必须是 600

编写 playbook 安装 Docker

先回顾一下通过命令行安装 Docker 的步骤,然后根据命令行编写 playbook,便于安装过程的记录与复用;

sudo apt remove docker docker-ce docker-engine docker.io containerd runc
sudo apt purge docker-ce
sudo rm -rf /var/lib/docker
sudo apt update
sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu \
 $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

在改写安装 docker 命令之前,先修改每个机器的 APT 源,防止安装过程出现错误;

首先写一个修改 apt 源的脚本:

vim update_apt_source.sh

#!/bin/bash
mv /etc/apt/sources.list /etc/apt/sources.list.bak
SYS_VERSION=$(lsb_release -c | grep -o "\s.*")
cat >> /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-proposed main restricted universe multiverse
EOF
apt update

接着写一个playbook,通过该 playbook 将脚本批量分发到集群机器上,并在各个机器执行该脚本:

vim update_apt_source.yml

- hosts: k8s_nodes1
  become: true
  vars:
    docker_version: 18.09.2
  tasks:
    - name: copy update_apt_source.sh
      copy:
        src: "/usr/GitCodes/CopyrightMonitor/DevOps/Ansible/playbooks/update_apt_source.sh"
        dest: '/etc/apt'
        owner: root
        group: root
        mode: 0777
    - name: update apt source.list
      shell: /etc/apt/update_apt_source.sh
 
ansible-playbook update_apt_source.yml

最后将安装 docker 的命令改写为 playbook:

vim install_docker_playbook1.yml

- hosts: k8s_nodes1
  become: true
  vars:
    docker_version: 18.09.2
  tasks:
    - name: remove old docker
      apt: name={{item}} state=absent
      with_items:
        - 'docker'
        - 'docker-ce'
        - 'docker-engine'
        - 'docker.io'
    - name: purge docker
      apt: name=docker-ce state=absent purge=yes
    - name: rm docker files
      command: rm -rf /var/lib/docker
    - name: apt update
      apt: update_cache=yes
    - name: install dependencies
      apt: name={{item}} state=present
      with_items:
        - 'apt-transport-https'
        - 'ca-certificates'
        - 'curl'
        - 'software-properties-common'
    - name: add GPG key2
      shell: sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    - name: set stable repo2
      shell: sudo add-apt-repository  "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu  $(lsb_release -cs) stable"
      #apt_key:
        #repo:  deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable
    - name: apt update
      apt:
        update_cache: yes
    - name: install docker
      apt: name={{item}} state=latest
      with_items:
        - "docker-ce"
        - 'docker-ce-cli'
        - 'containerd.io'

ansible-playbook install_docker_playbook1.yml

这个过程中可能遇到一些错误:

错误1:

	The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8

解决:

sudo gpg --keyserver keyserver.ubuntu.com --recv 7EA0A9C3F273FCD8//(这个公钥根据提示来写的
sudo gpg --export --armor 5523BAEEB01FA116 | sudo apt-key add -
sudo apt update

安装完成后,可以执行如下命令查看 每台机上的 Docker 是否安装成功,以及 Docker 的版本;

ansible k8s_nodes1 -m command -a "docker version"

编写 playbook 配置 Docker

和安装 Docker 一样,首先看下配置 Docker 的步骤;

1、将 Docker 用户 加入 Docker 用户组

sudo groupadd docker

sudo usermod -aG docker

2、在 /etc/docker 创建配置文件 daemon.json

sudo vim daemon.json
{
	  "registry-mirrors": ["https://mfrhehq4.mirror.aliyuncs.com"],
	  "insecure-registries":["172.17.0.2:5000"],
	  "exec-opts":["native.cgroupdriver=systemd"],
	  "log-driver": "json-file",
	  "log-opts": {
		         "max-size": "10m",
			 "max-file":"1"
		      }

}

esc -> :wq -> enter

3、设定开机自启动、重载配置文件、重启docker;

sudo systemctl enable docker
sudo systemctl daemon-reload
sudo service docker restart

4、将上面的步骤改写为 playbook

首先在当前路径下创建一个 daemon.json 文件

vim daemon.json

{
	 "registry-mirrors": ["https://mfrhehq4.mirror.aliyuncs.com"],
	 "insecure-registries":["120.133.xx.xxx:5000"],
	 "exec-opts":["native.cgroupdriver=systemd"],
	 "log-driver": "json-file",
	 "log-opts": {
				"max-size": "10m",
				"max-file":"1"
				}
}

然后 编写 playbook ,主要将配置文件放到各个机器上,然后添加当前用户到 docker 组 ,最后配置 docker 开机自启动,并重载配置、重启 Docker;

vim docker_config.yml

- hosts: k8s_nodes1 k8s_nodes2 k8s_nodes3
  become: true
  vars:
    docker_version: 18.09.2
  tasks:
    - name: create docker group
      shell:  "groupadd docker"
      ignore_errors: yes  #新增

    - name: add docker to user group
      shell:  "usermod -aG docker  {{ansible_user}}"
      
    - name: mv daemon.json
      copy:
        src: "./daemon.json"
        dest: '/etc/docker'
#        owner: root
#        group: root
#        mode: 0777

    - name: reload config and restart docker
      shell: systemctl enable docker && systemctl daemon-reload && service docker restart

ansible-playbook docker_config.yml

5、使用 ansible 命令查看 Docker 是否启动成功,以及配置是否生效;

ansible k8s_nodes1 -m command -a "systemctl status docker -l"

ansible k8s_nodes1 -m command -a "docker info"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值