使用 kubeadm 安装 kubernetes(k8s)

服务器需要可以科学上网

  • 安装 kubeadm

    请优先按官方安装文档安装: https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/

    • 使用阿里的YUM源

      1
      2
      3
      4
      5
      6
      7
      8
      9
      cat <<EOF > /etc/yum.repos.d/kubernetes.repo
      [kubernetes]
      name=Kubernetes
      baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
      enabled=1
      gpgcheck=0
      repo_gpgcheck=0
      gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
      EOF
    • 关闭 selinux

      1
      2
      3
      4
      5
      6
      7
      # Set SELinux in permissive mode (effectively disabling it)
      setenforce 0
      sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

      yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

      systemctl enable --now kubelet
      • 加载模块 br_netfilter

        1
        lsmod | grep br_netfilter # centos 7 查询不到结果,看别人文档说已经编译进了某个地方,所以这一步可跳过
  • 安装 k8s

    • 提前加载镜像

      1
      kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers
    • 安装 k8s

      –pod-network-cidr=10.244.0.0/16 是因为后面要使用 Flannel 网络

      1
      kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16

      init 如果顺利完成,会提示执行几行命令

      1
      2
      3
      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
    • 安装Flannel网络

      1
      kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

      通过 kubectl get pods --all-namespaces 检查是否已经全部为 Running 状态

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      [root@localhost k8s]# kubectl get pods --all-namespaces
      NAMESPACE NAME READY STATUS RESTARTS AGE
      kube-system coredns-78d4cf999f-677l2 1/1 Running 0 5m7s
      kube-system coredns-78d4cf999f-c6wng 1/1 Running 0 5m7s
      kube-system etcd-localhost 1/1 Running 0 4m40s
      kube-system kube-apiserver-localhost 1/1 Running 0 4m26s
      kube-system kube-controller-manager-localhost 1/1 Running 0 4m40s
      kube-system kube-flannel-ds-amd64-rl8mq 1/1 Running 0 26s
      kube-system kube-proxy-lzb5p 1/1 Running 0 5m7s
      kube-system kube-scheduler-localhost 1/1 Running 0 4m39s
    • 配置master结点也可以分配 pods

      1
      kubectl taint nodes --all node-role.kubernetes.io/master-
  • 添加 node 节点
    请参考官网