闲置几台屌丝版腾讯云服务器(2 core CPU
4GB Memory
40GB Disk
),难得清闲,用其中的3台部署一套TiKV用于测试功能和代码研究。
使用Docker进行部署,官方镜像pingcap/pd:v2.0.6、pingcap/tikv:v2.0.6
| Name | Host IP | Services | Docker Volume | Data Path |
| Node1(TB) | 172.22.0.6 | PD1 | pd-data | /data |
| Node2(TD) | 172.22.0.10 | PD2 | pd-data | /data |
| Node3(TE) | 172.22.0.15 | PD3 | pd-data | /data |
| Node1(TB) | 172.22.0.6 | TiKV1 | tikv-data | /data |
| Node2(TD) | 172.22.0.10 | TiKV2 | tikv-data | /data |
| Node3(TE) | 172.22.0.15 | TiKV3 | tikv-data | /data |
三节点(Node1/Node2/Node3)都执行
sudo docker pull pingcap/pd:v2.0.6
sudo docker pull pingcap/tikv:v2.0.6
sudo docker volume create pd-data
sudo docker volume create tikv-data
export TBIP="172.21.0.6"
export TDIP="172.21.0.10"
export TEIP="172.21.0.15"
sudo docker run -d --restart=always --name pd1 \
-p 2379:2379 \
-p 2380:2380 \
-v pd-data:/data \
pingcap/pd:v2.0.6 \
--name="pd1" \
--data-dir="/data/pd1" \
--client-urls="http://0.0.0.0:2379" \
--advertise-client-urls="http://${TBIP}:2379" \
--peer-urls="http://0.0.0.0:2380" \
--advertise-peer-urls="http://${TBIP}:2380" \
--initial-cluster="pd1=http://${TBIP}:2380,pd2=http://${TDIP}:2380,pd3=http://${TEIP}:2380"
sudo docker run -d --restart=always --name pd2 \
-p 2379:2379 \
-p 2380:2380 \
-v pd-data:/data \
pingcap/pd:v2.0.6 \
--name="pd2" \
--data-dir="/data/pd2" \
--client-urls="http://0.0.0.0:2379" \
--advertise-client-urls="http://${TDIP}:2379" \
--peer-urls="http://0.0.0.0:2380" \
--advertise-peer-urls="http://${TDIP}:2380" \
--initial-cluster="pd1=http://${TBIP}:2380,pd2=http://${TDIP}:2380,pd3=http://${TEIP}:2380"
sudo docker run -d --restart=always --name pd3 \
-p 2379:2379 \
-p 2380:2380 \
-v pd-data:/data \
pingcap/pd:v2.0.6 \
--name="pd3" \
--data-dir="/data/pd3" \
--client-urls="http://0.0.0.0:2379" \
--advertise-client-urls="http://${TEIP}:2379" \
--peer-urls="http://0.0.0.0:2380" \
--advertise-peer-urls="http://${TEIP}:2380" \
--initial-cluster="pd1=http://${TBIP}:2380,pd2=http://${TDIP}:2380,pd3=http://${TEIP}:2380"
sudo docker run -d --restart=always --name tikv1 \
--ulimit nofile=82920:82920 \
-p 20160:20160 \
-v tikv-data:/data \
pingcap/tikv:v2.0.6 \
--addr="0.0.0.0:20160" \
--advertise-addr="${TBIP}:20160" \
--data-dir="/data/tikv1" \
--pd="${TBIP}:2379,${TDIP}:2379,${TEIP}:2379"
sudo docker run -d --restart=always --name tikv2 \
--ulimit nofile=82920:82920 \
-p 20160:20160 \
-v tikv-data:/data \
pingcap/tikv:v2.0.6 \
--addr="0.0.0.0:20160" \
--advertise-addr="${TDIP}:20160" \
--data-dir="/data/tikv2" \
--pd="${TBIP}:2379,${TDIP}:2379,${TEIP}:2379"
sudo docker run -d --restart=always --name tikv3 \
--ulimit nofile=82920:82920 \
-p 20160:20160 \
-v tikv-data:/data \
pingcap/tikv:v2.0.6 \
--addr="0.0.0.0:20160" \
--advertise-addr="${TEIP}:20160" \
--data-dir="/data/tikv3" \
--pd="${TBIP}:2379,${TDIP}:2379,${TEIP}:2379"
curl ${TAIP}:2379/v2/members
或(使用httpie)
http ${TAIP}:2379/v2/members
curl ${TAIP}:2379/pd/api/v1/stores
或(使用httpie)
http ${TAIP}:2379/pd/api/v1/stores
package main
import (
"fmt"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/store/tikv"
)
func main() {
cli, err := tikv.NewRawKVClient([]string{":2379"}, config.Security{})
if err != nil {
panic(err)
}
defer cli.Close()
fmt.Printf("cluster ID: %d\n", cli.ClusterID())
key := []byte("Company")
val := []byte("PingCAP")
// put key into tikv
err = cli.Put(key, val)
if err != nil {
panic(err)
}
fmt.Printf("Successfully put %s:%s to tikv\n", key, val)
// get key from tikv
val, err = cli.Get(key)
if err != nil {
panic(err)
}
fmt.Printf("found val: %s for key: %s\n", val, key)
// delete key from tikv
err = cli.Delete(key)
if err != nil {
panic(err)
}
fmt.Printf("key: %s deleted\n", key)
// get key again from tikv
val, err = cli.Get(key)
if err != nil {
panic(err)
}
fmt.Printf("found val: %s for key: %s\n", val, key)
}
// output:
// INFO[0000] [pd] create pd client with endpoints [192.168.199.113:2379]
// INFO[0000] [pd] leader switches to: http://127.0.0.1:2379, previous:
// INFO[0000] [pd] init cluster id 6554145799874853483
// cluster ID: 6554145799874853483
// Successfully put Company:PingCAP to tikv
// found val: PingCAP for key: Company
// key: Company deleted
// found val: for key: Company
--ulimit nofile=82920:82920
选项使用docker启动TiKV