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

Add Leader Election for Housekeeping in Sharded Cluster using K8s Lease #521

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Stage 1: build binary
# Start from the latest golang base image
FROM golang:1.18-buster AS builder
FROM golang:1.19-buster AS builder

# Add Maintainer Info
LABEL maintainer="hackerwins <susukang98@gmail.com>"
Expand Down
12 changes: 12 additions & 0 deletions build/charts/yorkie-cluster/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ spec:
prometheus.io/port: "{{ .Values.yorkie.ports.profilingPort }}"
prometheus.io/path: "/metrics"
spec:
automountServiceAccountToken: true
serviceAccountName: leader-election
containers:
- name: yorkie
image: "{{ .Values.yorkie.image.repository }}:{{ .Values.yorkie.image.tag | default .Chart.AppVersion }}"
Expand All @@ -41,6 +43,10 @@ spec:
"--mongo-connection-uri",
"mongodb://{{ .Values.yorkie.args.dbUrl }}:{{ .Values.yorkie.args.dbPort }}/yorkie-meta",
"--enable-pprof",
"namespace",
"{{ .Values.yorkie.namespace }}",
"--leader-election",
"true",
]
ports:
- containerPort: {{ .Values.yorkie.ports.sdkPort }}
Expand All @@ -52,3 +58,9 @@ spec:
initialDelaySeconds: 10
resources:
{{ toYaml .Values.yorkie.resources | nindent 12 }}
env:
- name: POD_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
32 changes: 32 additions & 0 deletions build/charts/yorkie-cluster/templates/leader-election-rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: v1
automountServiceAccountToken: true
kind: ServiceAccount
metadata:
name: leader-election
namespace: yorkie
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: leader-election-role
namespace: yorkie
rules:
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: leader-election-rolebinding
namespace: yorkie
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: leader-election-role
subjects:
- kind: ServiceAccount
name: leader-election
20 changes: 20 additions & 0 deletions cmd/yorkie/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (

adminTokenDuration time.Duration
housekeepingInterval time.Duration
housekeepingLeaseDuration time.Duration
clientDeactivateThreshold string

mongoConnectionURI string
Expand Down Expand Up @@ -74,6 +75,7 @@ func newServerCmd() *cobra.Command {
conf.Backend.AuthWebhookCacheUnauthTTL = authWebhookCacheUnauthTTL.String()

conf.Housekeeping.Interval = housekeepingInterval.String()
conf.Housekeeping.LeaseDuration = housekeepingLeaseDuration.String()

if mongoConnectionURI != "" {
conf.Mongo = &mongo.Config{
Expand Down Expand Up @@ -233,6 +235,12 @@ func init() {
server.DefaultHousekeepingCandidatesLimitPerProject,
"candidates limit per project for a single housekeeping run",
)
cmd.Flags().DurationVar(
&housekeepingLeaseDuration,
"housekeeping-lease-duration",
server.DefaultHousekeepingLeaseDuration,
"lease duration for a leader election in housekeeping",
)
cmd.Flags().StringVar(
&mongoConnectionURI,
"mongo-connection-uri",
Expand Down Expand Up @@ -349,6 +357,18 @@ func init() {
server.DefaultHostname,
"Yorkie Server Hostname",
)
cmd.Flags().StringVar(
&conf.Backend.Namespace,
"namespace",
server.DefaultNamespace,
"Yorkie Server Namespace for Kubernetes",
)
cmd.Flags().BoolVar(
&conf.Backend.LeaderElection,
"leader-election",
server.DefaultLeaderElection,
"Enable leader election to run tasks only on the leader.",
)

rootCmd.AddCommand(cmd)
}
39 changes: 34 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,56 @@ require (
github.com/go-playground/validator/v10 v10.11.1
github.com/gogo/protobuf v1.3.2
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang/protobuf v1.5.2
github.com/golang/protobuf v1.5.3
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/hashicorp/go-memdb v1.3.3
github.com/jedib0t/go-pretty/v6 v6.4.0
github.com/prometheus/client_golang v1.13.0
github.com/rs/xid v1.4.0
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
github.com/undefinedlabs/go-mpatch v1.0.6
go.mongodb.org/mongo-driver v1.10.3
go.uber.org/zap v1.23.0
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
golang.org/x/net v0.7.0
golang.org/x/net v0.8.0
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91
google.golang.org/grpc v1.50.0
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.27.1
k8s.io/client-go v0.27.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -54,10 +71,22 @@ require (
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.27.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/hashicorp/go-memdb => github.com/hackerwins/go-memdb v1.3.3-0.20211225080334-513a74641622
Loading