Kubernetes搭建流程(基于Centos7)

安装docker
  • 安装docker环境必要的包

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    
  • 加入docker repo

    sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    
  • 安装docker-ce (ce 版本为社区版本,ee版本为商业版本)

    sudo yum makecache fast
    sudo yum install docker-ce
    

    安装过程中可能会出现依赖问题,缺少 container-selinux-2.9 这个安装包,此时需要执行以下安装命令

    sudo yum install ftp://fr2.rpmfind.net/linux/centos/7.3.1611/extras/x86_64/Packages/container-selinux-2.9-4.el7.noarch.rpm
    

    成功后继续执行

    sudo yum install docker-ce
    
  • 启动docker服务

    sudo systemctl start docker.service
    
安装 kubectl
  • 下载最新的kublet程序

    curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
    
  • 添加可执行权限

    chmod +x ./kubectl
    
  • 移动到系统环境中

    sudo mv ./kubectl /usr/local/bin/kubectl
    
安装 kubelet,kubectl
  • 切换到root环境

    su root  或者  sudo -i
    
  • 安装 kubelet 和 kubeadm

    cat < /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
    EOF
    setenforce 0
    yum install -y kubelet kubeadm
    
  • 启动kubelet服务

    systemctl enable kubelet && systemctl start kubelet
    
初始化 mater node(该操作只需要在主节点上执行)
  • 使用kubeadm初始化master

    kubeadm init
    
  • 初始化输出示例

      [kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
      [init] Using Kubernetes version: v1.7.0
      [init] Using Authorization modes: [Node RBAC]
      [preflight] Running pre-flight checks
      [preflight] Starting the kubelet service
      [certificates] Generated CA certificate and key.
      [certificates] Generated API server certificate and key.
      [certificates] API Server serving cert is signed for DNS names [kubeadm-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.138.0.4]
      [certificates] Generated API server kubelet client certificate and key.
      [certificates] Generated service account token signing key and public key.
      [certificates] Generated front-proxy CA certificate and key.
      [certificates] Generated front-proxy client certificate and key.
      [certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
      [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
      [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
      [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
      [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
      [apiclient] Created API client, waiting for the control plane to become ready
      [apiclient] All control plane components are healthy after 16.502136 seconds
      [token] Using token: 
      [apiconfig] Created RBAC rules
      [addons] Applied essential addon: kube-proxy
      [addons] Applied essential addon: kube-dns
    
      Your Kubernetes master has initialized successfully!
    
      To start using your cluster, you need to run (as a regular user):
    
        mkdir -p $HOME/.kube
        sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
        sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
      You should now deploy a pod network to the cluster.
      Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
        http://kubernetes.io/docs/admin/addons/
    
      You can now join any number of machines by running the following on each node
      as root:
    
        kubeadm join --token  :
    
  • 执行上述输入中的内容(遗漏该步骤可能会导致 kube-dns pod 一直无法running的错误)

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  • 安装 pod network (用于pod以及docker容器之间进行网络通信)

    kubectl apply -f https://git.io/weave-kube-1.6
    

    Note:pod network 有多种,此处我们选择 weave,其他网络可自行研究

加入子节点
  • 子节点加入到 Cluster

    kubeadm join --token  :
    
安装dashboard
  • 安装命令

    kubectl create -f https://git.io/kube-dashboard
    
  • 运行dashboard(需要在主机上查看)

    执行 kubectl proxy
    

    浏览器访问: http://localhost:8081/ui

    Kubernetes搭建流程(基于Centos7)_第1张图片
    k8s dashboard
问题汇总
  • 执行 kubeadm init 后一直卡在

    [apiclient] Created API client, waiting for the control plane to become ready
    

    可使用以下命令查看日志

    journalctl -xeu kubelet
    

    问题原因: gcr.io/google_containers/pause-amd64:3.0 没有pull下来
    解决方式:docker pull gcr.io/google_containers/pause-amd64:3.0,若仍然pull不下来,可开启外网代理再使用docker pull

  • kubelet: error: failed to run Kubelet: failed to create kubelet: misconfiguration: kubelet cgroup driver: "systemd" is different from docker cgroup driver: "cgroupfs"
    问题原因:kubelet 使用的cgroup driver和docker使用的不一致
    解决方式:修改kubelet配置文件: /etc/systemd/system/kubelet.service.d/10-kubeadm.conf,将systemd修改为 cgroupfs,重启kubelet ( systemctl restart kubelet )

  • 使用kubectl get nodes 命令报错:The connection to the server localhost:8080 was refused - did you specify the right host or port?
    解决方式:添加 KUBECONFIG环境变量

    vim ~/.bash_profile
    末尾添加 export KUBECONFIG=/etc/kubernetes/admin.conf  
    source ~/.bash_profile
    
  • 安装 kubelet kubeadm 报错:https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64/repodata/repomd.xml: [Errno 14] curl#7 - "Failed to connect to 2404:6800:4008:801::200e: 网络不可
    达"
    解决方式:开启外网代理安装

  • kube-dns 一直无法处于 3/3 running 状态
    问题原因:使用 kubectl describe pod kube-dns --namespace=kube-system 查看发现如下log

    Readiness probe failed: Get http://10.32.0.3:8081/readiness: dial tcp 10.32.0.3:8081: getsockopt: connection refused
    

    解决方式:执行init后的 output:

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

    重启 docker kubelet

    systemctl restart docker.service
    systemctl restart kubelet.service

你可能感兴趣的:(Kubernetes搭建流程(基于Centos7))