美文网首页
K8S集群部署

K8S集群部署

作者: EnjoyCode | 来源:发表于2019-07-26 09:15 被阅读0次

    kubeadm部署Kubernetes 1.15

    资源准备

    节点角色 系统版本 CPU 内存 ip地址
    master Centos7.6/64位 >=2C >=2G 192.168.105.10
    node1 Centos7.6/64位 >=2C >=2G 192.168.105.11
    node2 Centos7.6/64位 >=2C >=2G 192.168.105.12
    node3 Centos7.6/64位 >=2C >=2G 192.168.105.13

    环境准备

    注意:在所有节点进行如下操作

    1. 设置hostname,node节点对应设置即可
    hostnamectl set-hostname master
    
    1. 编辑 /etc/hosts 文件,添加域名解析。
    cat <<EOF >>/etc/hosts
    192.168.105.10 master
    192.168.105.11 node1
    192.168.105.12 node2
    192.168.105.13 node3
    EOF
    
    1. 关闭防火墙、selinux、swap。
    systemctl stop firewalld
    systemctl disable firewalld
    setenforce 0
    sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
    swapoff -a
    sed -i 's/.*swap.*/#&/' /etc/fstab
    
    1. 配置内核参数,将桥接的IPv4流量传递到iptables的链
    cat > /etc/sysctl.d/k8s.conf <<EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    sysctl --system
    echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
    sysctl -p
    
    1. 配置国内yum源
    yum install -y wget
    mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
    yum clean all && yum makecache
    
    1. 配置国内Kubernetes源
    cat <<EOF > /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
    
    1. 配置 docker 源
    wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
    

    软件安装

    注意:在所有节点操作

    1. 安装docker
    • docker服务为容器运行提供计算资源,是所有容器运行的基本平台。
    yum install -y docker-ce-18.06.1.ce-3.el7
    systemctl enable docker && systemctl start docker
    docker version
    
    1. 安装kubeadm、kubelet、kubectl
    • Kubelet负责与其他节点集群通信,并进行本节点Pod和容器生命周期的管理。Kubeadm是Kubernetes的自动化部署工具,降低了部署难度,提高效率。Kubectl是Kubernetes集群管理工具。
    yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
    systemctl enable kubelet
    

    部署Master节点服务

    注意:仅仅在master节点操作

    1. 在master进行Kubernetes集群初始化。
    kubeadm init --kubernetes-version=1.15.0 \
    --apiserver-advertise-address=192.168.105.10 \
    --image-repository registry.aliyuncs.com/google_containers \
    --service-cidr=10.233.0.0/16 \
    --pod-network-cidr=10.244.0.0/16 \
    --ignore-preflight-errors=all \
    --token-ttl=0
    

    定义POD的网段为: 10.244.0.0/16, api server地址就是master本机IP地址。
    这一步很关键,由于kubeadm 默认从官网k8s.grc.io下载所需镜像,国内无法访问,因此需要通过–image-repository指定阿里云镜像仓库地址,很多新手初次部署都卡在此环节无法进行后续配置。
    集群初始化成功后返回如下信息:
    记录生成的最后部分内容,此内容需要在其它节点加入Kubernetes集群时执行。

    kubeadm join 192.168.105.10:6443 --token ds40sk.3fmv5gr0g1a3j3ui \
        --discovery-token-ca-cert-hash sha256:056eaec84864754de11d50e1b600d378c209b1c5ec786d3b774b241f01577b52
    
    1. 配置kubectl工具
    mkdir -p /root/.kube
    cp /etc/kubernetes/admin.conf /root/.kube/config
    kubectl get nodes
    

    部署flannel网络

    注意:在所有节点操作

    docker pull quay.io/coreos/flannel:v0.9.1-amd64
    mkdir -p /etc/cni/net.d/
    cat <<EOF> /etc/cni/net.d/10-flannel.conf
    {"name":"cbr0","type":"flannel","delegate": {"isDefaultGateway": true}}
    EOF
    mkdir /usr/share/oci-umount/oci-umount.d -p
    mkdir /run/flannel/
    cat <<EOF> /run/flannel/subnet.env
    FLANNEL_NETWORK=10.244.0.0/16
    FLANNEL_SUBNET=10.244.0.1/24
    FLANNEL_MTU=1450
    FLANNEL_IPMASQ=true
    EOF
    

    注意:在master节点启动

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

    部署Dashboard

    注意:在master节点操作

    1. 修改镜像地址
    wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
    sed -i 's@k8s.gcr.io@registry.cn-hangzhou.aliyuncs.com/google_containers@g' kubernetes-dashboard.yaml
    

    k8s.gcr.io 修改为 registry.cn-hangzhou.aliyuncs.com/google_containers,后续所有 yaml 文件中,只要涉及到 image 的,都需要做同样的修改,因为国内 k8s.gcr.io 这个地址被墙了。

    1. 修改对外暴露的端口
    kind: Service
    apiVersion: v1
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      name: kubernetes-dashboard
      namespace: kube-system
    spec:
      ports:
        - port: 443
          targetPort: 8443
      selector:
        k8s-app: kubernetes-dashboard
    
    • 修改为
    kind: Service
    apiVersion: v1
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      name: kubernetes-dashboard
      namespace: kube-system
    spec:
      ports:
        - port: 443
          targetPort: 8443
          nodePort: 30000
      selector:
        k8s-app: kubernetes-dashboard
      type: NodePort
    
    1. 创建登录令牌
    kubectl create serviceaccount  dashboard-admin -n kube-system
    kubectl create clusterrolebinding  dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
    kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
    
    • 获取toten对应的值:
    eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tc25mcjUiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNWUxZmNlYjMtZDM2Yy00MjI0LTkzYzMtOGZjMmE1OGQzMTMyIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.d3kHjYm1yYw1wEnFoIepMSm9VXdkxmFImxx6I6znk4cMRxPN_6kb-omJT4-8Xpsd5zbiLmT5pfbqs6qPuRgtjasyoo1bLhYdup7v9Q3_0XeFCQ9NVhTUC70VEielm2GRbPmew2Cr-Wou2QRvyqmVIy3OUpDIA5Ag8wKM-Wbiu9j14qsJomZ4EacwEiImw5qREMQVFzhtX8FDPvX7JqCVvJ2aqTQQrGV9bWAu88BoLGA3kD9YSUgZ3OdzfFEjRsSMQg4s_ZfZE2qGv_aKHxuZb9-EJX-GVai0CnX7GAwpSIG1sT_d99PnxxZE9g4C8gE2VJjr9fAUYOJc3qJemGhbWQ
    
    • 查看Dashboard的pod运行再哪个node上
    # master节点
    [root@master yaml]# kubectl get pods -A
    NAMESPACE     NAME                                    READY   STATUS              RESTARTS   AGE
    ......
    kube-system   kubernetes-dashboard-86844cc55f-fgvnj   1/1     Running             0          8m33s
    ......
    [root@master yaml]# kubectl describe pod kubernetes-dashboard-86844cc55f-fgvnj -n kube-system
    ......
    Node:           node1/192.168.105.11
    ......
    
    • 打开地址:https://192.168.105.11:30000
    • 选择令牌输入上面的toten,如下即可
    m_3017662075561b62f099605da455e13f_r.png

    相关文章

      网友评论

          本文标题:K8S集群部署

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