Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/password support #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
redis-sentinel-proxy
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.14.3-alpine AS build

COPY main.go /src/redis-sentinel-proxy/

WORKDIR /src/redis-sentinel-proxy/

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .

FROM alpine:3.4
MAINTAINER Anubhav Mishra <[email protected]>

# copy binary
COPY --from=build /src/redis-sentinel-proxy/redis-sentinel-proxy /usr/local/bin/redis-sentinel-proxy

ENTRYPOINT ["/usr/local/bin/redis-sentinel-proxy"]
CMD ["-master", "mymaster"]
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
IMAGE_NAME := anubhavmishra/redis-sentinel-proxy
.PHONY: test

.DEFAULT_GOAL := help
help: ## List targets & descriptions
@cat Makefile* | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

deps:
go get .

run-docker: ## Run dockerized service directly
docker run $(IMAGE_NAME):latest

push: ## docker push image to registry
docker push $(IMAGE_NAME):latest

build: ## Build the project
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .
docker build -t $(IMAGE_NAME):latest .

run: ## Build and run the project
go build . && ./redis-sentinel-proxy

clean:
-rm -rf build
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,38 @@ Small command utility that:
Usage:

`./redis-sentinel-proxy -listen IP:PORT -sentinel :SENTINEL_PORT -master NAME`

## Usage

Edit `kubernetes/redis-sentinel-proxy-deployment.yaml`:

```bash
vim kubernetes/redis-sentinel-proxy-deployment.yaml
...
args:
- "-master"
- "primary"
- "-sentinel"
- "redis-sentinel.$(NAMESPACE):26379" # change this to the sentinel address
```

Create `redis-sentinel-proxy-deployment` that uses `redis-sentinel-proxy`:

```bash
kubectl apply -f kubernetes/redis-sentinel-proxy-deployment.yaml
deployment "redis-sentinel-proxy" configured
```

Check if deployment is running:

```bash
kubectl get pods
redis-sentinel-proxy-2064359825-s4n0k 1/1 Running 0 1d
```

Expose `redis-sentinel-proxy-deployment`:

```bash
kubectl apply -f kubernetes/redis-sentinel-proxy-service.yaml
```

27 changes: 27 additions & 0 deletions kubernetes/redis-sentinel-proxy-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: redis-sentinel-proxy
namespace: redis
spec:
replicas: 1
template:
metadata:
labels:
app: redis-sentinel-proxy
spec:
containers:
- name: redis-sentinel-proxy
image: anubhavmishra/redis-sentinel-proxy:latest
env:
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
args:
- "-master"
- "primary"
- "-sentinel"
- "redis-sentinel.$(NAMESPACE):26379"
ports:
- containerPort: 9999
14 changes: 14 additions & 0 deletions kubernetes/redis-sentinel-proxy-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: redis-sentinel-proxy
namespace: redis
labels:
app: redis-sentinel-proxy
spec:
type: NodePort
ports:
- port: 9999
name: redis-sentinel-proxy
selector:
app: redis-sentinel-proxy
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var (
localAddr = flag.String("listen", ":9999", "local address")
sentinelAddr = flag.String("sentinel", ":26379", "remote address")
masterName = flag.String("master", "", "name of the master redis node")
password = flag.String("password", "", "password (if any) to authenticate")
debug = flag.Bool("debug", false, "sets debug mode")
)

func main() {
Expand Down Expand Up @@ -54,7 +56,7 @@ func main() {
func master() {
var err error
for {
masterAddr, err = getMasterAddr(saddr, *masterName)
masterAddr, err = getMasterAddr(saddr, *masterName, *password)
if err != nil {
log.Println(err)
}
Expand All @@ -78,14 +80,30 @@ func proxy(local io.ReadWriteCloser, remoteAddr *net.TCPAddr) {
go pipe(remote, local)
}

func getMasterAddr(sentinelAddress *net.TCPAddr, masterName string) (*net.TCPAddr, error) {
func getMasterAddr(sentinelAddress *net.TCPAddr, masterName string, password string) (*net.TCPAddr, error) {
conn, err := net.DialTCP("tcp", nil, sentinelAddress)
if err != nil {
return nil, err
}

defer conn.Close()

if len(password) > 0 {
conn.Write([]byte(fmt.Sprintf("AUTH %s\n", password)))
if *debug {
fmt.Println("> AUTH ", password)
}
authResp := make([]byte, 256)
_, err = conn.Read(authResp)

if *debug {
fmt.Println("< ", string(authResp))
}
}

if *debug {
fmt.Println("> sentinel get-master-addr-by-name ", masterName)
}
conn.Write([]byte(fmt.Sprintf("sentinel get-master-addr-by-name %s\n", masterName)))

b := make([]byte, 256)
Expand All @@ -95,9 +113,12 @@ func getMasterAddr(sentinelAddress *net.TCPAddr, masterName string) (*net.TCPAdd
}

parts := strings.Split(string(b), "\r\n")
if *debug {
fmt.Println("< ", string(b))
}

if len(parts) < 5 {
err = errors.New("Couldn't get master address from sentinel")
err = errors.New(fmt.Sprintf("Couldn't get master address from sentinel: %s", string(b)))
return nil, err
}

Expand Down