Skip to content

Commit

Permalink
iniciando integração do ansible com vagrant
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Ferino committed Mar 29, 2024
1 parent 1afdee4 commit a3a36f6
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 60 deletions.
36 changes: 36 additions & 0 deletions local_infra/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
all: run
.PHONY: all build run clean

requirements:
if [ -x /usr/bin/apt-get ]; then xargs -a requirements.txt sudo apt-get install -y; fi

build: clean
@echo
@echo "******************** BUILDING THE VIRTUAL MACHINE **********************************"
vagrant provision
@echo
vagrant status
@echo


run: build
@echo
@echo "******************** RUNNING THE VIRTUAL MACHINE **********************************"
vagrant up
@echo
vagrant status
@echo
vagrant ssh
@echo


clean:
@echo
@echo "******************** DESTROYING THE VIRTUAL MACHINE **********************************"
vagrant destroy -f
@echo
vagrant status
@echo



5 changes: 4 additions & 1 deletion local_infra/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Ambiente local

Este diretório possui `Linux Ubuntu 20.04 LTS`, onde configura um `MicroK8s`.
Este diretório possui `Linux Ubuntu 20.04 LTS`, onde configura um `MicroK8s`. A imagem abaixo mostra a arquitetura proposta, onde é instalado o Kong e a aplicação Flask de exemplo.

![cloud infrastructure design](../assets/local-environment_version_3.png)


## Requisitos

Expand Down
68 changes: 9 additions & 59 deletions local_infra/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,71 +28,21 @@ Vagrant.configure("2") do |config|
# your network.
# config.vm.network "public_network"


config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "4096"
vb.cpus = "2"
end

# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
echo ""
echo "************** Running Ansible script"
ansible-playbook -i /home/vagrant/ansible/inventory/localhost/hosts.yaml /home/vagrant/ansible/playbooks/main.yaml
echo ""
echo "************** Updating Linux Packages"
apt-get update
apt-get upgrade -y
echo ""
echo "************** Installing MicroK8s (version 1.27)"
snap install microk8s --classic --channel=1.27
microk8s status --wait-ready
mkdir -p ~/.kube
usermod -a -G microk8s vagrant
chown -f -R vagrant ~/.kube
alias kubectl='microk8s kubectl'
alias helm='microk8s helm'
# echo ""
# echo "************** Deploying Flask App into the MicroK8s"
# kubectl create namespace app
# kubectl apply -f /home/vagrant/k8s/flask-app-k8s.yaml --namespace app
# kubectl wait --for=condition=available deployment/flask-app-deployment --namespace app --timeout=300s
# kubectl wait --for=condition=Ready pod -l app=flask-app -n app --timeout=300s
# kubectl get service --namespace app
# kubectl get deployment --namespace app
echo ""
echo "************** Installing MicroK8s addons/plugins"
microk8s enable dns storage rbac
# microk8s enable metallb
# microk8s enable metrics-server observability dashboard
# echo ""
# echo "************** Installing Kong Ingress Controller (KIC)"
# kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/standard-install.yaml
# kubectl apply -f /home/vagrant/kong/gateway-api-crd.yaml
# helm repo add kong https://charts.konghq.com
# helm repo update
# helm install kong kong/ingress -n kong --create-namespace
# helm dependency build /home/vagrant/kong/helm
# helm install kong /home/vagrant/kong/helm --namespace kong --create-namespace
# export NODE_PORT="$(microk8s kubectl get services/flask-app-service --namespace app -o go-template='{{(index .spec.ports 0).nodePort}}')"
# echo "NODE_PORT=${NODE_PORT}"
# curl -sv localhost:${NODE_PORT}/api/comment/list/1
# microk8s kubectl --namespace kube-system patch svc kubernetes-dashboard -p '{"spec": {"type": "NodePort", "ports": [{"nodePort": 32000, "port": 443, "protocol": "TCP", "targetPort": 8443}]}}'
# microk8s kubectl --namespace observability patch svc kube-prom-stack-kube-prome-prometheus -p '{"spec": {"type": "NodePort", "ports": [{"nodePort": 32001, "port": 9090, "protocol": "TCP", "targetPort": 9090}]}}'
# microk8s kubectl --namespace observability patch svc kube-prom-stack-grafana -p '{"spec": {"type": "NodePort", "ports": [{"nodePort": 32002, "port": 80, "protocol": "TCP", "targetPort": 3000}]}}'
SHELL
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/playbooks/setup.yaml"
ansible.inventory_path = "ansible/inventory/localhost/hosts.yaml"
ansible.verbose = "v"
# ansible.user = "vagrant"
# ansible.private_key_path = "/path/to/your/private/key"
# ansible.ssh_extra_args = "-o ForwardAgent=yes"
end
end
95 changes: 95 additions & 0 deletions local_infra/Vagrantfile.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :


Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"

config.vm.provision "file", source: "./k8s", destination: "/home/vagrant/k8s"
config.vm.provision "file", source: "./kong", destination: "/home/vagrant/kong"
config.vm.provision "file", source: "./ansible", destination: "/home/vagrant/ansible"

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
config.vm.network "forwarded_port", guest: 8001, host: 8001, host_ip: "127.0.0.1"

# Create a private network, which allows host-only access to the machine using a specific IP.
config.vm.network "private_network", ip: "192.168.56.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"

config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "4096"
vb.cpus = "2"
end


# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
echo ""
echo "************** Updating Linux Packages"
apt-get update
apt-get upgrade -y
echo ""
echo "************** Installing MicroK8s (version 1.27)"
snap install microk8s --classic --channel=1.27
microk8s status --wait-ready
mkdir -p ~/.kube
usermod -a -G microk8s vagrant
chown -f -R vagrant ~/.kube
alias kubectl='microk8s kubectl'
alias helm='microk8s helm'
echo ""
echo "************** Deploying Flask App into the MicroK8s"
kubectl create namespace app
kubectl apply -f /home/vagrant/k8s/flask-app-k8s.yaml --namespace app
kubectl wait --for=condition=available deployment/flask-app-deployment --namespace app --timeout=300s
kubectl wait --for=condition=Ready pod -l app=flask-app -n app --timeout=300s
kubectl get service --namespace app
kubectl get deployment --namespace app
echo ""
echo "************** Installing MicroK8s addons/plugins"
microk8s enable dns storage rbac
# microk8s enable metallb
microk8s enable metrics-server observability dashboard
# echo ""
# echo "************** Installing Kong Ingress Controller (KIC)"
# kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/standard-install.yaml
# kubectl apply -f /home/vagrant/kong/gateway-api-crd.yaml
# helm repo add kong https://charts.konghq.com
# helm repo update
# helm install kong kong/ingress -n kong --create-namespace
# helm dependency build /home/vagrant/kong/helm
# helm install kong /home/vagrant/kong/helm --namespace kong --create-namespace
export NODE_PORT="$(microk8s kubectl get services/flask-app-service --namespace app -o go-template='{{(index .spec.ports 0).nodePort}}')"
echo "NODE_PORT=${NODE_PORT}"
curl -sv localhost:${NODE_PORT}/api/comment/list/1
# microk8s kubectl --namespace kube-system patch svc kubernetes-dashboard -p '{"spec": {"type": "NodePort", "ports": [{"nodePort": 32000, "port": 443, "protocol": "TCP", "targetPort": 8443}]}}'
# microk8s kubectl --namespace observability patch svc kube-prom-stack-kube-prome-prometheus -p '{"spec": {"type": "NodePort", "ports": [{"nodePort": 32001, "port": 9090, "protocol": "TCP", "targetPort": 9090}]}}'
# microk8s kubectl --namespace observability patch svc kube-prom-stack-grafana -p '{"spec": {"type": "NodePort", "ports": [{"nodePort": 32002, "port": 80, "protocol": "TCP", "targetPort": 3000}]}}'
SHELL
end
3 changes: 3 additions & 0 deletions local_infra/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
virtualbox
vagrant
ansible

0 comments on commit a3a36f6

Please sign in to comment.