iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 13
1
Kubernetes

在地端建置Angular+ASP.NET Core的DevOps環境系列 第 13

day13_Ansible01_安裝及介紹

Ansible是一個簡單的IT自動化平台,可使應用程序和系統更易於部署。
感覺就像是用python寫的後門程式,只不過用在好的地方,它就是好工具。

主要參考

凍仁哥

今天只介紹比較簡單的,完整的Ansible請參考上面連結喔

1.控制主機 (Control Machine)

安裝Ansible

  • macOS
#安裝Homebrew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install curl #安裝curl
$ curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python # 安裝easy_install,python應該有內建了
$ sudo easy_install pip # 安裝pip
$ sudo pip install ansible #安裝ansible
  • Ubuntu
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible
  • 用pip裝ansible
$ sudo apt-get install -y python-pip
$ sudo pip install -U pip
$ sudo pip install ansible

2.被控節點(Managed Node):

確認python版本(2.6以上或3以上)

ansible預設會跑/usr/bin/python
我的ubuntu 18.04,/usr/bin/python是2.7版,/usr/bin/python3是3.6.5
所以ubuntu原生就能當Managed Node

使用Vagrant and Ansible

參考:

在一個專案裡面,
跟vagrant有關的檔案大概只有:vagrantfile,inventory
而跟ansible有關的檔案就很多:
ansible.cfg、playbook、一堆roles...
roles的tasks除了基本語法、環境變數、還有各種modules要學

所以ansible不是一朝一夕可上手的工具,
我也是邊學邊寫,所以文章比較亂喔,甚至有些是理解錯誤
怕被打亂的朋友們,只要關注官網連結即可
例如: module裡的shell
官網連結
https://docs.ansible.com/ansible/latest/modules/shell_module.html
這樣你就知道有個shell module,
至於shell module怎麼用,自己看官網文件比較不會被我誤導

~/myDevEnv/Vagrantfile

Vagrant.require_version ">= 1.7.0"
Vagrant.configure(2) do |config| 
# 1是非常舊的版本,現在幾乎都是2了
# config 就想像成 object
  config.vm.box = "ubuntu/trusty64"
  # Disable the new default behavior introduced in Vagrant 1.7, to
  # ensure that all Vagrant machines will use the same SSH key pair.
  # See https://github.com/mitchellh/vagrant/issues/5005
  config.ssh.insert_key = false # Vagrant global insecure key 要設為 false
# 
  config.vm.provision "ansible" do |ansible|
    ansible.verbose = "v" # show 出 ansible-playbook 指令
    # 對除錯很有幫助
    ansible.playbook = "playbook.yml"
  end
end
$ vagrant up # 第1次建立VM
# vagrant up執行後,資料會被在$(PWD)/.vagrant/
$ vagrant provision # 在已存在的 VM 上重跑 playbook 
# 其實我還是搞不懂 vagrant provision
# 是要自己把環境清乾淨嗎?
# vagrant自動幫你建ansible inventory(電腦清單)
Vagrant automatically creates an Ansible inventory file in .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory.
# vagrant up後,會建基本的vm,包含 ssh private key
# 再來就要換手給 ansible 繼續「自動化」囉
# 把 Vagrant 設的 ssh private key 給 ansible
default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
.vagrant/machines/[machine name]/[provider]/private_key

$ ansible-playbook --private-key=.vagrant/machines/default/virtualbox/private_key -u vagrant -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml

~/myDevEnv/playbook.yml

- hosts: MasterNode01
  tasks:
    # task 1
    - name: test connection
      ping: # 內建測試模組,control machine->managed node(回傳"pong"訊息)
      register: message # 把managed node(回傳"pong"訊息)存在變數 {{ message }}

    # task 2
    - name: print debug message
      debug:
        msg: "{{ message }}" # print debug message

Ansible for VMware

ansible有支援vmware,可對ESXi server或vCenter server作一些操作(佈署vm、刪vm…)
https://docs.ansible.com/ansible/latest/vmware/index.html

vault

如果你要在真實環境使用ansible,可參考ansilbe vault,把一些機敏資訊加密
https://docs.ansible.com/ansible/latest/user_guide/vault.html

ansible vault可以拿來加密group_vars、host_vars這些inventory變數

$ ansible-vault create foo.yml # 建加密檔(原本沒foo.yml)
$ ansible-vault encrypt foo.yml # 原本已有的foo.yml
$ ansible-vault edit foo.yml
$ ansible-vault view foo.yml # 只看不改
$ ansible-vault decrypt foo.yml # 解開

依據官網以及凍仁哥的建議,我們的Ansible的「目錄結構」可參考Best Practices
Best Practices蠻重要的,裡頭也有建議一些原則性的作法,例如:
利用roles來分group、儘量簡化、如何放置variables跟vaults(保險庫)

看不懂的就先不放了

README.md               # 該專案的說明文件。
Vagrantfile
ansible.cfg             # configure for ansible
files/                  # 要使用 files module 複製到 Managed node 的檔案
group_vars/             # 放不同環境的變數
roles/                  # 可重複使用的role,可上galaxy.ansible.com下載
tasks/                  # 各個task yml檔
  setup.yml
  restart.yml
inventory/
  hosts                 # 除了主機清單,還可以描述ssh連線設定、環境(dev、test、prod)

ansible.cfg

ansible.cfg
[defaults]

inventory = hosts # 指定 inventory path,inventory有"清單"的意思
remote_user = vagrant #對應 ssh-config 的 User,ssh連線的帳號,也可以設定在hosts裡
private_key_file = .vagrant/machines/default/virtualbox/private_key #對應 ssh-config 的 IdentityFile
# 如果是用 vagrant 建立的 vm,會自動設定好 ssh key,很方便
# private_key_file = ~/.ssh/id-rsa 
# 如果你是自己設定ssh key,要指定 private key file(既有的vm或實體機)

# host_key_checking = flase # 不要詢問加入ssh key,通常都設false(才會一直跑下去麻)

hosts/hosts.ini

inventory/hosts.ini

放在哪都可能,要看ansible.cfg的inventory=inventory path

# k8s情境
[etcds]
172.16.35.[10:10] ansible_user=vagrant ansible_password=vagrant
# ansible_ssh_host,ansible_ssh_port,ansible_ssh_user,ansible_ssh_private_key_file,ansible_ssh_pass
# 比較安全的方式,private_key_file放在保險箱,不要用ansible_ssh_pass(明碼密碼)

[masters]
172.16.35.[10:10] ansible_user=vagrant ansible_password=vagrant
[nodes]
172.16.35.[11:12] ansible_user=vagrant ansible_password=vagrant

# DevOps情境
[dev]
...
[test]
...
[prod]
...

# 如果你想在localhost做一些tasks,那就是ssh連回本機(若有更好的作法請分享)
local  ansible_ssh_host=127.0.0.1  ansible_ssh_port=32805 ansible_ssh_pass=x ansible_ssh_user=x
[local]
local

[kube-cluster:children]
masters
nodes

List of Behavioral Inventory Parameters

在inventory裡的看到的參數ansible_ssh_host,有非常多,可參考官網文件
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#splitting-out-host-and-group-specific-data
我們這邊大概列一些,看參數名稱就能猜到用途,就不一一說明了

# General for all connections:
ansible_host
ansible_port
ansible_user

# Specific to the SSH connection:
ansible_ssh_pass # 密碼要放在Vaults(ansible也有自己的Vaults),最後幾天會介紹
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#best-practices-for-variables-and-vaults
ansible_ssh_private_key_file # 指定多個keys,如果你不用ssh agent的話(官網強烈建議用ssh agent)
ansible_ssh_common_args # 可能用在有proxy的網路環境吧
ansible_sftp_extra_args
ansible_scp_extra_args
ansible_ssh_extra_args
ansible_ssh_pipelining # 可以蓋掉 ansible.cfg的pipelining設定值
ansible_ssh_executable # 可以蓋掉 ansible.cfg的ssh_executable設定值

# ansible_become # 是否要提權
Equivalent to ansible_sudo or ansible_su, allows to force privilege escalation
ansible_become_method
Allows to set privilege escalation method
ansible_become_user
ansible_become_pass
ansible_become_exe
ansible_become_flags

安裝role

在實際的ansible目錄裡,我們很難從無到有,
1、目錄的結構依據官方的Best Practices
2、找一些別人已經寫好的role
3、再寫playbook.yml

這裡建議在1個vm裡,一步一步慢慢玩,
可參考別人的ansible專案,自己玩的話不要一開始玩太大太完整的,太多看不懂
例如:
(a)用vagrant建vm
(b)docker、docker_ubuntu、docker-compose、docker-swarm
(c)gitlab、gitlab_runner
(d)建k8s cluster (大魔王,難怪大家都要用minikube來練習k8s)

k8s實在太多參數了,
建議可以先用kubeadm手動建立k8s cluster,
了解各種參數後,確定該參數可行,再來試用ansible建k8s cluster

首先,到官網找別人封裝好的role

https://galaxy.ansible.com/

$ ansible-galaxy install -f -p roles Role名稱 
# 指定安裝到roles/

怎麼用galaxy上面的role呢?

例如,我們要利用ansible來安裝k8s的Node的環境,所以search kubernetes-modules
https://galaxy.ansible.com/ansible/kubernetes-modules
https://github.com/ansible/ansible-kubernetes-modules
下載數很高,支援Ubuntu,選Read Me有詳細的使用方式

  • 在你的ansible目錄底下
$ ansible-galaxy install ansible.kubernetes-modules -p roles
  • 就可以在 playbook (xxx.yml) 使用該Role了

---
- hosts: localhost
 remote_user: root
 roles:
  - role: ansible.kubernetes-modules
   install_python_requirements: no
  - role: hello-world
# 實際要用的話,相關參數還是得給的,例如:
# kubeconfig、ca憑證、private key、api token…等等

另一個比較高下載率,且支援Ubuntu的是
https://galaxy.ansible.com/grycap/kubernetes

  • 在既有的ansible目錄底下
$ ansible-galaxy install grycap.kubernetes -p roles
  • 在 playbook 中使用該 role
- hosts: server
  roles:
  - { role: 'grycap.kubernetes', kube_apiserver_options: [{option: "--insecure-port", value: "8080"}] }
# Role Variables,該給的k8s設定還是躲不掉
# kube_version、kube_type_of_node、kube_pod_network_cidr、kube_network…

這裡只示範怎麼 安裝Role(ansible-galaxy install)、使用Role(在playbook.yml)
而playbook.yml的task也可以抽到tasks/各別的.yml,以利重複使用
而 roles/、tasks/,則可以定義在ansible.cfg
明天介紹playbook喔


上一篇
day12_Vagrant基本安裝&說明
下一篇
day14_Ansible02_playbook
系列文
在地端建置Angular+ASP.NET Core的DevOps環境31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言