1. 关闭swap:

swapoff -a

#永久关闭 sudo vi /etc/fstab #/swap

2. 安装k8s

apt-get update && apt-get install -y apt-transport-https

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

cat «EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF

apt-get update apt-get install kubelet=1.20.15-00 kubeadm=1.20.15-00 kubectl=1.20.15-00

3. 生成默认配置

kubeadm config print init-defaults > k8s-init-default.yaml

4. 修改:

1
2
3
4
5
6
localAPIEndpoint.advertiseAddress #改为master ip
nodeRegistration.name #主机名
imageRepository: registry.aliyuncs.com/google_containers #指定仓库
kubernetesVersion: v1.20.15
networking
  podSubnet: 10.244.0.0/16 #新增

5. 测试初始化:

kubeadm init --dry-run

6. 查看使用的镜像版本:

1
2
3
4
5
6
7
8
9
kubeadm config images list

k8s.gcr.io/kube-apiserver:v1.20.15
k8s.gcr.io/kube-controller-manager:v1.20.15
k8s.gcr.io/kube-scheduler:v1.20.15
k8s.gcr.io/kube-proxy:v1.20.15
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.13-0
k8s.gcr.io/coredns:1.7.0

提前拉取镜像(通过指定仓库,这一步可以不做):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/bash

images=(
kube-apiserver:v1.20.15
kube-controller-manager:v1.20.15
kube-scheduler:v1.20.15
kube-proxy:vv1.20.15
pause:3.2
etcd:3.4.13-0
coredns:1.7.0
)

for imageName in ${images[@]} ; do
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
    docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
done

7. 指定仓库后,查看image list:

1
2
3
4
5
6
7
8
9
kubeadm config images list --config k8s-init-default.yaml 

registry.aliyuncs.com/google_containers/kube-apiserver:v1.20.15
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.20.15
registry.aliyuncs.com/google_containers/kube-scheduler:v1.20.15
registry.aliyuncs.com/google_containers/kube-proxy:v1.20.15
registry.aliyuncs.com/google_containers/pause:3.2
registry.aliyuncs.com/google_containers/etcd:3.4.13-0
registry.aliyuncs.com/google_containers/coredns:1.7.0

8. 提前拉取镜像,防止kubeadm init超时:

kubeadm config images pull --config=k8s-init-default.yaml

9. 开始安装:

kubeadm init --config=k8s-init-default.yaml

出现:

1
2
3
4
5
6
7
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

说明安装成功。

10. 安装网络插件:

1
2
3
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl apply -f kube-flannel.yml

11. work node 修改hostname:

hostnamectl set-hostname work-node-name
vim /etc/hosts 修改127.0.0.1那一项

12. 加入集群

在所有work node上执行1、2两步。 在master上执行:

kubeadm token create --print-join-command

在work node上执行以上命令的执行结果