本文使用官方etcd镜像生成一个容器,并进行功能测试。
一、镜像获取
请见上文:构建后端为etcd的CoreDNS的容器集群(二)、下载最新的etcd容器镜像
二、查看镜像
bash
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
quay.io/coreos/etcd v3.5.16 8523cb381f23 5 weeks ago 59MB
三、创建容器
为便于检查创建参数并进行保存,建议通过脚本进行创建,先进行单容器创建测试
bash
[root@localhost etcd]# vi etcd_docker_run.sh
docker run -d \
-p 2379:2379 \
-p 2380:2380 \
--name etcd-1 \
quay.io/coreos/etcd:v3.5.16 \
/usr/local/bin/etcd \
--name etcd-1 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://0.0.0.0:2380 \
--initial-cluster etcd-1=http://0.0.0.0:2380 \
--initial-cluster-token tkn \
--initial-cluster-state new \
[root@localhost etcd]# sh etcd_docker_run.sh
849f5cc7098a4d64a25aa624bac6d248e8a2ce475994ed233cf8e11b665df4d5
[root@localhost etcd]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
849f5cc7098a quay.io/coreos/etcd:v3.5.16 "/usr/local/bin/etcd..." 7 seconds ago Up 5 seconds 0.0.0.0:2379-2380->2379-2380/tcp etcd-1
四、程序版本检查
bash
[root@localhost etcd]# docker exec etcd-1 /usr/local/bin/etcd --version
etcd Version: 3.5.16
Git SHA: f20bbad
Go Version: go1.22.7
Go OS/Arch: linux/amd64
[root@localhost etcd]# docker exec etcd-1 /usr/local/bin/etcdctl version
etcdctl version: 3.5.16
API version: 3.5
[root@localhost etcd]# docker exec etcd-1 /usr/local/bin/etcdutl version
etcdutl version: 3.5.16
API version: 3.5
五、API健康检查
bash
[root@localhost etcd]# docker exec etcd-1 /usr/local/bin/etcdctl endpoint health
127.0.0.1:2379 is healthy: successfully committed proposal: took = 3.414086ms
六、容器内数据存取测试
bash
[root@localhost etcd]# docker exec etcd-1 /usr/local/bin/etcdctl put foo bar
OK
[root@localhost etcd]# docker exec etcd-1 /usr/local/bin/etcdctl get foo
foo
bar
以上测试了"foo":"bar"键值对,可见数据存取正常
七、容器外接口测试
将容器内的etcdctl程序拷贝到本地,进行接口测试
bash
[root@localhost etcd]# docker cp etcd-1:/usr/local/bin/etcdctl .
[root@localhost etcd]# ./etcdctl --endpoints=http://192.168.80.135:2379 version
etcdctl version: 3.5.16
API version: 3.5
八、容器外数据存取测试
bash
[root@localhost etcd]# ./etcdctl --endpoints=http://192.168.80.135:2379 get foo
foo
bar
[root@localhost etcd]# ./etcdctl --endpoints=http://192.168.80.135:2379 put www.sina.com.cn 192.168.8.8
[root@localhost etcd]# ./etcdctl --endpoints=http://192.168.80.135:2379 get www.sina.com.cn
www.sina.com.cn
192.168.8.8
测试新增"www.sina.com.cn":"192.168.8.8"键值对,存取正常。