Skip to content

Commit

Permalink
Fix MongoDB bind settings
Browse files Browse the repository at this point in the history
When we created the mongo configuration file, we always added loopback
address to the bind addresses, which causes mongo to stop responding
if the other bind address is 0.0.0.0. Note that mongo daemon is still
running and does not abort the start o this error, which is why we
missed this situation in the first place.

Fix for this situation is quite simple: if we bind to 0.0.0.0, no
other IP should be present in net.bindIp configuration.

Change-Id: I46c17603978c9be7e76d07144f2935f3fd74ffb9
  • Loading branch information
Tadej Borovšak committed Jan 29, 2018
1 parent 8b8d94f commit c90d1c0
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cookbooks/mongodb/.kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ suites:
node_id: sample_node_id
node_name: test_name
deployment_id: sample_deploy_id
properties: {}
properties:
bind_ip: global
runtime_properties: {}

- name: replica
Expand All @@ -28,7 +29,6 @@ suites:
node_name: test_replica
deployment_id: sample_deploy_id
properties:
bind_ip: global
type: replica
runtime_properties: {}

Expand Down
8 changes: 6 additions & 2 deletions cookbooks/mongodb/recipes/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
props = node['cloudify']['properties']

# IP address that we bind to
ip = props.fetch('bind_ip', '') == 'global' ? '0.0.0.0' : node['ipaddress']
ips = if props.fetch('bind_ip', '') == 'global'
['0.0.0.0']
else
[node['ipaddress'], '127.0.0.1']
end
node_type = props.fetch 'type', 'standalone'
replica_name = node['cloudify']['node_name']

template '/etc/mongod.conf' do
source "mongod-#{node_type}.conf.erb"
variables ip: ip, replica_name: replica_name
variables ips: ips, replica_name: replica_name
end
8 changes: 6 additions & 2 deletions cookbooks/mongodb/recipes/configure_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
rt_props = node['cloudify']['runtime_properties']

# IP address that we bind to
ip = props.fetch('bind_ip', '') == 'global' ? '0.0.0.0' : node['ipaddress']
ips = if props.fetch('bind_ip', '') == 'global'
['0.0.0.0']
else
[node['ipaddress'], '127.0.0.1']
end
replica_name = rt_props['replica_name']
hosts = rt_props['members'].map { |x| x + ':27017' }.join ','

template '/etc/mongod.conf' do
source 'mongod-router.conf.erb'
variables ip: ip, replica_name: replica_name, hosts: hosts
variables ips: ips, replica_name: replica_name, hosts: hosts
end
2 changes: 1 addition & 1 deletion cookbooks/mongodb/templates/default/mongod-config.conf.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render "mongod-standalone.conf.erb", variables: {ip: @ip} %>
<%= render "mongod-standalone.conf.erb", variables: {ips: @ips} %>

sharding:
clusterRole: configsvr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render "mongod-standalone.conf.erb", variables: {ip: @ip} %>
<%= render "mongod-standalone.conf.erb", variables: {ips: @ips} %>

replication:
replSetName: <%= @replica_name %>
2 changes: 1 addition & 1 deletion cookbooks/mongodb/templates/default/mongod-router.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ systemLog:

net:
port: 27017
bindIp: <%= @ip %>,127.0.0.1
bindIp: <%= @ips.join(',') %>

sharding:
configDB: <%= @replica_name %>/<%= @hosts %>
Expand Down
2 changes: 1 addition & 1 deletion cookbooks/mongodb/templates/default/mongod-shard.conf.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render "mongod-standalone.conf.erb", variables: {ip: @ip} %>
<%= render "mongod-standalone.conf.erb", variables: {ips: @ips} %>

sharding:
clusterRole: shardsvr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ systemLog:

net:
port: 27017
bindIp: <%= @ip %>,127.0.0.1
bindIp: <%= @ips.join(',') %>

security:
authorization: enabled
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe file('/etc/mongod.conf') do
its(:content_as_yaml) do
should include('net' => include('bindIp' => '0.0.0.0,127.0.0.1'))
should include('net' => include('bindIp' => '0.0.0.0'))
should include('replication' => include('replSetName' => 'config_replica'))
should include('sharding' => include('clusterRole' => 'configsvr'))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

describe file('/etc/mongod.conf') do
its(:content_as_yaml) do
should include('net' => include('bindIp' => '0.0.0.0,127.0.0.1'))
should include('replication' => include('replSetName' => 'test_replica'))
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe file('/etc/mongod.conf') do
its(:content_as_yaml) do
should include('net' => include('bindIp' => '0.0.0.0,127.0.0.1'))
should include('net' => include('bindIp' => '0.0.0.0'))
should include('sharding' => include('configDB' => hosts))
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe file('/etc/mongod.conf') do
its(:content_as_yaml) do
should include('net' => include('bindIp' => '0.0.0.0,127.0.0.1'))
should include('net' => include('bindIp' => '0.0.0.0'))
should include('replication' => include('replSetName' => 'shard_replica'))
should include('sharding' => include('clusterRole' => 'shardsvr'))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

describe port(27_017) do
it { should be_listening.with('tcp') }
it { should be_listening.on('127.0.0.1').with('tcp') }
end

describe command("mongo --eval 'db.foo.insert({x:3})'") do
Expand Down

0 comments on commit c90d1c0

Please sign in to comment.