在基于KVM的环境中添加一组开发服务器

好久没动 K8S,DevOps,准备折腾个 devops 的环境。考虑到 CentOS 未来的命运,这里使用 Ubuntu 20. 先在 KVM Host 上下载 ubuntu 并 copy 到目录/var/lib/libvirt/images/。

在 KVM HOST 上准备 Ubuntu20 的镜像

curl -O http://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
mv -iv focal-server-cloudimg-amd64.img /var/lib/libvirt/images/

在文件 virt-infra-ansible/inventory/devopServers.yml 中定义待创建的 devop servers 虚拟机参数。

进入目录 virt-infra-ansible/inventory,创建文件

参考:https://dhyuan.github.io/2020/09/28/devops/create_k8s_by_kubeadm/ 。对,本文就是在这个环境的基础上再添加 4 个与 degvops 相关的服务器。其中 repositoryServer 用于安装 Nexus、Harbor、GitLab 等。devopsServer0-2 用于其它 devops 相关服务。
需注意的是 focal 的 python 版本是 3,所以设置 /usr/bin/python3,考虑到 repositoryServer 的用途,其磁盘容量设为 500G。
… virt-infra-ansible/inventory/devopServers.yml,内容如下:

devopServers:
    hosts:
        repositoryServer:
        virt_infra_state: running
        virt_infra_distro: ubuntu
        virt_infra_distro_image: focal-server-cloudimg-amd64.img
        ansible_python_interpreter: /usr/bin/python3
        virt_infra_timezone: "Asia/Shanghai"
        virt_infra_disk_size: 500
        virt_infra_disk_cmd:
            - apt update
            - apt install -y net-tools
            - apt install -y lsb
            - useradd -m devops
            - echo "devops:password"| chpasswd
        devopsServer[0:2]:
        virt_infra_state: running
        virt_infra_distro: ubuntu
        virt_infra_distro_image: focal-server-cloudimg-amd64.img
        ansible_python_interpreter: /usr/bin/python3
        virt_infra_timezone: "Asia/Shanghai"
        virt_infra_disk_cmd:
            - apt update
            - apt install -y net-tools
            - apt install -y lsb
            - useradd -m devops
            - echo "devops:password"| chpasswd
    vars:
        virt_infra_root_password: password
        virt_infra_disk_size: 80
        virt_infra_ram: 4094
        virt_infra_ram_max: 8192
        virt_infra_cpus: 4
        virt_infra_cpus_max: 8
        virt_infra_state: running
        virt_infra_networks:
        - name: br0
            type: bridge

一键创建 devop servers 虚拟机

cd virt-infra-ansible
ansible-playbook ./virt-infra.yml --limit kvmhost,devopServers

如果出现问题,可删除刚才创建的 guest 虚拟机

通过使用参数 –extra-vars virt_infra_state=undefined 来删除。比如:

ansible-playbook ./virt-infra.yml --limit kvmhost,devopServers --extra-vars virt_infra_state=undefined

一键在 devop servers 节点上安装 docker

在 Ubuntu 环境中安装 docker,
1)添加 Docker 的阿里云源。
在文件 ansible-role-docker-ce/tasks/setup-repository-Ubuntu.yml 尾部设置 aliyun source。

- name: Add Aliyun Docker official GPG key
become: true
apt_key:
url: http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg
state: present
register: \_pkg_result
until: \_pkg_result is succeeded
when:

- docker_network_access | bool
- (\_docker_os_dist == "Ubuntu" and \_docker_os_dist_major_version | int > 14) or
    (\_docker_os_dist == "Debian" and \_docker_os_dist_major_version | int > 7)

- name: Add Aliyun Docker CE repository with correct channels (Ubuntu/Debian)
become: true
lineinfile:
path: /etc/apt/sources.list.d/docker-ce.list
line: "deb [arch={{ _docker_os_arch|lower }}] http://mirrors.aliyun.com/docker-ce/linux/{{ \_docker_os_dist|lower }} {{ ansible_lsb.codename }} stable"
insertbefore: BOF

2)找到适合自己 Ubuntu 版本的 Docker 版本。
可用命令 apt-cache madison docker-ce 查看。

root@basehost0:~# apt-cache madison docker-ce
docker-ce | 5:20.10.4~3-0~ubuntu-focal | http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.4~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.3~3-0~ubuntu-focal | http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.3~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.2~3-0~ubuntu-focal | http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
。。。 。。。

3)在 Ubuntu Guest 上安装 Docker。
因为 virt-infra-ansible/install_docker.yml 中 docker 版本默认为 19.03.8,并不适用于 Ubuntu20 的环境。
可以通过在命令行指定参数 docker_version 来安装,如下:

    ansible-playbook ./install_docker.yml --limit devopServers -vv --extra-vars docker_version=5:20.10.4~3-0~ubuntu-focal

之前 CentOS7 Guest 上安装 Docker 的命令:

ansible-playbook ./install_docker.yml \
--limit devopServers -v \
-e '{"docker_repository_url": {"Ubuntu focal": " http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" }}'

登录到一台 Ubuntu Guest 机器,查看已安装的 Docker 版本以确认成功。

root@basehost0:~# docker -v
Docker version 20.10.4, build d3cb89e