INDEX
########################################################### 2024-01-17 18:00 ########################################################### That Devops Guy... Redis for Beginners https://www.youtube.com/watch?v=L3zp347cWNw Redis is an open source data store - can be a critical DB or as a cache or to move large caches Can also be used as a pub/sub messaging system # If in kubernetes, need to make sure things can communicate with Redis docker network create redis docker run -it --rm --name redis --net redis -p 6379:6379 redis:6.0-alpine # Now can change redis configuration - go to default config for your version and copy docker run ... -v ${PWD}/config:/etc/redis/ \ redis:6.0-alpine redis-server /etc/redis/redis.conf Redis should not be exposed to public traffic as it does not have a password to connect Change default password: # config/redis.conf requirepass NEWPASSWORD # Used by all applications to interact with redis # Does have TLS support but needs to be set at compile time RDB persistence (point in time snapshots) and AOF persistence (log every write operation) dbfilename dump.rdb # Uncomment this to enable RDB logging to a specific file appendonly yes # Turn on AOF appendfilename = "appendonly.aof" # Set output file for AOF # Recommended to use both - but at minimum us RDB docker volume create redis cd ./storage/redis docker run ... -v ${PWD}/config:/etc/redis -v redis:/data ... # Mount config and volume Now how can you interact with redis? # client.go package main import (...) var redis_host = os.Getenv["REDIS_HOST"] var redis_port = os.Getenv["REDIS_PORT"] var redis_password = os.Getenv["REDIS_PASSWORD"] var rdb *redis.Client # Make global variable to store redis client var ctx = context.Background() var counter = 0 func main() { r := redis.NewClient(&redis.Options{ Addr: redis_host + ":" + redis_port:, Password: redis_password, DB: 0, }) rdb = r router := httprouter.New() router.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params){ increment_redis_key(w,r,p) }) log.fatal(http.listenAndServe(":80",router) } func incremenet_redis_key(writer http.ResponseWriter, r *http.Request, p httprouter.Params) { var, err := rdb.Get(ctx, "counter").Result() if err == redis.Nil { err := rdb.Set(ctx, "counter", 1, 0).Err() counter++ if err != nil { panic(err) } } elif if err != nil { panic(err) } else { counter,_ = strconv.Atoi(val) counter++ err := rdb.Set(ctx, "counter", counter, 0).Err() if err != nil { panic(err) } } } docker run -it -v ${PWD}:/go/src -w /go/src --net redis -p 80:80 golang:1.14-alpine # Now in container set up to build apk add git; go mod init example.com/hello go build client.go; ./client # dockerfile FROM golang:1.14-alpine as build RUN apk add --no-cache git WORKDIR /src/ COPY go.sum /src/ COPY go.mod /src/ COPY client.go /src/ RUN go build client.go ### FROM alpine as runtime COPY --from=build /src/client /app/client CMD ["/app/client"] docker run -it --net redis -e REDIS_HOST=redis -e REDIS_PORT=6379 \ -e REDIS_PASSWORD="..." -p 80:80 aimvector/redis-client:v1.0.0 # This runs the web app - so access localhost:80 - each refresh increments # Restarting redis container has persistent data