ETCD部署及解释
一、部署环境
主机账号 | 主机密码 |
---|---|
root | XXXXXXXXX |
二、资源配置
|主机名|IP地址|CPU(核心)|内存(GB)|系统硬盘(GB)|挂载硬盘(GB)|
|-|-|-|-|-|-|-|
|etcd-k8s-master-1|192.168.40.140|2|4|20|100|
|etcd-k8s-master-2|192.168.40.141|2|4|20|100|
|etcd-k8s-master-3|192.168.40.142|2|4|20|100|
三、安装、启动etcd
3.1 安装ansible
# 安装ansible
yum install ansible -y
# 配置ansible hosts文件
vim /etc/ansible/hosts
[etcd]
192.168.40.140
192.168.40.141
192.168.40.142
[all:vars]
ansible_ssh_user="root"
ansible_ssh_pass="XXXXXXXXX"
# 编写分发文件
vim ssh-key.yml
- hosts: all
become: yes
tasks:
- name: Auth
authorized_key:
user: root
key: "{{lookup('file','~/.ssh/id_rsa.pub')}}"
#生成ssh-key文件
ssh-keygen
3.2 编辑分发hosts文件
# 编辑hosts文件
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.40.140 etcd-1 etcd-k8s-master-1
192.168.40.141 etcd-2 etcd-k8s-master-2
192.168.40.142 etcd-3 etcd-k8s-master-3
# 分发hosts文件
ansible all -m copy -a "src=/etc/hosts dest=/etc/hosts"
3.2 安装golang
# 下载golang并创建对应目录
cd /opt
wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz
tar xvf go1.13.4.linux-amd64.tar.gz go1.13.4.linux-amd64.tar.gz
mkdir -P /opt/gopath/{src,bin,pkg}
# 配置golang环境变量
vim /etc/profile.d/go.sh
export GOROOT=/opt/go
export GOPATH=/opt/gopath
export PATH=$PATH:$GOROOT/bin
# 刷新环境变量
. /etc/profile.d/go.sh
3.2 编译etcd与etcdctl代码生成二进制文件与分发
3.2.1 编译生成与分发etcd
go get github.com/etcd-io/etcd
cd /opt/gopath/src/go.etcd.io/etcd
go build
ansible all -m copy -a "src=./etcd dest=/bin/etcd mode=u+rwx,g+rx,o+rx"
3.2.2 编译生成与分发etcd
cd /opt/gopath/src/go.etcd.io/etcd/etcdctl
go build
ansible all -m copy -a "src=./etcdctl dest=/bin/etcdctl mode=u+rwx,g+rx,o+rx"
此处如果发生报错:<font color=red>
build go.etcd.io/etcd/etcdctl: cannot load github.com/bgentry/speakeasy: github.com/bgentry/speakeasy@v0.1.0: Get https://proxy.golang.org/github.com/bgentry/speakeasy/@v/v0.1.0.zip: dial tcp 216.58.200.49:443: i/o timeout
</font>
执行:
git clone https://github.com/bgentry/speakeasy.git /opt/gopath/src/github.com/
3.3 etcd启动项
etcd节点1
nohup etcd --name etcd-1 \
--initial-advertise-peer-urls 'http://192.168.40.140:2380' \
--listen-peer-urls 'http://192.168.40.140:2380' \
--advertise-client-urls 'http://192.168.40.140:2379' \
--listen-client-urls 'http://192.168.40.140:2379' \
--initial-cluster-token 'etcd-1' \
--initial-cluster etcd-1=http://192.168.40.140:2380,etcd-2=http://192.168.40.141:2380,etcd-3=http://192.168.40.142:2380 \
--data-dir /opt/${HOSTNAME}.etcd \
--initial-cluster-state 'new'&
etcd节点2
nohup etcd --name etcd-2 \
--initial-advertise-peer-urls 'http://192.168.40.141:2380' \
--listen-peer-urls 'http://192.168.40.141:2380' \
--advertise-client-urls 'http://192.168.40.141:2379' \
--listen-client-urls 'http://192.168.40.141:2379' \
--initial-cluster-token 'etcd-1' \
--initial-cluster etcd-1=http://192.168.40.140:2380,etcd-2=http://192.168.40.141:2380,etcd-3=http://192.168.40.142:2380 \
--data-dir /opt/${HOSTNAME}.etcd \
--initial-cluster-state 'new'&
etcd节点3
nohup etcd --name etcd-3 \
--initial-advertise-peer-urls 'http://192.168.40.142:2380' \
--listen-peer-urls 'http://192.168.40.142:2380' \
--advertise-client-urls 'http://192.168.40.142:2379' \
--listen-client-urls 'http://192.168.40.142:2379' \
--initial-cluster-token 'etcd-1' \
--initial-cluster etcd-1=http://192.168.40.140:2380,etcd-2=http://192.168.40.141:2380,etcd-3=http://192.168.40.142:2380 \
--data-dir /opt/${HOSTNAME}.etcd \
--initial-cluster-state 'new'&
3.4 etcd启动项信息
-
--name
:etcd在集群中的节点名称,在同一个集群中必须是唯一的 -
--initial-advertise-peer-urls
:建议用于集群内部几点之间的交互的URL地址,几点见讲义该值进行通信 -
--listen-peer-urls
:用于集群内各个节点之间通信的URL地址,每个节点可以监听多个url地址,集群内部将通过这些URL地址进行数据交互,例如,leader几点的选举、Message消息的传输或者快照的传输等。 -
--advertise-client-urls
:建议客户端使用的RUL地址,该值用于etcd代理或etcd成员与etcd几点通信 -
--listen-client-urls
:用于当前地址与客户端交互的URL地址,每个几点可以向客户端提供多个URL地址 -
--initial-cluster-token
:集群的唯一标识 -
--initial-cluster
:集群中所有的initial-advertise-peer-urls -
--data-dic
:数据的存放位置,${name}该节点的名字 -
--initial-cluster-state new
:新集群的标识
测试集群否正常
[root@etcd-k8s-master-1 ~]# etcdctl --endpoints=192.168.40.140:2379,192.168.40.141:2379,192.168.40.142:2379 endpoint health
192.168.40.140:2379 is healthy: successfully committed proposal: took = 2.268118ms
192.168.40.141:2379 is healthy: successfully committed proposal: took = 2.396264ms
192.168.40.142:2379 is healthy: successfully committed proposal: took = 3.042977ms
[root@etcd-k8s-master-1 ~]# etcdctl --write-out=table --endpoints=192.168.40.140:2379,192.168.40.141:2379,192.168.40.142:2379 endpoint status
+---------------------+------------------+-----------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+---------------------+------------------+-----------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 192.168.40.140:2379 | 1f5cb9a09297a825 | 3.5.0-pre | 20 kB | false | false | 9 | 9 | 9 | |
| 192.168.40.141:2379 | 8a5428646f2d78bf | 3.5.0-pre | 20 kB | true | false | 9 | 9 | 9 | |
| 192.168.40.142:2379 | f16ab1eb94b31b0d | 3.5.0-pre | 16 kB | false | false | 9 | 9 | 9 | |
+---------------------+------------------+-----------+---------+-----------+------------+-----------+------------+--------------------+--------+
有疑问加站长微信联系(非本文作者)