使用kubeadm搭建(kubernetes)k8s集群

文章目录

    • 安装准备
      • 环境准备
    • k8s Master 安装过程
    • k8s Node 添加
    • k8s 校验

安装准备

环境准备

虚拟机准备

Host OS vCPU/Memory
k8s-master CenOS7 64bit 最小:2vCPU/2G 推荐:4vCPU/8G
k8s-slave CenOS7 64bit 最小:2vCPU/2G 推荐:4vCPU/8G

k8s Master 安装过程

安装在k8s-master的机器上,以下过程全部用root用户执行:

  1. 关闭firewalld

    systemctl stop firewalld
    systemctl disable firewalld
    
  2. 关闭SELinux

    etenforce 0
    cp -p /etc/selinux/config /etc/selinux/config.bak$(date '+%Y%m%d%H%M%S')
    sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
    
  3. 关闭Swap

    swapoff -a
    cp -p /etc/fstab /etc/fstab.bak$(date '+%Y%m%d%H%M%S')
    sed -i "s/\/dev\/mapper\/rhel-swap/\#\/dev\/mapper\/rhel-swap/g" /etc/fstab
    sed -i "s/\/dev\/mapper\/centos-swap/\#\/dev\/mapper\/centos-swap/g" /etc/fstab
    mount -a
    free -m
    
  4. 修改hostname

    hostnamectl set-hostname k8s-master
    
  5. 设置iptables(routing)

     cat <  /etc/sysctl.d/k8s.conf
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    net.bridge.bridge-nf-call-arptables = 1
    EOF
    sysctl --system
    
  6. 使用阿里云的源

    wget -O /etc/yum.repos.d/CentOS7-Aliyun.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    wget -O /etc/yum.repos.d/epel-7-Aliyun.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum clean all
    yum makecache -y
    yum repolist all
    
    cat < /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    
    yum clean all
    yum makecache -y
    yum repolist all
    
  7. 安装docker

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    yum install docker-ce-18.06.0.ce -y
    
    systemctl enable docker
    systemctl start docker
    
    docker version
    
  8. docker 配置阿里云仓库

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://dfh5ot83.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    
  9. 安装kubernetes

    yum install -y kubelet-1.13.0 kubeadm-1.13.0 kubectl-1.13.0 kubernetes-cni-0.6.0
    
  10. 使用阿里云pull k8s 的master主要组件

    # For running kubeadm without an internet connection you have to pre-pull the required master images for the version of choice:
    KUBE_VERSION=v1.13.0
    KUBE_PAUSE_VERSION=3.1
    ETCD_VERSION=3.2.24
    CORE_DNS_VERSION=1.2.6
    
    GCR_URL=k8s.gcr.io
    ALIYUN_URL=registry.cn-shenzhen.aliyuncs.com/nick_docker_registry
    
    # In Kubernetes 1.12 and later, the k8s.gcr.io/kube-*, k8s.gcr.io/etcd and k8s.gcr.io/pause images don’t require an -${ARCH} suffix
    images=(kube-proxy:${KUBE_VERSION}
    kube-scheduler:${KUBE_VERSION}
    kube-controller-manager:${KUBE_VERSION}
    kube-apiserver:${KUBE_VERSION}
    pause:${KUBE_PAUSE_VERSION}
    etcd:${ETCD_VERSION}
    coredns:${CORE_DNS_VERSION})
    
    
    for imageName in ${images[@]} ; do
      docker pull $ALIYUN_URL/$imageName
      docker tag  $ALIYUN_URL/$imageName $GCR_URL/$imageName
      docker rmi $ALIYUN_URL/$imageName
    done
    
    docker images
    
  11. 使用kubeadm 初始化k8s

    kubeadm init --kubernetes-version=v1.13.0 --pod-network-cidr=10.244.0.0/16
    
    
    mkdir -p $HOME/.kube
    sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    cp -p $HOME/.bash_profile $HOME/.bash_profile.bak$(date '+%Y%m%d%H%M%S')
    echo "export KUBECONFIG=$HOME/.kube/config" >> $HOME/.bash_profile
    source $HOME/.bash_profile
    
  12. 安装kube_flannel 组件

    FLANNEL_VERSION=v0.11.0
    
    
    COREOS_URL=quay.io/coreos
    ALIYUN_URL=registry.cn-shenzhen.aliyuncs.com/nick_docker_registry
    
    images=(flannel:${FLANNEL_VERSION}-amd64
    flannel:${FLANNEL_VERSION}-arm64
    flannel:${FLANNEL_VERSION}-arm
    flannel:${FLANNEL_VERSION}-ppc64le
    flannel:${FLANNEL_VERSION}-s390x)
    
    
    for imageName in ${images[@]} ; do
      docker pull $ALIYUN_URL/$imageName
      docker tag  $ALIYUN_URL/$imageName $COREOS_URL/$imageName
      docker rmi $ALIYUN_URL/$imageName
    done
    
    docker images
    
    wget -O kube-flannel.yml https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
    
    kubectl apply -f kube-flannel.yml
    

k8s Node 添加

安装在k8s-slave的机器上
前9步参照k8s Master 安装过程

  1. 使用阿里云pull k8s 的slave主要组件

    KUBE_VERSION=v1.13.0
    KUBE_PAUSE_VERSION=3.1
    
    GCR_URL=k8s.gcr.io
    ALIYUN_URL=registry.cn-shenzhen.aliyuncs.com/nick_docker_registry
    
    
    # In Kubernetes 1.12 and later, the k8s.gcr.io/kube-*, k8s.gcr.io/etcd and k8s.gcr.io/pause images don’t require an -${ARCH} suffix
    images=(kube-proxy:${KUBE_VERSION}
    pause:${KUBE_PAUSE_VERSION})
    
    
    for imageName in ${images[@]} ; do
      docker pull $ALIYUN_URL/$imageName
      docker tag  $ALIYUN_URL/$imageName $GCR_URL/$imageName
      docker rmi $ALIYUN_URL/$imageName
    done
    
    docker images
    
  2. 使用kubeadm 将Node 加入到 Master
    使用Master 初始化时生成的token,ip为Master的ip

    kubeadm join 192.168.37.101:6443 --token mmxy0q.sjqca7zrzzj7czft --discovery-token-ca-cert-hash sha256:099421bf9b3c58e4e041e816ba6477477474614a17eca7f5d240eb733e7476bb
    

k8s 校验

  1. 获取node

    kubectl get nodes
    

    结果
    在这里插入图片描述

你可能感兴趣的:(k8s,devops)