Verbindung zu einer Memorystore for Redis Cluster-Instanz herstellen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Auf dieser Seite wird erläutert, wie Sie eine Verbindung zu einer Memorystore for Redis Cluster-Instanz herstellen.
Clientunterstützung
Memorystore for Redis Cluster bietet keine eigenen Clientbibliotheken, sondern unterstützt vorhandene Drittanbieter-Clientbibliotheken, die das Redis-Protokoll unterstützen.
Eine Beispiel-Clientliste finden Sie auf der Redis-Seite Clients.
Best Practices für redis-py-Clients
Wenn Sie mit dem Python-Client redis-py eine Verbindung zu Ihrer Memorystore for Redis-Clusterinstanz herstellen möchten, müssen Sie beim Deklarieren eines Redis-Clusters skip_full_coverage_check=True hinzufügen:
Ports in der Firewall auf die Zulassungsliste setzen
Wenn Sie die richtigen Ports in Ihrer Firewall nicht auf die Zulassungsliste setzen, können auf Ihrer Instanz Verbindungsfehler auftreten. Weitere Informationen zum Zulassen von Ports finden Sie unter Fehlerbehebung.
Regionenübergreifender Zugriff
Sie können von einem Client aus, der sich in einer anderen Region als die Memorystore-Instanz befindet, auf eine Memorystore for Redis-Clusterinstanz zugreifen.
Notieren Sie sich im Abschnitt Mit dieser Instanz verbinden die IP-Adresse und Portnummer neben Discovery-Endpunkt. Zusammen bilden diese beiden Werte den Discovery-Endpunkt Ihres Clusters.
gcloud
Führen Sie die describegcloud CLI aus, wie im Abschnitt Instanzdetails ansehen beschrieben, um den Discovery-Endpunkt Ihres Clusters aufzurufen.
Notieren Sie sich die Endpunktadresse und die Portnummer, die im Abschnitt discovery_endpoints: aufgeführt sind. Zusammen bilden diese beiden Werte den Erkennungsendpunkt Ihres Clusters.
Verbindung über eine Compute Engine-VM mit redis-cli herstellen
Sie können eine Verbindung zur Memorystore for Redis-Clusterinstanz über eine beliebige Compute Engine-VM herstellen, die das autorisierte Netzwerk der Memorystore-Instanz verwendet.
So stellen Sie eine Verbindung zur Instanz her:
Wenn Sie noch keine Linux Compute Engine-VM haben, die das autorisierte Netzwerk für Ihre Memorystore for Redis-Clusterinstanz verwendet, erstellen Sie eine VM und stellen Sie eine Verbindung zu ihr her: Folgen Sie dazu der Kurzanleitung zum Erstellen einer Linux-VM.
Installieren Sie redis-cli Version 6.0 oder höher auf der Compute Engine-VM. Folgen Sie dazu der Anleitung unter Redis unter Linux installieren.
DISCOVERY_ENDPOINT_ADDRESS und PORT_NUMBER sind die Werte, die Sie im vorherigen Schritt notiert haben.
Führen Sie den Befehl CLUSTER SHARDS aus, um die Clustertopologie aufzurufen. Notieren Sie sich eine der IP-Adressen und Portnummern des Knotens.
Stellen Sie mit dem folgenden Befehl eine Verbindung zum gewünschten Knoten her:
redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c
Ersetzen Sie Folgendes:
NODE_IP_ADDRESS ist die IP-Adresse des Knotens, die Sie im vorherigen Schritt ermittelt haben.
NODE_PORT ist die Portnummer des Knotens, die Sie im vorherigen Schritt ermittelt haben.
Geben Sie nach der Verbindung zum Knoten einige Redis-Befehle ein:
Geben Sie Folgendes ein:
PING
Ergebnis:
PONG
Eingeben
SET HELLO WORLD
Ergebnis:
OK
Geben Sie Folgendes ein:
GET HELLO
Ergebnis:
"WORLD"
Nachdem Sie Ihre Verbindung zum Redis-Knoten getestet haben, sollten Sie die Compute Engine-VM löschen, mit der Sie eine Verbindung zur Redis-Instanz hergestellt haben. Dadurch vermeiden Sie, dass Ihrem Cloud-Rechnungskonto Gebühren berechnet werden.
Verbindung zu einer Instanz herstellen, für die die Verschlüsselung während der Übertragung aktiviert ist
In diesem Abschnitt wird ein Beispiel für die Verbindung zu einer Memorystore for Redis-Clusterinstanz mit aktivierter Verschlüsselung während der Übertragung dargestellt.
Wenn Sie noch keine Compute Engine-VM haben, die dasselbe autorisierte Netzwerk wie die Redis-Instanz aus dem vorherigen Schritt verwendet, erstellen Sie eine VM und stellen Sie eine Verbindung zu ihr her: Folgen Sie dazu der Kurzanleitung: Linux-VM verwenden.
Installieren Sie redis-cli Version 6.0 oder höher auf der Compute Engine-VM. Folgen Sie dazu der Anleitung unter Redis unter Linux installieren.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[],[],null,["# Connect to a Memorystore for Redis Cluster instance\n\nThis page explains how to connect to a Memorystore for Redis Cluster instance.\n\nClient support\n--------------\n\nMemorystore for Redis Cluster does not provide its own client libraries, but rather\nsupports existing third-party client libraries that support the Redis protocol.\nAn example list of clients can be found on the Redis [Clients](https://redis.io/docs/clients/)\npage.\n\n### Redis-py client best practice\n\nTo connect to your Memorystore for Redis Cluster instance using the [redis-py](https://github.com/redis/) Python client, you must add the `skip_full_coverage_check=True` when declaring a Redis\nCluster: \n\n from rediscluster import RedisCluster\n endpoints = [{\"host\": \"IPADDRESS\", \"port\": \"6379\"}]\n\n rdb = RedisCluster(\n startup_nodes=endpoints,\n skip_full_coverage_check=True, # Required for Memorystore\n decode_responses = True)\n\n print(rdb.set('PYTHON', 'CLUSTER'))\n print(rdb.get('PYTHON'))\n print(rdb.unlink('PYTHON'))\n\nAllowlist ports on firewall\n---------------------------\n\nIf you don't allowlist the correct ports on your firewall, your instance can\nencounter connection errors. For more information about allowlisting ports, see\n[Troubleshoot issues](/memorystore/docs/cluster/troubleshoot-issues#connection_error_caused_by_firewall_rules).\n\nCross region access\n-------------------\n\nYou can access a Memorystore for Redis Cluster instance from a client located in a\ndifferent region than where the Memorystore instance is located.\n\nView your cluster's discovery endpoint\n--------------------------------------\n\nTo view your Memorystore cluster's [discovery endpoint](/memorystore/docs/cluster/overview#discovery_endpoint), use the following instructions: \n\n### Console\n\n1. Go to the **Memorystore for Redis Cluster** page in the Google Cloud console.\n\n [Memorystore for Redis Cluster](https://console.cloud.google.com/memorystore/redis/clusters?)\n2. Click your Cluster ID.\n\n3. In the **Connect to this instance** section, take a note of the IP address\n and port number next to **Discovery Endpoint**. These two values used\n together are your cluster's discovery endpoint.\n\n### gcloud\n\nTo view your cluster's discovery endpoint, run the `describe`\ngcloud CLI as explained in the [view instance details](/memorystore/docs/cluster/create-manage-instances#view_instance_details) section.\n\nThe output should be similar to the following: \n\n authorizationMode: AUTH_MODE_DISABLED\n createTime: '2023-08-04T17:49:07.988918793Z'\n discoveryEndpoints:\n - address: 10.142.0.13\n port: 6379\n pscConfig:\n - network: projects/my-project-364018/global/networks/default\n name: projects/my-project-364018/locations/us-east1/clusters/my-cluster-1\n pscConnections:\n - address: 10.142.0.13\n forwardingRule: https://www.googleapis.com/compute/v1/projects/my-project-364018/regions/us-east1/forwardingRules/sca-auto-fr-5bc46f69-ec80-4c5a-8971-ce267a4d8ddd\n network: projects/my-project-364018/global/networks/default\n projectId: my-project-364018\n pscConnectionId: '25734306048376845'\n - address: 10.142.0.10\n forwardingRule: https://www.googleapis.com/compute/v1/projects/my-project-364018/regions/us-east1/.\n forwardingRules/sca-auto-fr-817b64c5-85ac-48d0-82e3-c6fa99b4e67d\n network: projects/my-project-364018/global/networks/default\n projectId: my-project-364018\n pscConnectionId: '25734306048376842'\n replicaCount: 0\n shardCount: 3\n sizeGb: 39\n state: ACTIVE\n tier: PERFORMANCE\n transitEncryptionMode: TRANSIT_ENCRYPTION_MODE_DISABLED\n uid: cddda5de-d91f-449b-8755-3a665430271a\n\nTake a note of the endpoint address and port number listed in the\n`discovery_endpoints:` section. These two values used together are your\ncluster's discovery endpoint.\n\nConnect from a Compute Engine VM using redis-cli\n------------------------------------------------\n\nYou can connect to the Memorystore for Redis Cluster instance from any\nCompute Engine VM that uses the Memorystore instance's\nauthorized network.\n\nTo connect to the instance:\n\n1. If you don't already have a Linux Compute Engine VM that uses the\n authorized network for your Memorystore for Redis Cluster instance, create one and\n connect to it by following [Quickstart to create a Linux VM](/compute/docs/create-linux-vm-instance).\n\n2. Install `redis-cli` version 6.0 or newer on the Compute Engine VM by\n following the instructions at [Install Redis on Linux](https://redis.io/docs/getting-started/installation/install-redis-on-linux/).\n\n3. [View your cluster's discovery endpoint](/memorystore/docs/cluster/connect-cluster-instance#view_your_clusters_discovery_endpoint)\n and make a note of it.\n\n4. Connect to your instance's discovery endpoint by running the following\n command:\n\n ```\n redis-cli -h DISCOVERY_ENDPOINT_ADDRESS -p PORT_NUMBER -c\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eDISCOVERY_ENDPOINT_ADDRESS\u003c/var\u003e and \u003cvar translate=\"no\"\u003ePORT_NUMBER\u003c/var\u003e are the values you noted in the previous step.\n\n | **Note:** The `-c` switch is used to enable the `redis-cli` cluster support mode. If the `-c` switch is not provided, when you try to access keys from a different shard than the one you are connected to, it results in a `MOVED` error. In the cluster support mode the client is automatically reconnected to the address returned in the error response. For more details, see [Using Redis-CLI with a Redis Cluster](https://developer.redis.com/operate/redis-at-scale/scalability/redis-cli-with-redis-cluster/).\n5. Run the `CLUSTER SHARDS` command to view your cluster topology. Take a note\n of one of the node's IP addresses and port numbers.\n\n6. Connect to your desired node by running the following command:\n\n ```\n redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eNODE_IP_ADDRESS\u003c/var\u003e is the node's IP address you found in the previous step.\n - \u003cvar translate=\"no\"\u003eNODE_PORT\u003c/var\u003e is the node's port number you found in the previous step.\n7. Once connected to the node, enter some Redis commands:\n\n Enter: \n\n ```\n PING\n ```\n\n Result: \n\n ```\n PONG\n ```\n\n Enter \n\n ```\n SET HELLO WORLD\n ```\n\n Result: \n\n ```\n OK\n ```\n\n Enter: \n\n ```\n GET HELLO\n ```\n\n Result: \n\n ```\n \"WORLD\"\n ```\n8. Once you are done testing your connection to the Redis node, you should\n consider deleting the Compute Engine VM you used to connect to the\n Redis instance. Doing so helps you avoid incurring charges to your\n Cloud Billing account.\n\nConnect to an in-transit encryption enabled instance\n----------------------------------------------------\n\nThis section provides an example of how to connect to a Memorystore for Redis Cluster\ninstance that has in-transit encryption enabled.\n\n1. If you don't already have a Compute Engine VM that uses the same\n authorized network as the Redis instance from the previous step, create one\n and connect to it by following [Quickstart using a Linux VM](/compute/docs/create-linux-vm-instance).\n\n 1. Install `redis-cli` version 6.0 or newer on the Compute Engine VM by following the instructions at [Install Redis on Linux](https://redis.io/docs/getting-started/installation/install-redis-on-linux/).\n2. Install your instance's certificate authority on the Linux VM by following\n the instructions at [Install certificate authorities on your client](https://cloud.google.com/memorystore/docs/cluster/manage-in-transit-encryption#install_certificate_authorities_on_your_client).\n\n3. [View your cluster's discovery endpoint](/memorystore/docs/cluster/connect-cluster-instance#view_your_clusters_discovery_endpoint)\n and make a note of it.\n\n4. Connect to your instance's discovery endpoint by running the following\n command:\n\n ```\n redis-cli -h DISCOVERY_ENDPOINT_ADDRESS -p PORT_NUMBER -c --tls --cacert /tmp/server_ca.pem\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eDISCOVERY_ENDPOINT_ADDRESS\u003c/var\u003e and \u003cvar translate=\"no\"\u003ePORT_NUMBER\u003c/var\u003e are the values you noted in the previous step.\n\n | **Note:** The `-c` switch is used to enable the `redis-cli` cluster support mode. If the `-c` switch is not provided, when you try to access keys from a different shard than the one you are connected to, it results in a `MOVED` error. In the cluster support mode the client is automatically reconnected to the address returned in the error response. For more details, see [Using Redis-CLI with a Redis Cluster](https://developer.redis.com/operate/redis-at-scale/scalability/redis-cli-with-redis-cluster/).\n5. Run the `CLUSTER SHARDS` command to view your cluster topology. Take a note\n of one of the node's IP addresses and port numbers.\n\n6. Connect to your desired node by running the following command:\n\n ```\n redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c --tls --cacert /tmp/server_ca.pem\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eNODE_IP_ADDRESS\u003c/var\u003e is the node's IP address you found in the previous step.\n - \u003cvar translate=\"no\"\u003eNODE_PORT\u003c/var\u003e is the node's port number you found in the previous step.\n7. Once connected to the node, enter some Redis commands:\n\n Enter: \n\n ```\n PING\n ```\n\n Result: \n\n ```\n PONG\n ```"]]