0

etcd proxy 配置和使用

 1 year ago
source link: https://blog.kelu.org/tech/2022/10/26/etcd-proxy.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
etcd.jpg

本文大部分转自:etcd proxy功能简介, 原文科普简单,没有涉及证书的内容,不太适用于在企业使用。我在末尾加了证书相关的内容和命令。

一、etcd proxy概述

etcd提供了proxy功能,即代理功能,etcd可以代理的方式来运行。

etcd代理可以运行在每一台主机,在这种代理模式下,etcd的作用就是一个反向代理,把客户端的etcd请求转发到真正的etcd集群。这种方式既加强了集群的弹性,又不会降低集群的写的性能。

etcd proxy支持2种运行模式:readwrite和readonly,缺省的是readwrite,即proxy会将所有的读写请求都转发给etcd集群;readonly模式下,只转发读请求,写请求将会返回http 501错误。

二、etcd proxy参数

启动etcd代理主要有3个参数:proxy、listen-client-urls、initial-cluster(或discovery)。

proxy指的是代理的模式,on是readwrite模式,readonly是只读模式。

listen-client-urls指的是代理的监听地址。

initial-cluster(或discovery)指的是请求将转发到在此url发现的etcd集群。discovery指的是使用服务发现方式搭建的集群url。

启动命令示例:

./etcd –proxy on –listen-client-urls http://0.0.0.0:22379  –initial-cluster myetcd1=http://0.0.0.0:12380 –data-dir /home/cmp/temp/proxy

三、注意事项

1、proxy只支持API v2,不支持v3;

2、在服务发现集群模式下,多启动的etcd节点将会自动降级成读写模式的代理;

3、代理不会自动变成etcd集群节点,如要加入集群需要手工进行如下操作:

etcd add命令将proxy节点加入集群、停止proxy进程或服务、删除proxy数据目录、使用正确的参数配置重新启动etcd进程或服务。

四、代理实例操作演示

1、先启动一个简单etcd集群

./etcd –name myetcd1 –listen-client-urls http://0.0.0.0:12379 –advertise-client-urls http://0.0.0.0:12379 –listen-peer-urls http://0.0.0.0:12380 –initial-advertise-peer-urls http://0.0.0.0:12380 –initial-cluster myetcd1=http://0.0.0.0:12380 –data-dir /home/cmp/temp/myetcd1

2、再启动一个proxy节点

./etcd –proxy on –listen-client-urls http://0.0.0.0:22379 –initial-cluster myetcd1=http://0.0.0.0:12380 –data-dir /home/cmp/temp/proxy &

3、查看etcd集群信息

./etcdctl –endpoints http://0.0.0.0:12379 member list
eb06dc1bc141ff4f: name=myetcd1 peerURLs=http://0.0.0.0:12380 clientURLs=http://0.0.0.0:12379 isLeader=true

etcd集群中只有一个leader成员。

4、通过代理进行key-value操作

./etcdctl –endpoints http://127.0.0.1:22379 set key1 xxxxx
xxxxx

./etcdctl –endpoints http://127.0.0.1:22379 get key1
xxxxx

API v2命令可以成功通过代理读写数据。

ETCDCTL_API=3 ./etcdctl –endpoints=http://127.0.0.1:12379 put key2 sssssssssss
OK

ETCDCTL_API=3 ./etcdctl –endpoints=http://127.0.0.1:22379 get key2
Error: context deadline exceeded

直接访问集群时,API v3命令成功将key值写入,但是通过proxy读取时,命令直接报错。

5. 是用ssl证书的相关内容

我使用systemd进行管理,配置如下:

# /etc/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
Documentation=https://github.com/coreos

[Service]
Type=notify
WorkingDirectory=/var/local/etcd/
User=root
ExecStart=/usr/bin/etcd \
  --proxy=on \
  --peer-cert-file=/var/local/etcd/ssl/peer.pem  \
  --peer-key-file=/var/local/etcd/ssl/peer-key.pem  \
  --peer-trusted-ca-file=/var/local/etcd/ssl/ca.pem  \
  --listen-client-urls http://0.0.0.0:2379  \
  --initial-cluster node1=https://xxx:2380,node2=https://xxx:2380,node3=https://xxx:2380 \
  --data-dir=/var/local/etcd/etcddata
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

要注意的是,我监听的端口2379并不包含 ssl 证书,使用的 peer 证书是连接到etcd-server所需的证书。

启动输出如下:

image-20221028午前114126830

客户端连接命令如下:

etcdctl --ca-file=/var/local/etcd/ssl/ca.pem --cert-file=/var/local/etcd/ssl/peer.pem --key-file=/var/local/etcd/ssl/peer-key.pem --endpoints=http://:2379  get /

需要注意的是,我在客户端中使用的是http,而不是https。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK