Search code examples
pythonazureredisredis-py

Unable to connect to Redis instance running on Azure Linux VM from Python


I created an Ubuntu VM on Azure. In its inbound networking filters, I added ports 22 (for SSHing) and 6379 (for Redis). Following this, I SSHed into an instance from my bash shell, downloaded, built and installed Redis from source. The resultant redis.conf file is located in /tmp/redis-stable, so I edited that to comment out the bind 127.0.0.1 rule.

Then I started redis-server redis.conf from the /tmp/redis-stable directory, and it started normally, following which I SSHed into another instance of the VM, started redis-cli and set some keys. Retrieved them, working correctly.

Now in Python I am running this command:

r = redis.Redis(host='same_which_I_use_for_SSHing', port=6379, password='pwd')

It connects immediately (looks weird). But then when I try a simple command like r.get("foo"), I get this error:

>>> r.get("foo")
Traceback (most recent call last):
  File "/lib/python3.5/site-packages/redis/connection.py", line 484, in connect
    sock = self._connect()
  File "/lib/python3.5/site-packages/redis/connection.py", line 511, in _connect
    socket.SOCK_STREAM):
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/lib/python3.5/site-packages/redis/client.py", line 667, in execute_command
    connection.send_command(*args)
  File "/lib/python3.5/site-packages/redis/connection.py", line 610, in send_command
    self.send_packed_command(self.pack_command(*args))
  File "/lib/python3.5/site-packages/redis/connection.py", line 585, in send_packed_command
    self.connect()
  File "/lib/python3.5/site-packages/redis/connection.py", line 489, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 8 connecting to username@ip_address. nodename nor servname provided, or not known.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/lib/python3.5/site-packages/redis/connection.py", line 484, in connect
    sock = self._connect()
  File "/lib/python3.5/site-packages/redis/connection.py", line 511, in _connect
    socket.SOCK_STREAM):
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/python3.5/site-packages/redis/client.py", line 976, in get
    return self.execute_command('GET', name)
  File "/lib/python3.5/site-packages/redis/client.py", line 673, in execute_command
    connection.send_command(*args)
  File "/lib/python3.5/site-packages/redis/connection.py", line 610, in send_command
    self.send_packed_command(self.pack_command(*args))
  File "/lib/python3.5/site-packages/redis/connection.py", line 585, in send_packed_command
    self.connect()
  File "/lib/python3.5/site-packages/redis/connection.py", line 489, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 8 connecting to username@ip_address. nodename nor servname provided, or not known.

Any idea how to fix this? FYI, the username@ip_address in the error message is the same I use for SSH from bash and connecting to Redis from Python respectively:

ssh username@ip_address
r = redis.Redis(host='username@ip_address', port=6379, password='pwd')

I also tried adding bind 0.0.0.0 in the redis.conf file after commenting out the bind 127.0.0.1 line. Same result.

Update: I tried setting protected mode to no in the config file, followed by running sudo ufw allow 6379 in the VM. Still same result. But now I get a weird error when I run redis-server redis.conf. I don't get the typical redis cube which shows up as a figure Instead I get this:

enter image description here

After this, if I enter redis-cli and issue a simple command like set foo boo, I get this error message:

127.0.0.1:6379> set foo 1
(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

Even shutdown fails after this. I have to run grep to find the process if of redis-server and kill it manually, following which redis-server command needs to be run to start it normally. But of course, I still cannot connect from remote Mac.


Solution

  • I tried to create an Ubuntu VM on Azure, build & configure redis server from source code, and add a port rule for my VM NSG on Azure portal, then I connected the redis server via the same code with python redis package successfully.

    Here is my steps as below.

    1. Create an Ubuntu 18.04 VM on Azure portal.
    2. Connect the Ubuntu VM via SSH to install the build packages (include build-essential, gcc and make), download & decompress the tar.gz file of redis source code from redis.io, and build & test the redis server with make & make test.
    3. Configure the redis.conf via vim, to change the bind configuration from 127.0.0.1 to 0.0.0.0.
    4. Add a new port rule of 6379 port with TCP into the NSG for the Network Interface shown in the tab Settings -> Networking of my VM on Azure portal, as the figures below.

    enter image description here Notes: There are two NSG in my Networking tab, the second one is related to my Network Interface which can be accessed via internet. enter image description here and enter image description here 5. Install redis via pip install redis on my local machine. 6. Open a termial to type python to try to connect the redis server hosted on my Azure Ubuntu VM and get the value of the foo key successfully.

    >>> import redis
    >>> r = redis.Redis(host='xxx.xx.xx.xxx')
    >>> r.get('foo')
    b'bar'
    

    There are two key issues I found within my testing.

    1. For redis.conf, the binding host must be 0.0.0.0. If not, redis server will be running in protected mode to refuse the query.
    2. For the NSG port rules, make sure the new port rule added in the NSG attached to the current network interface, not the default subnet.
    3. The python package redis is lazy evaluation, only connect redis server when first command request happened.

    Hope it helps.