准备三台Ubuntu20.04服务器
1、在服务器上安装snapd
sudo apt insall snapd -y
安装完成后用snap version 查看版本。
snap version
snap 2.57.1
snapd 2.57.1
series 16
ubuntu 20.04
kernel 5.4.0-121-generic
2、安装microk8s
sudo snap install microk8s --classic --channel=1.25
加channel可以指定版本安装,不加默认安装最新版本。
设置kubectl别名
sudo snap alias microk8s.kubectl kubectl
在国内由于众所周知的原因,安装microk8s后会发现 gcr.io的docker image无法下载的问题.(详细安装步骤参见 MicroK8s - Zero-ops Kubernetes for developers, edge and IoT),首先需要安装 pullk8s 工具,此工具可以通过 hub.docker.com 的 opsdockerimage 仓库下载k8s所需的 k8s.gcr.io 或 gcr.io 镜像,每天更新一次,包括所有image 的全平台的所有tags。
git clone https://github.com/OpsDocker/pullk8s.git
cd pullk8s
sudo cp pullk8s.sh /usr/local/bin/pullk8s
sudo chmod +x /usr/local/bin/pullk8s
pullk8s这个工具依赖docker 来拉镜像,需要安装好docker
sudo snap install docker
安装完后可以用pullk8s检查所缺的镜像
sudo pullk8s check --microk8s
k8s.gcr.io/pause:3.1
使用 pullk8s 拉取失败的镜像,并导入到 pod 空间中
sudo pullk8s pull k8s.gcr.io/pause:3.1 --microk8s
编辑文件/var/snap/microk8s/current/args/containerd-template.toml替换sandbox_image为国内镜像源。
stream_server_address = "127.0.0.1"
28 stream_server_port = "0"
29 enable_selinux = false
30 sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1"
31 stats_collect_period = 10
启动dns组件
sudo microk8s enable dns
这样microk8s就安装好了。
3、在另外两台服务器上重复上述步骤分别部署microk8s
选择一台机器作为主节点,在主节点机器上配置host
masterip master
node1ip node1
node2ip node2
在主节点上运行add-node命名
sudo microk8s add-node
From the node you wish to join to this cluster, run the following:
microk8s join 192.168.1.230:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05
Use the '--worker' flag to join a node as a worker not running the control plane, eg:
microk8s join 192.168.1.230:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05 --worker
If the node you are adding is not reachable through the default interface you can use one of the following:
microk8s join 192.168.1.230:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05
microk8s join 10.23.209.1:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05
microk8s join 172.17.0.1:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05
分别在另外两个字节点上运行刚返回的指令
microk8s join 192.168.1.230:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05
Contacting cluster at 192.168.1.230
The node has joined the cluster and will appear in the nodes list in a few seconds.
Currently this worker node is configured with the following kubernetes API server endpoints:
- 192.168.1.230 and port 16443, this is the cluster node contacted during the join operation.
If the above endpoints are incorrect, incomplete or if the API servers are behind a loadbalancer please update
/var/snap/microk8s/current/args/traefik/provider.yaml
将一个节点加入集群应该只需要几秒钟。之后应该能够看到节点已加入
kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready 43m v1.24.0-2+59bbb3530b6769
node1 Ready 89s v1.24.0-2+59bbb3530b6769
node2 Ready 34m v1.24.0-2+59bbb3530b6769