将 kubernetes 主节点从一台服务器迁移到另一台服务器

将 kubernetes 主节点从一台服务器迁移到另一台服务器

我有一台裸机服务器,其中包含一个主 Kubernetes 节点。我需要将主节点移至新的裸机服务器。我们如何移动或迁移它?

我已经尽我所能研究但大多数都与 GCP 集群有关,我们将 4 个目录从旧节点移动到新节点,并且还更改了 IP,而这个问题是 5 年前提出的,现在已经过时了。

/var/etcd
/srv/kubernetes
/srv/sshproxy
/srv/salt-overlay

假设我们使用的是最新的 k8s 版本 1.17,那么正确的移动方法是什么

答案1

下列的github 问题评论中提到Kubernetes 主节点中的 IP 地址变化

1. 确认您etcd data directory正在查看etcd pod in kube-system namespace

(使用 kubeadm 创建的 k8s v1.17.0 的默认值),

    volumeMounts:
    - mountPath: /var/lib/etcd
      name: etcd-data

2。 准备:

  • 复制/etc/kubernetes/pki主1新 Master2
    #create backup directory in Master2,
    mkdir ~/backup

    #copy from Master1 all key,crt files into the Master2 
    sudo scp -r /etc/kubernetes/pki  [email protected]:~/backup

    ./etcd/peer.crt
    ./apiserver.crt

    rm ~/backup/pki/{apiserver.*,etcd/peer.*}
  • 移动pki directory to /etc/kubernetes
 cp -r ~/backup/pki  /etc/kubernetes/

3。 在主1创造etcd 快照

验证您的API version

kubectl exec -it etcd-Master1 -n kube-system -- etcdctl  version

etcdctl version: 3.4.3
API version: 3.4
  • 使用当前etcd 容器
    kubectl exec -it etcd-master1 -n kube-system --  etcdctl --endpoints https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key /etc/kubernetes/pki/etcd/server.key snapshot save /var/lib/etcd/snapshot1.db
    ETCDCTL_API=3 etcdctl --endpoints https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key /etc/kubernetes/pki/etcd/server.key snapshot save /var/lib/etcd/snapshot1.db

4. 从复制创建的快照主1Master2备份目录:

scp ./snapshot1.db  [email protected]:~/backup

5。 准备Kubeadm 配置以反映主1配置

apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: x.x.x.x
  bindPort: 6443
nodeRegistration:
  name: master2
  taints: []     # Removing all taints from Master2 node.
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.17.0
networking:
  dnsDomain: cluster.local
  podSubnet: 10.0.0.0/16
  serviceSubnet: 10.96.0.0/12
scheduler: {}

6.恢复快照:

  • 使用etcd:3.4.3-0docker镜像:
    docker run --rm \
        -v $(pwd):/backup \
        -v /var/lib/etcd:/var/lib/etcd \
        --env ETCDCTL_API=3 \
        k8s.gcr.io/etcd:3.4.3-0 \
        /bin/sh -c "etcdctl snapshot restore './snapshot1.db' ; mv /default.etcd/member/ /var/lib/etcd/"
  • 或使用etcdctl二进制文件:
    ETCDCTL_API=3 etcdctl --endpoints https://127.0.0.1:2379 snapshot restore  './snapshot1.db' ; mv ./default.etcd/member/ /var/lib/etcd/

7.初始化Master2

    sudo kubeadm init --ignore-preflight-errors=DirAvailable--var-lib-etcd --config kubeadm-config.yaml
    # kubeadm-config.yaml prepared in 5 step.
  • 注意:

[WARNING DirAvailable--var-lib-etcd]: /var/lib/etcd is not empty
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master2 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 master2_IP]
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master2 localhost] and IPs [master2_ip 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master2 localhost] and IPs [master2_ip 127.0.0.1 ::1]
.
.
.  
  Your Kubernetes control-plane has initialized successfully!
      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • 经过k8s对象验证(简短示例):
    kubectl get nodes
    kubectl get pods -o wide
    kubectl get pods -n kube-system -o wide
    systemctl status kubelet 
  • 如果所有已部署的 k8s 对象(如 pod、部署等)都已移至新的Master2节点:
    kubectl drain Master1
    kubectl delete node Master1

笔记:

此外,请考虑创建高可用性集群在这种设置中,您应该有可能拥有多个主节点,在这种配置中,您可以以更安全的方式创建/删除额外的控制平面节点。

相关内容