Tuesday, July 2, 2019

UBUNTU18+KVM+VAGRANT+KUBERNETES 5)Install ansible and disk partition multi disks

 This article shows how to install ansible and using playbook(.yaml) on ubuntu18 and centos7 kvm guest os to create directory,filesystem and mounted(/etc/fstab).


Below Youtube also explains..^^

 https://www.youtube.com/watch?v=dVPiZi2daNA&feature=youtu.be

 oyj@oyj-X555QG:~/ansible/pb$ which pip
/usr/bin/pip
oyj@oyj-X555QG:~/ansible/pb$ pip install ansible --user
Collecting ansible
Collecting jinja2 (from ansible)
  Using cached https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl
Collecting PyYAML (from ansible)
Collecting cryptography (from ansible)
  Using cached https://files.pythonhosted.org/packages/e6/68/50698ce24c61db7d44d93a5043c621a0ca7839d4ef9dff913e6ab465fc92/cryptography-2.7-cp27-cp27mu-manylinux1_x86_64.whl
Collecting MarkupSafe>=0.23 (from jinja2->ansible)
  Using cached https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Collecting asn1crypto>=0.21.0 (from cryptography->ansible)
  Using cached https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl
Collecting enum34; python_version < "3" (from cryptography->ansible)
  Using cached https://files.pythonhosted.org/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl
Collecting ipaddress; python_version < "3" (from cryptography->ansible)
  Using cached https://files.pythonhosted.org/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl
Collecting cffi!=1.11.3,>=1.8 (from cryptography->ansible)
  Using cached https://files.pythonhosted.org/packages/8d/e9/0c8afd1579e5cf7bc0f06fbcd7cdb954cbc0baadd505973949a99337da1c/cffi-1.12.3-cp27-cp27mu-manylinux1_x86_64.whl
Collecting six>=1.4.1 (from cryptography->ansible)
  Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography->ansible)
Installing collected packages: MarkupSafe, jinja2, PyYAML, asn1crypto, enum34, ipaddress, pycparser, cffi, six, cryptography, ansible
Successfully installed MarkupSafe-1.1.1 PyYAML-5.1.1 ansible-2.8.1 asn1crypto-0.24.0 cffi-1.12.3 cryptography-2.7 enum34-1.1.6 ipaddress-1.0.22 jinja2-2.10.1 pycparser-2.19 six-1.12.0
Segmentation fault (core dumped)

oyj@oyj-X555QG:~/ansible/pb$ echo -e "\e[34m Some package might be not workingnow, but ok I think  \e[00m "


oyj@oyj-X555QG:~/ansible/pb$ echo -e "\e[34m Some package might be not workingnow, but ok I think  \e[00m "
 Some package might be not workingnow, but ok I think  


oyj@oyj-X555QG:~/ansible/pb$ cat ~/.bashrc | grep -i "ansible"
export ANSIBLE_HOME=$HOME/ansible
export ANSIBLE_CONFIG=$HOME/ansible/ansible.cfg
export ANSIBLE_HOST_KEY_CHECKING=false
oyj@oyj-X555QG:~/ansible/pb$ echo $PATH | grep ".local"
/home/oyj/.local/bin:/home/oyj/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin


oyj@oyj-X555QG:~/ansible/pb$ cat ~/ansible/ansible.cfg | grep hosts
inventory      = /home/oyj/ansible/hosts

oyj@oyj-X555QG:~/ansible/pb$ cat ~/ansible/hosts
[kuber]
10.1.0.2
10.1.0.3
10.1.0.4
10.1.0.5
[kuber:vars]
ansible_private_key_file=~/INSTALL/u18kvk8s/k8s/id_rsa
ansible_ssh_user=vagrant

oyj@oyj-X555QG:~/ansible/pb$ ansible kuber -m ping

oyj@oyj-X555QG:~/ansible/pb$ ansible kuber -m ping
^[[A10.1.0.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.1.0.5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.1.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.1.0.2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"

oyj@oyj-X555QG:~/ansible/pb$ echo -e "\e[34m Seems like it is good! \e[00m "
 Seems like it is good! 

oyj@oyj-X555QG:~/ansible/pb$ echo -e "\e[34m Now I will add disks and add filesystem onto k8s nodes \e[00m "

oyj@oyj-X555QG:~/ansible/pb$ echo -e "\e[34m For 3 nodes and 9 disks with onne command, it still needs some yaml \e[00m "

 Now I will add disks and add filesystem onto k8s nodes 

oyj@oyj-X555QG:~/ansible/pb$ cat INSTALL/u18kvk8s/k8s/Vagrantfile 

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
 
   #config.vm.synced_folder ".","/vagrant",type:"basic"
   config.vm.define "kubemaster", primary: true do |kubemaster|
   kubemaster.vm.box = "centos/7"
   kubemaster.vm.provision "shell", path: "check_key.sh"
   kubemaster.vm.provision "shell", :path => "swapoff.sh"
   kubemaster.vm.provision "shell", :path => "k8s_docker_install.sh"
   kubemaster.vm.provision "shell", :path => "k8s_install.sh"
   kubemaster.vm.provision "shell", :path => "kubeadm-flannel.sh"
   kubemaster.vm.network "private_network", ip:"10.1.0.2"
   kubemaster.vm.host_name = "kubemaster"
     kubemaster.vm.provider :libvirt do |lv|

      lv.cpus = 2
      lv.memory = 2048
     end
   end


   config.vm.define "kubeworker1" do |kubeworker1|
   kubeworker1.vm.box = "centos/7"
   kubeworker1.vm.provision "shell", path: "check_key.sh"
   kubeworker1.vm.provision "file", source: "id_rsa",destination: "/home/vagrant/.ssh/id_rsa"
   kubeworker1.vm.provision "shell", :path => "swapoff.sh"
   kubeworker1.vm.provision "shell", :path => "k8s_docker_install.sh"
   kubeworker1.vm.provision "shell", :path => "k8s_install.sh"
   kubeworker1.vm.provision "shell", :path => "admjoin.sh"
   kubeworker1.vm.network "private_network", ip:"10.1.0.3"
   kubeworker1.vm.host_name = "kubeworker1"
     kubeworker1.vm.provider :libvirt do |lv|
      lv.memory = 2048
      lv.storage :file, :size => '1G', :type => 'raw'
      lv.storage :file, :size => '1G', :type => 'raw'
      lv.storage :file, :size => '1G', :type => 'raw'
     end
   end

  
   config.vm.define "kubeworker2" do |kubeworker2|
   kubeworker2.vm.box = "centos/7"
   kubeworker2.vm.provision "shell", path: "check_key.sh"
   kubeworker2.vm.provision "file", source: "id_rsa",destination: "/home/vagrant/.ssh/id_rsa"
   kubeworker2.vm.provision "shell", :path => "swapoff.sh"
   kubeworker2.vm.provision "shell", :path => "k8s_docker_install.sh"
   kubeworker2.vm.provision "shell", :path => "k8s_install.sh"
   kubeworker2.vm.provision "shell", :path => "admjoin.sh"
   kubeworker2.vm.network "private_network", ip:"10.1.0.4"
   kubeworker2.vm.host_name = "kubeworker2"
     kubeworker2.vm.provider :libvirt do |lv|
      lv.memory = 2048
      lv.storage :file, :size => '1G', :type => 'raw'
      lv.storage :file, :size => '1G', :type => 'raw'
      lv.storage :file, :size => '1G', :type => 'raw'
     end
   end

   config.vm.define "kubeworker3" do |kubeworker3|
   kubeworker3.vm.box = "centos/7"
   kubeworker3.vm.provision "shell", path: "check_key.sh"
   kubeworker3.vm.provision "file", source: "id_rsa",destination: "/home/vagrant/.ssh/id_rsa"
   kubeworker3.vm.provision "shell", :path => "swapoff.sh"
   kubeworker3.vm.provision "shell", :path => "k8s_docker_install.sh"
   kubeworker3.vm.provision "shell", :path => "k8s_install.sh"
   kubeworker3.vm.provision "shell", :path => "admjoin.sh"
   kubeworker3.vm.network "private_network", ip:"10.1.0.5"
   kubeworker3.vm.host_name = "kubeworker3"
     kubeworker3.vm.provider :libvirt do |lv|
      lv.memory = 2048
      lv.storage :file, :size => '1G', :type => 'raw'
      lv.storage :file, :size => '1G', :type => 'raw'
      lv.storage :file, :size => '1G', :type => 'raw'
     end
   end
end

oyj@oyj-X555QG:~/ansible/pb$ cat disk_ext4.yaml
---
#Below is k8s workder nodes that has 3 disks
- hosts: 10.1.0.5,10.1.0.4,10.1.0.3
  become: yes

  tasks:
    # Create file system on each nodes' local disks
    - name: Create ext4 file systems
      filesystem:
        fstype: ext4
        dev: "{{ item }}"
        force: yes
      with_items:
        - /dev/vdb
        - /dev/vdc
        - /dev/vdd 

    - name: Create mount dir for /dev/vdxx
      file:
        path: "{{ item }}"
        state: directory
        owner: root
        group: root
      with_items:
        - /db1
        - /db2
        - /web

    - name: Mount each dev to each dir
      mount:
        path: "{{item.tar}}"
        src: "{{item.src}}"
        fstype: ext4
        state: mounted
      with_items:
        - { src: '/dev/vdb', tar: '/db1' }
        - { src: '/dev/vdc', tar: '/db2' }
        - { src: '/dev/vdd', tar: '/web' }

oyj@oyj-X555QG:~/ansible/pb$ ansible-playbook disk_ext4.yaml

PLAY [10.1.0.5,10.1.0.4,10.1.0.3] ***********************************************************************

TASK [Gathering Facts] **********************************************************************************
ok: [10.1.0.3]
ok: [10.1.0.5]
ok: [10.1.0.4]

TASK [Create ext4 file systems] *************************************************************************
changed: [10.1.0.5] => (item=/dev/vdb)
changed: [10.1.0.3] => (item=/dev/vdb)
changed: [10.1.0.4] => (item=/dev/vdb)
changed: [10.1.0.5] => (item=/dev/vdc)
changed: [10.1.0.3] => (item=/dev/vdc)
changed: [10.1.0.4] => (item=/dev/vdc)
changed: [10.1.0.5] => (item=/dev/vdd)
changed: [10.1.0.4] => (item=/dev/vdd)
changed: [10.1.0.3] => (item=/dev/vdd)

TASK [Create mount dir for /dev/vdxx] *******************************************************************
ok: [10.1.0.5] => (item=/db1)
ok: [10.1.0.4] => (item=/db1)
ok: [10.1.0.3] => (item=/db1)
ok: [10.1.0.3] => (item=/db2)
ok: [10.1.0.5] => (item=/db2)
ok: [10.1.0.4] => (item=/db2)
ok: [10.1.0.3] => (item=/web)
ok: [10.1.0.5] => (item=/web)
ok: [10.1.0.4] => (item=/web)

TASK [Mount each dev to each dir] ***********************************************************************
changed: [10.1.0.4] => (item={u'src': u'/dev/vdb', u'tar': u'/db1'})
changed: [10.1.0.3] => (item={u'src': u'/dev/vdb', u'tar': u'/db1'})
changed: [10.1.0.5] => (item={u'src': u'/dev/vdb', u'tar': u'/db1'})
changed: [10.1.0.4] => (item={u'src': u'/dev/vdc', u'tar': u'/db2'})
changed: [10.1.0.3] => (item={u'src': u'/dev/vdc', u'tar': u'/db2'})
changed: [10.1.0.5] => (item={u'src': u'/dev/vdc', u'tar': u'/db2'})
changed: [10.1.0.4] => (item={u'src': u'/dev/vdd', u'tar': u'/web'})
changed: [10.1.0.3] => (item={u'src': u'/dev/vdd', u'tar': u'/web'})
changed: [10.1.0.5] => (item={u'src': u'/dev/vdd', u'tar': u'/web'})

PLAY RECAP **********************************************************************************************
10.1.0.3                   : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
10.1.0.4                   : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
10.1.0.5                   : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  


oyj@oyj-X555QG:~/ansible/pb$ ansible kuber -m shell -a 'df -h | grep /web' -b
10.1.0.5 | CHANGED | rc=0 >>
/dev/vdd        976M  2.6M  907M   1% /web

10.1.0.3 | CHANGED | rc=0 >>
/dev/vdd        976M  2.6M  907M   1% /web

10.1.0.4 | CHANGED | rc=0 >>
/dev/vdd        976M  2.6M  907M   1% /web

10.1.0.2 | FAILED | rc=1 >>
non-zero return code

oyj@oyj-X555QG:~/ansible/pb$ ansible kuber -m shell -a 'df -h | grep /db' -b
10.1.0.4 | CHANGED | rc=0 >>
/dev/vdb        976M  2.6M  907M   1% /db1
/dev/vdc        976M  2.6M  907M   1% /db2

10.1.0.3 | CHANGED | rc=0 >>
/dev/vdb        976M  2.6M  907M   1% /db1
/dev/vdc        976M  2.6M  907M   1% /db2

10.1.0.5 | CHANGED | rc=0 >>
/dev/vdb        976M  2.6M  907M   1% /db1
/dev/vdc        976M  2.6M  907M   1% /db2

10.1.0.2 | CHANGED | rc=0 >>
overlay          40G  4.2G   36G  11% /var/lib/docker/overlay2/db364a3d78c278920f9564755bf51cbee21cdf00951326cff7d4d0907a621b0b/merged


No comments:

Post a Comment