(52条消息) Ubuntu20.04安装Kubernetes v1.22.1版本

Ubuntu20.04安装k8s Kubernetes v1.22.1版本

  • 安装步骤
    • 准备
      • 环境要求
      • 1.关闭swap分区
      • 2.更改net.bridge.bridge-nf-call-iptables的值为1.(Ubuntu 20.04默认为1)
      • 3.安装Docker
    • 安装k8s
      • 配置源
      • 添加key
      • 作为节点加入集群
    • 初始化
      • 清空信息:
      • 问题报错的解决
      • 测试

初始化的过程遭遇了崩溃的长久报错,查了好多资料改了好久终于成功了!


安装步骤

准备

环境要求

Ubuntu机器的硬件环境的要求:

cpu:2c
memory:4G/2G

软件环境的要求:

root@node138:/etc/apt/sources.list.d# lsb_release -a
No LSB modules are available.
Distributor ID:Ubuntu
Description:Ubuntu 20.04 LTS
Release:20.04
Codename:focal

1.关闭swap分区

swap分区:交换分区,从磁盘里分一块空间来充当内存使用,性能比真正的物理内存要差
docker容器在内存里运行 --》 k8s不允许容器到swap分区运行,要关闭swap分区–》所以关闭swap分区是k8s为了追求高性能

[root@kafka02 ~]# swapoff -a   临时关闭

[root@kafka02 ~]# cat /proc/swaps
FilenameTypeSizeUsedPriority
# 永久关闭
[root@kafka02 ~]# vim /etc/fstab
注释掉swap那一行,每台机器都要配置

2.更改net.bridge.bridge-nf-call-iptables的值为1.(Ubuntu 20.04默认为1)

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.安装Docker

Ubuntu安装docker可以看笔者之前写的文章!按照官方一步一步安装的!
Ubuntu安装Docker

安装k8s

sudo apt install -y apt-transport-https curl
sudo apt-get install -y kubelet kubeadm kubectl

但是这样直接安装kubelet、kubeadm、kubectl会报如下错(只截取一部分)

root@node138:/etc/apt# sudo apt-get install -y kubelet kubeadm kubectl

No apt package "kubeadm", but there is a snap with that name.
Try "snap install kubeadm"

所以接下来需要配置k8s相关安装的源

配置源

修改Sources.list
可以打开 /etc/apt/sources.list 文件,添加一行

deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main

或者在/etc/apt/sources.list.d下添加一个文件 kubernetes.list

文件内容:

deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main

然后执行apt-get update

执行apt-get update

如果这个时候报错如下,说明我们没有导入aliyun的key

root@node138:~# apt-get update
Get:1 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial InRelease [9,383 B]
Err:1 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY FEEA9169307EA071 NO_PUBKEY 8B57C5C2836F4BEB

接下来我们添加aliyun的key

添加key

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add

root@node138:/etc/apt/sources.list.d# curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2537  100  2537    0     0  14836      0 --:--:-- --:--:-- --:--:-- 14836
OK

一定要记住添加了key之后重新update!!!

root@node138:/etc/apt/sources.list.d# apt-get update
Get:1 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial InRelease [9,383 B]
Ign:2 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
Get:2 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages [49.4 kB]
Hit:3 http://cn.archive.ubuntu.com/ubuntu focal InRelease
Hit:4 https://download.docker.com/linux/ubuntu focal InRelease
Get:5 http://cn.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:7 http://cn.archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:8 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,175 kB]
Get:9 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [853 kB]
Fetched 2,406 kB in 20s (120 kB/s)
Reading package lists... Done

接下来就能正常安装了

apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated

作为节点加入集群

因为笔者是利用这台Ubuntu做node节点的,所以接下来可以直接输入master中的命令加入集群

kubeadm join 192.168.254.129:6443 --token t5mat5.4312obzrfk4bnnu0
–discovery-token-ca-cert-hash sha256:05b0b09ce2a915ed6e3009dff885a52b95fe02359ae203a641dfcdf15819115a

注意这个token有效期只有24h喔 过期要重新新建!
kubeadm token create

root@node138:/etc/apt/sources.list.d# kubeadm join 192.168.254.129:6443 --token t5mat5.4312obzrfk4bnnu0 > --discovery-token-ca-cert-hash sha256:05b0b09ce2a915ed6e3009dff885a52b95fe02359ae203a641dfcdf15819115a
[preflight] Running pre-flight checks
[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.

在主master上查看 成功加入!

[root@kafka02 docker]# kubectl get nodes
NAME      STATUS   ROLES                  AGE     VERSION
kafka01   Ready    <none>                 3h16m   v1.22.1
kafka02   Ready    control-plane,master   8d      v1.22.1
node138   Ready    <none>                 49m     v1.22.1

接下来扩展一下初始化做master的操作

初始化

如果和笔者一样之前利用这台机器部署过k8s,必须要清空信息否则会报如下错

root@node138:/etc/docker# sudo kubeadm init
invalid or incomplete external CA: failure loading key for apiserver: couldn't load the private key file /etc/kubernetes/pki/apiserver.key: open /etc/kubernetes/pki/apiserver.key: no such file or directory
To see the stack trace of this error execute with --v=5 or higher

清空信息:

kubeadm reset

再重新init初始化

kubeadm init --kubernetes-version=v1.22.1 --pod-network-cidr=10.244.0.0/16

卡住一直不动

即要去拉去镜像 但是国内镜像下载k8s.gcr.io这种类型的镜像然后太慢了一直卡在这一步
此命令 kubeadm config images list 获取需要的docker镜像名称

kubeadm config images list

这是笔者的kubeadm版本需要的镜像版本

root@node138:/etc/docker# kubeadm config images list
k8s.gcr.io/kube-apiserver:v1.22.1
k8s.gcr.io/kube-controller-manager:v1.22.1
k8s.gcr.io/kube-scheduler:v1.22.1
k8s.gcr.io/kube-proxy:v1.22.1
k8s.gcr.io/pause:3.5
k8s.gcr.io/etcd:3.5.0-0
k8s.gcr.io/coredns/coredns:v1.8.4

问题报错的解决

所以我们要改阿里云的镜像去拉去这个

kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16

然后笔者是较新版本的Kubernetes,所以当拉取阿里云的registry.aliyuncs.com/google_containers/coredns:v1.8.4这个版本的镜像,拉取失败就会报这个错误

root@node138:/etc/docker# kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.22.1
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR ImagePull]: failed to pull image registry.aliyuncs.com/google_containers/coredns:v1.8.4: output: Error response from daemon: manifest for registry.aliyuncs.com/google_containers/coredns:v1.8.4 not found: manifest unknown: manifest unknown
, error: exit status 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

这时候我们需要手动安装一下

root@node138:/etc/docker# docker pull  registry.aliyuncs.com/google_containers/coredns

然后修改镜像的tag

root@node138:/etc/docker# docker tag registry.aliyuncs.com/google_containers/coredns:latest registry.aliyuncs.com/google_containers/coredns:v1.8.4

然后就可以安装成功了!!!
以下是笔者安装成功的样子

按照提示做接下来要进行的操作

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

这样master就配置好啦,然后可以在node机器上输入这个提示命令即可!

测试

在master机器上输入这条命令,可以查看子节点 (这是笔者用centos搭的master)

[root@kafka02 docker]# kubectl get nodes
NAME      STATUS   ROLES                  AGE     VERSION
kafka01   Ready    <none>                 3h16m   v1.22.1
kafka02   Ready    control-plane,master   8d      v1.22.1
node138   Ready    <none>                 49m     v1.22.1

可以在master上创建pod,成功创建能 get pod 即可!

[root@kafka02 docker]# kubectl run sc-nginx --image=nginx --port=7770
pod/sc-nginx created
[root@kafka02 docker]# kubectl get pod
NAME       READY   STATUS              RESTARTS   AGE
sc-nginx   0/1     ContainerCreating   0          7s
(0)

相关推荐