栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

kubeadmin安装k8s v1.20

文章目录
  • kubeadmin安装k8s v1.20
    • 一、环境准备
    • 二、安装容器运行时---Docker
      • 2.1、添加阿里镜像源
      • 2.2、安装docker
      • 2.3、启动
      • 2.4、配置加速
    • 三、安装kubernetes
      • 3.1、基础环境
      • 3.2、安装kubelet、kubeadm、kubectl
      • 3.3、下载镜像
      • 3.4、配置hosts解析
      • 3.5、master节点初始化集群
      • 3.6、执行初始化命令
      • 3.7、加入剩余两个worker节点
      • 3.8、验证
    • 四、安装calico网络组件
      • 4.1、下载calico的配置文件
      • 4.2、修改配置文件
      • 4.3、通过命令安装calico网络插件

一、环境准备

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

操作系统角色硬盘CPU & MEM
Centos 7.9master40GB4核8G
Centos 7.9node0140GB2核4G
Centos 7.9node0240GB2核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 < 
3.2、安装kubelet、kubeadm、kubectl 
cat < 

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 node01n192.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                    2m3s    v1.20.9
node02   NotReady                    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                     33m   v1.20.9
node02   Ready                     33m   v1.20.9
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号