Redis: Deploying Redis on Kubernetes, Building Chat Applications with Redis Pub/Sub on Kubernetes, and MongoDB Caching with Redis.
In this comprehensive guide, we’ll walk through the step-by-step process of setting up Redis on Kubernetes, building a real-time chat application with Redis Pub/Sub on Kubernetes, and leveraging MongoDB caching with Redis.
Part 2: https://medium.com/@harshaljethwa19/redis-master-and-slave-architecture-with-kubernetes-b46c360d16c2
Part 3: https://medium.com/@harshaljethwa19/redis-mongodb-caching-with-redis-72dd92be9a1a
Part 1: Setting Up Redis on Kubernetes:
- Install Kubernetes: Before diving into Redis deployment, it’s essential to have a Kubernetes cluster up and running. Whether you choose a managed service like GKE or EKS or opt for a local setup using Minikube or kind, ensure your Kubernetes environment is ready to go. I have created my k8 cluster with 1 master and 2 worker Nodes
- Deploy Redis: Using a Deployment YAML file, we’ll deploy a Redis instance on Kubernetes. This YAML file defines the configuration for the Redis Deployment,
Save this below YAML to a file, for example `redis-deployment.yaml`, and apply it to your Kubernetes cluster. This will deploy a single Redis pod running the latest Redis image :
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
kubectl apply -f redis-deployment.yaml
Create a Service: To expose Redis within the Kubernetes cluster, create a Service YAML file. Save this YAML to a file, for example `redis-service.yaml`, and apply it.
This will create a Service named “redis” that exposes Redis on port 6379 within the Kubernetes cluster.:
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
selector:
app: redis
ports:
- protocol: TCP
port: 6379
targetPort: 6379
kubectl apply -f redis-service.yaml
You can test the Redis deployment by connecting to it from another pod within the Kubernetes cluster. For example, you can deploy a simple Redis client pod. Save this YAML to a file, for example `redis-client-pod.yaml`, and apply it:
apiVersion: v1
kind: Pod
metadata:
name: redis-client
spec:
containers:
- name: redis-client
image: redis:latest
command: ["redis-cli"]
tty: true
kubectl apply -f redis-client-pod.yaml
Once the pod is running, you can connect to the Redis server using the Redis CLI:
kubectl exec -it redis-client -- redis-cli -h redis
Once you’ve connected to the Redis server using the Redis CLI, you can interact with it by executing various commands. Here are a few examples to get you started:
1. Set and Get a Key-Value Pair:
SET mykey "Hello"
GET mykey
2. Increment a Counter:
SET counter 0
INCR counter
3. Store and Retrieve Lists:
LPUSH mylist "item1"
LPUSH mylist "item2"
LRANGE mylist 0 -1
4. Hashes:
HSET user:id1 username "john_doe"
HSET user:id1 email "john@example.com"
HGETALL user:id1
```
5. Sets:
SADD myset "member1"
SADD myset "member2"
SMEMBERS myset
6. Sorted Sets:
ZADD myzset 1 "member1"
ZADD myzset 2 "member2"
ZRANGE myzset 0 -1 WITHSCORES
These are just a few basic Redis commands to get you started. Experimenting with these commands will help you understand how Redis works and how to leverage its features for your applications
Pub/Sub Messaging:
Redis supports Publish/Subscribe messaging, allowing clients to subscribe to channels and receive messages published to those channels.
SUBSCRIBE channel
PUBLISH channel "message"
Transactions:
Redis supports transactions, allowing you to execute a group of commands as a single atomic operation.
MULTI
INCR counter
SET mykey "value"
EXEC
After verifying the Redis deployment, we delve into the rich feature set that Redis offers. From basic key-value operations to advanced data structures like lists, hashes, sets, and sorted sets, Redis provides a versatile toolkit for managing data. We cover essential Redis commands and demonstrate their usage, highlighting how they can be leveraged in real-world scenarios.
Part 2: https://medium.com/@harshaljethwa19/redis-master-and-slave-architecture-with-kubernetes-b46c360d16c2
Part 3: https://medium.com/@harshaljethwa19/redis-mongodb-caching-with-redis-72dd92be9a1a
Follow me :
Linkedin: https://www.linkedin.com/in/harshaljethwa/
GitHub: https://github.com/HARSHALJETHWA19/
Twitter: https://twitter.com/harshaljethwaa
Thank You!!!