美文网首页
Kubeadm、Kubelet 和 Kubectl 安装部署

Kubeadm、Kubelet 和 Kubectl 安装部署

作者: 不同而大同 | 来源:发表于2022-01-15 16:19 被阅读0次

    kubeadm是官方社区推出的一个用于快速部署 kubernetes 集群的工具,这个工具能通过两条指令完成一个kubernetes集群的部署;

    Kubernetes部署环境准备(每台同样操作)

    1. 关闭防火墙
    systemctl stop firewalld
    systemctl disable firewalld
    
    1. 关闭selinux
    #永久
    sed -i 's/enforcing/disabled/' /etc/selinux/config  
    #临时
    setenforce 0  
    
    1. 关闭swap(k8s禁止虚拟内存以提高性能)
    #永久
    sed -ri 's/.*swap.*/#&/' /etc/fstab 
    #临时
    swapoff -a
    
    1. 在master添加hosts
    cat >> /etc/hosts << EOF
    192.168.116.103 k8smaster
    192.168.116.104 k8snode
    EOF
    
    1. 设置网桥参数
    cat > /etc/sysctl.d/k8s.conf << EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    sysctl --system  #生效
    
    1. 时间同步
    yum install ntpdate -y
    ntpdate time.windows.com
    

    Kubernetes安装具体步骤

    1、创建一个Master节点:
    kubeadm init
    2、将Node节点加入到Master集群中:
    $ kubeadm join <Master节点的IP和端口>

    安装 Docker(每台同样操作)

    1. 安装Docker

    1. 下载docker源,安装docker v20.10.12
      先卸载docker旧版本
    systemctl stop docker
     yum list installed |grep docker
    rpm -qa |grep docker
    yum -y remove docker-ce-18.06.1.ce-3.el7.x86_64
    # 再次查看
    rpm -qa |grep docker
    #删除docker文件夹
    rm -rf /var/lib/docker
    
    yum install wget -y
    wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
    yum install docker-ce-20.10.12 -y
    
    1. 开机启动和启动docker
      systemctl enable docker.service
      systemctl start docker

    2. 配置加速器加速下载
      vi /etc/docker/daemon.json

    {
    "registry-mirrors": ["https://hdi5v8p1.mirror.aliyuncs.com/"]
    }
    

    安装 kubeadm、kubelet 和 kubectl

    1. 新增Kubernets阿里云YUM源
    cat > /etc/yum.repos.d/kubernetes.repo << EOF
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    enabled=1
    gpgcheck=0
    repo_gpgcheck=0
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    
    1. 安装 kubeadm 1.23.1-0 版本
      yum install kubeadm-1.23.1-0 kubectl-1.23.1-0 kubelet-1.23.1-0 -y
    2. 开机启动
      systemctl enable kubelet.service
    3. 查看有没有安装:
    yum list installed | grep kubelet
    yum list installed | grep kubeadm
    yum list installed | grep kubectl
    

    查看版本
    kubelet --version
    Kubernetes v1.23.1

    Kubelet:运行在cluster所有节点上,负责启动POD和容器;
    Kubeadm:用于初始化cluster的一个工具;
    Kubectl:kubectl是kubenetes命令行工具,通过kubectl可以部署和管理应用,查看各种资源,创建,删除和更新组件;

    重启centos服务器
    reboot

    部署Kubernetes Master主节点

    1. Master 初始化
    kubeadm init --apiserver-advertise-address=192.168.116.103 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.1 --service-cidr=10.66.0.0/18 --pod-network-cidr=10.88.0.0/20
    

    报错,

    kubeadmin init 报错
    两台服务器执行 kubeadm reset 再执行上面 kubeadm init命令

    查看出错日志 journalctl -xefu kubelet

    # -xefu说明
    -x --catalog             Add message explanations where available
    -e --pager-end           Immediately jump to the end in the pager
    -f --follow              Follow the journal
    -u --unit=UNIT           Show logs from the specified unit
    

    在万不得已操作 忽略错误 --ignore-preflight-errors=all
    kubeadm init --apiserver-advertise-address=192.168.116.103 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.1 --service-cidr=10.66.0.0/18 --pod-network-cidr=10.88.0.0/20 --ignore-preflight-errors=all

    1. master机器上执行
      kubeadm成功后显示下面提示
    Your Kubernetes control-plane has initialized successfully!
    
    To start using your cluster, you need to run the following as a regular user:
    # 执行下面三行命令
      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
    Alternatively, if you are the root user, you can run:
    # 执行下面一行命令
      export KUBECONFIG=/etc/kubernetes/admin.conf
    
    You should now deploy a pod network to the cluster.
    Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
      https://kubernetes.io/docs/concepts/cluster-administration/addons/
    #执行下面代码
    kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
    
    Then you can join any number of worker nodes by running the following on each as root:
    # 其他nodes加入到master,执行下面代码
    kubeadm join 192.168.116.103:6443 --token iobdu9.nzpsqo600hfukwk8 \
        --discovery-token-ca-cert-hash sha256:61a43240406a53fcbda25158be98cee216aa32bca051f48c480f2c245cd9f154
    

    查看是否成功下载相关镜像

    [root@k8smaster ~]# docker images
    REPOSITORY                                                        TAG       IMAGE ID       CREATED        SIZE
    rancher/mirrored-flannelcni-flannel                               v0.16.1   404fc3ab6749   12 days ago    69.9MB
    registry.aliyuncs.com/google_containers/kube-apiserver            v1.23.1   b6d7abedde39   4 weeks ago    135MB
    registry.aliyuncs.com/google_containers/kube-proxy                v1.23.1   b46c42588d51   4 weeks ago    112MB
    registry.aliyuncs.com/google_containers/kube-controller-manager   v1.23.1   f51846a4fd28   4 weeks ago    125MB
    registry.aliyuncs.com/google_containers/kube-scheduler            v1.23.1   71d575efe628   4 weeks ago    53.5MB
    registry.aliyuncs.com/google_containers/etcd                      3.5.1-0   25f8c7f3da61   2 months ago   293MB
    rancher/mirrored-flannelcni-flannel-cni-plugin                    v1.0.0    cd5235cd7dc2   2 months ago   9.03MB
    registry.aliyuncs.com/google_containers/coredns                   v1.8.6    a4ca41631cc7   3 months ago   46.8MB
    registry.aliyuncs.com/google_containers/pause                     3.6       6270bb605e12   4 months ago   683kB
    

    查看是否成功kubectl get nodes

    [root@k8smaster ~]# kubectl get nodes
    NAME        STATUS   ROLES                  AGE   VERSION
    k8smaster   Ready    control-plane,master   53m   v1.23.1
    

    相关文章

      网友评论

          本文标题:Kubeadm、Kubelet 和 Kubectl 安装部署

          本文链接:https://www.haomeiwen.com/subject/lskbcrtx.html