Kubernetes

ETCD

2020-12-14  本文已影响0人  voidFan

ETCD

一、ETCD概述

A highly-available key value store for shared configuration and service discovery.
一个用于配置共享和服务发现的KV存储系统。

常用概念词汇

ETCD总体架构图

etcd.png

从上述架构图来看,ETCD主要分为四个部分:

二、ETCD安装(linux环境)

1、二进制安装: 下载目标版本

单机安装场景

tar zxvf etcd-v3.1.5-linux-amd64.tar.gz
cd etcd-v3.1.5-linux-amd64
ls 
   Documentation  etcd  etcdctl  README-etcdctl.md  README.md  READMEv2-etcdctl.md
cp etcd /usr/bin
cp etcdctl /usr/bin

直接使用 etcd启动,会使用默认配置

通过配置文件conf.yml启动

# this is the configuration file for the etcd server.
    name: 'etcd_1'
    data-dir:
    listen-peer-urls: http://0.0.0.0:2380
    listen-client-urls: http://0.0.0.0:2379
    initial-advertise-peer-urls: http://0.0.0.0:2380
    advertise-client-urls: http://0.0.0.0:2379
> etcd -config-file ./config.yml

ETCD server其它启动方式

- 修改etcd server的端口和绑定host(连接时,必须指定endpoints)
./etcd -listen-client-urls http://192.168.1.100:2379 --advertise-client-urls http://192.168.1.100:2380
./etcdctl --endpoints=192.168.1.100:2379 put k v
./etcdctl --endpoints=192.168.1.100:2379 get name

- 绑定所有的host  0.0.0.0代表任意host
./etcd -listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2380
listen-client-urls 用于指定etcd和客户端的连接端口,
advertise-client-urls 用于指定etcd服务器之间通讯的端口,
etcd有要求,如果-listen-client-urls被设置了,那么就必须同时设置-advertise-client-urls,所以即使设置和默认相同,也必须显式设置。

2、 源码安装与启动(linux)

git clone https://github.com/etcd-io/etcd.git
cd etcd
./build
./etcdctl version   #执行测试命令,查看是否编译安装成功

三、etcdctl工具使用

put 数据入库

etcdctl put /testdir/testkey "Hello world"

get 数据查询

etcdctl get /testdir/testkey

update 数据更新

etcdctl updata /testdir/testkey "New Hello World"

del 删除key

etcdctl del /testdir/testkey  #成功返回1 失败返回0

member 集群节点管理

etcdctl member list      #查询etcd集群实例
etcdctl member remove    #删除 etcd 实例到 etcd 集群中。
etcdctl member add       #添加etcd 实例到 etcd 集群中。

四、ETCD集群

集群配置成功:读写访问在集群里任意一台都可以读写。

# 启动主ETCD主节点
etcd --name etcd0 --initial-advertise-peer-urls http://10.122.120.122:2380 --listen-peer-urls http://0.0.0.0:2380 --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://10.122.120.122:2379 --initial-cluster-token etcd-cluster-1 --initial-cluster etcd0=http://10.122.120.122:2380,etcd1=http://10.122.120.122:12380,etcd2=http://10.122.120.122:22380 --initial-cluster-state new
# 启动主ETCD第一个从节点
etcd --name etcd1 --initial-advertise-peer-urls http://10.122.120.122:12380 --listen-peer-urls http://0.0.0.0:12380 --listen-client-urls http://0.0.0.0:12379 --advertise-client-urls http://10.122.120.122:12379 --initial-cluster-token etcd-cluster-1 --initial-cluster etcd0=http://10.122.120.122:2380,etcd1=http://10.122.120.122:12380,etcd2=http://10.122.120.122:22380 --initial-cluster-state new
# 启动主ETCD第二个从节点
etcd --name etcd2 --initial-advertise-peer-urls http://10.122.120.122:22380 --listen-peer-urls http://0.0.0.0:22380 --listen-client-urls http://0.0.0.0:22379 --advertise-client-urls http://10.122.120.122:22379 --initial-cluster-token etcd-cluster-1 --initial-cluster etcd0=http://10.122.120.122:2380,etcd1=http://10.122.120.122:12380,etcd2=http://10.122.120.122:22380 --initial-cluster-state new
# 查看集群成员
etcdctl member list  
a81f9775765c360, started, etcd1, http://10.122.120.122:12380, http://10.122.120.122:12379, false
4482718e78ca3fcb, started, etcd2, http://10.122.120.122:22380, http://10.122.120.122:22379, false
cd9c1c919d8babf1, started, etcd0, http://10.122.120.122:2380, http://10.122.120.122:2379, false

当主节点挂掉时:会触发选举机制
raft2020/12/11 22:55:42 INFO: raft.node: 57ba42bdda8443b7 lost leader 46b60d5e31b21815 at term 12
raft2020/12/11 22:55:42 INFO: 57ba42bdda8443b7 received MsgVoteResp from 68c82a8a88706a94 at term 12
raft2020/12/11 22:55:42 INFO: 57ba42bdda8443b7 has received 2 MsgVoteResp votes and 0 vote rejections
raft2020/12/11 22:55:42 INFO: 57ba42bdda8443b7 became leader at term 12
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
其它配置类似单机多节点.

五、ETCD身份验证

etcdctl user add root  #添加root用户并设置密码
etcdctl auth enable    #开启身份验证
etcdctl --user=root:123456 get /dir/key2   #客户端请求数据时,需要带上--user
上一篇下一篇

猜你喜欢

热点阅读