[Etcd]简介与集群部署

0、简介

https://github.com/etcd-io/etcd

etcd是一个分布式可靠的键值存储,用于分布式系统的最关键数据,重点是:

  • 简单:定义明确,面向用户的API(gRPC)
  • 安全:具有可选客户端证书身份验证的自动TLS
  • 快速:基准测试10,000次/秒
  • 可靠:使用Raft正确分布

etcd是用Go编写的,使用Raft一致性算法来管理高度可用的复制日志。

etcd被许多公司用于生产,开发团队在关键部署场景中支持它,其中etcd经常与KuberneteslocksmithvulcandDoorman等许多应用程序配合使用。通过严格的测试进一步确保可靠性。

本次配置etcd集群服务,为后续flannel网络及kubernetes(k8s)集群提供基础服务

1、环境信息
ip信息:
     ip地址      主机名
192.168.2.132 master1
192.168.2.133 master2
192.168.2.134 master3
系统:centos7.6
配置:4C8G20G

2、安装

master1:配置和启动

cat > /etc/etcd/etcd.conf << EOF
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.2.132:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.2.132:2379,http://localhost:2379"
ETCD_NAME="master1"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.132:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.132:2379,http://localhost:2379"
ETCD_INITIAL_CLUSTER="master1=http://192.168.2.132:2380,master2=http://192.168.2.133:2380,master3=http://192.168.2.134:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

etcd --name master1 --initial-advertise-peer-urls http://192.168.2.132:2380   --listen-peer-urls http://192.168.2.132:2380   --listen-client-urls http://192.168.2.132:2379,http://127.0.0.1:2379   --advertise-client-urls http://192.168.2.132:2379   --initial-cluster-token etcd-cluster   --initial-cluster master1=http://192.168.2.132:2380,master2=http://192.168.2.133:2380,master3=http://192.168.2.134:2380   --initial-cluster-state new &


master2:配置和启动

cat > /etc/etcd/etcd.conf << EOF
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.2.133:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.2.133:2379,http://localhost:2379"
ETCD_NAME="master1"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.133:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.133:2379,http://localhost:2379"
ETCD_INITIAL_CLUSTER="master1=http://192.168.2.132:2380,master2=http://192.168.2.133:2380,master3=http://192.168.2.134:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF


etcd --name master2 --initial-advertise-peer-urls http://192.168.2.133:2380   --listen-peer-urls http://192.168.2.133:2380   --listen-client-urls http://192.168.2.133:2379,http://127.0.0.1:2379   --advertise-client-urls http://192.168.2.133:2379   --initialetcdctl cluster-healthuster   --initial-cluster master1=http://192.168.2.132:2380,master2=http://192.168.2.133:2380,master3=http://192.168.2.134:2380   --initial-cluster-state new &


master3:配置和启动

cat > /etc/etcd/etcd.conf << EOF
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.2.134:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.2.134:2379,http://localhost:2379"
ETCD_NAME="master3"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.134:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.134:2379,http://localhost:2379"
ETCD_INITIAL_CLUSTER="master1=http://192.168.2.132:2380,master2=http://192.168.2.133:2380,master3=http://192.168.2.134:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="existing"
EOF


etcd --name master3 --initial-advertise-peer-urls http://192.168.2.134:2380 --listen-peer-urls http://192.168.2.134:2380 --listen-client-urls http://192.168.2.134:2379,http://127.0.0.1:2379 --advertise-client-urls http://192.168.2.134:2379 --initial-cluster-token etcd-cluster --initial-cluster master1=http://192.168.2.132:2380,master2=http://192.168.2.133:2380,master3=http://192.168.2.134:2380 --initial-cluster-state new &

3、检查集群服务状态

[root@master1 ~]# etcdctl cluster-health
member 3447d51c282e7f7f is healthy: got healthy result from http://192.168.2.132:2379
member 71e607720370dbee is healthy: got healthy result from http://192.168.2.133:2379
member dd0fb13f53112f2c is healthy: got healthy result from http://192.168.2.134:2379
cluster is healthy

或:

[root@master1 ~]# etcdctl member list
3447d51c282e7f7f: name=master1 peerURLs=http://192.168.2.132:2380 clientURLs=http://192.168.2.132:2379 isLeader=true
71e607720370dbee: name=master2 peerURLs=http://192.168.2.133:2380 clientURLs=http://192.168.2.133:2379 isLeader=false
dd0fb13f53112f2c: name=master3 peerURLs=http://192.168.2.134:2380 clientURLs=http://192.168.2.134:2379 isLeader=false

4、测试etcd数据

master1执行:

[root@master1 etcd]# etcdctl mkdir /testdir
[root@master1 etcd]# etcdctl ls
/testdir

[root@master1 etcd]# etcdctl set /testdir/key1 value1
value1
[root@master1 etcd]# etcdctl get /testdir/key1
value1

[root@master1 etcd]# etcdctl -o extended get /testdir/key1
Key: /testdir/key1
Created-Index: 7
Modified-Index: 7
TTL: 0
Index: 7

value1

[root@master1 etcd]# etcdctl ls --recursive
/testdir
/testdir/key1

master2或3执行检查

[root@master2 etcd]# etcdctl ls
/testdir
[root@master2 etcd]# etcdctl ls --recursive
/testdir
/testdir/key1

[root@master2 etcd]# etcdctl get /testdir/key1
value1

至此ok

5、其他命令

[root@master1 ~]# etcdctl

COMMANDS:
      backup          backup an etcd directory
      cluster-health  check the health of the etcd cluster
      mk              make a new key with a given value
      mkdir           make a new directory
      rm              remove a key or a directory
      rmdir           removes the key if it is an empty directory or a key-value pair
      get             retrieve the value of a key
      ls              retrieve a directory
      set             set the value of a key
      setdir          create a new directory or update an existing directory TTL
      update          update an existing key with a given value
      updatedir       update an existing directory
      watch           watch a key for changes
      exec-watch      watch a key for changes and exec an executable
      member          member add, remove and list subcommands
      user            user add, grant and revoke subcommands
      role            role add, grant and revoke subcommands
      auth            overall auth controls
      help, h         Shows a list of commands or help for one command

posted @ 2019-05-28 16:45  deyuan  阅读(238)  评论(0)    收藏  举报