added more sections to the post
This commit is contained in:
@@ -211,6 +211,10 @@ and make any changes you feel are necessary.
|
|||||||
|
|
||||||
> 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.
|
> 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.
|
||||||
|
|
||||||
|
**This script assumes you are using SSH keys, which you should but if
|
||||||
|
not you can skip the whole "Configure SSH" block,**
|
||||||
|
***I RECCOMEND YOU USE SSH KEYS*** - [read this](https://blog.ahmza.com/posts/securing-ssh/)
|
||||||
|
|
||||||
**Create the deploy.yml**
|
**Create the deploy.yml**
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@@ -218,27 +222,35 @@ and make any changes you feel are necessary.
|
|||||||
- hosts: piCluster
|
- hosts: piCluster
|
||||||
become: yes
|
become: yes
|
||||||
tasks:
|
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
|
- name: Configure SSH
|
||||||
copy:
|
block:
|
||||||
src: ./pi_rsa
|
|
||||||
dest: /home/<pi_user>/.ssh/id_rsa
|
|
||||||
mode: '0600'
|
|
||||||
|
|
||||||
|
- 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'
|
||||||
|
owner: <pi_user>
|
||||||
|
group: <pi_user>
|
||||||
|
|
||||||
|
- name: Copy SSH Pi Intercoms Public Key
|
||||||
|
copy:
|
||||||
|
src: ./pi_rsa.pub
|
||||||
|
dest: /home/<pi_user>/.ssh/id_rsa.pub
|
||||||
|
mode: '0644'
|
||||||
|
owner: <pi_user>
|
||||||
|
group: <pi_user>
|
||||||
|
|
||||||
- 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
|
- name: Check for any updates and install them
|
||||||
apt:
|
apt:
|
||||||
@@ -254,42 +266,46 @@ and make any changes you feel are necessary.
|
|||||||
- vim
|
- vim
|
||||||
- curl
|
- curl
|
||||||
|
|
||||||
|
|
||||||
- name: Check Docker is installed
|
- name: Check Docker is installed
|
||||||
shell: "docker --version"
|
shell: "docker --version"
|
||||||
register: docker_version
|
register: docker_version
|
||||||
|
|
||||||
|
- name: Install Docker
|
||||||
|
block:
|
||||||
|
|
||||||
|
- name: Download Docker script
|
||||||
|
command: curl -fsSL https://get.docker.com -o get-docker.sh
|
||||||
|
|
||||||
|
- name: Run Docker install script
|
||||||
|
command: sudo sh get-docker.sh
|
||||||
|
|
||||||
|
- name: Enable docker service
|
||||||
|
service:
|
||||||
|
name: "{{ item }}"
|
||||||
|
enabled: true
|
||||||
|
state: started
|
||||||
|
loop:
|
||||||
|
- docker
|
||||||
|
- containerd
|
||||||
|
|
||||||
|
- name: Add user to docker group
|
||||||
|
user:
|
||||||
|
name: <pi_user>
|
||||||
|
groups: docker
|
||||||
|
append: yes
|
||||||
|
|
||||||
|
when: docker_version.stdout.find('Docker version')
|
||||||
|
|
||||||
|
|
||||||
- name: check Docker compose is installed
|
- name: check Docker compose is installed
|
||||||
shell: "docker-compose --version"
|
shell: "docker-compose --version"
|
||||||
register: dc_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
|
- name: Install docker compose
|
||||||
command: sudo pip3 -v install docker-compose
|
command: sudo pip3 -v install docker-compose
|
||||||
when: dc_version.stdout.find('docker-compose version')
|
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
|
- name: wait for network on boot
|
||||||
command: raspi-config nonint do_boot_wait 0
|
command: raspi-config nonint do_boot_wait 0
|
||||||
@@ -304,31 +320,36 @@ The steps this playbook takes are as follows:
|
|||||||
3. Check for and Install updates
|
3. Check for and Install updates
|
||||||
4. Install any build dependancies
|
4. Install any build dependancies
|
||||||
5. Check if Docker is Installed
|
5. Check if Docker is Installed
|
||||||
|
* Download the Docker Script
|
||||||
|
* Install Docker from script
|
||||||
|
* Enable the Docker service
|
||||||
|
* Add user to Docker group
|
||||||
6. Check if docker-compose 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
|
* Install docker-compose (This will take a while on each pi so be patient, grab a coffee)
|
||||||
7. Download the Docker Script
|
9. Change raspi config to wait for network before boot
|
||||||
8. Install Docker from script
|
10. Reboot
|
||||||
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
|
take some time to read through the playbook, read the docs and try to
|
||||||
understand whats going on. Change the usernames to your username.
|
understand whats going on. Change the usernames to your username.
|
||||||
|
|
||||||
Make the hosts file in the same directory as the deploy.yml
|
**Make the hosts file in the same directory as the deploy.yml**
|
||||||
``` yaml
|
``` yaml
|
||||||
[piCluster]
|
[piCluster]
|
||||||
10.10.10.20
|
10.10.10.20
|
||||||
10.10.10.21
|
10.10.10.21
|
||||||
10.10.10.22
|
10.10.10.22
|
||||||
```
|
```
|
||||||
|
|
||||||
* `[piCluster]` - specifies a group of hosts
|
* `[piCluster]` - specifies a group of hosts
|
||||||
* `10.10.10.xx` - specifies the Pis IP
|
* `10.10.10.xx` - specifies the Pis IP
|
||||||
|
|
||||||
heres the command to run the playbook.
|
**create the Pi Intercommunication keys**
|
||||||
|
``` bash
|
||||||
|
ssh-keygen
|
||||||
|
Enter file in which to save the key (/home/ahmza/.ssh/id_rsa): .pi_rsa
|
||||||
|
```
|
||||||
|
* save the keys in the same directory the deploy.yml file is
|
||||||
|
|
||||||
|
**heres the command to run the playbook.**
|
||||||
|
|
||||||
`ansible-playbook -i hosts -kK deploy.yml -c paramiko`
|
`ansible-playbook -i hosts -kK deploy.yml -c paramiko`
|
||||||
|
|
||||||
@@ -341,23 +362,86 @@ heres the command to run the playbook.
|
|||||||
You can do so much with _Ansible_, for example mount network shares etc,
|
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_ !!
|
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
|
Now that the _Ansible_ script is done we can go ahead and actually
|
||||||
configure all the Pis into the cluster.
|
configure all the Pis into the cluster.
|
||||||
|
|
||||||
### **Creating/Joining the swarm **
|
### **Creating/Joining the swarm**
|
||||||
Creating and joining the cluster is easy - It is a bit manual so this
|
Creating and joining the cluster is easy - It is manual and tedious so
|
||||||
is something you could impliment into the ansible playbook using
|
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
|
multiple host groups.
|
||||||
implement this myself.
|
|
||||||
|
|
||||||
**Follow the steps below to create the swarm:**
|
**Follow these steps to create the swarm:**
|
||||||
1. ssh to the main swarm node, this will be the manager node.
|
1. ssh to the main Pi, this will be the manager node.
|
||||||
2. run `docker swarm init`
|
2. run `docker swarm init`
|
||||||
* This will return a command and token. Make sure to copy this token to your clipboard
|
* This will return a command and token. Make sure to copy them
|
||||||
3. exit the main swarm node
|
3. exit the main Pi
|
||||||
4. ssh to one of the worker nodes
|
4. ssh to one of the worker nodes
|
||||||
5. paste and run the copied command
|
5. paste and run the copied command
|
||||||
6. do the same on the remaining worker nodes
|
6. do the same on the remaining worker nodes
|
||||||
|
|
||||||
You should now have a correctly configured cluster !!
|
You should now have a correctly configured cluster !! From here there
|
||||||
|
are multiple ways to go about managing your cluster. I'll demonstrate
|
||||||
|
one here but will link others too for further reading.
|
||||||
|
|
||||||
|
### **Portainer**
|
||||||
|
Portainer is a webgui that will allow us to manage the cluster, it will
|
||||||
|
make deploying and destroying containers across the cluster a breeze.
|
||||||
|
I do understand that all of what portainer does can simply be accomplished
|
||||||
|
in the command line, however portainer just makes it less hastle to manage.
|
||||||
|
|
||||||
|
**Install Portainer**
|
||||||
|
|
||||||
|
Firstly we want to download the yaml file that will be used to deploy
|
||||||
|
the portainer contiainer on our cluster.
|
||||||
|
|
||||||
|
`curl -L https://downloads.portainer.io/portainer-agent-stack.yml -o portainer-agent-stack.yml`
|
||||||
|
|
||||||
|
Now we can actually deploy portainer to the cluster using the following
|
||||||
|
command:
|
||||||
|
|
||||||
|
`docker stack deploy -c portainer-agent-stack.yml portainer`
|
||||||
|
|
||||||
|
Once the stack is up and running navigate to:
|
||||||
|
|
||||||
|
`https://raspberry-ip:9443`
|
||||||
|
|
||||||
|
where raspberry-ip is the IP address of the manager node in the swarm.
|
||||||
|
|
||||||
|
### **Optional last step**
|
||||||
|
|
||||||
|
If you intend to build any of your own docker images then it may be useful
|
||||||
|
to create a private repository to host any images that you need to run
|
||||||
|
across the cluster.
|
||||||
|
|
||||||
|
to create this private repository create a file called docker-compose.yml
|
||||||
|
in a folder called repository or whatever and enter the following.
|
||||||
|
``` yaml
|
||||||
|
|
||||||
|
version: '3.9'
|
||||||
|
services:
|
||||||
|
registry:
|
||||||
|
image: registry:2
|
||||||
|
ports:
|
||||||
|
- 5000:5000
|
||||||
|
volumes:
|
||||||
|
- /home/<pi-user>/docker/registry/data:/var/lib/registry
|
||||||
|
```
|
||||||
|
|
||||||
|
**Be sure to change pi-user to your pi user**
|
||||||
|
|
||||||
|
**create a file /etc/docker/daemon.json and eter the following**
|
||||||
|
``` json
|
||||||
|
{
|
||||||
|
"insecure-registries": ["localhost:5000", "<pi-address>:5000"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
where `pi-address` is the ip address of the main swarm node.
|
||||||
|
|
||||||
|
**restart the docker service with `sudo systemctl restart docker`**
|
||||||
|
|
||||||
|
now we need to deploy the registry to the stack like we did with portainer
|
||||||
|
|
||||||
|
`docker stack deploy -c docker-compose.yml registry`
|
||||||
|
|
||||||
|
and we should have a private registry setup that we can use to push our
|
||||||
|
custom docker images to.
|
||||||
|
|||||||
Reference in New Issue
Block a user