$ echo$GOPATH# /usr/local/go/path go get -v go.etcd.io/etcd go get -v go.etcd.io/etcd/etcdctl
Step4.测试安装通过启动etcd并设置密钥,检查etcd二进制文件是否正确构建。
1 2 3 4 5 6
./usr/local/go/path/bin/etcd {"level":"warn","ts":"2020-04-23T15:12:17.368+0800","caller":"etcdmain/etcd.go:89","msg":"'data-dir' was empty; using default","data-dir":"default.etcd"} {"level":"info","ts":"2020-04-23T15:12:17.368+0800","caller":"embed/etcd.go:113","msg":"configuring peer listeners","listen-peer-urls":["http://localhost:2380"]} {"level":"info","ts":"2020-04-23T15:12:17.990+0800","caller":"membership/cluster.go:524","msg":"set initial cluster version","cluster-id":"cdf818194e3a8c32","local-member-id":"8e9e05c52164694d","cluster-version":"3.5"} {"level":"info","ts":"2020-04-23T15:12:17.990+0800","caller":"etcdserver/server.go:1850","msg":"published local member to cluster through raft","local-member-id":"8e9e05c52164694d","local-member-attributes":"{Name:default ClientURLs:[http://localhost:2379]}","request-path":"/0/members/8e9e05c52164694d/attributes","cluster-id":"cdf818194e3a8c32","publish-timeout":"7s"} {"level":"info","ts":"2020-04-23T15:12:17.991+0800","caller":"embed/serve.go:139","msg":"serving client traffic insecurely; this is strongly discouraged!","address":"127.0.0.1:2379"}
Step5.put一个关键key-value进行测试
1 2 3 4 5 6 7 8
#如果OK被打印,那么etcd正在工作 [root@initiator bin]# /usr/local/go/bin/etcdctl put name WeiyiGeek OK [root@initiator bin]# /usr/local/go/bin/etcdctl get name name WeiyiGeek [root@node3 ~]# etcdctl --endpoints=$ENDPOINTS --write-out="json" get name {"header":{"cluster_id":2819294416482393232,"member_id":17704130064291257467,"revision":7300,"raft_term":301},"kvs":[{"key":"bmFtZQ==","create_revision":7300,"mod_revision":7300,"version":1,"value":"V2VpeWlHZWVr"}],"count":1}
#写入|替换Key etcdctl --endpoints=$ENDPOINTS put name "WeiyiGeek" #读取Key etcdctl --endpoints=$ENDPOINTS get name #删除Key etcdctl --endpoints=$ENDPOINTS del name
#debug 查看 $etcdctl --endpoints=$ENDPOINTS --debug get --from-key '\0' ETCDCTL_CACERT= ETCDCTL_CERT= ETCDCTL_COMMAND_TIMEOUT=5s ETCDCTL_DEBUG=true ETCDCTL_DIAL_TIMEOUT=2s ETCDCTL_DISCOVERY_SRV= ETCDCTL_DISCOVERY_SRV_NAME= ETCDCTL_ENDPOINTS=[192.168.10.241:2379,192.168.10.242:2379,192.168.10.243:2379] ETCDCTL_HEX=false ETCDCTL_INSECURE_DISCOVERY=true ETCDCTL_INSECURE_SKIP_TLS_VERIFY=false ETCDCTL_INSECURE_TRANSPORT=true ETCDCTL_KEEPALIVE_TIME=2s ETCDCTL_KEEPALIVE_TIMEOUT=6s ETCDCTL_KEY= ETCDCTL_PASSWORD= ETCDCTL_USER= ETCDCTL_WRITE_OUT=simple WARNING: 2020/04/26 00:18:51 Adjusting keepalive ping interval to minimum period of 10s WARNING: 2020/04/26 00:18:51 Adjusting keepalive ping interval to minimum period of 10s INFO: 2020/04/26 00:18:51 parsed scheme: "endpoint" INFO: 2020/04/26 00:18:51 ccResolverWrapper: sending new addresses to cc: [{192.168.10.241:2379 0 <nil>} {192.168.10.242:2379 0 <nil>} {192.168.10.243:2379 0 <nil>}]
Step5.Watch进行监听我们在etcd集群中的操作
1 2 3 4
#写入 v #读取 x #删除 v etcdctl --endpoints=$ENDPOINTS watch [key]
Step6.集群状态查看
1 2 3 4 5 6 7 8 9 10 11
etcdctl --endpoints=$ENDPOINTS --write-out=table member list etcdctl --endpoints=$ENDPOINTS --write-out=table endpoint status etcdctl --endpoints=$ENDPOINTS --write-out=table endpoint health etcdctl --endpoints=$ENDPOINTS -w table endpoint status #--write-out简写-w +--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | +--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | 192.168.10.241:2379 | ef8199869f22c2b7 | 3.4.7 | 20 kB | true | false | 301 | 8 | 8 | | | 192.168.10.242:2379 | cbd80ba26fce8c16 | 3.4.7 | 20 kB | false | false | 301 | 8 | 8 | | | 192.168.10.243:2379 | f5b1b47e3364dc7b | 3.4.7 | 20 kB | false | false | 301 | 9 | 9 | | +--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
Step7.权限与认证设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
export ETCDCTL_API=3 #权限赋予 etcdctl --endpoints=${ENDPOINTS} role add root etcdctl --endpoints=${ENDPOINTS} role grant-permission root readwrite foo etcdctl --endpoints=${ENDPOINTS} role get root
#角色赋予 etcdctl --endpoints=${ENDPOINTS} user add root etcdctl --endpoints=${ENDPOINTS} user grant-role root root etcdctl --endpoints=${ENDPOINTS} user get root
etcdctl --endpoints=${ENDPOINTS} auth enable # now all client requests go through aute|现在所有的客户端请求都经过验证
etcdctl --endpoints=${ENDPOINTS} --user=root:123 put foo bar etcdctl --endpoints=${ENDPOINTS} get foo etcdctl --endpoints=${ENDPOINTS} --user=root:123 get foo
funccampaign(c *clientv3.Client, election string, prop string) { for { //gets the leased session for a client(获取客户端租用的会话) s, err := concurrency.NewSession(c, concurrency.WithTTL(15)) if err != nil { log.Println(err) continue } //returns a new election on a given key prefix(返回对给定键前缀的新选择) e := concurrency.NewElection(s, election) ctx := context.TODO()
//Campaign puts a value as eligible for the election on the prefix key. //Multiple sessions can participate in the election for the same prefix, |多届会议可参加同一前缀的选举, //but only one can be the leader at a time 但是一次只能有一个领导者 if err = e.Campaign(ctx, prop); err != nil { log.Println(err) continue } log.Println("elect: success")
#(1)校验成员状态以及操作 $etcdctl member list member add #Adds a member into the cluster member list #Lists all members in the cluster member promote #Promotes a non-voting member in the cluster member remove #Removes a member from the cluster member update #Updates a member in the cluster
#(2)检查etcd集群的状态性能 $etcdctl --endpoints=$ENDPOINTS check perf #60 / 60 Booooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo! 100.00% 1m0s etcdctl --endpoints=$ENDPOINTS endpoint health etcdctl --endpoints=$ENDPOINTS endpoint status
#(4)用户相关 user add #Adds a new user user delete #Deletes a user user get #Gets detailed information of a user user grant-role #Grants a role to a user user list #Lists all users user passwd #Changes password of user user revoke-role #Revokes a role from a user
#(5)角色权限相关 role add #Adds a new role role delete #Deletes a role role get #Gets detailed information of a role role grant-permission #Grants a key to a role role list #Lists all roles role revoke-permission #Revokes a key from a role
问题1.Go下载etcd时候报错unrecognized import path - install error 错误信息:import path does not begin with hostname 解决办法:设置GOROOT环境变量或者删除执行unset GOROOT
问题2.采用systemctl管理etcd会触发以下类似报错“etcd: conflicting environment variable “ETCD_NAME” is shadowed by corresponding command-line flag (either unset environment variable or disable flag)” 原因:ETCD3.4版本会自动读取环境变量的参数,所以EnvironmentFile文件中有的参数,不需要再次在ExecStart启动参数中添加二选一即可解决(但是需要注意官网启动参数是否有旧参数被替代)
问题3.出现类似提示无法获取某个节点健康状态的提示 问题描述:
1 2 3 4 5
$/opt/etcd/bin/etcdctl --ca-file=/opt/etcd/ssl/ca.pem --cert-file=/opt/etcd/ssl/server.pem --key-file=/opt/etcd/ssl/server-key.pem --endpoints=$ENDPOINTS cluster-health member 11babd38de9e1f0f is healthy: got healthy result from https://10.0.52.13:2379 failed to check the health of member 22436a037c5adb3b on https://10.0.52.14:2379: Get https://10.0.52.14:2379/health: dial tcp 10.0.52.14:2379: i/o timeout member 22436a037c5adb3b is unreachable: [https://10.0.52.14:2379] are all unreachable member a5e80429e983b681 is healthy: got healthy result from https://10.0.52.6:2379
解决方式:各个主机应该关闭firewalld服务;
问题4.request cluster ID mismatch (got 4fb7ed98f0f6d1a7 want 4c0b05dc1b530742) 问题描述:
1 2 3 4 5 6 7 8
$journalctl -u etcd -f -- Logs begin at Thu 2019-05-23 14:29:05 CST. -- May 23 15:59:09 k8s.master etcd[13366]: request sent was ignored (cluster ID mismatch: peer[102b996c4aa7e55a]=4fb7ed98f0f6d1a7, local=4c0b05dc1b530742) May 23 15:59:09 k8s.master etcd[13366]: request cluster ID mismatch (got 4fb7ed98f0f6d1a7 want 4c0b05dc1b530742) May 23 15:59:09 k8s.master etcd[13366]: request sent was ignored (cluster ID mismatch: peer[102b996c4aa7e55a]=4fb7ed98f0f6d1a7, local=4c0b05dc1b530742) May 23 15:59:09 k8s.master etcd[13366]: request cluster ID mismatch (got 4fb7ed98f0f6d1a7 want 4c0b05dc1b530742) May 23 15:59:09 k8s.master etcd[13366]: request cluster ID mismatch (got 4fb7ed98f0f6d1a7 want 4c0b05dc1b530742) May 23 15:59:09 k8s.master etcd[13366]: request cluster ID mismatch (got 4fb7ed98f0f6d1a7 want 4c0b05dc1b530742)
openssl x509 -in etcd.pem -text -noout | grep "X509v3 Subject Alternative Name" -A 1 # X509v3 Subject Alternative Name: # DNS:etcd1, DNS:etcd2, DNS:etcd3, IP Address:127.0.0.1, IP Address:10.10.107.223, IP Address:10.10.107.224, IP Address:10.10.107.225
方式1.请访问本博主的B站【WeiyiGeek】首页关注UP主, 将自动随机获取解锁验证码。
Method 2.Please visit 【My Twitter】. There is an article verification code in the homepage.
方式3.扫一扫下方二维码,关注本站官方公众号
回复:验证码
将获取解锁(有效期7天)本站所有技术文章哟!