美文网首页Kubernetes/K8s学习交流
ubuntu 18.04一键安装K8s cluster

ubuntu 18.04一键安装K8s cluster

作者: 正Neng量 | 来源:发表于2020-01-18 04:06 被阅读0次

    直接运行脚本,一步完成配置。需要至少2台ubuntu,1个master-node,1-N个worker-node.

    配置master节点

    #!/bin/bash
    
    # user could update hostname
    export hostname=master-node
    
    # k8s version used
    export kube_version=1.17.0-00
    
    # install curl
    sudo apt-get update
    sudo apt-get install -y curl
    
    # add kubenetes repository
    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
    sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
    
    # install docker
    if ! [ -x "$(command -v docker)" ]; then
      echo "Docker installing ..."
      curl -sSL https://get.docker.com/ | sh
    else
      echo "docker has already been installed"
    fi
    
    # set user into docker group, need logout to take effect
    #sudo usermod -aG docker tas
    
    # install kubeadm kubelet kubectl
    sudo apt-get install -y kubelet=$kube_version  kubeadm=$kube_version kubectl=$kube_version
    
    #turn off swap
    sudo swapoff -a
    
    # set hostname and add line to hosts file
    sudo hostnamectl set-hostname $hostname
    sudo sed -i "1i127.0.1.1       ${hostname}" /etc/hosts
    
    # For master-node
    sudo kubeadm init --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
    
    #Deploy Pod Network to Cluster
    sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    

    执行上面的脚本,从返回消息中找到如下信息,将在配置worker节点的脚本中使用:

    Then you can join any number of worker nodes by running the following on each as root:
    
    kubeadm join 10.97.170.98:6443 --token fwhlz2.r0en0tkopmsjinit \
        --discovery-token-ca-cert-hash sha256:9844a8db82627a49838966d025d2d6b88f1ad7baabafe69d4f98d9979653a155 
    

    配置worker节点

    1. 自定义第四行hostname,如worker1, worker2
    2. 修改最后一行,用上一步中的返回信息替换,然后执行脚本
    #!/bin/bash
    
    # user could update hostname
    export hostname=worker-node1
    
    # k8s version used
    export kube_version=1.17.0-00
    
    # install curl
    sudo apt-get update
    sudo apt-get install -y curl
    
    # add kubenetes repository
    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
    sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
    
    # install docker
    if ! [ -x "$(command -v docker)" ]; then
      echo "Docker installing ..."
      curl -sSL https://get.docker.com/ | sh
    else
      echo "docker has already been installed"
    fi
    
    
    # set user into docker group, need logout to take effect
    #sudo usermod -aG docker tas
    
    # install kubeadm kubelet kubectl
    sudo apt-get install -y kubelet=$kube_version  kubeadm=$kube_version kubectl=$kube_version
    
    #turn off swap
    sudo swapoff -a
    
    # set hostname and add line to hosts file
    sudo hostnamectl set-hostname $hostname
    sudo sed -i "1i127.0.1.1       ${hostname}" /etc/hosts
    
    # copy the output from master node kubeadm init
    sudo kubeadm join 10.97.170.70:6443 --token icorqs.u4gbnyz7p11vkqz3 --discovery-token-ca-cert-hash sha256:cbbb51f01046558f6c7f2d6b5dee63a05fb14c35aae9cd9766191f10644abbf5
    

    卸载环境

    #!/bin/bash
    
    echo "reset kubeadm"
    sudo kubeadm reset
    
    while true; do
        echo "Do you want to uninstall kubernetes software?"
        read -p "(y/n) " yn
        case $yn in
            [Yy]* ) uninstall_kube=yes; break;;
            [Nn]* ) break;;
            * ) echo "Please answer yes or no.";;
        esac
    done
    
    if [ "$uninstall_kube" = "yes" ]; then
        echo "##########################################################################"
        echo "uninstall k8s components"
        echo "##########################################################################"
        sudo apt-get purge kubeadm kubectl kubelet kubernetes-cni kube*
        sudo apt-get autoremove
        sudo rm -rf ~/.kube
    fi
    
    while true; do
        echo "Do you want to uninstall docker?"
        read -p "(y/n) " yn
        case $yn in
            [Yy]* ) uninstall_docker=yes; break;;
            [Nn]* ) break;;
            * ) echo "Please answer yes or no.";;
        esac
    done
    
    if [ "$uninstall_docker" = "yes" ]; then
        echo "##########################################################################"
        echo "uninstall k8s components"
        echo "##########################################################################"
        sudo apt-get purge -y docker-engine docker docker.io docker-ce
        sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce
        sudo rm -rf /var/lib/docker /etc/docker
        sudo groupdel docker
        sudo rm -rf /var/run/docker.sock
    fi
    

    相关文章

      网友评论

        本文标题:ubuntu 18.04一键安装K8s cluster

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