CockroachDB
CockroachDB是一个分散式的数据库软件,目标是与PostgreSQL相容但可以提供横向扩充的能力。
安装
CockroachDB的设计是单一执行档,没有复杂的安装包,所以下载复制到目录里即可:
cd /tmp
wget -qO- https://binaries.cockroachdb.com/cockroach-v2.1.3.linux-amd64.tgz | tar xvz
cp -i cockroach-v2.1.3.linux-amd64/cockroach /usr/local/bin
设定
设定分成两块,一块是初始第一台机器:
cockroach start --background --insecure --listen-addr=0.0.0.0 --advertise-addr=$(ifconfig eth0 | grep inet | awk '{print $2}')
另外一块是后续的机器,多了--join
要设定:
cockroach start --background --insecure --listen-addr=0.0.0.0 --join=192.168.x.x --advertise-addr=$(ifconfig eth0 | grep inet | awk '{print $2}')
可以用以下指令确认机器都有连上cluster:
cockroach node ls --insecure
然后用PostgreSQL的client连上测试:
psql -h 192.168.x.x -p 26257 -U root
另外可能要修改replica
的数量(预设是5),这组指令已经被标为deprecated但目前还可以用:
cockroach zone get .meta --insecure
echo "num_replicas: 3" | cockroach zone set .meta -f - --insecure
cockroach zone get .meta --insecure
新的语法是透过SQL界面修改:
ALTER RANGE default CONFIGURE ZONE USING num_replicas = 3;
测试
这边是拿oltpbenchmark在AWS上测试,使用一台c5.2xlarge
与五台r5.large
架设Kubernetes后再里面建立六个Pod测试。
Kubernetes
使用Kubernetes时的设定档如下:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: poc-r5
spec:
replicas: 5
serviceName: cockroachdb-r5
selector:
matchLabels:
app: cockroachdb-r5
template:
metadata:
labels:
app: cockroachdb-r5
spec:
containers:
- name: cockroachdb-r5
image: ubuntu:18.04
command: ["/bin/sh", "-c"]
args:
- export DEBIAN_FRONTEND=noninteractive;
sed -i 's/archive.ubuntu.com/us.archive.ubuntu.com/' /etc/apt/sources.list;
apt update;
apt install -y iproute2 iputils-ping locales mtr-tiny net-tools tzdata vim-nox wget;
locale-gen en_US.UTF-8;
cd /tmp;
wget -qO- https://binaries.cockroachdb.com/cockroach-v2.1.3.linux-amd64.tgz | tar xvz;
cp -i cockroach-v2.1.3.linux-amd64/cockroach /usr/local/bin;
sleep 3153600000
resources:
requests:
memory: "15Gi"
env:
- name: LC_ALL
value: en_US.UTF-8
nodeSelector:
instancetype: r5
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: poc-c5
spec:
replicas: 1
serviceName: cockroachdb-c5
selector:
matchLabels:
app: cockroachdb-c5
template:
metadata:
labels:
app: cockroachdb-c5
spec:
containers:
- name: cockroachdb-c5
image: ubuntu:18.04
command: ["/bin/sh", "-c"]
args:
- export DEBIAN_FRONTEND=noninteractive;
sed -i 's/archive.ubuntu.com/us.archive.ubuntu.com/' /etc/apt/sources.list;
apt update;
apt install -y ant default-jdk git iproute2 iputils-ping locales mtr-tiny net-tools openjdk-8-jdk postgresql-client tzdata vim-nox wget;
update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java;
locale-gen en_US.UTF-8;
cd /root;
git clone https://github.com/oltpbenchmark/oltpbench.git;
cd oltpbench;
sed -i -e 's/^ALTER TABLE/-- ALTER TABLE/' src/com/oltpbenchmark/benchmarks/tpcc/ddls/tpcc-postgres-ddl.sql;
ant clean;
ant;
cp config/tpcc_config_postgres.xml config/tpcc.xml;
sed -i -e 's/TRANSACTION_READ_COMMITTED/TRANSACTION_SERIALIZABLE/' config/tpcc.xml;
sleep 3153600000
resources:
requests:
cpu: "7000m"
env:
- name: LC_ALL
value: en_US.UTF-8
nodeSelector:
instancetype: c5