在 docker 下安装 CockroachDB 集群

我是在我的mac上利用docker部署CockroachDB集群。官方的文档,也有教程,见 https://www.cockroachlabs.com/docs/stable/... 。这里是用docker的命令行手动创建,有点烦,所有我用了docker-compose。具体代码如下:

version: '2.1'

networks:
  huannet:     

services:
  cockroach01:
    image: cockroachdb/cockroach:v2.1.6
    container_name: cockroach01
    restart: always   
    networks:
      - huannet
    command: start --insecure
    ports:
      - "26257:26257" 
      - "8081:8080"
    volumes:
      - ~/docker/data/cockroach-data01:/cockroach/cockroach-data 
  cockroach02:
    image: cockroachdb/cockroach:v2.1.6
    container_name: cockroach02
    restart: always   
    networks:
      - huannet
    command: start --insecure --join=cockroach01
    ports:
      - "26258:26257" 
      - "8082:8080"
    volumes:
      - ~/docker/data/cockroach-data02:/cockroach/cockroach-data 
  cockroach03:
    image: cockroachdb/cockroach:v2.1.6
    container_name: cockroach03
    restart: always   
    networks:
      - huannet
    command: start --insecure --join=cockroach01
    ports:
      - "26259:26257" 
      - "8083:8080"
    volumes:
      - ~/docker/data/cockroach-data03:/cockroach/cockroach-data        

保存后,启动服务:

docker-compose up -d.
Creating network "app_huannet" with the default driver
Creating cockroach02 ... done
Creating cockroach03 ... done
Creating cockroach01 ... done

证明已经启动成功。上去看看:

docker exec -it cockroach01 ./cockroach sql --insecure
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v2.1.6 (x86_64-unknown-linux-gnu, built 2019/03/04 23:21:07, go1.10.7) (same version as client)
# Cluster ID: 3cbe28f1-db6c-4bfc-8f8f-670bcc756fef
#
# Enter \? for a brief introduction.
#
root@:26257/defaultdb>

我们创建数据库,创建表,插入数据试试:

CREATE DATABASE bank; 
CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
INSERT INTO bank.accounts VALUES (1, 1000.50);
INSERT INTO bank.accounts VALUES (2, 1001.50);
select * from bank.accounts;

都正常没有问题。这个时候我们可以通过管理界面看看系统情况,在浏览器中打开:http://127.0.0.1:8081

在docker下安装CockroachDB集群

可以看到三个node都正常。完事!

superwen
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

社区文档: