实例学习Ansible系列:K8S离线安装:二进制文件下载

这篇文章开始将对Ansible的使用进行稍微复杂一点的场景介绍,以kubernetes1.15.0为例,介绍如何使用ansible进行离线安装,整体以role的方式引入,这篇文章介绍一下如何进行二进制文件的下载。

所需内容

安装需要如下二进制文件:

  • kubernetes-server相关二进制
  • docker二进制
  • etcd二进制
  • cfssl相关二进制文件
  • cni相关二进制文件

下载role的主要功能实现

role的功能实现在tasks/main.yml中,详细如下所示:

[root@liumiaocn ansible]# cat download/tasks/main.yml 
---
# prepare download dir
- name: create download file in advance
  file: 
    path: "{{ item }}"
    state: directory
  with_items:
    - "{{ var_download_base_dir }}"
    - "{{ var_download_save_dir_k8s }}"
    - "{{ var_download_save_dir_cfssl }}"
    - "{{ var_download_save_dir_flannel }}"
    - "{{ var_download_save_dir_etcd }}"
    - "{{ var_download_save_dir_cni }}"
    - "{{ var_download_save_dir_docker }}"

# tasks file for download
- name: download binary kubernetes
  get_url:
    url:  '{{ var_download_url_k8s }}'
    dest: '{{ var_download_tmp_file_k8s }}'
    mode: '{{ var_download_save_mode }}'

- name: extract binary files of kubernetes
  unarchive:
    copy: no
    src: "{{ var_download_tmp_file_k8s }}"
    dest: "{{ var_download_base_dir }}" 
    extra_opts: "{{ var_download_unarchive_extras_files_k8s }}"

- name: create output kubernetes binary dir
  synchronize:
    src: "{{ var_download_tmp_dir_k8s }}/"
    dest: "{{ var_download_save_dir_k8s }}" 

- name: download binary :cfssl
  get_url:
    url:  '{{ var_download_url_cfssl }}'
    dest: '{{ var_download_file_cfssl }}'
    mode: '{{ var_download_save_mode_bin }}'

- name: download binary :cfssljson
  get_url:
    url:  '{{ var_download_url_cfssljson }}'
    dest: '{{ var_download_file_cfssljson }}'
    mode: '{{ var_download_save_mode_bin }}'

- name: download binary :cfsslcertinfo
  get_url:
    url:  '{{ var_download_url_cfsslcertinfo }}'
    dest: '{{ var_download_file_cfsslcertinfo }}'
    mode: '{{ var_download_save_mode_bin }}'

- name: download binary :flannel
  get_url:
    url:  '{{ var_download_url_flannel }}'
    dest: '{{ var_download_file_flannel }}'
    mode: '{{ var_download_save_mode }}'

- name: extract binary files of flannel
  unarchive:
    copy: no
    src: "{{ var_download_file_flannel }}"
    dest: "{{ var_download_save_dir_flannel }}" 
    extra_opts: "{{ var_download_unarchive_extras_files_flannel }}"

- name: download binary :etcd
  get_url:
    url:  '{{ var_download_url_etcd }}'
    dest: '{{ var_download_file_etcd }}'
    mode: '{{ var_download_save_mode }}'

- name: extract binary files of etcd
  unarchive:
    copy: no
    src: "{{ var_download_file_etcd }}"
    dest: "{{ var_download_save_dir_etcd }}" 
    extra_opts: "{{ var_download_unarchive_extras_files_etcd }}"

- name: create output docker binary dir
  synchronize:
    src: "{{ var_download_tmp_dir_etcd }}/"
    dest: "{{ var_download_save_dir_etcd }}" 

- name: download binary :cni
  get_url:
    url:  '{{ var_download_url_cni }}'
    dest: '{{ var_download_file_cni }}'
    mode: '{{ var_download_save_mode }}'

- name: extract binary files of cni
  unarchive:
    copy: no
    src: "{{ var_download_file_cni }}"
    dest: "{{ var_download_save_dir_cni }}" 

- name: download binary :docker
  get_url:
    url:  '{{ var_download_url_docker }}'
    dest: '{{ var_download_file_docker }}'
    mode: '{{ var_download_save_mode }}'

- name: extract binary files of docker
  unarchive:
    copy: no
    src: "{{ var_download_file_docker }}"
    dest: "{{ var_download_save_dir_docker }}" 

# change docker directory name because of docker file in directory
- name: copy docker/dockertmp/ from docker/docker/
  copy:
    src: "{{ var_download_tmp_dir_docker }}/"
    dest: "{{ var_download_tmp_dir_docker }}tmp/"

# delete docker/docker directory
- name : delete docker/docker directory for moving action
  file:
    path: "{{ var_download_tmp_dir_docker }}/"
    state: absent

- name: create output docker binary dir
  synchronize:
    src: "{{ var_download_tmp_dir_docker }}tmp/"
    dest: "{{ var_download_save_dir_docker }}" 

- name: delete useless temp files
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "{{ var_download_tmp_dir_k8s_server }}"
    - "{{ var_download_save_dir_flannel }}/README.md"
    - "{{ var_download_tmp_dir_docker }}"
    - "{{ var_download_tmp_dir_docker }}tmp"
    - "{{ var_download_tmp_dir_etcd }}"
    - "{{ var_download_file_cni }}"
    - "{{ var_download_file_docker }}"
    - "{{ var_download_file_etcd }}"
    - "{{ var_download_file_flannel }}"
    - "{{ var_download_tmp_file_k8s }}"
[root@liumiaocn ansible]# 

代码说明

此role主要实现了下载的功能,主要使用的基础知识和注意事项如下所示:

  • Ansible基本模块:只用到了copy/unarchive/synchronize/file/get_url此五个模块
  • 解压:解压时分两种情况,可以直接用到所有内容和只是一部分内容
  • 中间文件删除:不再使用的中间文件需要删除,最后使用file模块以列表的方式进行删除

执行日志

[root@liumiaocn ansible]# ansible-playbook download/tests/test.yml 

PLAY [localhost] ***********************************************************************************************************************

TASK [download : create download file in advance] **************************************************************************************
changed: [localhost] => (item=/tmp/binary)
changed: [localhost] => (item=/tmp/binary/kubernetes)
changed: [localhost] => (item=/tmp/binary/cfssl)
changed: [localhost] => (item=/tmp/binary/flannel)
changed: [localhost] => (item=/tmp/binary/etcd)
changed: [localhost] => (item=/tmp/binary/cni)
changed: [localhost] => (item=/tmp/binary/docker)

TASK [download : download binary kubernetes] *******************************************************************************************
changed: [localhost]

TASK [download : extract binary files of kubernetes] ***********************************************************************************
changed: [localhost]

TASK [download : create output kubernetes binary dir] **********************************************************************************
changed: [localhost]

TASK [download : download binary :cfssl] ***********************************************************************************************
changed: [localhost]

TASK [download : download binary :cfssljson] *******************************************************************************************
changed: [localhost]

TASK [download : download binary :cfsslcertinfo] ***************************************************************************************
changed: [localhost]

TASK [download : download binary :flannel] *********************************************************************************************
changed: [localhost]

TASK [download : extract binary files of flannel] **************************************************************************************
changed: [localhost]

TASK [download : download binary :etcd] ************************************************************************************************
changed: [localhost]

TASK [download : extract binary files of etcd] *****************************************************************************************
changed: [localhost]

TASK [download : create output docker binary dir] **************************************************************************************
changed: [localhost]

TASK [download : download binary :cni] *************************************************************************************************
changed: [localhost]

TASK [download : extract binary files of cni] ******************************************************************************************
changed: [localhost]

TASK [download : download binary :docker] **********************************************************************************************
changed: [localhost]

TASK [download : extract binary files of docker] ***************************************************************************************
changed: [localhost]

TASK [download : copy docker/dockertmp/ from docker/docker/] ***************************************************************************
changed: [localhost]

TASK [download : delete docker/docker directory for moving action] *********************************************************************
changed: [localhost]

TASK [download : create output docker binary dir] **************************************************************************************
changed: [localhost]

TASK [download : delete useless temp files] ********************************************************************************************
changed: [localhost] => (item=/tmp/binary/kubernetes/server)
ok: [localhost] => (item=/tmp/binary/flannel/README.md)
changed: [localhost] => (item=/tmp/binary/docker/docker)
changed: [localhost] => (item=/tmp/binary/docker/dockertmp)
changed: [localhost] => (item=/tmp/binary/etcd/etcd-v3.3.13-linux-amd64)
changed: [localhost] => (item=/tmp/binary/cni/cni-plugins-amd64-v0.7.4.tgz)
changed: [localhost] => (item=/tmp/binary/docker/docker-18.09.7.tgz)
changed: [localhost] => (item=/tmp/binary/etcd/etcd-v3.3.13-linux-amd64.tar.gz)
changed: [localhost] => (item=/tmp/binary/flannel/flannel-v0.11.0-linux-amd64.tar.gz)
changed: [localhost] => (item=/tmp/binary/kubernetes.tar.gz)

PLAY RECAP *****************************************************************************************************************************
localhost                  : ok=20   changed=20   unreachable=0    failed=0   

[root@liumiaocn ansible]#

结果确认

[root@liumiaocn ansible]# tree /tmp/binary/
/tmp/binary/
|-- cfssl
|   |-- cfssl
|   |-- cfssl-certinfo
|   `-- cfssljson
|-- cni
|   |-- bridge
|   |-- dhcp
|   |-- flannel
|   |-- host-device
|   |-- host-local
|   |-- ipvlan
|   |-- loopback
|   |-- macvlan
|   |-- portmap
|   |-- ptp
|   |-- sample
|   |-- tuning
|   `-- vlan
|-- docker
|   |-- containerd
|   |-- containerd-shim
|   |-- ctr
|   |-- dockerd
|   |-- docker-init
|   |-- docker-proxy
|   `-- runc
|-- etcd
|   |-- etcd
|   `-- etcdctl
|-- flannel
|   |-- flanneld
|   `-- mk-docker-opts.sh
`-- kubernetes
    |-- kube-apiserver
    |-- kube-controller-manager
    |-- kubectl
    |-- kubelet
    |-- kube-proxy
    `-- kube-scheduler

6 directories, 33 files
[root@liumiaocn ansible]#

ansible vs shell

和Shell脚本的比较可以参看:

  • https://liumiaocn.blog.csdn.net/article/details/88163677

代码路径

  • https://github.com/liumiaocn/easypack/tree/master/k8s/ansible

其他Ansible内容

  • https://liumiaocn.blog.csdn.net/article/details/87273800

你可能感兴趣的:(Kubernetes,Ansible)