Skip to content

kubernetes 环境搭建之 kind for Windows

kind start

官方文档:https://kind.sigs.k8s.io/docs/user/quick-start/

前提条件:安装好 Docker Destop(安装好后就可以使用 docker 和 kubectl 命令) 安装文档:https://docs.docker.com/docker-for-windows/install-windows-home/

Installation

bash
# 下载
curl.exe -Lo kind-windows-amd64.exe https://kind.sigs.k8s.io/dl/v0.10.0/kind-windows-amd64
# 移动到执行目录,并把这个目录添加到环境变量
mv .\kind-windows-amd64.exe C:\Program Files\Kubernetes\kind\kind.exe

kind create/delete cluster 单节点

bash
# 由于 kind create cluster 很慢,所以可以使用 docker 提前拉取镜像
docker pull kindest/node:v1.20.2

# 然后使用 kind 创建 Kubernetes 集群非常的方便,只需要一行命令即可
kind create cluster --image kindest/node:v1.20.2
# kind create cluster

# 删除集群
kind delete cluster

# 默认集群名称是 "kind",如果要创建多个或者指定集群名称,可以指定 name 参数:
kind create cluster --name=kind-2

# 删除集群
kind delete cluster --name=kind-2

# list clusters
kind get clusters

# 将Image加载到集群中
kind load docker-image my-custom-image-0 my-custom-image-1

# 如果使用命名集群,您需要指定希望将映像加载到的集群的名称
kind load docker-image my-custom-image-0 my-custom-image-1 --name kind-2

Kubernetes default pull policy

The Kubernetes default pull policy is IfNotPresent unless the image tag is :latest or omitted (and implicitly :latest) in which case the default policy is Always. IfNotPresent causes the Kubelet to skip pulling an image if it already exists. If you want those images loaded into node to work as expected, please:

don't use a :latest tag

and / or:

specify imagePullPolicy: IfNotPresent or imagePullPolicy: Never on your container(s).

See Kubernetes imagePullPolicy for more information.

TIP

你可以使用 docker exec 获得一个集群节点上的 Image 列表

docker exec -it my-node-name crictl images

其中 my-node-name 是 Docker 容器的名称(例如 kind-control-plane)。可通过下面命令查看:

kubectl get nodes

kubectl

bash
# 为了与特定的集群交互,你只需要在kubectl中指定集群名作为上下文:(kind-kind: 默认的集群名为:kind)
kubectl cluster-info --context kind-kind

# 查看节点
kubectl get nodes

# 查看集群运行情况
kubectl get po -n kube-system

# 创建集群成功之后,就可以使用 kubectl 来操作 k8s 集群了
# 获取所有资源
kubectl get all --all-namespaces

Kind Configuration

kind-config.yaml

yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
# clsuter name
name: app-1-cluster

# Kubernetes feature gates可以通过以下配置在集群范围内启用所有Kubernetes组件:
# 官网:https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/
featureGates:
  # any feature gate can be enabled here with "Name": true
  # or disabled here with "Name": false
  # not all feature gates are tested, however
  "CSIMigration": true

# Kubernetes API服务器运行时配置可以使用runtimeConfig键来切换,它映射到——runtime-config kube-apiserver标志。这可以用来禁用beta / alpha api。
# 官网:https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/
runtimeConfig:
  "api/alpha": "false"

# 可以在networking字段下定制集群网络的多个详细信息。
networking:
  # KIND对IPv6集群的支持有限(很快就会支持双栈!),你可以通过以下设置从默认的IPv4切换到IPV6
  ipFamily: ipv4

  # The API Server listen address and port can be customized with:
  # WARNING: It is _strongly_ recommended that you keep this the default
  # (127.0.0.1) for security reasons. However it is possible to change this.
  apiServerAddress: "127.0.0.1"
  # By default the API server listens on a random open port.
  # You may choose a specific port but probably don't need to in most cases.
  # Using a random port makes it easier to spin up multiple clusters.
  apiServerPort: 6443

  # You can configure the subnet used for pod IPs by setting
  podSubnet: "10.244.0.0/16"

  # You can configure the Kubernetes service subnet used for service IPs by setting
  serviceSubnet: "10.96.0.0/12"

  # the default CNI will not be installed
  disableDefaultCNI: true

  # You can configure the kube-proxy mode that will be used, between iptables and ipvs. By default iptables is used. To disable kube-proxy, set the mode to "none"
  kubeProxyMode: "ipvs"

# The kind: Cluster object has a nodes field containing a list of node objects. If unset this defaults to:
nodes:
  # one node hosting a control plane
  - role: control-plane
    # ou can also set a specific Kubernetes version by setting the node's container image. You can find available image tags on the releases page. Please include the @sha256: image digest from the image in the release notes, as seen in this example:
    # https://github.com/kubernetes-sigs/kind/releases
    image: kindest/node:v1.16.4@sha256:b91a2c2317a000f3a783489dfb755064177dbc3a0b2f4147d50f04825d016f55

    # 额外的挂载可以通过主机上的存储传递到一个kind节点,用于持久化数据,通过代码挂载等。
    # add a mount from /path/to/my/files on the host to /files on the node
    extraMounts:
      - hostPath: /path/to/my/files/
        containerPath: /files

    # 可以使用额外的端口映射将端口转发到kind节点。这是一个跨平台的选择,让流量进入你的集群。
    # 为了在NodePort上使用端口映射,kind节点containerPort和服务NodePort需要相等。
    # 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
bash
kind create cluster --config=kind-config.yaml

传递给 CLI 的参数优先于配置文件中的等价参数。无论配置文件中是否存在 my-cluster 这个值,它都将被优先使用。

bash
kind create cluster --name my-cluster

kind create cluster 创建命令,默认配置有几个限制大多数情况是不满足实际需要的,默认配置的主要限制如下:

  • APIServer 只监听了 127.0.0.1,也就意味着在 Kind 的本机环境之外无法访问 APIServer
  • 由于国内的网络情况关系,Docker Hub 镜像站经常无法访问或超时,会导致无法拉取镜像或拉取镜像非常的慢

这边提供一个配置文件来解除上诉的限制: kind-config.yaml

yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  apiServerAddress: "192.168.208.64"
containerdConfigPatches:
  - |-
    [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
      endpoint = ["https://bfflggen.mirror.aliyuncs.com"]
nodes:
  - role: control-plane
    # Create a kind cluster with extraPortMappings and node-labels.
    # extraPortMappings allow the local host to make requests to the Ingress controller over ports 80/443
    # node-labels only allow the ingress controller to run on a specific node(s) matching the label selector
    kubeadmConfigPatches:
      - |
        kind: InitConfiguration
        nodeRegistration:
          kubeletExtraArgs:
            node-labels: "ingress-ready=true"
    extraPortMappings:
      - containerPort: 80
        hostPort: 80
        protocol: TCP
      - containerPort: 443
        hostPort: 443
        protocol: TCP

注意:

然后创建集群:

bash
# 由于 kind create cluster 很慢,所以可以使用 docker 提前拉取镜像
docker pull kindest/node:v1.20.2

# 然后使用 kind 创建 Kubernetes 集群非常的方便,只需要一行命令即可
kind create cluster --config kind-config.yaml --image kindest/node:v1.20.2

# 后面命令跟前面单机集群一样