Kubernetes/部署文档/Kubeasz/Kubeasz部署Kubernetes集群.md
offends 7a2f41e7d6
All checks were successful
continuous-integration/drone Build is passing
synchronization
2024-08-07 18:54:39 +08:00

4.9 KiB
Raw Blame History

本文作者:丁辉

Kubeasz部署Kubernetes集群

Github官方文档

Github安装文档

节点名称 IP Kubernetes角色
k8s-master-1,Kubeasz管理 192.168.1.10 master,etcd,node
k8s-master-2 192.168.1.20 master,etcd,node
k8s-master-3 192.168.1.30 master,etcd,node

环境准备

  1. 配置免密

    ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -q
    
  2. 配置免密登录

    ssh-copy-id root@192.168.1.10
    ssh-copy-id root@192.168.1.20
    ssh-copy-id root@192.168.1.30
    
  3. 更改主机名

    • 192.168.1.10

      hostnamectl set-hostname k8s-master-1 && bash
      
    • 192.168.1.20

      hostnamectl set-hostname k8s-master-2 && bash
      
    • 192.168.1.30

      hostnamectl set-hostname k8s-master-3 && bash
      
  4. 编辑 /etc/hosts 文件

    vi /etc/hosts
    

    添加如下内容

    192.168.1.10 k8s-master-1
    192.168.1.20 k8s-master-2
    192.168.1.30 k8s-master-3
    

开始部署 Kubeasz

Github Releases

  1. 下载 ezdown

    wget https://github.com/easzlab/kubeasz/releases/download/3.6.2/ezdown && chmod 777 ezdown
    

    下载支持 Docker 的版本

    wget https://github.com/easzlab/kubeasz/releases/download/3.2.0/ezdown && chmod 777 ezdown
    
  2. 下载kubeasz代码、二进制、默认容器镜像

    • 国内环境

      ./ezdown -D
      

      下载 flannel 镜像

      ./ezdown -X flannel
      
    • 海外环境

      ./ezdown -D -m standard
      

      下载 flannel 镜像

      ./ezdown -D -m standard -X flannel
      
  3. 运行 Kubeasz

    ./ezdown -S
    

    配置快捷命令

    echo "alias dk='docker exec -it kubeasz'" >> ~/.bashrc
    source ~/.bashrc
    

    配置后执行 dk 即可

  4. 创建新集群

    dk ezctl new k8s-01
    
  5. 编辑 hosts 文件

    vi /etc/kubeasz/clusters/k8s-01/hosts
    

    内容如下

    # 'etcd' cluster should have odd member(s) (1,3,5,...)
    [etcd]
    192.168.1.10
    192.168.1.20
    192.168.1.30
    
    # master node(s)
    [kube_master]
    192.168.1.10 k8s_nodename='k8s-master-1'
    192.168.1.20 k8s_nodename='k8s-master-2'
    192.168.1.30 k8s_nodename='k8s-master-3'
    
    # work node(s)
    [kube_node]
    192.168.1.10 k8s_nodename='k8s-master-1'
    192.168.1.20 k8s_nodename='k8s-master-2'
    192.168.1.30 k8s_nodename='k8s-master-3'
    
    # VIP
    [ex_lb]
    192.168.1.10 LB_ROLE=master EX_APISERVER_VIP=192.168.1.100 EX_APISERVER_PORT=8443
    192.168.1.20 LB_ROLE=backup EX_APISERVER_VIP=192.168.1.100 EX_APISERVER_PORT=8443
    192.168.1.30 LB_ROLE=backup EX_APISERVER_VIP=192.168.1.100 EX_APISERVER_PORT=8443
    
    ...#此处省略
    # Cluster container-runtime supported: docker, containerd
    CONTAINER_RUNTIME="containerd"
    
    # Network plugins supported: calico, flannel, kube-router, cilium, kube-ovn
    CLUSTER_NETWORK="flannel"
    
    ...#此处省略
    
  6. 修改 config.yml 文件

    vi /etc/kubeasz/clusters/k8s-01/config.yml
    

    内容如下

    ############################
    # role:kube-master
    ############################
    # k8s 集群 master 节点证书配置可以添加多个ip和域名比如增加公网ip和域名尽量预留一些IP使用
    MASTER_CERT_HOSTS:
      - "192.168.1.10"
      - "192.168.1.20"
      - "192.168.1.30"
      - "192.168.1.100"
    

开始安装 Kubernetes

  1. 安装

    dk ezctl setup k8s-01 all
    

    或一步一步安装

    # 初始化集群配置
    dk ezctl setup k8s-01 01
    # 安装 etcd
    dk ezctl setup k8s-01 02
    ...#此处省略
    
  2. 加载新配置

    source ~/.bashrc
    
  3. 查看节点状态

    kubectl get node
    

添加节点

官方文档

  • 添加 worker

    dk ezctl add-node k8s-01 192.168.1.40 k8s_nodename='k8s-master-4'
    

    SSH 非 22 端口

    dk ezctl add-node k8s-01 192.168.1.40 k8s_nodename='k8s-master-4' ansible_ssh_port=10022
    
  • 添加 master

    dk ezctl add-master k8s-01 192.168.1.40 k8s_nodename='k8s-master-4'
    
  • 添加 etcd

    dk ezctl add-etcd k8s-01 192.168.1.40
    

删除节点

  • 删除 worker

    dk ezctl del-node k8s-01 192.168.1.40
    
  • 删除 master

    dk ezctl del-master k8s-01 192.168.1.40
    
  • 删除 etcd

    dk ezctl del-etcd k8s-01 192.168.1.40