ETCD 单机安装

本文内容纲要:

- 一、部署单机etcd

- 二、接口测试

- 三、问题

由于测试的需要,有时需要搭建一个单机版的etcd 环境,为了方便以后搭建查看,现在对单机部署进行记录。

一、部署单机etcd

  1. 下载 指定版本的etcd

    下载地址

    ftp://ftp.pbone.net/mirror/archive.fedoraproject.org/fedora/linux/updates/22/x86_64/e/etcd-2.2.5-1.fc22.x86_64.rpm

    先判断是否已经安装了etcd,已经安装的话就需要先删除掉,注意权限问题

    [vagrant@localhost wae]$ yum list installed | grep etcd

    etcd.x86_64 2.2.0-1.el7 installed

    [vagrant@localhost wae]$ yum remove etcd.x86_64

    Loaded plugins: fastestmirror

    You need to be root to perform this command.

    [vagrant@localhost wae]$ sudo yum remove etcd.x86_64

    Loaded plugins: fastestmirror

    Resolving Dependencies

    --> Running transaction check

    ---> Package etcd.x86_64 0:2.2.0-1.el7 will be erased

    --> Finished Dependency Resolution

    Dependencies Resolved

    ===================================================================================================

    Package Arch Version Repository Size

    ===================================================================================================

    Removing:

    etcd x86_64 2.2.0-1.el7 installed 26 M

    Transaction Summary

    ===================================================================================================

    Remove 1 Package

    Installed size: 26 M

    Is this ok [y/N]: y

    Downloading packages:

    Running transaction check

    Running transaction test

    Transaction test succeeded

    Running transaction

    Erasing : etcd-2.2.0-1.el7.x86_64 1/1

    warning: /etc/etcd/etcd.conf saved as /etc/etcd/etcd.conf.rpmsave

    Verifying : etcd-2.2.0-1.el7.x86_64 1/1

    Removed:

    etcd.x86_64 0:2.2.0-1.el7

    Complete!

    安装下载的rpm包

    [vagrant@localhost etcd]$ sudo yum localinstall etcd-2.2.5-1.fc22.x86_64.rpm

    验证安装成功

    [vagrant@localhost etcd]$ etcdctl --version

    etcdctl version 2.2.5

  2. 创建一个服务描述文件,放入systemd的服务目录下,一般安装完成都会自动创建

    [vagrant@localhost etcd]$ sudo vi /usr/lib/systemd/system/etcd.service

    [Unit]

    Description=Etcd Server

    After=network.target

    After=network-online.target

    Wants=network-online.target

    [Service]

    Type=notify

    WorkingDirectory=/var/lib/etcd/

    EnvironmentFile=-/etc/etcd/etcd.conf

    User=etcd

    # set GOMAXPROCS to number of processors

    ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\""

    Restart=on-failure

    LimitNOFILE=65536

    [Install]

    WantedBy=multi-user.target

  3. 修改etcd的配置

    一般默认访问的地址是http://localhost:2379,这边我们可以自定义,因为是单机,我们就直接127.0.0.1,有想法的就端口修改下好了

    [vagrant@localhost etcd]$ sudo vi /etc/etcd/etcd.conf

    # [member]

    ETCD_NAME=default

    ETCD_DATA_DIR="/var/lib/etcd/default.etcd"

    #ETCD_WAL_DIR=""

    #ETCD_SNAPSHOT_COUNT="10000"

    #ETCD_HEARTBEAT_INTERVAL="100"

    #ETCD_ELECTION_TIMEOUT="1000"

    #ETCD_LISTEN_PEER_URLS="http://localhost:2380"

    ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:4001"

    #ETCD_MAX_SNAPSHOTS="5"

    #ETCD_MAX_WALS="5"

    #ETCD_CORS=""

    #

    #[cluster]

    #ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"

    # if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."

    #ETCD_INITIAL_CLUSTER="default=http://localhost:2380"

    #ETCD_INITIAL_CLUSTER_STATE="new"

    #ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"

    ETCD_ADVERTISE_CLIENT_URLS="http://127.0.0.1:4001"

    #ETCD_DISCOVERY=""

    #ETCD_DISCOVERY_SRV=""

    #ETCD_DISCOVERY_FALLBACK="proxy"

    #ETCD_DISCOVERY_PROXY=""

    #

    #[proxy]

    #ETCD_PROXY="off"

    #ETCD_PROXY_FAILURE_WAIT="5000"

    #ETCD_PROXY_REFRESH_INTERVAL="30000"

    #ETCD_PROXY_DIAL_TIMEOUT="1000"

    #ETCD_PROXY_WRITE_TIMEOUT="5000"

    #ETCD_PROXY_READ_TIMEOUT="0"

    #

    #[security]

    #ETCD_CERT_FILE=""

    #ETCD_KEY_FILE=""

    #ETCD_CLIENT_CERT_AUTH="false"

    #ETCD_TRUSTED_CA_FILE=""

    #ETCD_PEER_CERT_FILE=""

    #ETCD_PEER_KEY_FILE=""

    #ETCD_PEER_CLIENT_CERT_AUTH="false"

    #ETCD_PEER_TRUSTED_CA_FILE=""

    #

    #[logging]

    #ETCD_DEBUG="false"

    # examples for -log-package-levels etcdserver=WARNING,security=DEBUG

    #ETCD_LOG_PACKAGE_LEVELS=""

    如果想要局域网可以访问的话,则将 127.0.0.1 换成宿主机的 ip 即可了,最后外部用 ip 进行接口访问即可。

  4. 启动并测试ETCD

    首先设置开机启动:systemctl enable etcd

    [vagrant@localhost etcd]$ sudo systemctl daemon-reload

    [vagrant@localhost etcd]$ sudo systemctl start etcd

    [vagrant@localhost etcd]$ sudo systemctl status etcd

    ● etcd.service - Etcd Server

    Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)

    Active: active (running) since Fri 2018-03-09 08:55:47 UTC; 8s ago

    Main PID: 4624 (etcd)

    Memory: 26.0M

    CGroup: /system.slice/etcd.service

    └─4624 /usr/bin/etcd --name=default --data-dir=/var/lib/etcd/default.etcd --listen-cl...

    Mar 09 08:55:46 localhost.localdomain systemd[1]: Starting Etcd Server...

    Mar 09 08:55:47 localhost.localdomain systemd[1]: Started Etcd Server.

    查看集群健康状态

    [vagrant@localhost etcd]$ sudo etcdctl cluster-health

    member ce2a822cea30bfca is healthy: got healthy result from http://127.0.0.1:4001

    cluster is healthy

    ok,没有问题

  5. 卸载与删除etcd

    [vagrant@localhost wae]$ yum list installed | grep etcd

    etcd.x86_64 2.2.0-1.el7 installed

    [vagrant@localhost wae]$ yum remove etcd.x86_64

二、接口测试

etcd的数据形式是以健对值方式存在的。

  1. 通过接口获取版本信息

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/version -XGET    

    {"etcdserver":"2.2.5","etcdcluster":"2.2.0"}

  2. 更新一个健对值信息,并通过 ke y获取到 value

    [vagrant@localhost etcd]$ etcdctl set /tmp/message wozainali

    wozainali

    [vagrant@localhost etcd]$ etcdctl get /tmp/message

    wozainali

    设置的方式:etcdctl set key value

    获取的方式:etcdctl get key

  3. 通过接口获取健对值信息

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XGET

    {"action":"get","node":{"key":"/tmp/message","value":"wozainali","modifiedIndex":12,"createdIndex":12}}

    再看看 详细的请求信息

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XGET

    {"action":"get","node":{"key":"/tmp/message","value":"wozainali","modifiedIndex":12,"createdIndex":12}}

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XGET -vv

    * About to connect() to 127.0.0.1 port 4001 (#0)

    * Trying 127.0.0.1...

    * Connected to 127.0.0.1 (127.0.0.1) port 4001 (#0)

    > GET /v2/keys/tmp/message HTTP/1.1

    > User-Agent: curl/7.29.0

    > Host: 127.0.0.1:4001

    > Accept: */*

    >

    < HTTP/1.1 200 OK

    < Content-Type: application/json

    < X-Etcd-Cluster-Id: 7e27652122e8b2ae

    < X-Etcd-Index: 12

    < X-Raft-Index: 37173

    < X-Raft-Term: 3

    < Date: Fri, 09 Mar 2018 09:16:15 GMT

    < Content-Length: 104

    <

    {"action":"get","node":{"key":"/tmp/message","value":"wozainali","modifiedIndex":12,"createdIndex":12}}

    * Connection #0 to host 127.0.0.1 left intact

  4. 通过接口更新一个健对值信息

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XPUT -d value=nihaoha   

    {"action":"set","node":{"key":"/tmp/message","value":"nihaoha","modifiedIndex":13,"createdIndex":13},"prevNode":{"key":"/tmp/message","value":"wozainali","modifiedIndex":12,"createdIndex":12}}

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XGET

    {"action":"get","node":{"key":"/tmp/message","value":"nihaoha","modifiedIndex":13,"createdIndex":13}}

    我们看到更新的时候,会有两条记录,一条是当前记录,一条是之前的记录,多次更新也只会保留最近的信息,获取的时候默认获取最新的信息

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XPUT -d value=wowowow

    {"action":"set","node":{"key":"/tmp/message","value":"wowowow","modifiedIndex":14,"createdIndex":14},"prevNode":{"key":"/tmp/message","value":"nihaoha","modifiedIndex":13,"createdIndex":13}}

    具体复杂的操作需要去看官方文档,这边只做了解使用。https://coreos.com/etcd/docs/latest/v2/api.html

  5. 通过接口删除健对值信息

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XDELETE

    {"action":"delete","node":{"key":"/tmp/message","modifiedIndex":15,"createdIndex":14},"prevNode":{"key":"/tmp/message","value":"wowowow","modifiedIndex":14,"createdIndex":14}}

    [vagrant@localhost etcd]$ curl http://127.0.0.1:4001/v2/keys/tmp/message -XGET

    {"errorCode":100,"message":"Key not found","cause":"/tmp/message","index":15}

    可以看到删除之后,重新去获取该 key 的值,提示Key not found,证明我们删除成功了。

三、问题

  1. 在修改访问ip问局域网ip之后发现,使用 etcdctl 进行操作的时候会出现如下错误

    [vagrant@localhost etcd]$ etcdctl ls

    Error: dial tcp 127.0.0.1:4001: getsockopt: connection refused

    [vagrant@localhost etcd]$

    [vagrant@localhost etcd]$

    [vagrant@localhost etcd]$ etcdctl cluster-health

    cluster may be unhealthy: failed to list members

    Error: client: etcd cluster is unavailable or misconfigured

    error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused

    error #1: dial tcp 127.0.0.1:2379: getsockopt: connection refused

    查询之后,发现应该是 ETCD_LISTEN_CLIENT_URLS 没有写入 http://127.0.0.1:4001 的原因,追加配置 http://127.0.0.1:4001,重新试一下

    [vagrant@localhost etcd]$ etcdctl ls

    /test

    /test123

    /tmp

    /test1223

    [vagrant@localhost etcd]$ etcdctl cluster-health

    member ce2a822cea30bfca is healthy: got healthy result from http://192.168.10.50:4001

    cluster is healthy

    [vagrant@localhost etcd]$

    现在就可以了。

    注意,添加配置的时候,用英文的逗号隔开.

本文内容总结:一、部署单机etcd,二、接口测试,三、问题,

原文链接:https://www.cnblogs.com/cxbhakim/p/8535054.html

以上是 ETCD 单机安装 的全部内容, 来源链接: utcz.com/z/296992.html

回到顶部