Set up Redis on GCP Virtual Machine

Why on VM instead of using GCP Memorystore?

Kuroun Seung
2 min readSep 8, 2019

We can create Redis instance via Memorystore but with Memorystore we cannot connect to redis instance from outside ip address.

Setup VM

Follow tutorial to create VM on google cloud platform.

Enable Static Ip Address

By default external ip address of a VM is changed from time to time. We need to set it statically.

Setup Firewall Rule for VM

In order for VM to be accessed from outside world we need to set firewall rule. Go to GCP firewall rules, then enable it to be accessed from all ip addresses (You can limit it access by some specific ip addresses). By default redis connection via port 6379.

Install Redis

SSH into VM and install redis via docker (you need to install docker if you don’t pre install it during VM setting up):

docker pull redis

Run Docker Container and Redis

docker run -d -p 6379:6379 --name redis-name redis

Set Require Pass for Redis

To access redis command line interface:

redis-cli -h vm_external_ip_address -p 6379

Set require pass:

config set requirepass mypass

After that every redis command would require pass to execute. In command line interface session, to authorize access, use command:

AUTH mypass

Limitation and Future Work

It is better to set require pass via redis configuration file, because every time vm is restarted we might lost setting require pass.

Other useful commands and links

To list all containers:

docker ps -a OR docker container ls -a 

To list all container images:

docker image list

To sh into container:

docker exec -it container_id sh

How to set up Redis slave and master replication: https://community.pivotal.io/s/article/How-to-setup-Redis-master-and-slave-replication

--

--