引言
因为了解到Kind可方便创建K8s集群,所以就看了下相关文档,这是源码:Kind源码 。这是文档:Kind官方文档
下面介绍下两个环境的安装及基本使用。
Kind安装
Linux安装
安装后使用
mac安装及使用
kubectl安装
kind安装时候的提醒:
这个安装只写Linux环境的
,kubectl安装完成后就直接在服务器内通过kubectl 命令查看kind创建的资源就行。
以下是 kubectl 安装过程
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 安装kubectl
yum install -y kubectl-1.22.3
安装成功后可以 使用命令kubectl version
验证下。
Kind相关使用
创建单节点集群
# 创建名称为test的集群。该方法创建的集群只有一个节点。
kind create cluster --name test
创建多节点集群
# 创建一个config.yaml文件
vi config.yaml
# 以下是config.yaml的内容,指定三个节点
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
# 根据config配置创建名称为test的集群
kind create cluster --config config.yaml --name test
将集群内所需端口映射出来
方法同创建多节点集群,这个部分也是官方文档ingress部分
# 创建一个config.yaml文件
vi config.yaml
# 以下是config.yaml的内容,指定三个节点,映射master节点上的端口,分别是:80、31180、31080(所有集群内想正常访问的端口,都需要这样映射出来。)后续可以通过 kind宿主机地址:80或者kind宿主机地址:31180等访问到集群内对应服务。
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
# port forward 80 on the host to 80 on this node
extraPortMappings:
- containerPort: 80
hostPort: 80
# optional: set the bind address on the host
# 0.0.0.0 is the current default
listenAddress: "127.0.0.1"
# optional: set the protocol to one of TCP, UDP, SCTP.
# TCP is the default
protocol: TCP
- containerPort: 31180
hostPort: 31180
protocol: TCP
- containerPort: 31080
hostPort: 31080
protocol: TCP
- role: worker
- role: worker
创建完成后,可通过docker ps查看创建的集群信息,每个节点都是以容器的形式展示,如下:
可以通过进入容器的形式进入对应节点:docker exec -it 容器名 /bin/bash
注意事项
clusters
⚠️ 有一个比较容易出错的地方,查询集群的命令是kind get clusters
删除集群的命令是kind delete clusters xxx (批量删)
或者kubectl delete cluster --name xxx (单删)
集群内基本环境配置
kind部署完的k8s环境,总是要有其他用法的,或许就是要再部署其他的服务,或者安装包啥的,这就需要一些基本的环境配置,以下是我部署相关安装包时候会装的基础工具:
# 必备软件安装
apt update
apt install -y vim curl net-tools iproute2 iputils-ping vim
# docker 安装(curl这个地方可能不成功,可忽略)
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
apt-get install -y docker.io
验证docker是否正常安装并启用
docker version
systemctl start docker
systemctl enable docker
systemctl status docker
补充
如果要创建一个集群,可被直接连接的可通过以下yaml文件:
kind: Cluster
apiVersion: "kind.x-k8s.io/v1alpha4"
networking:
apiServerPort: ${port}
apiServerAddress: "${ip_addr}"
其中,port为apiserver暴露的端口,ip_addr为本机地址