consul dev mode advertise flag not working - vagrant

I was trying to run consul in vagrant using the following comman
consul agent -dev -advertise 172.20.20.31
but it showing following error message
failed to get conn: dial tcp 127.0.0.1:0->172.20.20.31:8300: connect: invalid argument"
as a result when i connect form my local machine to this ip and run consul ui mode it showing
500 (The backend responded with an error)
my vagrant file is as follow:
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.define "centos-consul" do |cs|
cs.vm.hostname = "centos-consul"
cs.vm.network "private_network",ip:"172.20.20.31"
end
end
and connect file is as
{
"ui": true,
"retry_join": ["172.20.20.31"],
"advertise_addr": "172.20.20.01",
"data_dir": "/tmp/consul/self"
}
I am using consul 1.7.2 in mac os and Vagrant 2.2.7 centos7
I am at a lost what i did wrong.
so any suggestion will be helpful.

According to the documentation, the error you are getting is the result of IP address 172.20.20.31 being not routable from your Vagrant box.
-advertise - The advertise address is used to change the address that we advertise to other nodes in the cluster. By default, the -bind
address is advertised. However, in some cases, there may be a routable
address that cannot be bound. This flag enables gossiping a different
address to support this. If this address is not routable, the node
will be in a constant flapping state as other nodes will treat the
non-routability as a failure. In Consul 1.0 and later this can be set
to a go-sockaddr template.

Related

elasticsearch setup on Gcloud VM fails

I wish to run my elasticsearch remotely on gcloud VM, this is configured to run at 127.0.0.1 at a specific port 9200. How to access this from a website outside this vm? If I change the network host to 0.0.0.0 on the yml file, even 9200 port becomes inaccessible. How do I overcome this problem?
Changed network.host: [_site_ , _local_ , _global_ ]
_site_ = internal ip given by google cloud vm,
_local_ = 127.0.0.1,
_global_ = found using curl ifconfig.me,
Opened a specific port (9200) and tried to connect with global IP address.
curl to the global ip gives
>Output: Failed to connect to (_global_ ip) port 9200: connection refused.
So put network.host:0.0.0.0 and then try to allow 9200 and 9201 port and restart the elasticsearch service.If you are using ubuntu then sudo service elasticsearch restart then check by doing curl -XGET 'http://localhost:9200?pretty'.Let me know if you are still facing any issues.
Use following configurations for elasticsearch.yml
network.host: 0.0.0.0
action.auto_create_index: false
index.mapper.dynamic: false
Solved this problem by going through the logs and found out that the public ip address is re-mapped to the internal ip address, hence network.host can't be set to external ip directly. Elasticsearch yml config is as follows:
'network.host: xx.xx.xxx.xx' is set to the internal ip (given by google),
'http.cors.enabled: true',
'http.cors.allow-origin:"*", (Do not use * in production, its a security issue)
'discovery.type: single-node' in my case to make it work independently and not in a cluster
Now this sandboxed version can be accessed from outside the VM using the external IP address given by Google.

Working way to access Consul agent from a Vagrant box?

I have a Win2012R2 virtual machine running in VirtualBox 5 using a Vagrant provisioning.
In the VM I run a single Consul 0.8.0 agent (I'm not interested in clustering at the moment) with the following command line:
consul.exe agent -server -dev -client=0.0.0.0 -bind=0.0.0.0
This launches Consul with the following attributes:
Server: true (bootstrap: false)
Client Addr: 0.0.0.0 (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 10.0.2.15 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
So now I need to access this Consul agent, especially the REST api for putting and getting KV data, from the Host machine (Linux).
In order to do this, in the vagrantfile I configure the following port-forwarding:
config.vm.network "forwarded_port", guest: 8500, host: 8900
config.vm.network "forwarded_port", guest: 80, host: 5000
The port 80 is for exposing a WebApplication API that we deploy together in the VM, and which by the way is supposed to use Consul as configuration engine.
Accessing the application from the linux Host at address http://localhost:5000 works fine. However, if i try to read any Consul key at address http://localhost:8900/v1/kv/mykey, e.g. with curl, the session will be hanged indefinitely.
I guess it's worth remarking that both the webapplication API and the Consul APIs can be accessed from within the VM guest without problems.
I suspect the problem with Consul may lie with the need to expose / forward more ports from the Guest to the Host, like for example the port 8600? However I tried some experiments without success and at the moment I am blocked by this.
Or maybe the reason is the consul.exe command line launch, where I have to add more attributes?
What am I missing? Any idea how to make Consul agent accessible from the external world?
thanks!

How to access Redis running in Vagrant Virtual Machine

We are trying to use Vagrant to run a Redis server on Mac (using an Ubuntu Guest OS) with this Vagrantfile
Sadly we are unable to access the Redis database inside the Vagrant Box.
We get this error:
Error: Redis connection to 127.0.0.1:6379 failed - read ECONNRESET
This is the Network configuration in the VirtualBox VM:
What else do we need to add to the Vagrantfile to expose Redis to Mac?
(Note: the reason for using Vagrant is both to let people try redis without having to install it on their main OS, but more importantly to ensure that other elements of the app run as expected)
This may be helpful https://serverfault.com/questions/248248/cannot-connect-to-redis-installed-on-virtualbox-running-ubuntu-from-windows-7. I know question is about connecting from Windows, but the solution is mostly modifications to the Redis config within the VM in order to not bind Redis only to a local port in the redis.conf so that it can be accessed on the host machine(Mac in your case).
Also, depending on how you are trying to access Redis you may be able to configure a SSH tunnel on the host machine(Mac) in order to gain access to the Redis server within the Vagrant VM. I ended up going this route for my case to connect to Redis inside of a Vagrant VM for local development of an Ember JS app using ember-cli and ember-cli deploy with ember-cli-deploy-redis
You can give a Private IP to the Vagrant box and then access redis using the private IP.
For example, lets say you want to configure 192.168.33.10 as you Vagrant box IP. Simply add this line in Vagrant file.
Vagrant.configure(2) do |config|
config.vm.network 'private_network', ip: '192.168.33.10'
end
From now u can access your vagrant box using 192.168.33.10
I happen to run Redis within a Docker container in addition to the Vagrant setup. Running into this question again and again, I want to a add my findings.
Indeed making sure binding to net devices is to 0.0.0.0 instead of 127.0.0.1 is essential. Since I am running Docker I just had to make sure the container is properly run:
docker run -d redis -p 0.0.0.0:6379:6379 redis
Then, in addition the known Vagrant configuration:
config.vm.network :forwarded_port, guest: 6379, host: 6379
And voilĂ : running Redis in a Vagrant VM using Docker, able to access it from the Mac OSX host system.
This is without changing the Redis config, since Docker takes care of this.

How can I access a vagrant guest from another virtualbox guest?

The scenario is that my dev environment is on a Vagrant box on my laptop (host) and I would like to do browser testing in a vitualbox vm, so I need to see one vm from another.
The vagrant box's port is :8080 which is forwarded to the host on the same port :8080. So I can see the server from the host at localhost:8080
Which address should I be using for the browser testing vm?
The testing vm's default gateway?
The vagrant vm's ip?
The host's virtual network ip?
And should I be using a NAT or host only adapter on the browser testing vm?
That makes for a lot of combinations, all of which I believe I have tried. What else do I need to understand here?
In your use case, you should be using Bridged networking (Public Network in Vagrant). If the VMs reside on the same host, you can even use internal (Private Network in Vagrant).
If using Public Network, the VM's 2nd NIC will be able to obtain an IP address from the DHCP server in your network (e.g. your home router).
Simply add the following code block in your Vagrantfile and do a vagrant reload
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
You should be able to get the IP address by using vagrant ssh and ifconfig / ip addr show.
In case you don't want to go with public_network just like me then you should do the steps below using private_network:
Open Vagrantfile from your project root
Search for config.vm.network
Add this line config.vm.network "private_network", ip: "192.168.33.10". Remember this is not the IP of your base machine it's a virtual-box IP address and your machine IP should be different. You can say it's a fake IP address so change it to anything else like 192.168.30.20.
Reload your vagrant using vagrant reload.
Now go to your other virtual guest in my case it's the Windows Guest 2. My base is Linux Mint Vagrant box is on Ubuntu Guest 1. Open C:\Windows\System32\drivers\etc\hosts file as admin and do the above IP's entry in there like 192.168.33.10 local.youralias.com. And save the file, after that you can now browse the site now at http://local.youralias.com/.
In case your guest 2 is also Linux just edit this file sudo vi /etc/hosts, and add this line at top of it 192.168.33.10 local.youralias.com. Now save and exit and browse the URL :)
Enjoy! Happy coding.
Adding to accepted answer, you can actually set IP and specify which network interface to use.
My setup on linux box via wifi and static IP:
You can find your wifi interface name by running ifconfig command.
Vagrant.configure("2") do |config|
config.vm.network "public_network", :bridge => 'wlp8s0', ip: "192.168.1.199"
end
This may have many source cause. In my case, I use vagrant fedora boxe.
I tried:
First using the private_network that I attached to a host only adapter and launched httpd service to test the connection between guest and host
config.vm.network "private_network", type: "dhcp", name: "vboxnet2"
config.vm.network "forwarded_port", guest:80, host:7070
but I was not able to ping my guest machine from the host and could no telnet the httpd service opened
Second using public_network and launched httpd service to test connectivity
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", use_dhcp_assigned_default_route: true
I could ping my guest from my host but I could not telnet the httpd service.
For this two use case, the issue was that the port 80 on the fedora guest host was blocked by the firewall. Here is what fixed the issue and get all working for both privat_network and public_ntwork:
firewall-cmd --permanent --add-port 80/tcp #open the port permanently
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --list-port # list to check if the port was opened
systemctl stop firewalld # stop and open the firewall service
systemctl start firewalld
Old question, new answer: [disclaimer: i am not a vagrant expert]
both solutions might work but the solution in the "vagrant way of thinking" is that some component in your guest (rinetd?) should forward any requests to unknown ports to the host. From the host the request could then be mapped (via vagrant port forwarding) to a services that is running in the other guest.
So, to resume:
1.in guest-1 we do localhost:1234. Guest-1 will detect that this port is not available and forward to host
2. the host will check the vagrant port forwarding and forward to guest-2
3. in guest-2 we have some nice service listening to post 1234
4. done.

Putty fails to connect to Vagrant

I am trying to connect to the Vagrant server using putty with IP:127.0.0.1 and port:2222. Yesterday i login successfully using username:vagrant and pass:vagrant. Today, unforunately this is what i received is a dialog box:"PuTTy fatal error:network error:Connection refused". Any ideas/suggestions about how to fix that
You may want to try configuring a static IP address for your VM in your Vagrantfile. Through past experience, I've found this to be a more stable way of connecting to your VM via SSH/SCP/SFTP or similar.
To set a static IP address (e.g. in this case 192.168.50.10), you'd put the following in your VagrantFile:
# Create a private network, which allows host-only access to the VM
# using the Static IP 192.168.50.10
config.vm.network :private_network, ip: "192.168.50.10"
This is documented in more detail in the Vagrant Documentation on Private Networks.
Once you've configured a static IP (and rerun 'vagrant up' or 'vagrant reload'), you should be able to connect to your VM using Putty/SSH by using that Static IP (192.168.50.10) and port 22.

Resources