什么是 Kind
Kind
(Kubernetes in Docker) 是一个 Kubernetes
孵化项目,Kind
是一套开箱即用的 Kubernetes
环境搭建方案。顾名思义,就是将 Kubernetes
所需要的所有组件,全部部署在一个 Docker
容器中,可以很方便的搭建 Kubernetes
集群。
Kind
已经广泛的应用于 Kubernetes
上游及相关项目的 CI
环境中,官方文档中也把 Kind
作为一种本地集群搭建的工具推荐给大家。
项目地址:https://github.com/kubernetes-sigs/kind
Kind 可以做什么?
- 快速创建一个或多个
Kubernetes
集群 - 支持部署高可用的
Kubernetes
集群 - 支持从源码构建并部署一个
Kubernetes
集群 - 可以快速低成本体验一个最新的
Kubernetes
集群,并支持Kubernetes
的绝大部分功能 - 支持本地离线运行一个多节点集群
Kind 有哪些优势?
- 最小的安装依赖,仅需要安装
Docker
即可 - 使用方法简单,只需
Kind Cli
工具即可快速创建集群 - 使用容器来模似
Kubernetes
节点 - 内部使用
Kubeadm
的官方主流部署工具 - 通过了
CNCF
官方的K8S Conformance
测试
安装 docker
让apt可以支持HTTPS
$ apt install apt-transport-https ca-certificates curl software-properties-common -y
将官方Docker库的GPG公钥添加到系统中
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 阿里源
$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
将Docker库添加到APT里
# 官方源
$ add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
# 阿里源
$ add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal stable"
# echo "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal stable" >> /etc/apt/source.list
更新包列表
$ apt update
为了确保修改生效,让新的安装从Docker库里获取,而不是从Ubuntu自己的库里获取,执行:
$ apt-cache policy docker-ce
安装 docker-ce
$ apt install -y docker-ce
配置 docker 阿里源镜像地址
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://nol6uuul.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
踩坑: 我用了阿里云的镜像地址反而无法拉取 kind 的镜像, 此时需要删除阿里源
检查 docker 运行状态
$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-01-05 16:02:08 CST; 17min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 241165 (dockerd)
Tasks: 12
Memory: 29.6M
CGroup: /system.slice/docker.service
└─241165 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Jan 05 16:02:08 master1 dockerd[241165]: time="2024-01-05T16:02:08.136300388+08:00" level=info msg="Docker daemon" commit=311b9ff graphdriver=overla>
Jan 05 16:02:08 master1 dockerd[241165]: time="2024-01-05T16:02:08.136351488+08:00" level=info msg="Daemon has completed initialization"
Jan 05 16:02:08 master1 dockerd[241165]: time="2024-01-05T16:02:08.254789219+08:00" level=info msg="API listen on /run/docker.sock"
Jan 05 16:02:08 master1 systemd[1]: Started Docker Application Container Engine.
Jan 05 16:02:46 master1 dockerd[241165]: time="2024-01-05T16:02:46.543587374+08:00" level=warning msg="Error persisting manifest" digest="sha256:2fc>
Jan 05 16:03:38 master1 dockerd[241165]: time="2024-01-05T16:03:38.612170892+08:00" level=info msg="ignoring event" container=6d8d34f0066bde04811038>
Jan 05 16:04:50 master1 dockerd[241165]: time="2024-01-05T16:04:50.406175352+08:00" level=info msg="ignoring event" container=504632135d04b87b5ed83f>
Jan 05 16:05:35 master1 dockerd[241165]: time="2024-01-05T16:05:35.656175212+08:00" level=error msg="Error setting up exec command in container 5046>
Jan 05 16:09:52 master1 dockerd[241165]: time="2024-01-05T16:09:52.944844810+08:00" level=info msg="Layer sha256:a87ab3c028db3c73f360b49c9dcd171e236>
Jan 05 16:09:52 master1 dockerd[241165]: time="2024-01-05T16:09:52.946428917+08:00" level=info msg="Layer sha256:82ae998286b2bba64ce571578647adcabef>
lines 1-21/21 (END)
安装 kubectl
apt 安装
# 安装依赖
apt install apt-transport-https ca-certificates -y
# 编辑镜像源文件,文件末尾加入阿里云k8s镜像源配置
echo 'deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main' >> /etc/apt/sources.list
#更新证书
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add
#更新源
apt update
apt-get install -y kubectl
二进制安装
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
mv kubectl /usr/bin/
配置命令行自动补全(可选)
apt install bash-completion -y
cat << EOF >> ~/.profile
alias k='kubectl'
source <(kubectl completion bash)
complete -F __start_kubectl k
EOF
source ~/.profile
安装 Kind
首先进到 官网, 获取你操作系统的 kind 版本
本教程提供 Linux 下的二进制安装
来自官网的最新安装脚本:
# For AMD64 / x86_64
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
# For ARM64
[ $(uname -m) = aarch64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-arm64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
使用 kind 创建集群
搭建一主三从集群 1c3w
1 control plane and 3 worker
cluster.yaml:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 31000 # 将主机 31000 端口映射到容器的 31000 端口
hostPort: 31000
listenAddress: "0.0.0.0" # Optional, defaults to "0.0.0.0"
protocol: tcp # Optional, defaults to tcp
- role: worker
- role: worker
- role: worker
**注意: ** 因为 kind 搭建的集群也是容器, 我们要访问容器的服务, 我们需要把集群的 Service 暴露为 NodePort 类型进行访问, (NodePort 范围30000~32767)
$ kind create cluster --config cluster.yaml --name 1c3w
Creating cluster "1c3w" ...
✓ Ensuring node image (kindest/node:v1.27.3) 🖼
✓ Preparing nodes 📦 📦 📦 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining worker nodes 🚜
Set kubectl context to "kind-1c3w"
You can now use your cluster with:
kubectl cluster-info --context kind-1c3w
Thanks for using kind! 😊
创建 service
# 更换 context
$ kubectl cluster-info --context kind-1c3w
# 创建一个 nginx 的 deploy 测试服务
$ k create deploy nginx --image=nginx
# 冗余部署实现高可用
$ k scale deployment nginx --replicas 3
# 暴露服务
$ k expose deployment nginx --name nginx --port=80 --target-port=80 --type=NodePort
# 修改 NodePort 端口为 31000 让主机能够访问
$ k edit deployment nginx
......
ports:
- nodePort: 31000 # 修改
port: 80
protocol: TCP
targetPort: 80
....
$ k get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6m5s
nginx NodePort 10.96.91.4 <none> 80:31000/TCP 3m7s
验证服务
$ curl localhost:31000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
也可在浏览器输入<虚拟机 ip>:31000