Etcd是coreos开发的一个kv的软件,我使用他做存储数据,比如持久化固定ip,就是把固定的docker容器ip信息存放到etcd里,智能配置docker防火墙的信息也是存放到etcd里,这个软件很小型,并且支持集群,很是方便,现在给大家介绍一下如何在centos6系统里安装此软件。
先简单介绍一下etcd,从网上复制的
etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现。 etcd的特性如下: 简单: curl可访问的用户的API(HTTP+JSON) 安全: 可选的SSL客户端证书认证 快速: 单实例每秒 1000 次写操作 可靠: 使用Raft保证一致性
安装环境
系统:centos6.6
etcd版本0.4.6
虽然有最新的版本,但我一直使用这个版本,比较稳定
下面是安装过程
1、下载
curl -L https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz -o etcd-v0.4.6-linux-amd64.tar.gz
2、解压
tar xzvf etcd-v0.4.6-linux-amd64.tar.gz cd etcd-v0.4.6-linux-amd64
3、复制程序到系统环境
cp etcd /usr/local/bin cp etcdctl /usr/local/bin
4、设置运行脚本,绝对路径是/etc/init.d/etcd
#!/bin/bash # Copyright 2013, Nathan Milford # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # /etc/init.d/etcd # # Startup script for etcd # # chkconfig: 2345 20 80 # description: Starts and stops etcd . /etc/init.d/functions prog="etcd" prog_bin="/usr/local/bin/$prog" desc="etcd shared configuration and service discovery daemon" if ! [ -f $prog_bin ]; then echo "$prog binary not found." exit 5 fi if [ -f /etc/sysconfig/$prog ]; then . /etc/sysconfig/$prog else echo "No sysconfig file found in /etc/sysconfig/$prog... exiting." exit 5 fi start() { echo "Starting $desc ($prog): " su $ETCD_USER -c "nohup $prog_bin $ETCD_OPTS >>$ETCD_OUT_FILE 2>&1 &" RETVAL=$? return $RETVAL } stop() { echo "Shutting down $desc ($prog): " pkill -f $prog_bin } restart() { stop start } status() { if [ -z $pid ]; then pid=$(pgrep -f $prog_bin) fi if [ -z $pid ]; then echo "$prog is NOT running." return 1 else echo "$prog is running (pid is $pid)." fi } case "$1" in start) start;; stop) stop;; restart) restart;; status) status;; *) echo "Usage: $0 {start|stop|restart|status}" RETVAL=2;; esac exit $RETVAL
5、设置etcd软件环境配置,根据自己的实际环境修改
09:21:19 # cat /etc/sysconfig/etcd #!/bin/bash # Copyright 2014, Nathan Milford # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. lib_dir='/var/lib/etcd/' # Will be used to populate IP address values below. Setting some items to # '0.0.0.0' is not compatable with the discovery API. #_MY_IPADDR=$(/sbin/ifconfig | grep 'inet'| grep -Ev '(127|117|172|::1|fe)' |awk '{print $2}'|head -n 1) _MY_IPADDR=10.10.29.236 # Daemon User #ETCD_USER="etcd" # Cluster Seeds # You can specify a list here sepearated by commas, or leave it blank if # you're playing with a single node. ETCD_SEEDS="" # Discovery Endpoint # Leave it as the public URL unless you are running your own. ETCD_DISCOVER_ENDPOINT="https://discovery.etcd.io/" # Discovery Token # If you are using the discovery protocol you can grab your cluster token # from https://discovery.etcd.io/new if you are not hosting it yourself. ETCD_DISCOVERY_TOKEN="cb5940a807d44287e05f29f3170883e3" # This node's name as it represents itself on the cluster. ETCD_NODE_NAME=$(hostname -s) # Hostname and port for the etcd server to work on. ETCD_LISTEN="$_MY_IPADDR:4001" # Directory to store log and snapshot. ETCD_DATA_DIR="/var/lib/etcd/" # File to log stdout/stderr to. ETCD_OUT_FILE="/var/log/etcd/etcd.log" # Set logging vebosity for the file above. # Valid options are "", "v" or "vv" ETCD_LOGGING="" # Max size of the cluster. ETCD_MAXSIZE=9 # Max size of result buffer. ETCD_MAXRESULT=1024 # Number of retries to attempt while joining a cluster ETCD_RETRIES=3 # Set security settings for the etcd server. # Leave blank if you do not plan to use this feature, otherwise add appropriate # paths. ETCD_CAFILE="" ETCD_CERT="" ETCD_KEY="" # Toggles snapshotting. # Keep blank or set to true. ETCD_SNAPSHOT="" # Hostname and port for the RAFT server to work on. RAFT_LISTEN="$_MY_IPADDR:7001" # Set security settings for the RAFT server. # Leave blank if you do not plan to use this feature, otherwise add appropriate # paths. RAFT_CAFILE="" RAFT_CERT="" RAFT_KEY="" # Below we build the opts to pass to the init script. ETCD_OPTS="-name=${ETCD_NODE_NAME} \ -addr=${ETCD_LISTEN} \ -peer-addr=${RAFT_LISTEN} \ -data-dir=${ETCD_DATA_DIR} \ -max-result-buffer=${ETCD_MAXRESULT} \ -max-cluster-size=${ETCD_MAXSIZE} \ -max-retry-attempts=${ETCD_RETRIES}" if [ x$ETCD_SEEDS != "x" ]; then ETCD_OPTS="$ETCD_OPTS -peers=${ETCD_SEEDS}" fi if [ x$ETCD_DISCOVERY_TOKEN != "x" ]; then ETCD_OPTS="$ETCD_OPTS -discovery=${ETCD_DISCOVER_ENDPOINT}${ETCD_DISCOVERY_TOKEN}" fi if [ "$ETCD_LOGGING" == "v" ]; then ETCD_OPTS="$ETCD_OPTS -v" elif [ "$ETCD_LOGGING" == "vv" ]; then ETCD_OPTS="$ETCD_OPTS -vv" fi if [ x$ETCD_SNAPSHOT != "x" ]; then ETCD_OPTS="$ETCD_OPTS -snapshot" fi if [ ! -d $lib_dir ];then mkdir -p $lib_dir fi #if [ x$ETCD_CAFILE != "x" ] && [ x$ETCD_CERT != "x" ] && [ x$ETCD_KEY != "x" ]; then # ETCD_OPTS="$ETCD_OPTS -ca-file=${ETCD_CAFILE} -cert-file=${ETCD_CERT} -key-file=${ETCD_KEY}" #fi #if [ x$RAFT_CAFILE != "x" ] && [ x$RAFT_CERT != "x" ] && [ x$RAFT_KEY != "x" ]; then # ETCD_OPTS="$ETCD_OPTS -peer-ca-file=${RAFT_CAFILE} -peer-cert-file=${RAFT_CERT} -peer-key-file=${RAFT_KEY}" #fi #echo $ETCD_OPTS # TODO # Add support for: # -peers-file # -config # -cors # -cpuprofile
如果不想加入集群,就把ETCD_DISCOVER_ENDPOINT与ETCD_DISCOVERY_TOKEN给注释了,另外_MY_IPADDR是本机的ip
6、创建用户
useradd etcd -M -s /sbin/nologin
7、创建日志与数据存放目录
mkdir /var/log/etcd mkdir /var/lib/etcd
8、授予权限
chown -R etcd:etcd /var/log/etcd chown -R etcd:etcd /var/lib/etcd
9、启动
/etcd/init.d/etcd start
10、设置开机启动
chkconfig --level 345 etcd on
11、查看运行情况
查看集群主机ip
12:22:48 # curl -L http://127.0.0.1:4001/v2/machines http://10.10.21.199:4001, http://10.10.27.221:4001, http://10.10.17.3:4001, http://10.10.17.4:4001, http://10.10.29.236:4001
查看集群主机列表(详细)
12:23:07 # curl -L http://127.0.0.1:4001/v2/keys/_etcd/machines {"action":"get","node":{"key":"/_etcd/machines","dir":true,"nodes":[{"key":"/_etcd/machines/docker-test1","value":"etcd=http%3A%2F%2F10.10.21.199%3A4001\u0026raft=http%3A%2F%2F10.10.21.199%3A7001","modifiedIndex":1,"createdIndex":1},{"key":"/_etcd/machines/docker-test2","value":"etcd=http%3A%2F%2F10.10.27.221%3A4001\u0026raft=http%3A%2F%2F10.10.27.221%3A7001","modifiedIndex":9,"createdIndex":9},{"key":"/_etcd/machines/docker-test3","value":"etcd=http%3A%2F%2F10.10.17.3%3A4001\u0026raft=http%3A%2F%2F10.10.17.3%3A7001","modifiedIndex":16,"createdIndex":16},{"key":"/_etcd/machines/ip-10-10-17-4","value":"etcd=http%3A%2F%2F10.10.17.4%3A4001\u0026raft=http%3A%2F%2F10.10.17.4%3A7001","modifiedIndex":177304,"createdIndex":177304},{"key":"/_etcd/machines/ip-10-10-29-236","value":"etcd=http%3A%2F%2F10.10.29.236%3A4001\u0026raft=http%3A%2F%2F10.10.29.236%3A7001","modifiedIndex":2101510,"createdIndex":2101510}],"modifiedIndex":1,"createdIndex":1}}
查看集群header
12:23:15 # curl -L http://127.0.0.1:4001/v2/leader http://10.10.17.4:7001
详细请参考https://github.com/coreos/etcd