linux12k8s --> KubeAdmin安装k8s

Posted FikL-09-19

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux12k8s --> KubeAdmin安装k8s相关的知识,希望对你有一定的参考价值。

KubeAdmin安装k8s

一、准备环境

1、部署软件、系统要求

软件版本
CentosCentOS Linux release 7.5及以上
Docker19.03.12
KubernetesV0.13.0
FlannelV1.19.1
Kernel-lmkernel-lt-4.4.245-1.el7.elrepo.x86_64.rpm
Kernel-lm-devekernel-lt-devel-4.4.245-1.el7.elrepo.x86_64.rpm

2、节点规划

  • IP建议采用192网段,避免与kubernetes内网冲突
准备机器IP配置系统内核版本
k8s-master1192.168.15.1112核2G4.4+
k8s-node1192.168.15.1122核2G4.4+
k8s-node2192.168.15.1132核2G4.4+

二、kubeadm安装k8s

服务器配置至少是2G2核的。如果不是则可以在集群初始化后面增加 --ignore-preflight-errors=NumCPU

一、内核优化脚本(所有机器)

[root@k8s-m-01 ~]# cat base.sh  
#!/bin/bash

# 1、修改主机名和网卡
hostnamectl set-hostname $1 &&\\
sed -i "s#111#$2#g" /etc/sysconfig/network-scripts/ifcfg-eth[01] &&\\
systemctl restart network &&\\
# 2、关闭selinux和防火墙和ssh连接
setenforce 0 &&\\
sed -i 's#enforcing#disabled#g' /etc/selinux/config &&\\
systemctl disable --now firewalld &&\\
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config &&\\
systemctl restart sshd &&\\
# 3、关闭swap分区
# 一旦触发 swap,会导致系统性能急剧下降,所以一般情况下,K8S 要求关闭 swap
# cat /etc/fstab 
# 注释最后一行swap,如果没有安装swap就不需要
swapoff -a &&\\
echo 'KUBELET_EXTRA_ARGS="--fail-swap-on=false"' > /etc/sysconfig/kubelet &&\\ #忽略swap

# 4、修改本机hosts文件
cat >>/etc/hosts <<EOF
192.168.15.111  k8s-m-01 m1
192.168.15.112  k8s-n-01 n1
192.168.15.113  k8s-n-02 n2
EOF

#  5、配置镜像源(国内源)
# 默认情况下,CentOS 使用的是官方 yum 源,所以一般情况下在国内使用是非常慢,所以我们可以替换成 国内的一些比较成熟的 yum 源,例如:清华大学镜像源,网易云镜像源等等
rm -rf /ect/yum.repos.d/* &&\\
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &&\\
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &&\\
yum clean all &&\\
yum makecache &&\\
# 6、更新系统
#查看内核版本,若内核高于4.0,可不加--exclud选项
yum update -y --exclud=kernel* &&\\
# 由于 Docker 运行需要较新的系统内核功能,例如 ipvs 等等,所以一般情况下,我们需要使用 4.0+以上版 本的系统内核要求是 4.18+,如果是 CentOS 8 则不需要内核系统更新
# 7、安装基础常用软件,是为了方便我们的日常使用
yum install wget expect vim net-tools ntp bash-completion ipvsadm ipset jq iptables conntrack sysstat libseccomp ntpdate -y &&\\
# 8、更新系统内核
#如果是centos8则不需要升级内核
cd /opt/ &&\\
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-5.4.127-1.el7.elrepo.x86_64.rpm &&\\
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-devel-5.4.127-1.el7.elrepo.x86_64.rpm &&\\

# 官网https://elrepo.org/linux/kernel/el7/x86_64/RPMS/

# 9、安装系统内容
yum localinstall /opt/kernel-lt* -y &&\\  
# 10、调到默认启动
grub2-set-default  0 && grub2-mkconfig -o /etc/grub2.cfg &&\\
# 11、查看当前默认启动的内核 
grubby --default-kernel &&\\
reboot
# 安装完成就是5.4内核

二、 免密脚本(所有机器)

# 1、免密
[root@k8s-master-01 ~]# ssh-keygen -t rsa
[root@k8s-master-01 ~]# for i in m1 n1 n2;do ssh-copy-id -i ~/.ssh/id_rsa.pub root@$i;done
# 方式二:
root@openvpn ~]# ssh-keygen
[root@openvpn ~]# cat ssh-jump.sh  
#!/bin/bash
for i in 'm1''n1' 'n2'
do
expect -c "
spawn ssh-copy-id -i root@$i
expect {
\\"(yes/no)\\" {send \\"yes\\r\\";exp_continue}
\\"password\\" {send \\"123\\r\\";exp_continue}
}
"
done


# 在集群当中,时间是一个很重要的概念,一旦集群当中某台机器时间跟集群时间不一致,可能会导致集群面 临很多问题。所以,在部署集群之前,需要同步集群当中的所有机器的时间
# 2、时间同步写入定时任务
crontab -e
*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null

三、安装IPVS和内核优化(所有机器)

# 1、安装 IPVS 、加载 IPVS 模块 (所有节点)
ipvs 是系统内核中的一个模块,其网络转发性能很高。一般情况下,我们首选 ipvs
[root@k8s-n-01 ~]# cat /etc/sysconfig/modules/ipvs.modules 
#!/bin/bash 
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_fo ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack" 

for kernel_module in ${ipvs_modules}; do 
/sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1 
if [ $? -eq 0 ]; then 
/sbin/modprobe ${kernel_module} 
fi 
done 
# 2、授权(所有节点)
[root@k8s-n-01 ~]# chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs
# 3、内核参数优化(所有节点)
加载IPVS 模块、生效配置
内核参数优化的主要目的是使其更适合 kubernetes 的正常运行
[root@k8s-n-01 ~]# cat /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1 
net.bridge.bridge-nf-call-iptables = 1   # 可以之间修改这两个
net.bridge.bridge-nf-call-ip6tables = 1  # 可以之间修改这两个
fs.may_detach_mounts = 1 
vm.overcommit_memory=1 
vm.panic_on_oom=0 
fs.inotify.max_user_watches=89100 
fs.file-max=52706963 
fs.nr_open=52706963 
net.ipv4.tcp_keepalive_time = 600 
net.ipv4.tcp.keepaliv.probes = 3 
net.ipv4.tcp_keepalive_intvl = 15 
net.ipv4.tcp.max_tw_buckets = 36000 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp.max_orphans = 327680 
net.ipv4.tcp_orphan_retries = 3 
net.ipv4.tcp_syncookies = 1 
net.ipv4.tcp_max_syn_backlog = 16384 
net.ipv4.ip_conntrack_max = 65536 
net.ipv4.tcp_max_syn_backlog = 16384 
net.ipv4.top_timestamps = 0 
net.core.somaxconn = 16384
# 立即生效 
sysctl --system

四、安装Docker(所有机器)

Docker 主要是作为 k8s 管理的常用的容器工具之一
1、CentOS 7版
进入华为镜像站选择docker源参考依次安装就好

# 1、若您安装过docker,需要先删掉,之后再安装依赖:
sudo yum remove docker docker-common docker-selinux docker-engine &&\\
sudo yum install -y yum-utils device-mapper-persistent-data lvm2   &&\\
# 2、安装doceker源
wget -O /etc/yum.repos.d/docker-ce.repo https://repo.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo &&\\
# 3、软件仓库地址替换
sudo sed -i 's+download.docker.com+repo.huaweicloud.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo &&\\
# 4、重新生成源
yum clean all &&\\
yum makecache  &&\\
# 5、安装docker
sudo yum makecache fast &&\\
sudo yum install docker-ce -y &&\\
# 6、设置docker开机自启动
systemctl enable --now docker.service

# 如果安装centos7步骤若出现报错:
GPG key retrieval failed: [Errno 14] curl#6 - "Could not resolve host: download.docker.com; Unknown error"
# 原因:无法解析主机
# 解决方法1:临时添加114域名解析到配置文件(临时,且不可重启网卡)
[root@k8s-master1 ~] cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 114.114.114.114
nameserver 223.5.5.5
# 解决方法2:添加到eth0网卡配置文件内(永久,需重启网卡)
DNS1=114.114.114.114
2、CentOS 8版
# 下载rpm包
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io1.2.13-3.2.el7.x86_64.rpm
# 安装
yum install containerd.io-1.2.13-3.2.el7.x86_64.rpm -y
# 安装扩展
yum install -y yum-utils device-mapper-persistent-data lvm2
# 配置源
yum-config-manager --add-repo
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装docker
yum install docker-ce -y

# 7、创建docker目录、启动服务(所有节点)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://8mh75mhz.mirror.aliyuncs.com"]
}

五、安装kubernetes(所有机器)

# 1、安装kubernetes(所有节点)
cat <<EOF > /etc/yum.repos.d/kubernetes.repo    #华为源
[kubernetes] 
name=Kubernetes 
baseurl=https://repo.huaweicloud.com/kubernetes/yum/repos/kubernetes-el7-\\$basearch 
enabled=1 
gpgcheck=1 
repo_gpgcheck=0
gpgkey=https://repo.huaweicloud.com/kubernetes/yum/doc/yum-key.gpg https://repo.huaweicloud.com/kubernetes/yum/doc/rpm-package-key.gpg 
EOF
#方式二
[root@k8s-n-02 yum.repos.d]# cat kubernetes.repo  #阿里源
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
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
# 方式三
[root@k8s-m-01 yum.repos.d]# cat kubernetes.repo  #清华源
[kubernetes]
name=kubernetes
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-\\$basearch
enabled=1
gpgcheck=0
# 2、重新生成源
yum clean all &&\\
yum makecache 
# 3、此处指定版本下载为了与下边初始化节点版本对应
yum install kubectl-1.21.2 kubeadm-1.21.2 kubelet-1.21.2 -y 
# 4、此时只需开机自启,无需启动,因为还未初始化
systemctl enable --now kubelet

六、m01主节点初始化配置

# 1、初始化master节点
# --image-repository
kubeadm init \\
--image-repository=registry.cn-hangzhou.aliyuncs.com/k8sos \\
--kubernetes-version=v1.21.2 \\
--service-cidr=10.96.0.0/12 \\
--pod-network-cidr=10.244.0.0/16 \\
--ignore-preflight-errors=Swap

# 过程中可监控初始化日志,出现successfully即为成功!
# tailf /var/log/messages
# [root@k8s-master1 ~] cat /var/log/messages | grep successfully
# Mar 24 21:02:07 k8s-master1 containerd: time="2021-03-24T21:02:07.063840628+08:00" level=info msg="containerd successfully booted in 0.181480s"

# 2、 初始化完成查看kubernetes
[root@k8s-m-01 ~]# systemctl status kubelet

# 3、配置 kubernetes 用户信息
master节点执行
# 建立用户集群权限 
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 4、如果使用root用户,则添加至环境变量
[root@k8s-m-01 ~]# export KUBECONFIG=/etc/kubernetes/admin.conf

# 5、增加命令提示(选做)
所有节点执行
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

# 6、安装集群网络插件(主节点)
kubernetes 需要使用第三方的网络插件来实现 kubernetes 的网络功能,这样一来,安装网络插件成为必要前提
第三方网络插件有多种,常用的有 flanneld、calico 和 cannel(flanneld+calico),不同的网络组件,都提供基本的网络功能,为各个 Node 节点提供 IP 网络等
# 此方式可能会失败,建议复制下方flannel.yaml直接部署~
docker pull registry.cn-hangzhou.aliyuncs.com/k8sos/flannel:v0.12.0-amd64 ;\\
docker tag registry.cn-hangzhou.aliyuncs.com/k8sos/flannel:v0.12.0-amd64 quay.io/coreos/flannel:v0.12.0-amd64
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

七、flannel.yaml

# 1、编写flannel.yaml
cat >> /root/flannel.yaml <<EOF
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: psp.flannel.unprivileged
  annotations:
    seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
    seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
    apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
    apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
  privileged: false
  volumes:
  - configMap
  - secret
  - emptyDir
  - hostPath
  allowedHostPaths:
  - pathPrefix: "/etc/cni/net.d"
  - pathPrefix: "/etc/kube-flannel"
  - pathPrefix: "/run/flannel"
  readOnlyRootFilesystem: false
  # Users and groups
  runAsUser:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  # Privilege Escalation
  allowPrivilegeEscalation: false
  defaultAllowPrivilegeEscalation: false
  # Capabilities
  allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
  defaultAddCapabilities: []
  requiredDropCapabilities: []
  # Host namespaces
  hostPID: false
  hostIPC: false
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  # SELinux
  seLinux:
    # SELinux is unused in CaaSP
    rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups: ['extensions']
  resources: ['podsecuritypolicies']
  verbs: ['use']
  resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-system
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-system
  labels:
    tier: node
    app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
      hostNetwork: true
      priorityClassName: system-node-critical
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: registry.cn-hangzhou.aliyuncs.com/alvinos/flanned:v0.13.1-rc1
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
        image: registry.cn-hangzhou.aliyuncs.com/alvinos/flanned:v0.13.1-rc1
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN", "NET_RAW"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg
EOF

# 2、查看镜像
[root@k8s-m-01 ~]# cat flannel.yaml | grep image
        image: registry.cn-hangzhou.aliyuncs.com/alvinos/flanned:v0.13.1-rc1
        image: registry.cn-hangzhou.aliyuncs.com/alvinos/flanned:v0.13.1-rc1

# 3、安装网络插件
[root@k8s-m-01 ~]# kubectl apply -f flannel.yaml

# 4、检查集群初始化状态
# 方式一:
[root@k8s-m-01 ~]# kubectl get node  
NAME       STATUS   ROLES                  AGE   VERSION
k8s-m-01   Ready    control-plane,master   16m   v1.21.2
# 方式二:
[root@k8s-m-01 ~]#  kubectl get pods -o wide -n kube-system
NAME                               READY   STATUS    RESTARTS   AGE     IP               NODE       NOMINATED NODE   READINESS GATES
coredns-978bbc4b6-54ng4            1/1     Running   0          17m     10.244.0.2       k8s-m-01   <none>           <none>
coredns-978bbc4b6-tnng7            1/1     Running   0          17m     10.244.0.3       k8s-m-01   <none>           <none>
etcd-k8s-m-01                      1/1     Running   0          18m     192.168.15.111   k8s-m-01   <none>           <none>
kube-apiserver-k8s-m-01            1/1     Running   0          18m     192.168.15.111   k8s-m-01   <none>           <none>
kube-controller-manager-k8s-m-01   1/1     Running   0          18m     192.168.15.111   k8s-m-01   <none>           <none>
kube-flannel-ds-stcll              1/1     Running   0          3m45s   192.168.15.111   k8s-m-01   <none>           <none>
kube-proxy-xk7hm                   1/1     Running   0          17m     192.168.15.111   k8s-m-01   <none>           <none>
kube-scheduler-k8s-m-01            1/1     Running   0          18m     192.168.15.111   k8s-m-01   <none>           <none>
# READY状态为1/1即为成功!

七、将其他节点加入集群

# 1、主节点创建集群
node节点要查看token,主节点生成token可重复执行查看,不会改变
[root@k8s-m-01 ~]# kubeadm token create --print-join-command
kubeadm join 192.168.15.111:6443 --token m8g1q7.eqxte0aao5vgalim --discovery-token-ca-cert-hash sha256:8db2681f9c8f812e0d650f77abdc02f6277b5a45d93c4f9b81ebf424676001d0

# 2、从节点加入集群
[root@k8s-n-01 yum.repos.d]# kubeadm join 192.168.15.111:6443 --token m8g1q7.eqxte0aao5vgalim --discovery-token-ca-cert-hash sha256:8db2681f9c8f812e0d650f77abdc02f6277b5a45d93c4f9b81ebf424676001d0

[root@k8s-n-02 yum.repos.d]# kubeadm join 192.168.15.111:6443 --token m8g1q7.eqxte0aao5vgalim --discovery-token-ca-cert-hash sha256:8db2681f9c8f812e0d650f77abdc02f6277b5a45d93c4f9b81ebf424676001d0

#3、NDS测试
[root@k8s-m-01 ~]# kubectl run test -it --rm --image=busybox:1.28.3
If you don't see a command prompt, try pressing enter.
/ # nslookup kubernetes #输入这条命令,成功后就是以下内容
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
/ #
#出现以上界面成功

node节点加入集群失败,状态为NotReady 解决方法

#node节点与master节点版本不一致
[root@gdx1 ~]# kubectl get node
NAME   STATUS     ROLES                  AGE   VERSION
gdx1   Ready      control-plane,master   73m   v1.20.2
gdx2   NotReady   <none>                 10m   v1.21.0
gdx3   NotReady   <none>                 26s   v1.21.0

解决办法:
# 默认下载是最新版本,难免出现版本不一致的问题,所以下载时指定同一版本才行
# 1.从节点删除下载版本重新指定版本格式:
yum remove kubectl kubeadm kubelet -y
yum install kubectl-1.20.2 kubeadm-1.20.2 kubelet-1.20.2 -y

#2.设置开机自启
systemctl enable --now kubelet

#3.重置nonde节点配置(因为上述已经加入过集群,会报错证书,配置文件,端口号已存在,需要格式化子节点配置)
[ERROR FileAvailable--etc-kubernetes-kubelet.conf]: /etc/kubernetes/kubelet.conf already exists
[ERROR Port-10250]: Port 10250 is in use
[ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists
[root@gdx2 ~]# kubectl reset  #报错以上内容执行此命令格式化子节点

#4.从集群移除状态为notready的node节点
[root@gdx1 ~]# kubectl delete node gdx3

#5.重新将node节点加入集群,此时需注意token值是否相同,如果多次生成token值,需确认最后生成的token值
注:此处做好在master节点重新生成一次token值用来node节点加入集群使用
[root@gdx1 ~]# kubeadm token create    --print-join-command 
kubeadm join 192.168.12.11:6443 --token fm0387.iqixomz5jmsukwsi     --discovery-token-ca-cert-hash sha256:d8ff83ffed5967000034d07b3da738ae4f1f0254e8417bb30c21f3ed15ac5d18
注:将生成结果在node节点执行

#6.将node节点重新加入集群
[root@gdx2 ~]# kubeadm join 192.168.12.11:6443 --token fm0387.iqixomz5jmsukwsi     --discovery-token-ca-cert-hash sha256:d8ff83ffed5967000034d07b3da738ae4f1f0254e8417bb30c21f3ed15ac5d18

八、如果测试成功,无需执行以下命令

# node节点加入集群失败,状态为NotReady 解决方法

# 1、加入集群报错解决
1> 报错原因1
从节点加入集群可能会出现如下报错:
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher

PS:前提安装Docker+启动,再次尝试加入节点!

# 报错原因:
	swap没关,一旦触发 swap,会导致系统性能急剧下降,所以一般情况下,所以K8S 要求关闭 swap
# 解决方法:
	1>  执行以下三条命令后再次执行添加到集群命令:
		modprobe br_netfilter
		echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
		echo 1 > /proc/sys/net/ipv4/ip_forward

	2> 追加 --ignore-preflight-errors=Swap 参数重新执行即可!
[root@k8s-node1 ~] kubeadm join 192.168.15.11:6443 --token iypm65.p5nmdzzw1zifxy6c --discovery-token-ca-cert-hash sha256:8bdbe324980e3350aaa3b9cea58edf576dc0a6d937da6b7bff6dbe6a01e0b525 --ignore-preflight-errors=Swap

2> 报错原因2
# 报错原因:
	可能是内核参数忘记优化所有节点都需优化

# 解决方法:
	回到上面第七小节,复制粘贴优化参数即可: cat > /etc/sysctl.d/k8s.conf <<EOF

3> 报错原因3
ode节点加入集群失败,状态为NotReady
情况1:软件版本不一致
[root@gdx1 ~]# kubectl get node
NAME   STATUS     ROLES                  AGE   VERSION
gdx1   Ready      control-plane,master   73m   v1.20.2
gdx2   NotReady   <none>                 10m   v1.21.0
gdx3   NotReady   <none>                 26s   v1.21.0

# 原因分析:
	默认下载是最新版本,难免出现版本不一致的问题,所以下载时指定同一版本才行
# 解决方法:
	yum install kubectl-1.20.2 kubeadm-1.20.2 kubelet-1.20.2 -y	# 主从节点安装指定版本格式
	kubectl reset	# 重新初始化
	kubeadm join 192.168.15.11:6443 --token fm0387.iqixomz5jmsukwsi     --discovery-token-ca-cert-hash sha256:d8ff83ffed5967000034d07b3da738ae4f1f0254e8417bb30c21f3ed15ac5d18	# 重新将node节点加入集群

    # PS:查看token值命令
    [root@gdx1 ~]# kubeadm token list
    kubeadm join 192.168.15.11:6443 --token fm0387.iqixomz5jmsukwsi     --discovery-token-ca-cert-hash sha256:d8ff83ffed5967000034d07b3da738ae4f1f0254e8417bb30c21f3ed15ac5d18 
    8ff83ffed5967000034d07b3da738ae4f1f0254e8417bb30c21f3ed15ac5d18

情况2:软件版本一致,touken值可能不对
    [root@gdx1 ~]# kubectl get node
    NAME   STATUS     ROLES                  AGE   VERSION
    gdx1   Ready      control-plane,master   73m   v1.20.2
    gdx2   NotReady   <none>                 10m   v1.20.2
    gdx3   NotReady   <none>                 26s   v1.20.2

# node节点为notready状态,加入从节点时报错:
	[ERROR FileAvailable--etc-kubernetes-kubelet.conf]: /etc/kubernetes/kubelet.conf already exists
	[ERROR Port-10250]: Port 10250 is in use
	[ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists
	# 原因分析:因为多次生成主节点的token值,导致token值加入不一致或输入错误

# 解决方法:
    # 从集群移除状态为notready的node节点
    [root@gdx1 ~]# kubectl delete node gdx3

    # node节点重置touken值以及证书端口号等信息
    [root@gdx3 ~]#  kubeadm reset

    # 主节点重新创建token
    [root@gdx1 ~]# kubeadm token create    --print-join-command 
    kubeadm join 192.168.15.11:6443 --token fm0387.iqixomz5jmsukwsi     --discovery-token-ca-cert-hash sha256:d8ff83ffed5967000034d07b3da738ae4f1f0254e8417bb30c21f3ed15ac5d18 

    # 将node节点重新加入集群
    kubeadm join 192.168.15.11:6443 --token fm0387.iqixomz5jmsukwsi     --discovery-token-ca-cert-hash sha256:d8ff83ffed5967000034d07b3da738ae4f1f0254e8417bb30c21f3ed15ac5d18

以上是关于linux12k8s --> KubeAdmin安装k8s的主要内容,如果未能解决你的问题,请参考以下文章

mac上kubeadmin安装k8s(完整)

linux12k8s --> 02理论介绍,架构图

linux12k8s --> 17图形化界面

linux12k8s --> 05实战入门

linux12k8s --> 03二进制安装

linux12k8s -->16 kubernetes必备面试题