added ansible section of the guide
This commit is contained in:
@@ -182,7 +182,7 @@ thats totally optional.
|
|||||||
|
|
||||||
### **Provision with** ***Ansible***
|
### **Provision with** ***Ansible***
|
||||||
you want to **create an _Ansible_ control node** and that is as simple as
|
you want to **create an _Ansible_ control node** and that is as simple as
|
||||||
installing a package onto your machine. Ansible is built using python
|
installing a package onto your machine. _Ansible_ is built using python
|
||||||
which makes it easy to install as **we can use the python pip package
|
which makes it easy to install as **we can use the python pip package
|
||||||
manger.** To check you have it installed do ```pip --version``` if
|
manger.** To check you have it installed do ```pip --version``` if
|
||||||
it's not installed do the following
|
it's not installed do the following
|
||||||
@@ -198,8 +198,8 @@ it's not installed do the following
|
|||||||
|
|
||||||
```pip install ansible```
|
```pip install ansible```
|
||||||
|
|
||||||
Now that ansible is installed we can create a runbok that defines the
|
Now that _Ansible_ is installed we can create a runbok that defines the
|
||||||
steps ansible needs to take on the remote machine to correctly
|
steps _Ansible_ needs to take on the remote machine to correctly
|
||||||
provision it. This playbook is written in yaml so get ready to make
|
provision it. This playbook is written in yaml so get ready to make
|
||||||
sure your indents are correct 😬
|
sure your indents are correct 😬
|
||||||
|
|
||||||
@@ -209,6 +209,155 @@ thing or two - which is fine however I encourage you to copy what I
|
|||||||
have, read the [Docs](https://docs.ansible.com/ansible/latest/user_guide/index.html)
|
have, read the [Docs](https://docs.ansible.com/ansible/latest/user_guide/index.html)
|
||||||
and make any changes you feel are necessary.
|
and make any changes you feel are necessary.
|
||||||
|
|
||||||
```yaml
|
> Check the [git repo](https://gitlab.com/AHMZA/pi-docker-swarm-ansible-deploy/-/tree/main) for the most up to date yaml file im using.
|
||||||
|
|
||||||
|
**Create the deploy.yml**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
---
|
||||||
|
- hosts: piCluster
|
||||||
|
become: yes
|
||||||
|
tasks:
|
||||||
|
- name: Auth local SSH Keys
|
||||||
|
ansible.posix.authorized_key:
|
||||||
|
user: <pi_user>
|
||||||
|
state: present
|
||||||
|
key: "{{ item }}"
|
||||||
|
with_file:
|
||||||
|
- /home/<local_user>/.ssh/id_rsa.pub
|
||||||
|
- ./pi_rsa.pub
|
||||||
|
|
||||||
|
- name: Copy SSH Pi Intercoms Private Key
|
||||||
|
copy:
|
||||||
|
src: ./pi_rsa
|
||||||
|
dest: /home/<pi_user>/.ssh/id_rsa
|
||||||
|
mode: '0600'
|
||||||
|
|
||||||
|
|
||||||
|
- name: Copy SSH Pi Intercoms Public Key
|
||||||
|
copy:
|
||||||
|
src: ./pi_rsa.pub
|
||||||
|
dest: /home/<pi_user>/.ssh/id_rsa.pub
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
|
- name: Check for any updates and install them
|
||||||
|
apt:
|
||||||
|
update_cache: true
|
||||||
|
upgrade: dist
|
||||||
|
|
||||||
|
- name: Install Required build dependencies
|
||||||
|
apt:
|
||||||
|
update_cache: true
|
||||||
|
pkg:
|
||||||
|
- python3
|
||||||
|
- python3-pip
|
||||||
|
- vim
|
||||||
|
- curl
|
||||||
|
|
||||||
|
- name: Check Docker is installed
|
||||||
|
shell: "docker --version"
|
||||||
|
register: docker_version
|
||||||
|
|
||||||
|
- name: check Docker compose is installed
|
||||||
|
shell: "docker-compose --version"
|
||||||
|
register: dc_version
|
||||||
|
|
||||||
|
- name: Download Docker script
|
||||||
|
command: curl -fsSL https://get.docker.com -o get-docker.sh
|
||||||
|
when: docker_version.stdout.find('Docker version')
|
||||||
|
|
||||||
|
- name: Run Docker install script
|
||||||
|
command: sudo sh get-docker.sh
|
||||||
|
when: docker_version.stdout.find('Docker version')
|
||||||
|
|
||||||
|
- name: Install docker compose
|
||||||
|
command: sudo pip3 -v install docker-compose
|
||||||
|
when: dc_version.stdout.find('docker-compose version')
|
||||||
|
|
||||||
|
- name: Enable docker service
|
||||||
|
service:
|
||||||
|
name: "{{ item }}"
|
||||||
|
enabled: true
|
||||||
|
state: started
|
||||||
|
loop:
|
||||||
|
- docker
|
||||||
|
- containerd
|
||||||
|
when: docker_version.stdout.find('Docker version')
|
||||||
|
|
||||||
|
- name: Add user to docker group
|
||||||
|
user:
|
||||||
|
name: <pi_user>
|
||||||
|
groups: docker
|
||||||
|
append: yes
|
||||||
|
when: docker_version.stdout.find('Docker version')
|
||||||
|
|
||||||
|
- name: wait for network on boot
|
||||||
|
command: raspi-config nonint do_boot_wait 0
|
||||||
|
|
||||||
|
- name: Reboot
|
||||||
|
reboot:
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The steps this playbook takes are as follows:
|
||||||
|
1. Copy your SSH keys to the pi authorized_keys
|
||||||
|
2. Copy same Pi ssh key between the Pis for intercommunication
|
||||||
|
3. Check for and Install updates
|
||||||
|
4. Install any build dependancies
|
||||||
|
5. Check if Docker is Installed
|
||||||
|
6. Check if docker-compose is installed
|
||||||
|
* Check this because if they are already installed we can skip these steps and save time
|
||||||
|
7. Download the Docker Script
|
||||||
|
8. Install Docker from script
|
||||||
|
9. Install docker-compose (This will take a while on each pi so be patient, grab a coffee)
|
||||||
|
10. Enable the Docker service
|
||||||
|
11. Add user to Docker group
|
||||||
|
12. Change raspi config to wait for network before boot
|
||||||
|
13. Reboot
|
||||||
|
|
||||||
|
take some time to read through the playbook, read the docs and try to
|
||||||
|
understand whats going on. Change the usernames to your username.
|
||||||
|
|
||||||
|
Make the hosts file in the same directory as the deploy.yml
|
||||||
|
``` yaml
|
||||||
|
[piCluster]
|
||||||
|
10.10.10.20
|
||||||
|
10.10.10.21
|
||||||
|
10.10.10.22
|
||||||
|
```
|
||||||
|
|
||||||
|
* `[piCluster]` - specifies a group of hosts
|
||||||
|
* `10.10.10.xx` - specifies the Pis IP
|
||||||
|
|
||||||
|
heres the command to run the playbook.
|
||||||
|
|
||||||
|
`ansible-playbook -i hosts -kK deploy.yml -c paramiko`
|
||||||
|
|
||||||
|
* `ansible-playbook` - the command to run the playbook
|
||||||
|
* `-i hosts` - specifies the host/inventory file
|
||||||
|
* `-kK` - asks you for password for the SSH connection(-k) and the sudo password(-K)
|
||||||
|
* `deploy.yml` - the _Ansible_ playbook
|
||||||
|
* `-c paramiko` - Use the paramiko connection method, I found this works best with ssh keys
|
||||||
|
|
||||||
|
You can do so much with _Ansible_, for example mount network shares etc,
|
||||||
|
if you can do it in the cli you can do it in _Ansible_ !!
|
||||||
|
|
||||||
|
Now that the ansible script is done we can go ahead and actually
|
||||||
|
configure all the Pis into the cluster.
|
||||||
|
|
||||||
|
### **Creating/Joining the swarm **
|
||||||
|
Creating and joining the cluster is easy - It is a bit manual so this
|
||||||
|
is something you could impliment into the ansible playbook using
|
||||||
|
multiple host groups. Keep an eye out on the git repo as I may
|
||||||
|
implement this myself.
|
||||||
|
|
||||||
|
**Follow the steps below to create the swarm:**
|
||||||
|
1. ssh to the main swarm node, this will be the manager node.
|
||||||
|
2. run `docker swarm init`
|
||||||
|
* This will return a command and token. Make sure to copy this token to your clipboard
|
||||||
|
3. exit the main swarm node
|
||||||
|
4. ssh to one of the worker nodes
|
||||||
|
5. paste and run the copied command
|
||||||
|
6. do the same on the remaining worker nodes
|
||||||
|
|
||||||
|
You should now have a correctly configured cluster !!
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user