Hola a todos,
Como sabéis llevo tiempo trabajando en el mundo de las TI y escribiendo algunos artículos sobre tecnología. Echemos un vistazo atrás y busquemos un articulo aleatorio sobre el asunto, por ejemplo, este mismo [intlink id=»2817″ type=»post»]GlusterFS[/intlink]. En ese articulo como podeis apreciar se hace uso de 5 máquinas, en mi caso, en esa época las instale y configure manualmente para poder escribirlo ejecutando los comandos directamente en una infraestructura real.
Bien, desde hace un tiempo, el aprovisionamiento de la infraestructura lo podemos obtener «automagicamente» con diversas herramientas, pero hoy quiero hablar de una en concreto, Vagrant.
Lo que voy a hacer en este articulo es lo siguiente
- Voy a comentar un poco sobre Vagrant, aunque si enrollarme mucho, puesto que tenéis multitud de documentación para poder ahondar en el asunto. Lo importante no es conocer con todo nivel de detalle la herramienta, sino conocer el concepto para después desarrollarlo con paciencia
- Voy a definir un proyecto Vagrant para poder crear toda la infraestrcutra del articulo origen con un solo comando
Bien comenzamos
¿Que es Vagrant?
Es una tecnología que nos permite crear entornos portables, ligeros y lo mas importante, al menos para mi, reproducibles. Dicho de otra forma, definamos la infraestructura del proyecto o en mi caso articulo en el que estoy trabajando, escribamosla en un fichero Vagrant, y ejecutemos el comando para que el entorno se levante adecuadamente, tan sencillo como eso. Ahora bien, el asunto es escribir adecuadamente el fichero de descripción de entorno o Vagrantfile.
¿Que necesitamos a nivel Vagrant para poder comenzar?
- Necesitamos descargar el software de Vagrant e instalarlo, nada mas. No voy a entrar en mas detalles porque es muy sencillo, solo bajar e instalar
- Un detalle importante es que Vagrant emplea la virtualización para poder aprovisionar las máquinas, lo que significa que necesitamos el software correspondiente previamente instalado en el servidor correspondiente o en la máquina en la que estemos lanzando el comando para aprovisionar. Más información en Vagrant
¿Como empezamos con el «proyecto»?
- Creamos un directorio para dicho proyecto, el aprovisionamiento de la estructura necesaria para poder escribir el articulo [intlink id=»2817″ type=»post»]GlusterFS[/intlink]
- Entramos en el directorio seleccionado y lanzamos el comando para establecer dicho directorio
cd GlusterFS vagrant init
eloy@MacBookPro13~/vagrant $ mkdir GlusterFS eloy@MacBookPro13~/vagrant $ cd GlusterFS/ eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant init A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. eloy@MacBookPro13~/vagrant/GlusterFS $
Máquinas
- server1.casamier: IP 192.168.2.100 (servidor)
- server2.casamier: IP 192.168.2.101 (servidor)
- server3.casamier: IP 192.168.2.102 (servidor)
- server4.casamier: IP 192.168.2.103 (servidor)
- client1.casamier: IP 192.168.2.104 (cliente)
- server1.casamier: IP 192.168.0.100 (servidor) / Ubuntu SRV 12.04
- server2.casamier: IP 192.168.0.101 (servidor) / Ubuntu SRV 12.04
- server3.casamier: IP 192.168.0.102 (servidor) / Ubuntu SRV 12.04
- server4.casamier: IP 192.168.0.103 (servidor) / Ubuntu SRV 12.04
- client1.casamier: IP 192.168.0.104 (cliente) / Ubuntu 12.04
- ubuntu/precise64
- ubuntu/precise32
Vagrant.configure(2) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.box = "ubuntu/precise64"
.... Vagrant.configure(2) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.define "server1" do |server1| server1.vm.box = "ubuntu/precise64" end config.vm.define "server2" do |server2| server2.vm.box = "ubuntu/precise64" end config.vm.define "server3" do |server3| server3.vm.box = "ubuntu/precise64" end config.vm.define "server4" do |server4| server4.vm.box = "ubuntu/precise64" end config.vm.define "client1" do |client1| client1.vm.box = "ubuntu/precise64" end end .....
- De un entorno con una sola máquina a uno con múltiples máquinas de alguna forma hay que poder referenciarlas, para eso se usan las variables server1, server2, server3, ….,client1
- Cuando el entorno completo arranque tendremos que referirnos a las máquinas concretas en los comandos, por ejemplo para hacer un ssh contra la máquina server 1 tendremos que hacer vagrant ssh server1, tan sencillo como eso
- De todo el conjunto de máquinas podemos definir una como principal de forma que los comandos anónimos vayan por defecto contra esa
- Cuando levantemos el entorno, se levantarán por defecto todas las máquinas, pero hay formas de controlarlo de forma mas detallada
- En estas condiciones ya tenemos las 5 máquinas funcionando, aunque falta la config concreta. Si ejecutamos un vagrant up en el directorio correspondiente debiéramos ver la siguiente salida
eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant up Bringing machine 'server1' up with 'virtualbox' provider... Bringing machine 'server2' up with 'virtualbox' provider... Bringing machine 'server3' up with 'virtualbox' provider... Bringing machine 'server4' up with 'virtualbox' provider... Bringing machine 'client1' up with 'virtualbox' provider... ==> server1: Box 'ubuntu/precise64' could not be found. Attempting to find and install... server1: Box Provider: virtualbox server1: Box Version: >= 0 ==> server1: Loading metadata for box 'ubuntu/precise64' server1: URL: https://atlas.hashicorp.com/ubuntu/precise64 ==> server1: Adding box 'ubuntu/precise64' (v20160519.1.3) for provider: virtualbox server1: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/precise64/versions/20160519.1.3/providers/virtualbox.box ==> server1: Successfully added box 'ubuntu/precise64' (v20160519.1.3) for 'virtualbox'! ==> server1: Importing base box 'ubuntu/precise64'... ==> server1: Matching MAC address for NAT networking... ==> server1: Checking if box 'ubuntu/precise64' is up to date... ==> server1: Setting the name of the VM: GlusterFS_server1_1464342812043_40078 ==> server1: Clearing any previously set forwarded ports... ==> server1: Fixed port collision for 22 => 2222. Now on port 2200. ==> server1: Clearing any previously set network interfaces... ==> server1: Preparing network interfaces based on configuration... server1: Adapter 1: nat ==> server1: Forwarding ports... server1: 22 (guest) => 2200 (host) (adapter 1) ==> server1: Booting VM... ==> server1: Waiting for machine to boot. This may take a few minutes... server1: SSH address: 127.0.0.1:2200 server1: SSH username: vagrant server1: SSH auth method: private key server1: Warning: Remote connection disconnect. Retrying... server1: Warning: Remote connection disconnect. Retrying... server1: server1: Vagrant insecure key detected. Vagrant will automatically replace server1: this with a newly generated keypair for better security. server1: server1: Inserting generated public key within guest... server1: Removing insecure key from the guest if it's present... server1: Key inserted! Disconnecting and reconnecting using new SSH key... ==> server1: Machine booted and ready! ==> server1: Checking for guest additions in VM... server1: The guest additions on this VM do not match the installed version of server1: VirtualBox! In most cases this is fine, but in rare cases it can server1: prevent things such as shared folders from working properly. If you see server1: shared folder errors, please make sure the guest additions within the server1: virtual machine match the version of VirtualBox you have installed on server1: your host and reload your VM. server1: server1: Guest Additions Version: 4.1.44 server1: VirtualBox Version: 5.0 ==> server1: Mounting shared folders... server1: /vagrant => /Users/eloy/vagrant/GlusterFS ==> server2: Box 'ubuntu/precise64' could not be found. Attempting to find and install... server2: Box Provider: virtualbox server2: Box Version: >= 0 ... server2: /vagrant => /Users/eloy/vagrant/GlusterFS ==> server3: Box 'ubuntu/precise64' could not be found. Attempting to find and install... server3: Box Provider: virtualbox server3: Box Version: >= 0 ... server3: /vagrant => /Users/eloy/vagrant/GlusterFS ==> server4: Box 'ubuntu/precise64' could not be found. Attempting to find and install... server4: Box Provider: virtualbox server4: Box Version: >= 0 ... server4: /vagrant => /Users/eloy/vagrant/GlusterFS ==> client1: Box 'ubuntu/precise64' could not be found. Attempting to find and install... client1: Box Provider: virtualbox client1: Box Version: >= 0 ==> client1: Loading metadata for box 'ubuntu/precise64' client1: URL: https://atlas.hashicorp.com/ubuntu/precise64 ==> client1: Adding box 'ubuntu/precise64' (v20160519.1.3) for provider: virtualbox ==> client1: Importing base box 'ubuntu/precise64'... ==> client1: Matching MAC address for NAT networking... ==> client1: Checking if box 'ubuntu/precise64' is up to date... ==> client1: Setting the name of the VM: GlusterFS_client1_1464342992167_1902 ==> client1: Clearing any previously set forwarded ports... ==> client1: Fixed port collision for 22 => 2222. Now on port 2204. ==> client1: Clearing any previously set network interfaces... ==> client1: Preparing network interfaces based on configuration... client1: Adapter 1: nat ==> client1: Forwarding ports... client1: 22 (guest) => 2204 (host) (adapter 1) ==> client1: Booting VM... ==> client1: Waiting for machine to boot. This may take a few minutes... client1: SSH address: 127.0.0.1:2204 client1: SSH username: vagrant client1: SSH auth method: private key client1: Warning: Remote connection disconnect. Retrying... client1: Warning: Remote connection disconnect. Retrying... client1: client1: Vagrant insecure key detected. Vagrant will automatically replace client1: this with a newly generated keypair for better security. client1: client1: Inserting generated public key within guest... client1: Removing insecure key from the guest if it's present... client1: Key inserted! Disconnecting and reconnecting using new SSH key... ==> client1: Machine booted and ready! ==> client1: Checking for guest additions in VM... client1: The guest additions on this VM do not match the installed version of client1: VirtualBox! In most cases this is fine, but in rare cases it can client1: prevent things such as shared folders from working properly. If you see client1: shared folder errors, please make sure the guest additions within the client1: virtual machine match the version of VirtualBox you have installed on client1: your host and reload your VM. client1: client1: Guest Additions Version: 4.1.44 client1: VirtualBox Version: 5.0 ==> client1: Mounting shared folders... client1: /vagrant => /Users/eloy/vagrant/GlusterFS eloy@MacBookPro13~/vagrant/GlusterFS $
eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant halt ==> client1: Attempting graceful shutdown of VM... ==> server4: Attempting graceful shutdown of VM... ==> server3: Attempting graceful shutdown of VM... ==> server2: Attempting graceful shutdown of VM... ==> server1: Attempting graceful shutdown of VM... eloy@MacBookPro13~/vagrant/GlusterFS $
Red
- Comenzamos con el server1 y tenemos que darle la ip 192.168.0.100,respetando el rango de las redes privadas. Para ello modificamos Vagrantfile y ajustamos la configuración correspondiente a server1. Esa configuración queda de la siguiente forma:
config.vm.define "server1" do |server1| server1.vm.box = "ubuntu/precise64" server1.vm.network "private_network", ip: "192.168.0.100" end
vagrant up server1
eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant up server1 Bringing machine 'server1' up with 'virtualbox' provider... ==> server1: Checking if box 'ubuntu/precise64' is up to date... ==> server1: Clearing any previously set forwarded ports... ==> server1: Clearing any previously set network interfaces... ==> server1: Preparing network interfaces based on configuration... server1: Adapter 1: nat server1: Adapter 2: hostonly ==> server1: Forwarding ports... server1: 22 (guest) => 2222 (host) (adapter 1) ==> server1: Booting VM... ==> server1: Waiting for machine to boot. This may take a few minutes... server1: SSH address: 127.0.0.1:2222 server1: SSH username: vagrant server1: SSH auth method: private key server1: Warning: Remote connection disconnect. Retrying... server1: Warning: Remote connection disconnect. Retrying... ==> server1: Machine booted and ready! ==> server1: Checking for guest additions in VM... server1: The guest additions on this VM do not match the installed version of server1: VirtualBox! In most cases this is fine, but in rare cases it can server1: prevent things such as shared folders from working properly. If you see server1: shared folder errors, please make sure the guest additions within the server1: virtual machine match the version of VirtualBox you have installed on server1: your host and reload your VM. server1: server1: Guest Additions Version: 4.1.44 server1: VirtualBox Version: 5.0 ==> server1: Configuring and enabling network interfaces... ==> server1: Mounting shared folders... server1: /vagrant => /Users/eloy/vagrant/GlusterFS ==> server1: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> server1: flag to force provisioning. Provisioners marked to run always will still run. eloy@MacBookPro13~/vagrant/GlusterFS $
eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant ssh server1 Welcome to Ubuntu 12.04.5 LTS (GNU/Linux 3.2.0-102-virtual x86_64) ... vagrant@vagrant-ubuntu-precise-64:~$ ifconfig ... eth1 Link encap:Ethernet HWaddr 08:00:27:97:de:cc inet addr:192.168.0.100 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe97:decc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:468 (468.0 B) ... vagrant@vagrant-ubuntu-precise-64:~$
config.vm.define "server1" do |server1| server1.vm.box = "ubuntu/precise64" server1.vm.network "private_network", ip: "192.168.0.100" end config.vm.define "server2" do |server2| server2.vm.box = "ubuntu/precise64" server2.vm.network "private_network", ip: "192.168.0.101" end config.vm.define "server3" do |server3| server3.vm.box = "ubuntu/precise64" server3.vm.network "private_network", ip: "192.168.0.102" end config.vm.define "server4" do |server4| server4.vm.box = "ubuntu/precise64" server4.vm.network "private_network", ip: "192.168.0.103" end config.vm.define "client1" do |client1| client1.vm.box = "ubuntu/precise64" client1.vm.network "private_network", ip: "192.168.0.104" end
Software adicional
En el artículo original se indica que hay que instalar el software de glusterFS para poder configurar tanto los servidores como el cliente. Este paso también lo podemos abordar con vagrant, o incluso la propia configuración del mismo, pero en este articulo no voy a llegar a eso. En mi caso para la escritura del articulo solo lo hago 1 vez y no me merece la pena aunque desde el punto actual el esfuerzo es relativamente poco importante.
eloy@MacBookPro13~/vagrant/GlusterFS $ ls -lh total 24 -rw-r--r-- 1 eloy staff 4,0K 27 may 12:40 Vagrantfile -rw-r--r-- 1 eloy staff 154B 27 may 12:41 glusterFS_client.sh -rw-r--r-- 1 eloy staff 154B 27 may 12:41 glusterFS_server.sh
eloy@MacBookPro13~/vagrant/GlusterFS $ cat glusterFS_server.sh #!/usr/bin/env bash apt-get update apt-get install -y glusterfs-server
eloy@MacBookPro13~/vagrant/GlusterFS $ cat glusterFS_client.sh #!/usr/bin/env bash apt-get update apt-get install -y glusterfs-client
Vagrant.configure(2) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.define "server1" do |server1| server1.vm.box = "ubuntu/precise64" server1.vm.network "private_network", ip: "192.168.0.100" server1.vm.provision "shell", path: "glusterFS_server.sh" end config.vm.define "server2" do |server2| server2.vm.box = "ubuntu/precise64" server2.vm.network "private_network", ip: "192.168.0.101" server2.vm.provision "shell", path: "glusterFS_server.sh" end config.vm.define "server3" do |server3| server3.vm.box = "ubuntu/precise64" server3.vm.network "private_network", ip: "192.168.0.102" server3.vm.provision "shell", path: "glusterFS_server.sh" end config.vm.define "server4" do |server4| server4.vm.box = "ubuntu/precise64" server4.vm.network "private_network", ip: "192.168.0.103" server4.vm.provision "shell", path: "glusterFS_server.sh" end config.vm.define "client1" do |client1| client1.vm.box = "ubuntu/precise64" client1.vm.network "private_network", ip: "192.168.0.104" client1.vm.provision "shell", path: "glusterFS_client.sh" end ....
eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant up server1 Bringing machine 'server1' up with 'virtualbox' provider... ==> server1: Checking if box 'ubuntu/precise64' is up to date... ==> server1: VirtualBox VM is already running. eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant halt server1 ==> server1: Attempting graceful shutdown of VM... eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant up server1 Bringing machine 'server1' up with 'virtualbox' provider... ==> server1: Checking if box 'ubuntu/precise64' is up to date... ... server1: 22 (guest) => 2222 (host) (adapter 1) ==> server1: Booting VM... ==> server1: Waiting for machine to boot. This may take a few minutes... server1: SSH address: 127.0.0.1:2222 server1: SSH username: vagrant server1: SSH auth method: private key .... server1: /vagrant => /Users/eloy/vagrant/GlusterFS ==> server1: Machine already provisioned. Run `vagrant provision` or use the `--provision` ....
eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant provision server1 ==> server1: Running provisioner: shell... server1: Running: /var/folders/n3/3wywf5695blc96fwcxbd451h0000gn/T/vagrant-shell20160527-4583-49hlar.sh ==> server1: stdin: is not a tty ==> server1: Get:1 http://security.ubuntu.com precise-security Release.gpg [198 B] ... ==> server1: Hit http://archive.ubuntu.com precise-backports/universe Translation-en ==> server1: Fetched 11.3 MB in 8s (1,393 kB/s) ==> server1: Reading package lists... ==> server1: Reading package lists... ==> server1: Building dependency tree... ==> server1: Reading state information... ==> server1: The following extra packages will be installed: ==> server1: fuse-utils glusterfs-client glusterfs-common libibverbs1 ==> server1: Suggested packages: ==> server1: glusterfs-examples ==> server1: The following NEW packages will be installed: ==> server1: fuse-utils glusterfs-client glusterfs-common glusterfs-server libibverbs1 ==> server1: 0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded. ==> server1: Need to get 9,420 kB of archives. ... ==> server1: Setting up glusterfs-common (3.2.5-1ubuntu1) ... ==> server1: Setting up glusterfs-client (3.2.5-1ubuntu1) ... ==> server1: Setting up glusterfs-server (3.2.5-1ubuntu1) ... ==> server1: glusterfs-server start/running, process 2048 ==> server1: Processing triggers for libc-bin ... ==> server1: ldconfig deferred processing now taking place eloy@MacBookPro13~/vagrant/GlusterFS $
eloy@MacBookPro13~/vagrant/GlusterFS $ vagrant status Current machine states: server1 running (virtualbox) server2 running (virtualbox) server3 running (virtualbox) server4 running (virtualbox) client1 running (virtualbox) This environment represents multiple VMs. The VMs are all listed above with their current state. For more information about a specific VM, run `vagrant status NAME`.
vagrant up --provision
vagrant up ... vagrant provision
Muchas gracias a todos y un saludo.
You must be logged in to post a comment.