kubernetes安装

young 523 2022-04-22

官方安装方案

阿里云镜像安装方案

  1. 安装docker

  2. 关闭防火墙

    systemctl stop firewalld
    systemctl disable firewalld
    
  3. 关闭selinux

    # 永久关闭
    sed -i 's/enforcing/disabled' /etc/selinux/config
    # 临时关闭
    setenforce 0 
    
  4. 关闭swap

    # 临时关闭
    swapoff -a
    # 永久关闭
    vim /etc/fstab
    注释掉/dev/mapper/centos-swap swap swap defaults 0 0
    

    然后重启生效

    systemctl reboot
    # 查看swap交换器是否都为0
    free -m
    
  5. 设置hosts

    cat >> /etc/hosts << EOF
    192.168.0.104 k8s-master
    192.168.0.105 k8s-node1
    192.168.0.106 k8s-node2
    EOF
    
  6. 将桥接的IPV4流量传递到iptables

    cat > /etc/sysctl.d/k8s.conf <<EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    
    # 让上述配置生效
    sysctl --system
    
  7. 设置时间

    yum install -y ntpdate -y
    ntpdate -u npt.api.bz
    # 设置时区为中国时区
    timedatectl set-timezone Asia/Shanghai
    
  8. yum源切换与yum k8s

    如果是x86_64的cpu

    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    

    如果是mac m1的cpu

    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-aarch64/
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    
  9. 安装kubeadm,kubelet,kubectl

    在阿里云的安装步骤中写到

    由于官网未开放同步方式, 可能会有索引gpg检查失败的情况, 这时请用 yum install -y --nogpgcheck kubelet kubeadm kubectl 安装

    setenforce 0
    yum install -y --nogpgcheck kubelet kubeadm kubectl
    
  10. 配置docker

    在/etc/docker/daemon.json中增加配置

    "exec-opts": ["native.cgroupdriver=systemd"]
    

    重载配置,重启docker和kubelet

    systemctl daemon-reload
    # docker需开机启动,否则初始化会失败
    systemctl enable docker
    systemctl restart docker
    systemctl enable kubelet
    systemctl restart kubelet
    

    后续步骤,无特殊说明的情况下,仅在master节点上执行

  11. 初始化

    ip为master的ip地址,其次注意版本,剩下的无需改动

    kubeadm init --apiserver-advertise-address=192.168.0.104 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version v1.26.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
    

    如果出现异常,需要用 kubeadm reset命令重置之后,再重新init

  12. 配置kubectl命令工具

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    

    验证kubectl命令

    kubectl get nodes
    
  13. 安装calico

    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml