本文是胡涛大佬所出版的《Kubernetes Operator进阶开发》,强烈推荐各位阅读原书,本文仅仅留作个人心得,如有侵权立马删除。
1 快速上手
首先是下载对应的二进制文件:
go install sigs.k8s.io/kind@v0.22.0
然后可以提前先下载好一个版本:
docker pull kindest/node:v1.26.24
然后可以快速搭建一套 Kubernetes 的环境:
kind create cluster --image=kindest/node:v1.26.14 --name=dev
从而得到下面的结果:
然后可以查看当前的集群的情况:
base ❯ kubectl get node
NAME STATUS ROLES AGE VERSION
dev-control-plane Ready control-plane 57s v1.26.14
base ❯ kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-787d4945fb-jw6xs 1/1 Running 0 2m27s
coredns-787d4945fb-xqc6x 1/1 Running 0 2m27s
etcd-dev-control-plane 1/1 Running 0 2m41s
kindnet-m4xvt 1/1 Running 0 2m27s
kube-apiserver-dev-control-plane 1/1 Running 0 2m41s
kube-controller-manager-dev-control-plane 1/1 Running 0 2m42s
kube-proxy-w4pvj 1/1 Running 0 2m27s
kube-scheduler-dev-control-plane 1/1 Running 0 2m40s
如果有多个环境也可以通过 set-context 来切换环境:
# 查看信息
kubectl cluster-info --context kind-dev
# 切换环境
kubectl config use-context kind-dev
2 搭建多节点环境
在 kind 中,可以通过 --config 来自定义配置。
# 来创建一个根据yaml的多节点文件
kind create cluster --config xxxx.yaml
其中关于 config 的具体的配置项可以参看网站 Kind Configuration,此处简单介绍。
如果需要客制 subnet 的话:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
podSubnet: "10.244.0.0/16"
serviceSubnet: "10.96.0.0/12"
kubeProxyMode: "ipvs" # to disable kube-proxy, set mode to "none"
以及对应的 node 的 role:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
# One control plane node and three "workers".
#
# While these will not add more real compute capacity and
# have limited isolation, this can be useful for testing
# rolling updates etc.
#
# The API-server and other control plane components will be
# on the control-plane node.
#
# You probably don't need this unless you are testing Kubernetes itself.
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
以及设定集群的版本:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: kindest/node:v1.16.4@sha256:b91a2c2317a000f3a783489dfb755064177dbc3a0b2f4147d50f04825d016f55
- role: worker
image: kindest/node:v1.16.4@sha256:b91a2c2317a000f3a783489dfb755064177dbc3a0b2f4147d50f04825d016f55
以及建立 cluster 和 host 之间的交互情况:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
# add a mount from /path/to/my/files on the host to /files on the node
extraMounts:
- hostPath: /path/to/my/files
containerPath: /files
# optional: if set, the mount is read-only.
# default false
readOnly: true
# optional: if set, the mount needs SELinux relabeling.
# default false
selinuxRelabel: false
propagation: None # 建议我们最好就按照默认值
那么我们现在就使用下面的 multi-node.yaml 来创建集群:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
下面的命令:
kind create cluster --config multi-node.yaml --image=kindest/node:v1.26.14 --name=multi-dev
base ❯ kind get clusters
dev
multi-dev
kubectl cluster-info --context kind-multi-dev
base ❯ kubectl get node
NAME STATUS ROLES AGE VERSION
multi-dev-control-plane Ready control-plane 39s v1.26.14
multi-dev-worker Ready <none> 17s v1.26.14
multi-dev-worker2 Ready <none> 18s v1.26.14
multi-dev-worker3 Ready <none> 17s v1.26.14
3 其他功能呢
3.1 端口映射
在 yaml 中添加 extraPortMapping 可以完成 kind 集群的端口映射:Kind Extra Port Mapping
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
3.2 导入镜像
下面的代码展示了如何导入镜像:
kind load docker-image xxx:v1 --name <kind-cluster>