美文网首页kebernetes
Kubernetes 自动化安装脚本(单节点离线安装)

Kubernetes 自动化安装脚本(单节点离线安装)

作者: 川人1588 | 来源:发表于2022-06-08 15:18 被阅读0次

    环境情况

    • 操作系统:centos 8(最小安装)
    • k8s 版本:1.24.1
    • 容器版本:containerd 1.6.5

    准备工作

    1. 安装centos 8.5.2111 操作系统,选择最小安装
    2. 将必要的文件下载到服务器的本地目录,下载的文件包括,
      注意:文件的存方目录结构与网盘中的结构相同
      链接:https://pan.baidu.com/s/1At5VS3o1qpOYEyXNdctc9g?pwd=ykno
      提取码:ykno
    3. 根据实际情况修改localinstall.sh脚本中的主机名和IP地址


      修改脚本中红色方框中标记的内容
    4. 修改脚本可执行属性
      chmod +x ./localinstall.sh
    5. 执行脚本
      ./localinstall.sh

    脚本内容

    #!/bin/bash
    local_node="k8s-node2"      # 本节点名称
    nodes_name=(k8s-node2)
    nodes_ip=(172.168.1.109)
    # 准备基础环境,支持的操作系统为centos 7、centos 8
    os_version=$(cat /etc/redhat-release |grep -P "[1-9].[0-9].[0-9]*" -o)
    echo "os version is ${os_version}"
    
    # 设置时区为上海
    echo "set time zone to Asia/Shanghai"
    timedatectl set-timezone "Asia/Shanghai"
    timedatectl set-local-rtc 0
    systemctl restart rsyslog 
    systemctl restart crond
    timedatectl show
    
    # 设置主机名
    echo "set host name "${local_node}
    hostnamectl set-hostname ${local_node}
    for i in $(seq 0 `expr ${#nodes_name[@]} - 1`); do
        var="${nodes_ip[i]}  ${nodes_name[i]}"
        echo ${var}
        sed -i '$a\'"${var}"'' /etc/hosts
    done
    
    #关闭防火墙
    echo "disable the firewall"
    systemctl disable firewalld
    systemctl stop firewalld
    
    #关闭swap
    echo "disable the swap"
    swapoff -a
    sed -i 's/^[^#].*swap*/#&/g' /etc/fstab
    
    #关闭selinux功能
    echo "close selinux"
    setenforce 0
    sed -i 's/^SELINUX=enforcing/#&/' /etc/selinux/config
    sed -i 's/^SELINUX=disabled/#&/' /etc/selinux/config
    sed -i '$a\SELINUX=disabled' /etc/selinux/config
    
    #配置及加加载内核模块
    cat << EOF > /etc/modules-load.d/containerd.conf
    overlay
    br_netfilter
    EOF
    modprobe overlay
    modprobe br_netfilter
    
    conntack_module="nf_conntrack"
    if [ ${os_version:0:1} != '8' ]
    then
            conntack_module="nf_conntrack_ipv4"
    fi
    echo ${conntack_module}
    cat << EOF > /etc/modules-load.d/ipvs.conf
    ip_vs
    ip_vs_rr
    ip_vs_wrr
    ip_vs_sh
    ${conntack_module}
    EOF
    modprobe ip_vs
    modprobe ip_vs_rr
    modprobe ip_vs_wrr
    modprobe ip_vs_sh
    modprobe ${conntack_module}
    lsmod | grep -e ip_vs -e nf_conntrack
    
    systemctl restart systemd-modules-load.service
    
    # 优化内核参数
    cat > ./kubernetes.conf <<EOF
    net.bridge.bridge-nf-call-iptables=1
    net.bridge.bridge-nf-call-ip6tables=1
    net.ipv4.ip_forward=1
    vm.swappiness=0
    vm.overcommit_memory=1
    vm.panic_on_oom=0
    fs.inotify.max_user_instances=8192
    fs.inotify.max_user_watches=1048576
    fs.file-max=52706963
    fs.nr_open=52706963
    net.ipv6.conf.all.disable_ipv6=1
    net.netfilter.nf_conntrack_max=2310720
    EOF
    mv ./kubernetes.conf  /etc/sysctl.d/kubernetes.conf
    sysctl -p /etc/sysctl.d/kubernetes.conf
    
    # 安装软件
    rpm -Uvh --force --nodeps ./rpms/*.rpm
    
    # 安装containerd
    tar xf ./cri-containerd-cni-1.6.5-linux-amd64.tar.gz -C /
    mkdir -p /etc/containerd && containerd config default > /etc/containerd/config.toml
    
    sed -i '/SystemdCgroup/s/^/#/' /etc/containerd/config.toml
    sed -i '/SystemdCgroup/a\
                SystemdCgroup = true' /etc/containerd/config.toml
    
    sed -i '/sandbox_image/s/^/#/' /etc/containerd/config.toml
    sed -i '/sandbox_image/a\
        sandbox_image = "registry.aliyuncs.com/k8sxio/pause:3.7"' /etc/containerd/config.toml
        
    sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors/a\
          [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]\
            endpoint = ["https://bqr1dr1n.mirror.aliyuncs.com"]\
          [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]\
            endpoint = ["https://registry.aliyuncs.com/k8sxio"]' /etc/containerd/config.toml
    
    sed -i '/Delegate/s/^/#/' /etc/systemd/system/containerd.service
    sed -i '/Delegate/a\
    Delegate = yes' /etc/systemd/system/containerd.service
    sed -i '/KillMode/s/^/#/' /etc/systemd/system/containerd.service
    sed -i '/KillMode/a\
    KillMode=process' /etc/systemd/system/containerd.service
    
    # 启动相关服务
    systemctl daemon-reload
    systemctl enable containerd
    systemctl restart containerd
    systemctl enable kubelet
    systemctl restart kubelet
    
    # 导入镜像
    ctr -n k8s.io images import images/k8s.imgs
    
    
    kube_version=$(kubelet --version|grep -P "v[1-9].[0-9]+.[0-9]+" -o)
    kubeadm init --apiserver-advertise-address=${nodes_ip[0]} --image-repository registry.aliyuncs.com/google_containers --kubernetes-version ${kube_version} --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
    
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
    kubectl apply -f calico.yaml
    # 单节点去除master节点的污点
    kubectl taint node ${nodes_ip[0]} node-role.kubernetes.io/control-plane-
    kubectl apply -f dashboard.yaml
    kubectl  apply -f dashboard-user.yaml
    kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kubernetes-dashboard
    kubectl get svc -n kubernetes-dashboard
    

    相关文章

      网友评论

        本文标题:Kubernetes 自动化安装脚本(单节点离线安装)

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