etcdクラスタの導入

7377 ワード

参照先:https://www.cnblogs.com/51wansheng/p/10234036.html

一、環境説明

  • ubuntu 18.04 172.18.0.30 (master)
  • ubuntu 18.04 172.18.0.26 (node1)
  • etcdバージョン:v 2.3.7

  • 二、etcdのインストール


    masterとnode 1マシンにetcdをそれぞれ取り付ける
    # step1:     etcd release 
    $ wget https://github.com/etcd-io/etcd/releases/download/v2.3.7/etcd-v2.3.7-linux-amd64.tar.gz
    
    # step2:      /data/    (            )
    $ tar -C /data -zxvf etcd-v2.3.7-linux-amd64.tar.gz
    
    # step3:        
    $ /data/etcd-v2.3.7-linux-amd64/etcdctl -v
    etcdctl version 2.3.7
    

    三、etcdの配置


    etcdの重要な構成パラメータの説明、不通バージョンのetcdの可能なパラメータオプションとオプションのデフォルト値は多少出入りし、etcd --helpで表示できます.
    --name										# etcd    
    --data-dir									# etcd       
    --listen-client-urls						#          url
    --advertise-client-urls				#            url
    --listen-peer-urls						#          url
    --initial-advertise-peer-urls		#              url
    --initial-cluster							#         
    --initial-cluster-token				#      
    --initial-cluster-state					#       ,new     (master new,node existing)
    

    私たちはsystemdを通じてetcdサービスを管理しているので、/etc/systemd/systemディレクトリの下にetcd.serviceファイルを新築しました.systemdを知らない人はチェン一峰先生のブログを見ることができます.

    1、master機械(172.18.0.30)

    # vim /etcsystemd/system/etcd.service
    
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
    User=root
    ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd0 --data-dir /data/etcd_data/etcd1 \
        --listen-client-urls http://172.18.0.30:2379,http://localhost:2379 --advertise-client-urls http://172.18.0.30:2379,http://localhost:2379 \
        --listen-peer-urls http://172.18.0.30:2380 --initial-advertise-peer-urls http://172.18.0.30:2380 \
        --initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480 --initial-cluster-token etcd-cluster \
        --initial-cluster-state new
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

    注意:デフォルトの23792380ポートが使用されている場合は、etcdを構成して他の使用されていないポートをリスニングできます.

    2、node 1マシン(172.18.0.26)

    # vim /etc/systemd/system/etcd.service
    
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
    User=root
    ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd1 --data-dir /data/etcd_data/etcd0 \
        --listen-client-urls http://172.18.0.26:2479,http://localhost:2479 --advertise-client-urls http://172.18.0.26:2479,http://localhost:2479 \
        --listen-peer-urls http://172.18.0.26:2480 --initial-advertise-peer-urls http://172.18.0.26:2480 \
        --initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480 --initial-cluster-token etcd-cluster \
        --initial-cluster-state existing
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

    三、etcdサービスの起動


    etcdのサービスを構成したら、masterとnode 1ノードのetcdサービスをそれぞれ起動します
    # step1:    systemd    (  systemd           )
    $ systemctl daemon-reload
    
    # step2:  etcd  
    $ systemctl restart etcd
    
    #   etcd  
    $ systmectl stop etcd
    
    #   etcd  
    $ systemctl status etcd
    
    #   etcd  
    $ journalctl -u etcd
    
    #   etcd    
    $ systemctl enable etcd
    

    四、etcdの使用

    #   etcd    (                )
    $ /data/etcd-v2.3.7-linux-amd64/etcdctl member list
    bd3b67d271b1097d: name=etcd0 peerURLs=http://172.18.0.30:2380 clientURLs=http://172.18.0.30:2379,http://localhost:2379 isLeader=true
    e5ad2a6d82697b13: name=etcd1 peerURLs=http://172.18.0.26:2480 clientURLs=http://172.18.0.26:2479,http://localhost:2479 isLeader=false
    
    #           
    $ /data/etcd-v2.3.7-linux-amd64/etcdctl cluster-health
    member bd3b67d271b1097d is healthy: got healthy result from http://172.18.0.30:2379
    member e5ad2a6d82697b13 is healthy: got healthy result from http://172.18.0.26:2479
    cluster is healthy
    
    #  master set  key-value
    $ /data/etcd-v2.3.7-linux-amd64/etcdctl set name tab609
    tab609
    
    #  node1 get  key  ( :           2380      --endpoints)
    $ /data/etcd-v2.3.7-linux-amd64/etcdctl --endpoints http://localhost:2479 get name
    tab609
    
    #   etcdctl        
    $ /data/etcd-v2.3.7-linux-amd64/etcdctl help
    

    五、etcdクラスタにノードnode 2を追加(172.18.0.17)


    1、etcdのインストール


    インストール手順と同じ

    2、etcdの配置


    Node 2ノード
    #  vim /etc/systemd/sytem/etcd.service
    
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
    User=root
    ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd2 --data-dir /data/etcd_data/etcd2 \
        --listen-client-urls http://172.18.0.17:2479,http://localhost:2479 --advertise-client-urls http://172.18.0.17:2479,http://localhost:2479 \
        --listen-peer-urls http://172.18.0.17:2480 --initial-advertise-peer-urls http://172.18.0.17:2480 \
        --initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480 --initial-cluster-token yj1918-etcd-cluster \
        --initial-cluster-state existing
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

    masterノードプロファイルのクラスタメンバーnode 2ノードを追加するには、--initial-clusterパラメータオプションを変更するだけです.
    # vim /etc/systemd/sytem/etcd.service
    
    ExecStart= ... \
    	--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480
    

    同じnode 1ノードプロファイルのクラスタメンバーもnode 2ノードを追加し、--initial-clusterパラメータオプションを変更するだけです.
    # vim /etc/systemd/sytem/etcd.service
    
    ExecStart= ... \
    	--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480
    

    3、etcdサービスの起動


    それぞれmaster、node 1、node 2で同上の起動手順を実行しますが、node 2を起動したときに起動に失敗し、次のエラーを報告します.
    error validating peerURLs {ClusterID:b0f36b8e1c8349f4 Members:[&{ID:bd3b67d271b1097d RaftAttributes:{PeerURLs:[http://172.18.0.30:2380]} Attributes:{Name:etcd0 ClientURLs:[http://172.18.0.30:2379]
    

    つまり、エンドツーエンドのrulを検証中にエラーが発生しました.グーグルは解決策を見つけました.新しいノードを起動する前に、新しいノードをクラスタに接続してから、新しいノードを起動する必要があります.
    #  master         (master    )
    $  /data/etcd-v2.3.7-linux-amd64/etcdctl member add node2 http://172.18.0.17:2480
    
    #     node2 etcd        (node2    )
    $ systemctl restart etcd
    
    #        (   node2   ,           2379      --endpoints)
    $ /data/etcd-v2.3.7-linux-amd64/etcdctl --endpoints http://172.18.0.17:2479 member list
    bd3b67d271b1097d: name=etcd0 peerURLs=http://172.18.0.30:2380 clientURLs=http://172.18.0.30:2379,http://localhost:2379 isLeader=true
    e20ddba3b692fe46: name=etcd2 peerURLs=http://172.18.0.17:2480 clientURLs=http://172.18.0.17:2479,http://localhost:2479 isLeader=false
    e5ad2a6d82697b13: name=etcd1 peerURLs=http://172.18.0.26:2480 clientURLs=http://172.18.0.26:2479,http://localhost:2479 isLeader=false
    
    

    最後に書きます:etcdはクラスタで、私たちがgetでデータを取る時、どのようにデータの一致性と信頼性を保証しますか?etcdは選挙のような方法で,過半数は規則によってデータを返す.たとえばクラスタに3台のマシンがあり,2台のマシンのkey-valueが同じ,すなわち2/3が半分を超えるとデータが返される.データの一貫性と信頼性を保証するために、etcdクラスタのマシン数は奇数で、偶数ではなく1つのノードより大きい必要があります.より詳細には、https://www.jianshu.com/p/5aed73b288f7