手把手教你用 kubeadm 安装 k8s v1.20

本贴最后更新于 1155 天前,其中的信息可能已经东海扬尘

kubeadmin 安装 k8s v1.20

一、环境准备

kubernetes.jpg

PS:本次实验采用单 mster 节点,两个 worker 节点组成 kubernetes 集群,版本是 v1.20.9。由于是笔记本自建虚拟机,所以配置相对低一些,有条件的同学可以适当调高配置

操作系统 角色 硬盘 CPU & MEM
Centos 7.9 master 40GB 4 核 8G
Centos 7.9 node01 40GB 2 核 4G
Centos 7.9 node02 40GB 2 核 4G

二、安装容器运行时---Docker

2.1、添加阿里镜像源

sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2.2、安装 docker

yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7  containerd.io-1.4.6

2.3、启动

systemctl enable docker --now

2.4、配置加速

这里额外添加了 docker 的生产环境核心配置 cgroup

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

三、安装 kubernetes

3.1、基础环境

所有机器执行以下操作

#各个机器设置自己的域名
hostnamectl set-hostname xxxx


# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

#关闭swap
swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab

#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

3.2、安装 kubelet、kubeadm、kubectl

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
   http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF


sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes

sudo systemctl enable --now kubelet

kubelet 现在每隔几秒就会重启,因为它陷入了一个等待 kubeadm 指令的死循环

3.3、下载镜像

sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
   
chmod +x ./images.sh && ./images.sh

3.4、配置 hosts 解析

echo -e "192.168.1.10 master \n192.168.1.11 node01\n192.168.1.12 node02" >> /etc/hosts

3.5、master 节点初始化集群

注意:
1. 所有网络范围不重叠
2. advertise-address 等于 master 的 IP
3. plane-endpoint 等于填写在 hosts 文件中的 master

kubeadm init \
--apiserver-advertise-address=192.168.1.10 \
--control-plane-endpoint=master \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.168.0.0/16

初始化成功
提示信息保存一下,以后加入主节点或 worker 节点使用

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join master:6443 --token 4qw94d.i5fa5tgtztd5o5nu \
    --discovery-token-ca-cert-hash sha256:ec7c8b230762c533c055493a4e4c210e7a5587a8b153cf38da7d5e2bbeb621c6 \
    --control-plane 

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join master:6443 --token 4qw94d.i5fa5tgtztd5o5nu \
    --discovery-token-ca-cert-hash sha256:ec7c8b230762c533c055493a4e4c210e7a5587a8b153cf38da7d5e2bbeb621c6 

3.6、执行初始化命令

仅在 master 节点执行如下命令

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

3.7、加入剩余两个 worker 节点

两个 worker 节点分别执行如下命令(在上面有)

kubeadm join master:6443 --token 4qw94d.i5fa5tgtztd5o5nu \
>     --discovery-token-ca-cert-hash 
sha256:ec7c8b230762c533c055493a4e4c210e7a5587a8b153cf38da7d5e2bbeb621c6 
[preflight] Running pre-flight checks
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.7. Latest validated version: 19.03
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

3.8、验证

可以看到我们的集群已经搭建完成,但是节点都是 Notready,是因为 k8s 各节点通信通过第三方的网络插件,所以接下来我们需要安装第三方网络插件

[root@master ~]# kubectl get node 
NAME     STATUS     ROLES                  AGE     VERSION
master   NotReady   control-plane,master   7m51s   v1.20.9
node01   NotReady   <none>                 2m3s    v1.20.9
node02   NotReady   <none>                 2m      v1.20.9

四、安装 calico 网络组件

4.1、下载 calico 的配置文件

yum isntall -y wget && 
wget https://docs.projectcalico.org/manifests/calico.yaml --no-check-certificate 

4.2、修改配置文件

注意:
calico 默认的 CALICO_IPV4POOL_CIDR 是 192.168.0.0/16,在上面 3.5、master 节点初始化集群步骤中,为避免网段重复。所以更改了 CIDR 的值,所以在 calico 的配置文件中,要修改成相同的值,并取消注释
如下图:
在这里插入图片描述

4.3、通过命令安装 calico 网络插件

kubectl create -f calico.yaml
##等待4-5分钟
kubectl get pod -n kube-system
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-659bd7879c-jndd7   1/1     Running   0          8m20s
calico-node-f7jbc                          1/1     Running   0          8m20s
calico-node-x64f6                          1/1     Running   0          8m20s
calico-node-zcxl2                          1/1     Running   0          8m20s
coredns-5897cd56c4-js7h4                   1/1     Running   0          37m
coredns-5897cd56c4-t46w8                   1/1     Running   0          37m
etcd-master                                1/1     Running   0          37m
kube-apiserver-master                      1/1     Running   0          37m
kube-controller-manager-master             1/1     Running   0          37m
kube-proxy-4tmqd                           1/1     Running   0          31m
kube-proxy-d5sxh                           1/1     Running   0          37m
kube-proxy-hh6fv                           1/1     Running   0          31m
kube-scheduler-master                      1/1     Running   0          37m

当我们看到都是 Running 的时候,就代表我们 calico 网络插件安装成功了,
此时我们去看一下 node 的状态,发现已经全部 ready 了,至此 kubernetes 安装成功

[root@master ~]# kubectl get node 
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   39m   v1.20.9
node01   Ready    <none>                 33m   v1.20.9
node02   Ready    <none>                 33m   v1.20.9
  • Kubernetes

    Kubernetes 是 Google 开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理。

    110 引用 • 54 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...