封尘网

让学习成为一种习惯!

Centos7.7 安装Etcd集群

ETCD 是一个高可用的分布式键值数据库,可用于服务发现。ETCD 采用 raft 一致性算法,基于 Go 语言实现。

本次安装etcd集群是为了方便下次安装Kubernetes v1.16做准备的,由于资源有限以接下来的实验都将会在这三台机器上实现。

系统规划

 

192.168.18.71	c7-node1      master节点
192.168.18.72	c7-node2      node节点
192.168.18.73	c7-node3      node节点

 

机器配置

IP地址CPU内存
192.168.18.718核28G
192.168.18.7212核32G
192.168.18.7312核64G

硬件资源有限将就一下就好了。

 

 

系统版本

[root@c7-node1 yum.repos.d]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

[root@c7-node1 yum.repos.d]# uname -r
3.10.0-1062.el7.x86_64

 

安装依赖包

yum install -y net-tools conntrack-tools wget vim  ntpdate libseccomp libtool-ltdl lrzsz yum-utils ntp

 

关闭 Selinux

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

 

开放防火墙端口

firewall-cmd --add-port={2379,2380}/tcp --permanent
firewall-cmd --reload

 

配置hosts文件解析

#此处作为Master节点
cat >>/etc/hosts<<EOF
192.168.18.71   c7-node1
192.168.18.72   c7-node2
192.168.18.73   c7-node3
EOF

配置系统的文件描述符打开数量

echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
echo "* soft nproc 65536"  >> /etc/security/limits.conf
echo "* hard nproc 65536"  >> /etc/security/limits.conf
echo "* soft  memlock  unlimited"  >> /etc/security/limits.conf
echo "* hard memlock  unlimited"  >> /etc/security/limits.conf

 

配置时间同步

启动自动同步时间:
timedatectl set-ntp yes  #此处可用yes,no,1或0
  
配置时区:
timedatectl set-timezone Asia/Shanghai

 

系统免密认证

ssh-copy-id c7-node1
ssh-copy-id c7-node2
ssh-copy-id c7-node3

 

安装Etcd集群环境

由于只有三台机器所以每台机器上都安装etcd, 本次通过二进制包安装,以下操作在每个需要etcd服务的机器上都要执行。

本文记录时etcd最新的版本为:v3.4.1

  • 下载最新版本的二进制包

    curl -s https://api.github.com/repos/etcd-io/etcd/releases/latest \
      | grep browser_download_url \
      | grep linux-amd64 \
      | cut -d '"' -f 4 \
      | wget -qi -
    
    • 如果要下载指定的版本可以这样操作

      curl -s https://api.github.com/repos/etcd-io/etcd/releases \
        | grep v3.4.1 \
        | grep browser_download_url \
        | grep linux-amd64 \
        | cut -d '"' -f 4 \
        | wget -qi -
      
  • 解压安装

    tar xf etcd-v3.4.1-linux-amd64.tar.gz
    cd etcd-v3.4.1-linux-amd64
    cp etcd etcdctl /usr/bin/
    

     

  • 添加户用和组

    groupadd --system etcd
    useradd -s /sbin/nologin --system -g etcd etcd
    

     

  • 添加目录并授权

    mkdir -p /var/lib/etcd/
    chown -R etcd:etcd /var/lib/etcd/
    

     

  • 添加几个环境变量

    INT_NAME="ens192"   #注意这里,每个机器的网卡号不同
    ETCD_HOST_IP=$(ip addr show $INT_NAME | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
    ETCD_NAME=$(hostname -s)
    

     

  • 添加启动服务etcd.service

    cat <<EOF | sudo tee /etc/systemd/system/etcd.service
    [Unit]
    Description=etcd service
    Documentation=https://github.com/etcd-io/etcd
    
    [Service]
    Type=notify
    User=etcd
    ExecStart=/usr/bin/etcd \\
      --name ${ETCD_NAME} \\
      --data-dir=/var/lib/etcd \\
      --initial-advertise-peer-urls http://${ETCD_HOST_IP}:2380 \\
      --listen-peer-urls http://${ETCD_HOST_IP}:2380 \\
      --listen-client-urls http://${ETCD_HOST_IP}:2379,http://127.0.0.1:2379 \\
      --advertise-client-urls http://${ETCD_HOST_IP}:2379 \\
      --initial-cluster-token etcd-cluster-0 \\
      --initial-cluster c7-node1=http://c7-node1:2380,c7-node2=http://c7-node2:2380,c7-node3=http://c7-node3:2380 \\
      --initial-cluster-state new \
    
    [Install]
    WantedBy=multi-user.target
    EOF
    

     

  • 启动服务

    systemctl enable etcd
    systemctl start etcd
    

     

几个常用的命令

 

  • 检查集群中的成员列表。

    etcdctl member list
    
  • 通过API方式查看版本信息

    curl -L "-w \n" http://127.0.0.1:2379/version
    

     

  • 通过快照方式备份etcd

    etcdctl snapshot save snapshot.db
    

 

  • 还原集群[需要每个成员都执行相同操作]

    etcdctl snapshot restore snapshot.db
    

     

  • 查看备份文件中的信息

    etcdctl --write-out=table snapshot status snapshot.db
    

     

  • 碎片整理[服务运行时]

    • 服务运行中操作

      etcdctl defrag --cluster
      

       

    • 服务未运行时操作[数据目录]

      etcdctl defrag --data-dir <path-to-etcd-data-dir>
      

       

 

 

  • 查看节点信息etcdctl endpoint

    • 查看单个节点的信息

      • 健康信息

        etcdctl endpoint health
        
      • 状态信息

        etcdctl endpoint status
        
      • Hash历史记录

        etcdctl endpoint hashkv
        
    • 查看集群的信息

      etcdctl endpoint --cluster=true status
      etcdctl endpoint --cluster=true health
      etcdctl endpoint --cluster=true hashkv
      

       

    • 以表格形式输出[直观]

      etcdctl --write-out=table --cluster=true endpoint status
      
  • 写操作[put]

    etcdctl put foo python
    
  • 读操作[get]

    • 直接获取,会返回Key,和Value

      etcdctl get foo
      
      #指定输出格式[fields,json,table,simple,protobuf],默认simple
      etcdctl -w fields get foo
      
    • 输出指定前缀

      etcdctl get foo --prefix
      
    • 限制返回数量[limit]返回2条记录

      etcdctl get --prefix --limit=2 foo
      

       

    • 查询当前集群中所有的数据

      etcdctl get --from-key ""
      
      

       

 

  • 删除[del]

    etcdctl del foo
    
    • 删除以[foo]前缀的Key

      etcdctl del foo --prefix
      

 

  • 监听指定前缀的key变化[watch]

    etcdctl watch foo --prefix
    

     

 

  • 租约[lease]

    • 创建一个租约300秒[过期后会自动删除]

      [root@c7-node1 ~]# etcdctl lease grant 300
      lease 44c26d7b2af0b125 granted with TTL(300s)
      
    • 查看集群中的租约列表[list]

      etcdctl lease list
      
    • 查看指定租约的过期时间[timetolive]

      [root@c7-node1 ~]# etcdctl lease timetolive 44c26d7b2af0b125
      lease 44c26d7b2af0b125 granted with TTL(300s), remaining(194s)
      
    • 删除指定的租约[revoke]

      etcdctl lease revoke [租约的ID]
      
    • 续约[keep-alive]

      etcdctl lease keep-alive [租约的ID]
      
    • 绑定一个Key在租约上,租约到期Key就会自动删除

      etcdctl put foo val2 --lease=694d5765fc71500b
      

 

  • 用户权限操作

    角色操作

    • 查看角色列表

      etcdctl role list
      
    • 添加角色[角色没有密码;它仅定义了一组新的访问权限]

      etcdctl role add test
      
    • 给角色添加权[只有readwrite时才同时有读写操作]

      • write :写操作

        etcdctl role grant-permission test write /foo
        
      • read :读操作

        etcdctl role grant-permission test read /foo
        
      • readwrite读写操作

        etcdctl role grant-permission test readwrite /foo
        
      • 对指定前缀的Key授权

        etcdctl role grant-permission test --prefix=true read /foo/
        

       

    • 删除角色

      etcdctl delete [角色名称]
      

       

    • 移除角色中的授权

      etcdctl role revoke-permission test /foo
      

       

    用户操作

    • 查看用户列表[list]

      etcdctl user list
      
    • 添加用户[会要求输入密码]

      etcdctl user add swper
      
    • 查看用户的权限

      etcdctl user get swper
      
    • 授权用户角色权限[swper为用户名,test为角色名]

      etcdctl user grant-role swper test
      
    • 移除用户权限

      etcdctl user revoke-role swper test
      
    • 修改密码

      etcdctl user passwd swper [ 新密码]
      

       

     

     

    启用身份验证

    • 创建root管理员用户

      etcdctl user add root
      
    • 当创建完上面的管理员帐号后再启动认证,不然没有权限

      etcdctl auth enable
      #当执行上面命令时就会自动创建一个role组为root的角色权限
      

       

    • 启动验证后需要带上认证才可以获取信息

      etcdctl --user root --password abcd123 get foo
      
      #也可以这样写:
      etcdctl get foo --user root:abcd123
      

       

禁用验证

  • 关闭验证需要管理员帐号

    etcdctl --user root:abcd123 auth disable
    

     

备注

本次实验中Etcd服务将不使用身份验证。

提醒:本文最后更新于 1446 天前,文中所描述的信息可能已发生改变,请谨慎使用。