Dockerfile
$ vim Dockfile
FROM golang:1.15-alpine3.12 as builder
ARG goproxy
COPY . /go/src/github.com/flike/kingshard
RUN cd /go/src/github.com/flike/kingshard \
&& GO111MODULE=off GOPROXY=$goproxy GOOS=linux CGO_ENABLED=0 go install -v ./vendor/golang.org/x/tools/cmd/goyacc \
&& /go/bin/goyacc -o ./sqlparser/sql.go ./sqlparser/sql.y \
&& GO111MODULE=off GOPROXY=$goproxy GOOS=linux CGO_ENABLED=0 go install -v ./cmd/kingshard
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk --update add --no-cache tzdata git
FROM scratch
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/passwd /etc/
COPY --from=builder /go/bin/kingshard /bin/
WORKDIR /
# server listen addr
EXPOSE 3306
# the web api server
EXPOSE 9797
USER nobody
ENTRYPOINT ["/bin/kingshard"]
构建镜像
$ git clone -b 1.6 https://github.com/flike/kingshard.git
$ docker build . \
--force-rm \
--build-arg goproxy=https://goproxy.io \
-t kingshard:1.6
KingShard 配置文件
$ vim ks.yaml
# server listen addr
addr : 0.0.0.0:3306
# prometheus server listen addr
#prometheus_addr : 0.0.0.0:7080
# server user and password
user_list:
-
user : root
password : 123456
# the web api server
web_addr : 0.0.0.0:9797
#HTTP Basic Auth
web_user : admin
web_password : admin
# if set log_path, the sql log will write into log_path/sql.log,the system log
# will write into log_path/sys.log
#log_path : /Users/flike/log
# log level[debug|info|warn|error],default error
log_level : debug
# if set log_sql(on|off) off,the sql log will not output
log_sql: on
# only log the query that take more than slow_log_time ms
#slow_log_time : 100
# the path of blacklist sql file
# all these sqls in the file will been forbidden by kingshard
#blacklist_sql_file: /Users/flike/blacklist
# only allow this ip list ip to connect kingshard
# support ip and ip segment
#allow_ips : 127.0.0.1,192.168.15.0/24
# the charset of kingshard, if you don't set this item
# the default charset of kingshard is utf8.
#proxy_charset: utf8
# node is an agenda for real remote mysql server.
nodes :
-
name : node1
# default max conns for mysql server
max_conns_limit : 200
# all mysql in a node must have the same user and password
user : root
password : 123456
# master represents a real mysql master server
master: 192.168.2.115:3306
# slave represents a real mysql salve server
slave:
# slave represents a real mysql salve server,and the number after '@' is
# read load weight of this slave.
#slave : 192.168.59.101:3307@2,192.168.59.101:3307@3
down_after_noalive : 32
# schema defines sharding rules, the db is the sharding table database.
schema_list :
-
user: root
nodes: [node1]
default: node1
shard:
-
db : demo
table: demo_table
key: sound_id
nodes: [node1]
# https://github.com/doumadou/kingshard/blob/master/doc/KingDoc/kingshard_sharding_introduce.md
type: hash
locations: [4]
运行 KingShard
$ docker run --rm -it -p 3306:3306 -p 9797:9797 -v $PWD/ks.yaml:/etc/ks.yaml kingshard:1.6
参考
- https://github.com/flike/kingshard.git