diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fbccc37 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +## version +SERVER_VERSION = v1.0.0 +## command +GO = go +GO_VENDOR = go mod +MKDIR_P = mkdir -p + +## build +.PHONY: build +build: + GO111MODULE=on $(GO) build -v -o _output/balancer ./ + + +## dockerfile + +.PHONY: docker.build +docker.build: + docker build --no-cache --rm --tag balancer:$(SERVER_VERSION) -f ./build/Dockerfile . diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..f6a2f7a --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,20 @@ +# Building stage +FROM golang:1.17.3 AS builder + +ENV GO111MODULE=on \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOPROXY="https://goproxy.cn,direct" + +WORKDIR /go/src + +COPY . /go/src + +ENV TIMEZONE "Asia/Shanghai" + +RUN go build -v -o _output/balancer ./ + +FROM reg.hrlyit.com/base/ubuntu:20.04 +COPY --from=builder /go/src/_output/balancer /opt/ + +ENTRYPOINT ["./opt/balancer"] diff --git a/deploy/configmap.yaml b/deploy/configmap.yaml new file mode 100644 index 0000000..797b918 --- /dev/null +++ b/deploy/configmap.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: balancer + namespace: default +data: + config.yaml: | + schema: http # support http and https + port: 8089 # port for balancer + ssl_certificate: + ssl_certificate_key: + tcp_health_check: true + health_check_interval: 3 # health check interval (second) + # The maximum number of requests that the balancer can handle at the same time + # 0 refers to no limit to the maximum number of requests + max_allowed: 100 + location: # route matching for reverse proxy + - pattern: / + proxy_pass: # URL of the reverse proxy + - "http://192.168.1.1" + - "http://192.168.1.2:1015" + - "https://192.168.1.2" + - "http://my-server.com" + balance_mode: round-robin # load balancing algorithm \ No newline at end of file diff --git a/deploy/deployment.yaml b/deploy/deployment.yaml new file mode 100644 index 0000000..a0fc15a --- /dev/null +++ b/deploy/deployment.yaml @@ -0,0 +1,29 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: balancer + namespace: default + labels: + app: balancer +spec: + replicas: 1 + selector: + matchLabels: + app: balancer + template: + metadata: + labels: + app: balancer + spec: + containers: + - name: balancer + image: balancer:v1.0.0 + imagePullPolicy: IfNotPresent + volumeMounts: + - name: config + mountPath: /opt + readOnly: true + volumes: + - name: config + configMap: + name: balancer \ No newline at end of file diff --git a/deploy/service.yaml b/deploy/service.yaml new file mode 100644 index 0000000..9b88526 --- /dev/null +++ b/deploy/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: balancer + namespace: default +spec: + type: NodePort + selector: + app: balancer + ports: + - name: balancer + protocol: TCP + nodePort: 30089 + port: 80 + targetPort: 8089 \ No newline at end of file