概述
快速本地构建k8s集群
官方地址:https://kind.sigs.k8s.io/
k8s官网也推荐了kind作为k8s的一种安装方式。https://kubernetes.io/docs/tasks/tools/
kind安装
需要安装go开发环境
源码编译安装
对go的版本要求是>=1.14,建议把安装GO111MODULE=”on” go get sigs.k8s.io/kind@v0.11.1 执行后会把kind的二进制文件放在$(go env GOPATH)/bin 建议把go/bin的路径设置成环境变量
GO111MODULE="on" go get sigs.k8s.io/kind@v0.11.1
go: downloading sigs.k8s.io/kind v0.11.1
go: downloading github.com/spf13/cobra v1.1.1
go: downloading k8s.io/apimachinery v0.20.2
go: downloading github.com/alessio/shellescape v1.4.1
go: downloading github.com/pelletier/go-toml v1.8.1
go: downloading github.com/evanphx/json-patch v4.9.0+incompatible
go: downloading github.com/evanphx/json-patch/v5 v5.2.0
二进制安装
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
chmod +x ./kind
mv ./kind /{PATH执行目录}/kind
macos 也可以直接使用brew
brew install kind
window安装
curl.exe -Lo kind-windows-amd64.exe https://kind.sigs.k8s.io/dl/v0.11.1/kind-windows-amd64
Move-Item .\kind-windows-amd64.exe c:\some-dir-in-your-PATH\kind.exe
kubectl安装
根据平台安装对应的kubectl,官方的安装教程就是标准。
- Install kubectl on Linux
- Install kubectl on macOS
- Install kubectl on Windows
帮助
```shell kind kind creates and manages local Kubernetes clusters using Docker container ‘nodes’
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.
<a name="V9Ixr"></a>
## 创建机器
```shell
kind create cluster
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.21.1) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-kind"
You can now use your cluster with:
kubectl cluster-info --context kind-kind
Not sure what to do next? 😅 Check out https://kind.sigs.k8s.io/docs/user/quick-start/
删除
kind delete cluster
Deleting cluster "kind" ...
删除所有集群
kind delete clusters --all
Deleted clusters: ["k8s" "mycluster"]
镜像版本
kind create cluster -h
Creates a local Kubernetes cluster using Docker container 'nodes'
Usage:
kind create cluster [flags]
Flags:
--config string path to a kind config file
-h, --help help for cluster
--image string node docker image to use for booting the cluster
--kubeconfig string sets kubeconfig path instead of $KUBECONFIG or $HOME/.kube/config
--name string cluster name, overrides KIND_CLUSTER_NAME, config (default kind)
--retain retain nodes for debugging when cluster creation fails
--wait duration wait for control plane node to be ready (default 0s)
Global Flags:
--loglevel string DEPRECATED: see -v instead
-q, --quiet silence all stderr output
-v, --verbosity int32 info log verbosity
https://registry.hub.docker.com/r/kindest/node
kind create cluster --name k8s --image kindest/node:v1.19.7
查看容器
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
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
$docker exec k8s-control-plane crictl ps
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
f45838e47358a 38858e77904d0 13 minutes ago Running kube-controller-manager 1 ea5ce3281ea14
810f12742c561 e422121c9c5f9 13 minutes ago Running local-path-provisioner 1 1b6337b06b28a
894672913740d 4b7bb91932023 13 minutes ago Running kube-scheduler 1 662ef581558d8
33fb275c70fa8 bfe3a36ebd252 46 minutes ago Running coredns 0 b095ceb7cff2d
05daf08a5e6e3 bfe3a36ebd252 46 minutes ago Running coredns 0 97c4f744b37c4
29609f5ad3159 6b17089e24fdb 47 minutes ago Running kindnet-cni 0 081016461b364
62c2df652e8e6 96cb067c0d468 47 minutes ago Running kube-proxy 0 02831473d01f2
d6cd7d73d53d5 0369cf4303ffd 47 minutes ago Running etcd 0 f3979a039e74f
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
docker build -t my-custom-image:unique-tag ./my-image-dir
kind load docker-image my-custom-image:unique-tag
通过kind load docker-image 命令把镜像加载进去
kind load docker-image k8s.gcr.io/ingress-nginx/controller:v0.47.0 --name=k8s
Image: "k8s.gcr.io/ingress-nginx/controller:v0.47.0" with ID "sha256:bf621a764db545fb64e7681c42e44ef33ad58165696359fbd26edc9a7e74a0c5" not yet present on node "k8s-control-plane", loading..
查看容器内镜像
$docker exec -it k8s-control-plane crictl images |grep ingress-nginx
k8s.gcr.io/ingress-nginx/controller v0.47.0 bf621a764db54 101MB
配置文件搭建
设置集群执行使用配置文件方式
kind create cluster --config=yaml配置文件
官方文档 https://kind.sigs.k8s.io/docs/user/configuration/
设置名称
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: app-1-cluster
设置宿主机端口
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: k8s
nodes:
- role: control-plane
image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
extraPortMappings:
- containerPort: 30000
hostPort: 30000
listenAddress: "0.0.0.0"
protocol: tcp
通过参数extraPortMappings 设置 hostPort 和容器内容端口实现
设置多节点集群
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
设置HA集群
# a cluster with 3 control-plane nodes and 3 workers
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: control-plane
- role: control-plane
- role: worker
- role: worker
- role: worker
集群搭建需要使用配置文件方式,创建配置文件
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
apiServerPort: 6443
podSubnet: "10.244.0.0/16"
kubeProxyMode: "ipvs"
nodes:
- role: control-plane
- role: worker
- role: worker
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值:
mages built for this release:
1.21: kindest/node:v1.21.1@sha256:69860bda5563ac81e3c0057d654b5253219618a22ec3a346306239bba8cfa1a6
1.20: kindest/node:v1.20.7@sha256:cbeaf907fc78ac97ce7b625e4bf0de16e3ea725daf6b04f930bd14c67c671ff9
1.19: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
1.18: kindest/node:v1.18.19@sha256:7af1492e19b3192a79f606e43c35fb741e520d195f96399284515f077b3b622c
1.17: kindest/node:v1.17.17@sha256:66f1d0d91a88b8a001811e2f1054af60eef3b669a9a74f9b6db871f2f1eeed00
1.16: kindest/node:v1.16.15@sha256:83067ed51bf2a3395b24687094e283a7c7c865ccc12a8b1d7aa673ba0c5e8861
1.15: kindest/node:v1.15.12@sha256:b920920e1eda689d9936dfcf7332701e80be12566999152626b2c9d730397a95
1.14: kindest/node:v1.14.10@sha256:f8a66ef82822ab4f7569e91a5bccaf27bceee135c1457c512e54de8c6f7219f8
创建集群命令
kind create cluster --config=kind-cluster.yaml
查看当前结果
kubectl get nodes
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready control-plane,master 3m40s v1.21.1
kind-worker Ready <none> 3m8s v1.21.1
kind-worker2 Ready <none> 3m8s v1.19.11
操作命令
kind get clusters
k8s
kind
查看当前的集群,kubectl上下文目前已经被设置为最新的集群
kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
docker-desktop docker-desktop docker-desktop
kind-k8s kind-k8s kind-k8s
* kind-kind kind-kind kind-kind
修改上下文
kubectl config set-context kind-k8s
Context "kind-k8s" modified.
ingress-nginx
kind机器配置文件
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: k8s
nodes:
- role: control-plane
image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
或者可以修改暴露接口8080
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: k8s
nodes:
- role: control-plane
image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 8080
hostPort: 8080
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
因为有node选择和亲和选择
官方教程https://kind.sigs.k8s.io/docs/user/ingress/#ingress-nginx
VERSION=$(curl https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/stable.txt)
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/${VERSION}/deploy/static/provider/kind/deploy.yaml
v0.47.0版本的
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.47.0/deploy/static/provider/kind/deploy.yaml
如果不是80端口
镜像拉取失败替换成国内registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v0.47.0@sha256:a1e4efc107be0bb78f32eaec37bef17d7a0c81bec8066cdf2572508d21351d0b
imagePullPolicy: IfNotPresent
验证kind-usage.yaml
kind: Pod
apiVersion: v1
metadata:
name: foo-app
labels:
app: foo
spec:
containers:
- name: foo-app
image: hashicorp/http-echo:0.2.3
args:
- "-text=foo"
---
kind: Service
apiVersion: v1
metadata:
name: foo-service
spec:
selector:
app: foo
ports:
# Default port used by the image
- port: 5678
---
kind: Pod
apiVersion: v1
metadata:
name: bar-app
labels:
app: bar
spec:
containers:
- name: bar-app
image: hashicorp/http-echo:0.2.3
args:
- "-text=bar"
---
kind: Service
apiVersion: v1
metadata:
name: bar-service
spec:
selector:
app: bar
ports:
# Default port used by the image
- port: 5678
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- http:
paths:
- pathType: Prefix
path: "/foo"
backend:
service:
name: foo-service
port:
number: 5678
- pathType: Prefix
path: "/bar"
backend:
service:
name: bar-service
port:
number: 5678
如果出现https://github.com/kubernetes/ingress-nginx/issues/5401这个问题
Internal error occurred: failed calling webhook "validate.nginx.ingress.kubernetes.io
最简单快捷的方式
kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission
测试
$ curl localhost:8080/bar
bar
$ curl localhost:8080/foo
foo
Nodeport
创建nginx.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: default
spec:
selector:
app: nginx
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30001
执行
kubectl apply -f nginx.yaml
查看svc
kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default nginx NodePort 10.96.8.57 <none> 80:30001/TCP 7m37s
执行
kubectl port-forward --address 0.0.0.0 service/nginx 30001:80
或者通过docker exec 进入kind容器内,下面的495d75d015a8就是kind创建的k8s容器
docker exec -it 495d75d015a8 bash
root@k8s-control-plane:/# curl localhost:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
hello
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/