K8s kubeadm 安装

首先准备3台VM,1 master + 2 work nodes。本文提供的是centos7 的安装方式。
首先在机器上都安装docker yum install -y docker

Master node:

  1. 配置kubernetes yum 源。
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](https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg) exclude=kube* EOF 

  1. Set SELinux in permissive mode (effectively disabling it)
setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config  
  1. 安装kubernetes
    yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes 

    systemctl enable kubelet && systemctl start kubelet 
  1. 设置网络:
    sysctl net.bridge.bridge-nf-call-iptables=1

  2. 配置docker代理
    在类似于/usr/lib/systemd/system/docker.service里面加入proxy信息。

[Service]
Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" "HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
修改之后需要重启docker 服务。

  1. 初始化集群,一定要加上--pod-network-cidr,否则后续网络部分配不通。

    kubeadm init --pod-network-cidr=10.244.0.0/16

  2. 安装flannel
    https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml
    kubectl apply -f kube-flannel.yml

Work nodes:

  1. 配置yum源:
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](https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg) exclude=kube* EOF

  1. 设置linux安全模式
Set SELinux in permissive mode (effectively disabling it) setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config  
  1. 安装kubernetes
    yum install -y kubelet kubeadm --disableexcludes=kubernetes systemctl enable kubelet && systemctl start kubelet

  2. 把node加入集群里
    kubeadm join 15.119.89.44:6443 --token j2u3ty.huksbtt2flywldk9 \ --discovery-token-ca-cert-hash sha256:512ef7bfa201cfa9f1e0e56a9f48e25ccc0b2f98f199efc721c208d41280434f

image.png

注意: 在执行过程中如果遇到问题想重新安装的话,可以使用kubeadm reset,记住要删除$HOME/.kube,否则当重新init的时候,执行命令时会出现证书相关的问题Unable to connect to the server: x509: certificate signed by unknown authority

Q/A

Q1:用Kubeadm安装K8s后,kube-flannel-ds一直CrashLoopBackOff
A:安装的时候执行kubeadm init的时候没有指定--pod-network-cidr 10.244.0.0/16参数

image.png

Q2: kubernetes忘记token或者token过期怎么加入k8s集群
可以通过一下命令重新生成一个token

kubeadm token create --print-join-command --ttl=0
然后copy生成的token,执行kubeadm join like: kubeadm join 15.119.89.44:6443 --token 4mdf46.5zv9o4r5o4y932qg --discovery-token-ca-cert-hash sha256:daca5bdc31a15729637b5472df1114ffa5afe3f04c8d2ee96675f0ae9cd66b85

Q3: 有时候遇到搭好的环境无法下载image?pod状态一直pending
A:需要配置docker proxy。具体docker配置的位置可通过systemctl status docker,然后在类似于/usr/lib/systemd/system/docker.service里面加入proxy信息。
[Service] Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" "HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
配置完后,一定需要执行sudo systemctl daemon-reload && sudo systemctl restart docker。一定要加上sudo,无论是不是root用户,否则不生效。
Q4:搭好的环境执行kubectl get node,发现node的状态为Not Ready

image.png

A:通过journalctl排查
journalctl -f

May 25 10:44:00 shc-sma-sd44 kubelet[1566]: E0525 10:44:00.817714    1566 kubelet.go:2187] Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
May 25 10:44:00 shc-sma-sd44 dockerd-current[349]: 2020-05-25 02:44:00.902776 I | embed: rejected connection from "15.119.89.46:22292" (error "tls: first record does not look like a TLS handshake", ServerName "")

发现网络没有配置,需要安装flannel。

你可能感兴趣的:(K8s kubeadm 安装)