Skip to content

Commit

Permalink
atualizando script ansible
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Ferino committed Mar 30, 2024
1 parent 9cb11ca commit 829ee89
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 4 deletions.
2 changes: 2 additions & 0 deletions COMMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ Dia 29/Março (manhã/tarde)
- Na verdade, estou deixando por ultimo e usando vagrant justamente para economizar.
- Exercitei pouco a parte de monitoramento. Minha ideia é instalar o pacote observability e importar uma dashboard no grafana

Dia 29 (noite)
Finalização da conversão do script shell para script ansible.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Este projeto tem como intuito a implantação automatizada de uma aplicação Flask: API de comentários. Com isso serão praticados os seguintes conhecimentos:


* Automação da infra, provisionamento dos hosts (IaaS)

* Automação de setup e configuração dos hosts (IaC)
Expand All @@ -11,7 +10,7 @@ Este projeto tem como intuito a implantação automatizada de uma aplicação Fl

* Monitoramento dos serviços e métricas da aplicação

Nesse contexto, a implantação segue dois formatos: em infraestrutura local e em infraestrutura na nuvem. A infraestrutura local foi desenvolvida com o intuito de efetuar todas a etapas de testes localmente, economizando uso de serviços na clouds.
A implantação segue dois formatos: em infraestrutura local e em infraestrutura na nuvem. A infraestrutura local foi desenvolvida com o intuito de efetuar todas a etapas de testes localmente, economizando uso de serviços na clouds.

### Conteúdo
- [Infraestrutura Local](#infraestrutura-local)
Expand Down
134 changes: 134 additions & 0 deletions local_infra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,140 @@ Use 'sudo apt autoremove' to remove them.

Para criação da VM e obtenção de acesso, use o comando abaixo:

```bash
$ make

******************** DESTROYING THE VIRTUAL MACHINE **********************************
vagrant destroy -f
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

vagrant status
Current machine states:

default not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.


******************** BUILDING THE VIRTUAL MACHINE **********************************
vagrant provision
==> default: VM not created. Moving on...

vagrant status
Current machine states:

default not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.


******************** RUNNING THE VIRTUAL MACHINE **********************************
vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/focal64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/focal64' version '20240306.0.0' is up to date...
==> default: Setting the name of the VM: local_infra_default_1711728740564_58789
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection reset. Retrying...
default: Warning: Remote connection disconnect. Retrying...
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /home/samuel/Music/desafio-devops/local_infra
==> default: Running provisioner: file...
default: ./k8s => /home/vagrant/k8s
==> default: Running provisioner: file...
default: ./kong => /home/vagrant/kong
==> default: Running provisioner: file...
default: ./ansible => /home/vagrant/ansible
==> default: Running provisioner: ansible...
default: Running ansible-playbook...
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Update package cache] ****************************************************
changed: [localhost]
TASK [Upgrade all packages] ****************************************************
changed: [localhost]
TASK [Install MicroK8s] ********************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
vagrant ssh
Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.4.0-173-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro
System information as of Fri Mar 29 16:16:10 UTC 2024
System load: 1.12 Processes: 158
Usage of /: 5.2% of 38.70GB Users logged in: 0
Memory usage: 10% IPv4 address for enp0s3: 10.0.2.15
Swap usage: 0% IPv4 address for enp0s8: 192.168.56.10
Expanded Security Maintenance for Applications is not enabled.
0 updates can be applied immediately.
Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status
New release '22.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
*** System restart required ***
Last login: Fri Mar 29 16:14:38 2024 from 192.168.56.1
```
Para destruir a VM, basta executar o comando abaixo:
```bash
Expand Down
69 changes: 67 additions & 2 deletions local_infra/ansible/playbooks/setup.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
- hosts: localhost
vars:
- ansible_user_name: vagrant
- ansible_user_group_name: vagrant
- microk8s_version: 1.27

- bash_aliases:
- { alias: "kubectl", command: "microk8s kubectl" }
- { alias: 'helm', command: 'microk8s helm' }
- metallb_ip_range: 192.168.56.12-192.168.56.20


tasks:
- name: Update package cache
apt:
Expand All @@ -13,6 +20,64 @@
upgrade: safe
become: true

- name: Install prerequisites for MicroK8s
apt:
name: "{{ item }}"
state: present
become: true
loop:
- curl
- iptables
- snapd

- name: Install MicroK8s
command: snap install microk8s --classic --channel={{microk8s_version}}
become: true
become: true

- name: Check MicroK8s status
command: microk8s status --wait-ready
become: true

- name: Create .kube directory
file:
path: "{{ ansible_env.HOME }}/.kube"
state: directory
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
become: true

- name: Checking if the group exists
group:
name: "{{ ansible_user_group_name }}"
state: present
become: true

- name: Add Ansible user to microk8s group
user:
name: "{{ ansible_user_name }}"
groups: microk8s
append: true
become: true

- name: Change ownership to Ansible user (and group)
file:
path: "{{ ansible_env.HOME }}/.kube"
state: directory
owner: "{{ ansible_user_name }}"
group: "{{ ansible_user_group_name }}"
become: true

- name: Add kubectl and helm aliases
lineinfile:
dest: "{{ ansible_env.HOME }}/.bash_aliases"
line: 'alias {{ item.alias }}="{{ item.command }}"'
create: yes
become: true
with_items: "{{ bash_aliases }}"

- name: Enable add-ons
command: microk8s enable dns storage rbac

- name: Instal Metallb
command: microk8s enable metallb:{{ metallb_ip_range }}
become: true

0 comments on commit 829ee89

Please sign in to comment.