coreos etcd 部署过程

1. 打包升级go至最新版
下载go源码包,https://golang.org/dl/

# wget https://storage.googleapis.com/golang/go1.4.2.src.tar.gz

解决debug造成打包失败,依赖造成安装失败,在rpmbuild spec文件开始添加

%define debug_package %{nil}
%define __strip /bin/true
%define __requires_exclude_from ^(%{_datadir}|/usr/lib)/%{name}/(doc|src)/.*$
%define _use_internal_dependency_generator 0
%define __find_requires %{nil}
%define __spec_install_post /usr/lib/rpm/check-rpaths   /usr/lib/rpm/check-buildroot /usr/lib/rpm/brp-compress

编译 build

%build
export GOOS=linux  
export GOARCH=amd64  
export GOROOT=%{_builddir}/%{_name}-%{version}  
export GOBIN=$GOROOT/bin  
export GOPATH=%{_builddir}  
export GOROOT_FINAL=%{_libdir}/%{_name}

pushd src/  
bash make.bash  
popd  

安装 install

%install
export GOROOT=%{_builddir}/%{_name}-%{version}  
export GOBIN=$GOROOT/bin

mkdir -p %{buildroot}/%{_prefix} %{buildroot}/%{_libdir}/go  
cp -a bin %{buildroot}/%{_prefix}  
cp -a pkg %{buildroot}/%{_libdir}/go  
cp -a $GOROOT/src %{buildroot}/%{_libdir}/go  
cp -a $GOROOT/lib %{buildroot}/%{_libdir}/go  
cp -a $GOROOT/include %{buildroot}/%{_libdir}/go

find %{buildroot}/%{_libdir}/go/src -type f -name '*.[ao]' -delete  

2. 编译打包etcd
github下载etcd源码包

# git clone https://github.com/coreos/etcd.git

编译 build

%build
export GOROOT=%{_libdir}/go  
export GOPATH=%{_builddir}/%{_name}-%{version}

go get github.com/coreos/etcd/etcdmain  
#go get github.com/coreos/etcd/migrate/starter
#go get github.com/coreos/etcd/pkg/coreos
go build  
./build

3. 部署cluster
测试节点: 10.13.32.23, 10.13.32.92
分别执行初始化启动命令

  • 10.13.32.23
# etcd -name hathor23 -initial-advertise-peer-urls http://10.13.32.23:2380 -listen-peer-urls http://10.13.32.23:2380 -initial-cluster-token etcd-cluster-test -initial-cluster hathor23=http://10.13.32.23:2380,hathor92=http://10.13.32.92:2380 -initial-cluster-state new
  • 10.13.32.92
# etcd -name hathor92 -initial-advertise-peer-urls http://10.13.32.92:2380 -listen-peer-urls http://10.13.32.92:2380 -initial-cluster-token etcd-cluster-test -initial-cluster hathor23=http://10.13.32.23:2380,hathor92=http://10.13.32.92:2380 -initial-cluster-state new

初始化之后重新启动

  • 10.13.32.23
# etcd -name hathor23 -listen-peer-urls http://10.13.32.23:2380  -listen-client-urls http://10.13.32.23:2379,http://127.0.0.1:4001 -advertise-client-urls http://10.13.32.23:2379
  • 10.13.32.92
# etcd -name hathor92 -listen-peer-urls http://10.13.32.92:2380  -listen-client-urls http://10.13.32.92:2379,http://127.0.0.1:4001 -advertise-client-urls http://10.13.32.92:2379

查看集群成员,任选一个节点

# etcdctl member list
3b88eca0e5239c13: name=hathor23 peerURLs=http://10.13.32.23:2380 clientURLs=http://10.13.32.23:2379  
7f8ec6ffc556b7ec: name=hathor92 peerURLs=http://10.13.32.92:2380 clientURLs=http://10.13.32.92:2379  

或通过http接口

# curl http://10.13.32.92:2379/v2/members -s | python -m json.tool 
{
    "members": [
        {
            "clientURLs": [
                "http://10.13.32.23:2379"
            ], 
            "id": "3b88eca0e5239c13", 
            "name": "hathor23", 
            "peerURLs": [
                "http://10.13.32.23:2380"
            ]
        }, 
        {
            "clientURLs": [
                "http://10.13.32.92:2379"
            ], 
            "id": "7f8ec6ffc556b7ec", 
            "name": "hathor92", 
            "peerURLs": [
                "http://10.13.32.92:2380"
            ]
        }
    ]
}

etcd 参数说明
-initial 开头的参数在初始化启动时有效,初始化后重启会被忽略

-name
成员名称

-initial-advertise-peer-urls
通知给集群内其他成员的节点url列表,用于互相传输数据

-listen-peer-urls
监听的url列表,用于节点间传输

-initial-cluster-token
集群token

-initial-cluster
集群配置,所有成员的节点url列表

-initial-cluster-state
集群状态,初始化启动时用"new",加入现有集群用"exsiting"

-listen-client-urls
客户端监听url列表,用于客户端读写操作

-advertise-client-urls
通知给集群内其他成员的客户端url列表