使用kubeadm部署k8s集羣02-配置etcd高可用

使用kubeadm部署k8s集羣02-配置etcd高可用

2018/1/4

配置 etcd 高可用

  • 新建一個 2 節點的 etcd cluster
  • 查看 etcd 的狀態
  • 遷移原來 master 節點上的 etcd 數據到上面新建的 etcd cluster 中
  • 切換 kube-apiserver 使用新的 etcd endpoint 地址
  • 清理掉原來的單節點 etcd 服務
  • 重建一個 etcd 服務,加入新集羣
  • 部署新的 etcd 節點
  • 更新另外2個節點的 etcd.yaml 配置
新建一個 2 節點的 etcd cluster
### 基於當前 master 節點 tvm-00 的 etcd 配置來修改:
[root@tvm-00 ~]# scp /etc/kubernetes/manifests/etcd.yaml 10.10.9.68:/tmp/
[root@tvm-00 ~]# scp /etc/kubernetes/manifests/etcd.yaml 10.10.9.69:/tmp/

### 修改 etcd 配置,設置成一個全新的 cluster
[root@tvm-01 ~]# cat /tmp/etcd.yaml
### (略過部分沒有改動的輸出內容)
spec:
  containers:
  - command:
    - etcd
    - --name=etcd-01
    - --initial-advertise-peer-urls=http://10.10.9.68:2380
    - --listen-peer-urls=http://10.10.9.68:2380
    - --listen-client-urls=http://127.0.0.1:2379,http://10.10.9.68:2379
    - --advertise-client-urls=http://10.10.9.68:2379
    - --initial-cluster-token=etcd-cluster
    - --initial-cluster=etcd-01=http://10.10.9.68:2380,etcd-02=http://10.10.9.69:2380
    - --initial-cluster-state=new
    - --data-dir=/var/lib/etcd
    image: gcr.io/google_containers/etcd-amd64:3.1.10
### (略過部分沒有改動的輸出內容)

[root@tvm-02 ~]# cat /tmp/etcd.yaml
### (略過部分沒有改動的輸出內容)
spec:
  containers:
  - command:
    - etcd
    - --name=etcd-02
    - --initial-advertise-peer-urls=http://10.10.9.69:2380
    - --listen-peer-urls=http://10.10.9.69:2380
    - --listen-client-urls=http://127.0.0.1:2379,http://10.10.9.69:2379
    - --advertise-client-urls=http://10.10.9.69:2379
    - --initial-cluster-token=etcd-cluster
    - --initial-cluster=etcd-01=http://10.10.9.68:2380,etcd-02=http://10.10.9.69:2380
    - --initial-cluster-state=new
    - --data-dir=/var/lib/etcd
    image: gcr.io/google_containers/etcd-amd64:3.1.10
### (略過部分沒有改動的輸出內容)

### 啓動 etcd cluster
### 配置文件同步到 manifests 後將會被 kubelet 檢測到然後自動將 pod 啓動
[root@tvm-01 ~]# rm /var/lib/etcd -fr
[root@tvm-01 ~]# cp -a /tmp/etcd.yaml /etc/kubernetes/manifests/

[root@tvm-02 ~]# rm /var/lib/etcd -fr
[root@tvm-02 ~]# cp -a /tmp/etcd.yaml /etc/kubernetes/manifests/
查看 etcd 的狀態
### 下載一個 etcdctl 工具來管理集羣:
[root@tvm-00 ~]# cd /usr/local/bin/
[root@tvm-00 ~]# wget https://github.com/coreos/etcd/releases/download/v3.1.10/etcd-v3.1.10-linux-amd64.tar.gz
[root@tvm-00 ~]# tar zxf etcd-v3.1.10-linux-amd64.tar.gz
[root@tvm-00 ~]# mv etcd-v3.1.10-linux-amd64/etcd* .
[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl --endpoints "http://10.10.9.68:2379,http://10.10.9.69:2379" endpoint status
http://10.10.9.68:2379, 21b9c7066a7e525, 3.1.10, 25 kB, true, 7, 194
http://10.10.9.69:2379, 516e519b2158e83a, 3.1.10, 25 kB, false, 7, 194

### 注意:輸出的列從左到右分別表示:endpoint URL, ID, version, database size, leadership status, raft term, and raft status.
### 符合預期。
遷移原來 master 節點上的 etcd 數據到上面新建的 etcd cluster 中
### 注意:etcdctl 3.x 版本提供了一個 make-mirror 功能來同步數據
### 在當前 master 節點 tvm-00 上執行:
[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl make-mirror --no-dest-prefix=true --endpoints=127.0.0.1:2379 --insecure-skip-tls-verify=true 10.10.9.68:2379

### 將數據同步到遠端剛纔新建的 etcd 集羣中
### 注意1:數據是從 127.0.0.1:2379 寫入到 10.10.9.68:2379
### 注意2:這個同步只能是手動中止,間隔 30s 打印一次輸出

### 通過對比集羣到狀態來判斷是否同步完成:
###(新開一個窗口)
[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl endpoint status
127.0.0.1:2379, 8e9e05c52164694d, 3.1.10, 1.9 MB, true, 2, 342021

[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl --endpoints "http://10.10.9.68:2379,http://10.10.9.69:2379" endpoint status
http://10.10.9.68:2379, 21b9c7066a7e525, 3.1.10, 1.9 MB, true, 7, 1794
http://10.10.9.69:2379, 516e519b2158e83a, 3.1.10, 1.9 MB, false, 7, 1794
切換 kube-apiserver 使用新的 etcd endpoint 地址

### 停止 kubelet 服務:
[root@tvm-00 ~]# systemctl stop kubelet

### 更新 kube-apiserver.yaml 中 etcd 服務到地址,切到我們到新集羣中:
[root@tvm-00 ~]# sed -i 's#127.0.0.1:2379#10.10.9.68:2379#' /etc/kubernetes/manifests/kube-apiserver.yaml

### 啓動 kubelet 服務:
[root@tvm-00 ~]# systemctl start kubelet
[root@tvm-00 ~]# kubectl get pods --all-namespaces |grep 'etcd-tvm'
kube-system   etcd-tvm-00                      1/1       Running   1          4h
kube-system   etcd-tvm-01                      1/1       Running   0          1h
kube-system   etcd-tvm-02                      1/1       Running   0          1h
清理掉原來的單節點 etcd 服務
[root@tvm-00 ~]# mv /etc/kubernetes/manifests/etcd.yaml /tmp/orig.master.etcd.yaml
[root@tvm-00 ~]# mv /var/lib/etcd /tmp/orig.master.etcd
### 觀察 pods 的變化:
[root@tvm-00 ~]# kubectl get pods --all-namespaces |grep 'etcd-tvm'
kube-system   etcd-tvm-01                      1/1       Running   0          1h
kube-system   etcd-tvm-02                      1/1       Running   0          1h

### 符合預期 etcd-tvm-00 停止服務
重建一個 etcd 服務,加入新集羣
[root@tvm-00 ~]# cat /tmp/etcd.yaml
### (略過部分沒有改動的輸出內容)
spec:
  containers:
  - command:
    - etcd
    - --name=etcd-00
    - --initial-advertise-peer-urls=http://10.10.9.67:2380
    - --listen-peer-urls=http://10.10.9.67:2380
    - --listen-client-urls=http://127.0.0.1:2379,http://10.10.9.67:2379
    - --advertise-client-urls=http://10.10.9.67:2379
    - --initial-cluster-token=etcd-cluster
    - --initial-cluster=etcd-00=http://10.10.9.67:2380,etcd-01=http://10.10.9.68:2380,etcd-02=http://10.10.9.69:2380
    - --initial-cluster-state=existing
    - --data-dir=/var/lib/etcd
    image: gcr.io/google_containers/etcd-amd64:3.1.10
### (略過部分沒有改動的輸出內容)

### 注意:上述新節點的配置有一個地方不一樣:
--initial-cluster-state=existing
  • 先配置 etcd cluster 增加一個 member 用於後續操作
    
    [root@tvm-00 ~]# ETCDCTL_API=3 etcdctl --endpoints="http://10.10.9.68:2379" member list
    21b9c7066a7e525, started, etcd-01, http://10.10.9.68:2380, http://10.10.9.68:2379
    516e519b2158e83a, started, etcd-02, http://10.10.9.69:2380, http://10.10.9.69:2379

[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl --endpoints="http://10.10.9.68:2379" member add etcd-00 --peer-urls=http://10.10.9.67:2380
Member 6cc2e7728adb6b28 added to cluster 3742ed98339167da

ETCD_NAME="etcd-00"
ETCD_INITIAL_CLUSTER="etcd-01=http://10.10.9.68:2380,etcd-02=http://10.10.9.69:2380,etcd-00=http://10.10.9.67:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"

[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl --endpoints="http://10.10.9.68:2379" member list
21b9c7066a7e525, started, etcd-01, http://10.10.9.68:2380, http://10.10.9.68:2379
516e519b2158e83a, started, etcd-02, http://10.10.9.69:2380, http://10.10.9.69:2379
6cc2e7728adb6b28, unstarted, , http://10.10.9.67:2380,


##### 部署新的 etcd 節點
```bash
[root@tvm-00 ~]# rm /var/lib/etcd -fr
[root@tvm-00 ~]# cp -a /tmp/etcd.yaml /etc/kubernetes/manifests/

### 再次查看 k8s cluster 信息

[root@tvm-00 ~]# kubectl get pods --all-namespaces |grep 'etcd-tvm'
kube-system   etcd-tvm-00                      1/1       Running   1          4h
kube-system   etcd-tvm-01                      1/1       Running   0          1h
kube-system   etcd-tvm-02                      1/1       Running   0          1h

### etcd 的日誌:
[root@tvm-00 ~]# kubectl logs -n kube-system --tail=20 etcd-tvm-00

### etcd clister 狀態:
[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl --endpoints="http://10.10.9.67:2379,http://10.10.9.68:2379,http://10.10.9.69:2379" member list
21b9c7066a7e525, started, etcd-01, http://10.10.9.68:2380, http://10.10.9.68:2379
516e519b2158e83a, started, etcd-02, http://10.10.9.69:2380, http://10.10.9.69:2379
6cc2e7728adb6b28, started, etcd-00, http://10.10.9.67:2380, http://10.10.9.67:2379

[root@tvm-00 ~]# ETCDCTL_API=3 etcdctl --endpoints "http://10.10.9.67:2379,http://10.10.9.68:2379,http://10.10.9.69:2379" endpoint status
http://10.10.9.67:2379, 6cc2e7728adb6b28, 3.1.10, 3.8 MB, false, 7, 5236
http://10.10.9.68:2379, 21b9c7066a7e525, 3.1.10, 3.3 MB, true, 7, 5236
http://10.10.9.69:2379, 516e519b2158e83a, 3.1.10, 3.3 MB, false, 7, 5236
更新另外2個節點的 etcd.yaml 配置
### 區別之處:

- --initial-cluster=etcd-00=http://10.10.9.67:2380,etcd-01=http://10.10.9.68:2380,etcd-02=http://10.10.9.69:2380
- --initial-cluster-state=existing

### 將節點 tvm-00 上 kube-apiserver 使用的 etcd endpoint 切換回來
[root@tvm-00 ~]# sed -i 's#10.10.9.68:2379#127.0.0.1:2379#' /etc/kubernetes/manifests/kube-apiserver.yaml
[root@tvm-00 ~]# kubectl get pods --all-namespaces |grep api
kube-system   kube-apiserver-tvm-00            1/1       Running   0          1m
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章