概述

快速本地构建k8s集群
官方地址:https://kind.sigs.k8s.io/
k8s官网也推荐了kind作为k8s的一种安装方式。https://kubernetes.io/docs/tasks/tools/

图片.pngkind安装

需要安装go开发环境

图片.png

源码编译安装

对go的版本要求是>=1.14,建议把安装GO111MODULE=”on” go get sigs.k8s.io/kind@v0.11.1 执行后会把kind的二进制文件放在$(go env GOPATH)/bin 建议把go/bin的路径设置成环境变量

  1. GO111MODULE="on" go get sigs.k8s.io/kind@v0.11.1
  2. go: downloading sigs.k8s.io/kind v0.11.1
  3. go: downloading github.com/spf13/cobra v1.1.1
  4. go: downloading k8s.io/apimachinery v0.20.2
  5. go: downloading github.com/alessio/shellescape v1.4.1
  6. go: downloading github.com/pelletier/go-toml v1.8.1
  7. go: downloading github.com/evanphx/json-patch v4.9.0+incompatible
  8. go: downloading github.com/evanphx/json-patch/v5 v5.2.0

二进制安装

  1. curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
  2. chmod +x ./kind
  3. mv ./kind /{PATH执行目录}/kind

macos 也可以直接使用brew

  1. brew install kind

window安装

  1. curl.exe -Lo kind-windows-amd64.exe https://kind.sigs.k8s.io/dl/v0.11.1/kind-windows-amd64
  2. Move-Item .\kind-windows-amd64.exe c:\some-dir-in-your-PATH\kind.exe

kubectl安装

根据平台安装对应的kubectl,官方的安装教程就是标准。

Usage: kind [command]

Available Commands: build Build one of [node-image] completion Output shell completion code for the specified shell (bash, zsh or fish) create Creates one of [cluster] delete Deletes one of [cluster] export Exports one of [kubeconfig, logs] get Gets one of [clusters, nodes, kubeconfig] help Help about any command load Loads images into nodes version Prints the kind CLI version

Flags: -h, —help help for kind —loglevel string DEPRECATED: see -v instead -q, —quiet silence all stderr output -v, —verbosity int32 info log verbosity —version version for kind

Use “kind [command] —help” for more information about a command.

  1. <a name="V9Ixr"></a>
  2. ## 创建机器
  3. ```shell
  4. kind create cluster
  5. Creating cluster "kind" ...
  6. ✓ Ensuring node image (kindest/node:v1.21.1) 🖼
  7. ✓ Preparing nodes 📦
  8. ✓ Writing configuration 📜
  9. ✓ Starting control-plane 🕹️
  10. ✓ Installing CNI 🔌
  11. ✓ Installing StorageClass 💾
  12. Set kubectl context to "kind-kind"
  13. You can now use your cluster with:
  14. kubectl cluster-info --context kind-kind
  15. Not sure what to do next? 😅 Check out https://kind.sigs.k8s.io/docs/user/quick-start/

删除

  1. kind delete cluster
  2. Deleting cluster "kind" ...

删除所有集群

  1. kind delete clusters --all
  2. Deleted clusters: ["k8s" "mycluster"]

镜像版本

  1. kind create cluster -h
  2. Creates a local Kubernetes cluster using Docker container 'nodes'
  3. Usage:
  4. kind create cluster [flags]
  5. Flags:
  6. --config string path to a kind config file
  7. -h, --help help for cluster
  8. --image string node docker image to use for booting the cluster
  9. --kubeconfig string sets kubeconfig path instead of $KUBECONFIG or $HOME/.kube/config
  10. --name string cluster name, overrides KIND_CLUSTER_NAME, config (default kind)
  11. --retain retain nodes for debugging when cluster creation fails
  12. --wait duration wait for control plane node to be ready (default 0s)
  13. Global Flags:
  14. --loglevel string DEPRECATED: see -v instead
  15. -q, --quiet silence all stderr output
  16. -v, --verbosity int32 info log verbosity

https://registry.hub.docker.com/r/kindest/node

  1. kind create cluster --name k8s --image kindest/node:v1.19.7

查看容器

  1. $ docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 1c13d9625e23 kindest/node:v1.19.7 "/usr/local/bin/entr…" 46 minutes ago Up 46 minutes 127.0.0.1:50035->6443/tcp k8s-control-plane
  4. $docker exec k8s-control-plane crictl ps
  5. CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
  6. f45838e47358a 38858e77904d0 13 minutes ago Running kube-controller-manager 1 ea5ce3281ea14
  7. 810f12742c561 e422121c9c5f9 13 minutes ago Running local-path-provisioner 1 1b6337b06b28a
  8. 894672913740d 4b7bb91932023 13 minutes ago Running kube-scheduler 1 662ef581558d8
  9. 33fb275c70fa8 bfe3a36ebd252 46 minutes ago Running coredns 0 b095ceb7cff2d
  10. 05daf08a5e6e3 bfe3a36ebd252 46 minutes ago Running coredns 0 97c4f744b37c4
  11. 29609f5ad3159 6b17089e24fdb 47 minutes ago Running kindnet-cni 0 081016461b364
  12. 62c2df652e8e6 96cb067c0d468 47 minutes ago Running kube-proxy 0 02831473d01f2
  13. d6cd7d73d53d5 0369cf4303ffd 47 minutes ago Running etcd 0 f3979a039e74f
  14. 8d711d2a80009 6d4f78553215b 47 minutes ago Running kube-apiserver 0 5cdbd226f8756

kind load

官方文档说明https://kind.sigs.k8s.io/docs/user/quick-start/#loading-an-image-into-your-cluster

  1. docker build -t my-custom-image:unique-tag ./my-image-dir
  2. kind load docker-image my-custom-image:unique-tag

通过kind load docker-image 命令把镜像加载进去

  1. kind load docker-image k8s.gcr.io/ingress-nginx/controller:v0.47.0 --name=k8s
  2. Image: "k8s.gcr.io/ingress-nginx/controller:v0.47.0" with ID "sha256:bf621a764db545fb64e7681c42e44ef33ad58165696359fbd26edc9a7e74a0c5" not yet present on node "k8s-control-plane", loading..

查看容器内镜像

  1. $docker exec -it k8s-control-plane crictl images |grep ingress-nginx
  2. k8s.gcr.io/ingress-nginx/controller v0.47.0 bf621a764db54 101MB

配置文件搭建

设置集群执行使用配置文件方式

  1. kind create cluster --config=yaml配置文件

官方文档 https://kind.sigs.k8s.io/docs/user/configuration/

设置名称

  1. kind: Cluster
  2. apiVersion: kind.x-k8s.io/v1alpha4
  3. name: app-1-cluster

设置宿主机端口

  1. kind: Cluster
  2. apiVersion: kind.x-k8s.io/v1alpha4
  3. name: k8s
  4. nodes:
  5. - role: control-plane
  6. image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
  7. extraPortMappings:
  8. - containerPort: 30000
  9. hostPort: 30000
  10. listenAddress: "0.0.0.0"
  11. protocol: tcp

通过参数extraPortMappings 设置 hostPort 和容器内容端口实现

设置多节点集群

  1. # three node (two workers) cluster config
  2. kind: Cluster
  3. apiVersion: kind.x-k8s.io/v1alpha4
  4. nodes:
  5. - role: control-plane
  6. - role: worker
  7. - role: worker

设置HA集群

  1. # a cluster with 3 control-plane nodes and 3 workers
  2. kind: Cluster
  3. apiVersion: kind.x-k8s.io/v1alpha4
  4. nodes:
  5. - role: control-plane
  6. - role: control-plane
  7. - role: control-plane
  8. - role: worker
  9. - role: worker
  10. - role: worker

集群搭建需要使用配置文件方式,创建配置文件

  1. kind: Cluster
  2. apiVersion: kind.x-k8s.io/v1alpha4
  3. networking:
  4. apiServerPort: 6443
  5. podSubnet: "10.244.0.0/16"
  6. kubeProxyMode: "ipvs"
  7. nodes:
  8. - role: control-plane
  9. - role: worker
  10. - role: worker
  11. image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729

设置集群中k8s版本

image 设置版本 https://github.com/kubernetes-sigs/kind/releases 需要设置docker的sha256信息
[

](https://github.com/kubernetes-sigs/kind/releases)
常见的几个版本sha256值:

  1. mages built for this release:
  2. 1.21: kindest/node:v1.21.1@sha256:69860bda5563ac81e3c0057d654b5253219618a22ec3a346306239bba8cfa1a6
  3. 1.20: kindest/node:v1.20.7@sha256:cbeaf907fc78ac97ce7b625e4bf0de16e3ea725daf6b04f930bd14c67c671ff9
  4. 1.19: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
  5. 1.18: kindest/node:v1.18.19@sha256:7af1492e19b3192a79f606e43c35fb741e520d195f96399284515f077b3b622c
  6. 1.17: kindest/node:v1.17.17@sha256:66f1d0d91a88b8a001811e2f1054af60eef3b669a9a74f9b6db871f2f1eeed00
  7. 1.16: kindest/node:v1.16.15@sha256:83067ed51bf2a3395b24687094e283a7c7c865ccc12a8b1d7aa673ba0c5e8861
  8. 1.15: kindest/node:v1.15.12@sha256:b920920e1eda689d9936dfcf7332701e80be12566999152626b2c9d730397a95
  9. 1.14: kindest/node:v1.14.10@sha256:f8a66ef82822ab4f7569e91a5bccaf27bceee135c1457c512e54de8c6f7219f8

创建集群命令

  1. kind create cluster --config=kind-cluster.yaml

查看当前结果

  1. kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. kind-control-plane Ready control-plane,master 3m40s v1.21.1
  4. kind-worker Ready <none> 3m8s v1.21.1
  5. kind-worker2 Ready <none> 3m8s v1.19.11

操作命令

  1. kind get clusters
  2. k8s
  3. kind

查看当前的集群,kubectl上下文目前已经被设置为最新的集群

  1. kubectl config get-contexts
  2. CURRENT NAME CLUSTER AUTHINFO NAMESPACE
  3. docker-desktop docker-desktop docker-desktop
  4. kind-k8s kind-k8s kind-k8s
  5. * kind-kind kind-kind kind-kind

修改上下文

  1. kubectl config set-context kind-k8s
  2. Context "kind-k8s" modified.

ingress-nginx

kind机器配置文件

  1. kind: Cluster
  2. apiVersion: kind.x-k8s.io/v1alpha4
  3. name: k8s
  4. nodes:
  5. - role: control-plane
  6. image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
  7. kubeadmConfigPatches:
  8. - |
  9. kind: InitConfiguration
  10. nodeRegistration:
  11. kubeletExtraArgs:
  12. node-labels: "ingress-ready=true"
  13. extraPortMappings:
  14. - containerPort: 80
  15. hostPort: 80
  16. protocol: TCP
  17. - containerPort: 443
  18. hostPort: 443
  19. protocol: TCP

或者可以修改暴露接口8080

  1. kind: Cluster
  2. apiVersion: kind.x-k8s.io/v1alpha4
  3. name: k8s
  4. nodes:
  5. - role: control-plane
  6. image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
  7. kubeadmConfigPatches:
  8. - |
  9. kind: InitConfiguration
  10. nodeRegistration:
  11. kubeletExtraArgs:
  12. node-labels: "ingress-ready=true"
  13. extraPortMappings:
  14. - containerPort: 8080
  15. hostPort: 8080
  16. protocol: TCP
  17. - containerPort: 443
  18. hostPort: 443
  19. protocol: TCP

因为有node选择和亲和选择
图片.png

官方教程https://kind.sigs.k8s.io/docs/user/ingress/#ingress-nginx

  1. VERSION=$(curl https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/stable.txt)
  2. kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/${VERSION}/deploy/static/provider/kind/deploy.yaml

v0.47.0版本的

  1. kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.47.0/deploy/static/provider/kind/deploy.yaml

如果不是80端口
图片.png
图片.png
镜像拉取失败替换成国内registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller

  1. image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v0.47.0@sha256:a1e4efc107be0bb78f32eaec37bef17d7a0c81bec8066cdf2572508d21351d0b
  2. imagePullPolicy: IfNotPresent

验证kind-usage.yaml

  1. kind: Pod
  2. apiVersion: v1
  3. metadata:
  4. name: foo-app
  5. labels:
  6. app: foo
  7. spec:
  8. containers:
  9. - name: foo-app
  10. image: hashicorp/http-echo:0.2.3
  11. args:
  12. - "-text=foo"
  13. ---
  14. kind: Service
  15. apiVersion: v1
  16. metadata:
  17. name: foo-service
  18. spec:
  19. selector:
  20. app: foo
  21. ports:
  22. # Default port used by the image
  23. - port: 5678
  24. ---
  25. kind: Pod
  26. apiVersion: v1
  27. metadata:
  28. name: bar-app
  29. labels:
  30. app: bar
  31. spec:
  32. containers:
  33. - name: bar-app
  34. image: hashicorp/http-echo:0.2.3
  35. args:
  36. - "-text=bar"
  37. ---
  38. kind: Service
  39. apiVersion: v1
  40. metadata:
  41. name: bar-service
  42. spec:
  43. selector:
  44. app: bar
  45. ports:
  46. # Default port used by the image
  47. - port: 5678
  48. ---
  49. apiVersion: networking.k8s.io/v1
  50. kind: Ingress
  51. metadata:
  52. name: example-ingress
  53. spec:
  54. rules:
  55. - http:
  56. paths:
  57. - pathType: Prefix
  58. path: "/foo"
  59. backend:
  60. service:
  61. name: foo-service
  62. port:
  63. number: 5678
  64. - pathType: Prefix
  65. path: "/bar"
  66. backend:
  67. service:
  68. name: bar-service
  69. port:
  70. number: 5678

如果出现https://github.com/kubernetes/ingress-nginx/issues/5401这个问题

  1. Internal error occurred: failed calling webhook "validate.nginx.ingress.kubernetes.io

最简单快捷的方式

  1. kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission

测试

  1. $ curl localhost:8080/bar
  2. bar
  3. $ curl localhost:8080/foo
  4. foo

Nodeport

创建nginx.yaml

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-deploy
  6. namespace: default
  7. spec:
  8. replicas: 1
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:1.14
  20. ports:
  21. - name: http
  22. containerPort: 80
  23. ---
  24. apiVersion: v1
  25. kind: Service
  26. metadata:
  27. name: nginx
  28. namespace: default
  29. spec:
  30. selector:
  31. app: nginx
  32. type: NodePort
  33. ports:
  34. - port: 80
  35. targetPort: 80
  36. nodePort: 30001

执行

  1. kubectl apply -f nginx.yaml

查看svc

  1. kubectl get svc -A
  2. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. default nginx NodePort 10.96.8.57 <none> 80:30001/TCP 7m37s

执行

  1. kubectl port-forward --address 0.0.0.0 service/nginx 30001:80

或者通过docker exec 进入kind容器内,下面的495d75d015a8就是kind创建的k8s容器

  1. docker exec -it 495d75d015a8 bash
  2. root@k8s-control-plane:/# curl localhost:30001
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <title>Welcome to nginx!</title>
  7. <style>
  8. body {
  9. width: 35em;
  10. margin: 0 auto;
  11. font-family: Tahoma, Verdana, Arial, sans-serif;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <h1>Welcome to nginx!</h1>
  17. <p>If you see this page, the nginx web server is successfully installed and
  18. working. Further configuration is required.</p>
  19. <p>For online documentation and support please refer to
  20. <a href="http://nginx.org/">nginx.org</a>.<br/>
  21. Commercial support is available at
  22. <a href="http://nginx.com/">nginx.com</a>.</p>
  23. <p><em>Thank you for using nginx.</em></p>
  24. </body>
  25. </html>
  26. hello
  27. Wed Jul 14 10:39:16 UTC 2021

参考

https://kind.sigs.k8s.io/docs/user/quick-start/#installation
https://stackoverflow.com/questions/62432961/how-to-use-nodeport-with-kind
https://zhuanlan.zhihu.com/p/259543032
https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/