• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

白话kubeadm安装K8S[集群](史上最K8S搭建教程)

武飞扬头像
sunican
帮助1

前言

市面上很多k8s的安装工具,作为产品的设计者和推广者,K8S组织也知道自己的产品部署起来十分的困难,于是把开源爱好者写的工具kubeadmn收编为正规军,纳入到了自己的麾下。(关于kubeadm的博客,你可以看这篇《kubeadm探秘》)

为什么我们要用kubeadm来部署?因为kubeadm不仅直接相关的命令简单到只有两条,而且还可以放生产环境使用(这里有个前提,需要能很好的理解K8S的各个组件,处理好它们的关系,说人话就是能看得懂、玩得转)。

官方文档有中文教程,K8S最新版本1.26已经弃用了docker做自己的运行时,笔者还没有摸索出来怎么部署,这里就以老版本的1.18为例子来讲解

我的演示环境

系统:centos7.6

CPU:2核

内存:2G

最好是2核4G,20G硬盘,如果你想模拟更多的生产环境过程部署,比如jenkins、nginx、MySQL等,最好提升一下虚拟机配置,否则可能无法运行那么多的pod。

因为是演示环境,所以这里操作的是单节点。集群节点我会写备注

环境配置

[集群的node节点,这一步也需要执行]

  1.  
    # yum源改为阿里云
  2.  
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  3.  
     
  4.  
    # vim配置
  5.  
    echo -e 'set paste
  6.  
    set expandtab
  7.  
    set ts=4' >> ~/.vimrc
  8.  
     
  9.  
    # 一些工具
  10.  
    yum install net-tools vim telnet lsof -y
  11.  
     
  12.  
    # k8s用阿里云源,这样速度快一些
  13.  
    echo '#k8s
  14.  
    [kubernetes]
  15.  
    name=Kubernetes
  16.  
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  17.  
    enabled=1
  18.  
    gpgcheck=0
  19.  
    '>/etc/yum.repos.d/kubernetes.repo
  20.  
     
  21.  
    #
  22.  
    setenforce 0
  23.  
    sed -i 's/SELINUX=enforc.*/SELINUX=disabled/g' /etc/selinux/config
  24.  
    cat <<EOF > /etc/sysctl.d/k8s.conf
  25.  
    net.bridge.bridge-nf-call-ip6tables = 1
  26.  
    net.bridge.bridge-nf-call-iptables = 1
  27.  
    EOF
  28.  
    sysctl --system
  29.  
     
  30.  
    # 关闭swap
  31.  
    swapoff -a
  32.  
    sed -ri 's/.*swap.*/#&/' /etc/fstab
  33.  
    free -m |grep Swap
  34.  
    # 关闭防火墙
  35.  
    systemctl stop firewalld && systemctl disable firewalld
学新通

部署 docker [集群的node节点,这一步也需要执行]

  1.  
    yum install -y yum-utils device-mapper-persistent-data lvm2
  2.  
    yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  3.  
    yum install -y docker-ce docker-ce-cli containerd.io
  4.  
    mkdir /etc/docker
  5.  
    cat > /etc/docker/daemon.json <<EOF
  6.  
    {
  7.  
    "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
  8.  
    "exec-opts": ["native.cgroupdriver=systemd"],
  9.  
    "log-driver": "json-file",
  10.  
    "log-opts": {
  11.  
    "max-size": "100m"
  12.  
    }
  13.  
    }
  14.  
    EOF
  15.  
     
  16.  
    systemctl daemon-reload
  17.  
    systemctl restart docker.service
  18.  
    systemctl enable docker.service
  19.  
    docker info | grep "Cgroup Driver"
学新通

部署kubeadm

[集群的node节点,这一步也需要执行]

  1.  
    yum install -y kubelet-1.18.2 kubeadm-1.18.2 kubectl-1.18.2
  2.  
    systemctl enable kubelet && systemctl start kubelet
  3.  
     
  4.  
    #查看需要依赖的镜像版本
  5.  
    kubeadm config images list

kubeadm 部署的时候默认从k8s.gcr.io拉取镜像,对于国内用户来说要么速度慢,要么无法下载,换成阿里云的镜像,如何换?笔者猜kubeadm理论上是调用了"docker pull"命令,那么就跟自己手动没什么区别。

  1.  
    # 通过这个命令可以获取到需要拉取的镜像名称
  2.  
    kubeadm config images list | awk -F'/' '/k8s.gcr.io/{print $2}'
学新通
  1.  
    cat > ~/pull_image.sh <<EOF
  2.  
    #!/bin/bash
  3.  
    for imageName in \$(kubeadm config images list | awk -F'/' '/k8s.gcr.io/{print \$2}') ; do
  4.  
    docker pull registry.cn-hangzhou.aliyuncs.com/谷歌_containers/\${imageName}
  5.  
    docker tag registry.cn-hangzhou.aliyuncs.com/谷歌_containers/\${imageName} k8s.gcr.io/\${imageName}
  6.  
    docker rmi registry.cn-hangzhou.aliyuncs.com/谷歌_containers/\${imageName}
  7.  
    done
  8.  
    EOF

看下已经拉取下来的镜像,k8s.gcr.io开头的都是刚刚拉取的镜像文件

学新通

kubeadm初始化 master

  1.  
    # kubeadm config print init-defaults > /opt/kubeadm-config.yaml
  2.  
    kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.18.2 2>&1 | tee kubeadm-init.log

这执行真一步的时候我遇到了报错,

  1.  
    [init] Using Kubernetes version: v1.18.2
  2.  
    [preflight] Running pre-flight checks
  3.  
    [WARNING Firewalld]: firewalld is active, please ensure ports [6443 10250] are open or your cluster may not function correctly
  4.  
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 23.0.1. Latest validated version: 19.03
  5.  
    [preflight] Pulling images required for setting up a Kubernetes cluster
  6.  
    [preflight] This might take a minute or two, depending on the speed of your internet connection
  7.  
    [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  8.  
    error execution phase preflight: [preflight] Some fatal errors occurred:
  9.  
    [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver:v1.18.2: output: Error response from daemon: Get "https://k8s.gcr.io/v2/": dial tcp 142.250.157.82:443: i/o timeout
  10.  
    , error: exit status 1
  11.  
    [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-controller-manager:v1.18.2: output: Error response from daemon: Get "https://k8s.gcr.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  12.  
    , error: exit status 1
  13.  
    [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-scheduler:v1.18.2: output: Error response from daemon: Get "https://k8s.gcr.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  14.  
    , error: exit status 1
  15.  
    [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-proxy:v1.18.2: output: Error response from daemon: Get "https://k8s.gcr.io/v2/": dial tcp 142.250.157.82:443: i/o timeout
  16.  
    , error: exit status 1
  17.  
    [preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
  18.  
    To see the stack trace of this error execute with --v=5 or higher
学新通

如果你到这里也报错了,请看上面截图的输出,kube.*组件版本号是不是也是v1.18.20?如果是的话,我感觉这里可能是正则匹配的问题, kubeadm出错了,没关系,手动tag一下把镜像改为1.18.2即可,就可以走通了。

接着往下:

  1.  
    mkdir -p $HOME/.kube
  2.  
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3.  
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
  4.  
    kubectl get pod -A # 即可看到pod启动了
学新通

查看组件状态

kubectl get componentstatuses

部署网络组件

为什么有pod状态为pending?这个是因为网络组件没有安装。

  1.  
    wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  2.  
    # 下载之后本地执行命令, 虚拟机无法下载的话,翻墙浏览器打开URL
  3.  
    kubectl apply -f kube-flannel.yml

不能下载的话,可以复制下面的内容

  1.  
    ---
  2.  
    kind: Namespace
  3.  
    apiVersion: v1
  4.  
    metadata:
  5.  
    name: kube-flannel
  6.  
    labels:
  7.  
    pod-security.kubernetes.io/enforce: privileged
  8.  
    ---
  9.  
    kind: ClusterRole
  10.  
    apiVersion: rbac.authorization.k8s.io/v1
  11.  
    metadata:
  12.  
    name: flannel
  13.  
    rules:
  14.  
    - apiGroups:
  15.  
    - ""
  16.  
    resources:
  17.  
    - pods
  18.  
    verbs:
  19.  
    - get
  20.  
    - apiGroups:
  21.  
    - ""
  22.  
    resources:
  23.  
    - nodes
  24.  
    verbs:
  25.  
    - get
  26.  
    - list
  27.  
    - watch
  28.  
    - apiGroups:
  29.  
    - ""
  30.  
    resources:
  31.  
    - nodes/status
  32.  
    verbs:
  33.  
    - patch
  34.  
    - apiGroups:
  35.  
    - "networking.k8s.io"
  36.  
    resources:
  37.  
    - clustercidrs
  38.  
    verbs:
  39.  
    - list
  40.  
    - watch
  41.  
    ---
  42.  
    kind: ClusterRoleBinding
  43.  
    apiVersion: rbac.authorization.k8s.io/v1
  44.  
    metadata:
  45.  
    name: flannel
  46.  
    roleRef:
  47.  
    apiGroup: rbac.authorization.k8s.io
  48.  
    kind: ClusterRole
  49.  
    name: flannel
  50.  
    subjects:
  51.  
    - kind: ServiceAccount
  52.  
    name: flannel
  53.  
    namespace: kube-flannel
  54.  
    ---
  55.  
    apiVersion: v1
  56.  
    kind: ServiceAccount
  57.  
    metadata:
  58.  
    name: flannel
  59.  
    namespace: kube-flannel
  60.  
    ---
  61.  
    kind: ConfigMap
  62.  
    apiVersion: v1
  63.  
    metadata:
  64.  
    name: kube-flannel-cfg
  65.  
    namespace: kube-flannel
  66.  
    labels:
  67.  
    tier: node
  68.  
    app: flannel
  69.  
    data:
  70.  
    cni-conf.json: |
  71.  
    {
  72.  
    "name": "cbr0",
  73.  
    "cniVersion": "0.3.1",
  74.  
    "plugins": [
  75.  
    {
  76.  
    "type": "flannel",
  77.  
    "delegate": {
  78.  
    "hairpinMode": true,
  79.  
    "isDefaultGateway": true
  80.  
    }
  81.  
    },
  82.  
    {
  83.  
    "type": "portmap",
  84.  
    "capabilities": {
  85.  
    "portMappings": true
  86.  
    }
  87.  
    }
  88.  
    ]
  89.  
    }
  90.  
    net-conf.json: |
  91.  
    {
  92.  
    "Network": "10.244.0.0/16",
  93.  
    "Backend": {
  94.  
    "Type": "vxlan"
  95.  
    }
  96.  
    }
  97.  
    ---
  98.  
    apiVersion: apps/v1
  99.  
    kind: DaemonSet
  100.  
    metadata:
  101.  
    name: kube-flannel-ds
  102.  
    namespace: kube-flannel
  103.  
    labels:
  104.  
    tier: node
  105.  
    app: flannel
  106.  
    spec:
  107.  
    selector:
  108.  
    matchLabels:
  109.  
    app: flannel
  110.  
    template:
  111.  
    metadata:
  112.  
    labels:
  113.  
    tier: node
  114.  
    app: flannel
  115.  
    spec:
  116.  
    affinity:
  117.  
    nodeAffinity:
  118.  
    requiredDuringSchedulingIgnoredDuringExecution:
  119.  
    nodeSelectorTerms:
  120.  
    - matchExpressions:
  121.  
    - key: kubernetes.io/os
  122.  
    operator: In
  123.  
    values:
  124.  
    - linux
  125.  
    hostNetwork: true
  126.  
    priorityClassName: system-node-critical
  127.  
    tolerations:
  128.  
    - operator: Exists
  129.  
    effect: NoSchedule
  130.  
    serviceAccountName: flannel
  131.  
    initContainers:
  132.  
    - name: install-cni-plugin
  133.  
    image: docker.io/flannel/flannel-cni-plugin:v1.1.2
  134.  
    #image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.2
  135.  
    command:
  136.  
    - cp
  137.  
    args:
  138.  
    - -f
  139.  
    - /flannel
  140.  
    - /opt/cni/bin/flannel
  141.  
    volumeMounts:
  142.  
    - name: cni-plugin
  143.  
    mountPath: /opt/cni/bin
  144.  
    - name: install-cni
  145.  
    image: docker.io/flannel/flannel:v0.21.2
  146.  
    #image: docker.io/rancher/mirrored-flannelcni-flannel:v0.21.2
  147.  
    command:
  148.  
    - cp
  149.  
    args:
  150.  
    - -f
  151.  
    - /etc/kube-flannel/cni-conf.json
  152.  
    - /etc/cni/net.d/10-flannel.conflist
  153.  
    volumeMounts:
  154.  
    - name: cni
  155.  
    mountPath: /etc/cni/net.d
  156.  
    - name: flannel-cfg
  157.  
    mountPath: /etc/kube-flannel/
  158.  
    containers:
  159.  
    - name: kube-flannel
  160.  
    image: docker.io/flannel/flannel:v0.21.2
  161.  
    #image: docker.io/rancher/mirrored-flannelcni-flannel:v0.21.2
  162.  
    command:
  163.  
    - /opt/bin/flanneld
  164.  
    args:
  165.  
    - --ip-masq
  166.  
    - --kube-subnet-mgr
  167.  
    resources:
  168.  
    requests:
  169.  
    cpu: "100m"
  170.  
    memory: "50Mi"
  171.  
    securityContext:
  172.  
    privileged: false
  173.  
    capabilities:
  174.  
    add: ["NET_ADMIN", "NET_RAW"]
  175.  
    env:
  176.  
    - name: POD_NAME
  177.  
    valueFrom:
  178.  
    fieldRef:
  179.  
    fieldPath: metadata.name
  180.  
    - name: POD_NAMESPACE
  181.  
    valueFrom:
  182.  
    fieldRef:
  183.  
    fieldPath: metadata.namespace
  184.  
    - name: EVENT_QUEUE_DEPTH
  185.  
    value: "5000"
  186.  
    volumeMounts:
  187.  
    - name: run
  188.  
    mountPath: /run/flannel
  189.  
    - name: flannel-cfg
  190.  
    mountPath: /etc/kube-flannel/
  191.  
    - name: xtables-lock
  192.  
    mountPath: /run/xtables.lock
  193.  
    volumes:
  194.  
    - name: run
  195.  
    hostPath:
  196.  
    path: /run/flannel
  197.  
    - name: cni-plugin
  198.  
    hostPath:
  199.  
    path: /opt/cni/bin
  200.  
    - name: cni
  201.  
    hostPath:
  202.  
    path: /etc/cni/net.d
  203.  
    - name: flannel-cfg
  204.  
    configMap:
  205.  
    name: kube-flannel-cfg
  206.  
    - name: xtables-lock
  207.  
    hostPath:
  208.  
    path: /run/xtables.lock
  209.  
    type: FileOrCreate
学新通

这个时候各节点就正常了

加入node节点

打开之前的文件 $HOME/.kube/config,放到node节点相同的位置

  1.  
    # 将主节点的内容拷贝一下,粘贴到下面的文件
  2.  
    vi ~/.kube/config
  3.  
     
  4.  
    # 去主节点查看 kubeadm-init.log 内容,重点是最后的一行
  5.  
    kubeadm join 172.16.11.23:6443 --token hwrdb3.qnjvvmgi3mose4ts --discovery-token-ca-cert-hash sha256:a26c63dab6c5936041c5aade8ef7e7d99f91ee4723b476e1fbda39153ae6a58b

FAQ

  1. pod coredns 状态异常

答:检查一下firewalld状态 "systemctl status firewalld.service",看看是不是之前的步骤中,忘记了执行关闭防火墙这一步。
  1. node节点显示NotReady,并且describe之后显示:Node node06 status is now: NodeHasSufficientPID

答:检查一下,node节点要执行的步骤是否有遗漏操作,导致node节点没有docker镜像。你可以通过命令"docker images"来检查。

欢迎小伙伴就部署中碰到的问题与我交流

你可能会想看这些文章

k8s集群恢复与重启

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibckhi
系列文章
更多 icon
同类精品
更多 icon
继续加载