Kubernetes安装脚本

参考脚本


不实用脚本,仅记录过程

Master

  1. master 节点,安装Kubernetes软件包(kubectl kubead kubelet )

    #!/bin/bash
    # echo "修改主机名"
    # echo "修改主机名"
      read -p "##输入要修改的主机名: " hostname
      hostnamectl --static set-hostname $hostname
    
    # 为服务配置代理服务
      export http_proxy=http://10.1.0.160:1087 && export https_proxy=http://10.1.0.160:1087
      
    # install docker as container runtimes as below
      yum install -y yum-utils
    
    # 安装docker-compose
      curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
      chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    
    # 下载docker yum源
      yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
    # 安装docker
      #yum install docker-ce docker-ce-cli containerd.io -y
      yum install -y docker-ce-19.03.11-3.el7 docker-ce-cli-19.03.11-3.el7 containerd.io
    
    # 修改docker daemon.json
      mkdir /etc/docker /data
      cat > /etc/docker/daemon.json <<-EOF
        {
          "exec-opts": ["native.cgroupdriver=systemd"],
          "data-root": "/data/docker",
          "log-driver": "json-file",
          "log-opts": {
            "max-size": "10m",
            "max-file":"5"
          },
          "storage-driver": "overlay2",
          "storage-opts": [
            "overlay2.override_kernel_check=true"
          ]
        }
        EOF
      mkdir -p /etc/systemd/system/docker.service.d
    
      cat > /etc/systemd/system/docker.service.d/https-proxy.conf <<-EOF
        [Service]
        Environment="HTTP_PROXY=http://10.1.0.160:1087/" "HTTPS_PROXY=http://10.1.0.160:1087/" "NO_PROXY=localhost,127.0.0.1,hub.nflow.cn"
        EOF
    
    # 启动docker
      systemctl enable docker && systemctl daemon-reload && systemctl start docker
    
    # Letting iptables see bridged traffic
      cat <<-EOF | sudo tee /etc/sysctl.d/k8s.conf
        net.bridge.bridge-nf-call-ip6tables = 1
        net.bridge.bridge-nf-call-iptables = 1
        EOF
    # 确认br_netfilter
      modprobe br_netfilter
    
    # 安装kubeadm kubectl kubelet
      cat <<-EOF > /etc/yum.repos.d/kubernetes.repo
        [kubernetes]
        name=Kubernetes
        baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
        enabled=1
        gpgcheck=1
        repo_gpgcheck=1
        gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
        exclude=kubelet kubeadm kubectl
        EOF
    
    # 关闭selinux
      setenforce 0
      sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
    
    # 开始安装
      yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
      #export http_proxy=http://10.1.0.160:1087 && export https_proxy=http://10.1.0.160:1087
    
    # enable kubelet
      systemctl enable --now kubelet
    
    # 开启kubead kubectl命令补全功能
      cat <<-EOF >> ~/.bashrc
        source <(kubectl completion bash)
        source <(kubeadm completion bash)
        EOF
      source ~/.bashrc
    
    # 重写Kubernetes 镜像
    #  #kubeadm config images pull --kubernetes-version=v1.18.3
    #  version=v1.18.3
    #  images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
    #  for i in ${images[@]} ; do
    #    docker tag k8s.gcr.io/${i} hub.nflow.cn/${i}
    #    docker push hub.nflow.cn/${i} 
    #  done
    # 开始安装Kubernetes(集群模式)
    #  kubeadm init --upload-certs --config=kubeadm-config.yaml --v=5
  2. 初始化脚本

    #!/bin/bash
    # 配置
    #echo -e "选择Kubernetes配置的环境"
    #select menu in "生产环境"  "测试环境"  "开发环境" "退出安装"
    #do
    #    case $menu in 
    #    "生产环境")
    #         read -p " 请输入公有云提供商负载均衡私有地址:" ip
    #         echo $ip
    #         ;;     
    #    esac
    #done
    # 生成kubead example config.yaml 
      cat <<-EOF > ./kubeadm-config-example.yaml
        apiVersion: kubeadm.k8s.io/v1beta2
        kind: InitConfiguration
        bootstrapTokens:
        - groups:
          - system:bootstrappers:kubeadm:default-node-token
          token: abcdef.0123456789abcdef
          ttl: 24h0m0s
          usages:
          - signing
          - authentication
        localAPIEndpoint:
          advertiseAddress: aaip
          bindPort: 6443
        nodeRegistration:
          criSocket: /var/run/dockershim.sock
          name: izt4n9u81t8muejzz4mqv0z
          taints:
          - effect: NoSchedule
            key: node-role.kubernetes.io/master
        ---
        apiServer:
          timeoutForControlPlane: 4m0s
        apiVersion: kubeadm.k8s.io/v1beta2
        certificatesDir: /etc/kubernetes/pki
        clusterName: kubernetes
        controllerManager: {}
        dns:
          type: CoreDNS
        etcd:
          local:
            dataDir: /var/lib/etcd
        imageRepository: k8s.gcr.io
        kind: ClusterConfiguration
        kubernetesVersion: v1.18.4
        controlPlaneEndpoint: cpeip:6443
        networking:
          dnsDomain: nflow.so
          serviceSubnet: 10.10.0.0/16
          podSubnet: 172.19.0.0/16
        scheduler: {}
        EOF
    
    # 配置之前要求
    main()
    {
        echo  "    配置要求     "
           echo -e "\e[1;33m------------------------------------------------------\e[0m"
           echo "  1)创建一个公有云私有负载均衡"
           echo "  2)为kubernetes apiserver配置一个6443端口的TCP转发策略"
           echo -e "\e[1;33m------------------------------------------------------\e[0m"  
        #
    }
    main
    
    # check control plane endpoint address is exsit
      if [ -z ${1} ];then
        #echo -e "\033[37;31m[\$1] control plane endpoint address of kubernetes is not exsit\033[39;49m"
        echo -e "\033[37;31m[\$1] control plane endpoint address of kubernetes is not exsit\033[39;49m"
        echo " excute script such as (sh k8s-config.sh 1.1.1.1)"  
        exit
         else
        echo " Control plane endpoint address of kubernetes is exsit"
      fi
    
    # replase AdvertiseAddress & control plane endpoint,如下
      sed -e "s/aaip/$(hostname -i)/g" -e "s/cpeip/$1/g"  kubeadm-config-example.yaml > kubeadm-config.yaml
    
    # bind slb ipaddress to localhost network main device 
      ip addr add $1/20 dev eth0
    
    # 开始初始化Kubernetes集群
      echo -e "\e[1;33m Start configure kubernetes cluster......\e[0m"
      kubeadm init --config=kubeadm-config.yaml --upload-certs --v=5

node

  1. node节点,安装Kubernetes软件包(kubectl kubead kubelet)
    #!/bin/bash
    # echo "修改主机名"
    # echo "修改主机名"
      read -p "##输入要修改的主机名: " hostname
      hostnamectl --static set-hostname $hostname
      
    # install docker as container runtimes as below
      yum install -y yum-utils
    
    # 安装docker-compose
      curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
      chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    
    # 下载docker yum源
      yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
    # 安装docker
      #yum install docker-ce docker-ce-cli containerd.io -y
      yum install -y docker-ce-19.03.11-3.el7 docker-ce-cli-19.03.11-3.el7 containerd.io
    
    # 修改docker daemon.json
      mkdir /etc/docker /data
      cat > /etc/docker/daemon.json <<-EOF
        {
          "exec-opts": ["native.cgroupdriver=systemd"],
          "data-root": "/data/docker",
          "log-driver": "json-file",
          "log-opts": {
            "max-size": "10m",
            "max-file":"5"
          },
          "storage-driver": "overlay2",
          "storage-opts": [
            "overlay2.override_kernel_check=true"
          ]
        }
        EOF
      mkdir -p /etc/systemd/system/docker.service.d
    
    # 启动docker
      systemctl enable docker && systemctl daemon-reload && systemctl start docker
    
    # Letting iptables see bridged traffic
      cat <<-EOF | sudo tee /etc/sysctl.d/k8s.conf
        net.bridge.bridge-nf-call-ip6tables = 1
        net.bridge.bridge-nf-call-iptables = 1
        EOF
    # 确认br_netfilter
      modprobe br_netfilter
    
    # 安装kubeadm kubectl kubelet
      cat <<-EOF > /etc/yum.repos.d/kubernetes.repo
        [kubernetes]
        name=Kubernetes
        baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
        enabled=1
        gpgcheck=1
        repo_gpgcheck=1
        gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
        exclude=kubelet kubeadm kubectl
        EOF
    
    # 关闭selinux
      setenforce 0
      sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
    
    # 开始安装
      yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
      export http_proxy=http://10.1.0.160:1087 && export https_proxy=http://10.1.0.160:1087
    
    # enable kubelet
      systemctl enable --now kubelet
    
    # 开启kubead kubectl命令补全功能
      cat <<-EOF >> ~/.bashrc
      source <(kubectl completion bash)
      source <(kubeadm completion bash)
      EOF
      source ~/.bashrc
    
    # 重写Kubernetes 镜像
    #  #kubeadm config images pull --kubernetes-version=v1.18.3
    #  version=v1.18.3
    #  images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
    #  for i in ${images[@]} ; do
    #    docker tag k8s.gcr.io/${i} hub.nflow.cn/${i}
    #    docker push hub.nflow.cn/${i} 
    #  done
    # 开始安装Kubernetes(集群模式)
    #  kubeadm init --upload-certs --config=kubeadm-config.yaml --v=5

     

posted @ 2022-05-20 20:46  MacoPlus  阅读(203)  评论(0编辑  收藏  举报