前言
kind 即 Kubernetes In Docker,顾名思义,就是将 k8s 所需要的所有组件,全部部署在一个docker容器中,是一套开箱即用的 k8s 环境搭建方案。
使用 kind 搭建的集群无法在生产中使用,但是如果你只是想在本地简单的玩玩 k8s,不想占用太多的资源,那么使用 kind 是你不错的选择。同样,kind 还可以很方便的帮你本地的 k8s 源代码打成对应的镜像,方便测试。
使用 kind create cluster 安装,是没有指定任何配置文件的安装方式。从安装打印出的输出来看,分为4步:
- 查看本地上是否存在一个基础的安装镜像,默认是 kindest/node:v1.13.4,这个镜像里面包含了需要安装的所有东西,包括了 kubectl、kubeadm、kubelet 二进制文件,以及安装对应版本 k8s 所需要的镜像,都以 tar 压缩包的形式放在镜像内的一个路径下
- 准备你的 node,这里就是做一些启动容器、解压镜像之类的工作
- 生成对应的 kubeadm 的配置,之后通过 kubeadm 安装,安装之后还会做另外的一些操作,比如像我刚才仅安装单节点的集群,会帮你删掉 master 节点上的污点,否则对于没有容忍的 pod 无法部署。
- 启动完毕
配置实践
1、下载kind
wget https://github.com/kubernetes-sigs/kind/releases/download/0.2.1/kind-linux-amd64
mv kind-linux-amd64 kind
chmod +x kind
mv kind /usr/bin
2、创建单节点集群
☁ ~ kind create cluster
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.13.4)
✓ Preparing nodes
✓ Creating kubeadm config
✓ Starting control-plane ️
Cluster creation complete. You can now use the cluster with:
export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
kubectl cluster-info
☁ ~ export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
☁ ~ kubectl cluster-info
Kubernetes master is running at https://localhost:20561
KubeDNS is running at https://localhost:20561/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
3、查看node、pod
☁ ~ kubectl get node
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready master 8m13s v1.13.4
☁ ~ kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-86c58d9df4-5fzwx 1/1 Running 0 8m20s
coredns-86c58d9df4-6wkdx 1/1 Running 0 8m20s
etcd-kind-control-plane 1/1 Running 0 7m11s
kube-apiserver-kind-control-plane 1/1 Running 0 7m25s
kube-controller-manager-kind-control-plane 1/1 Running 0 7m16s
kube-proxy-9blcg 1/1 Running 0 8m20s
kube-scheduler-kind-control-plane 1/1 Running 0 7m27s
weave-net-c7tg7 2/2 Running 1 8m20s
4、创建多节点集群
☁ ~ vim kind-config.yaml
apiVersion: kind.sigs.k8s.io/v1alpha3
kind: Cluster
nodes:
- role: control-plane
- role: worker
☁ ~ kind create cluster --name kk --config kind-config.yaml
Creating cluster "kk" ...
✓ Ensuring node image (kindest/node:v1.13.4)
✓ Preparing nodes
✓ Creating kubeadm config
✓ Starting control-plane ️
✓ Joining worker nodes
Cluster creation complete. You can now use the cluster with:
export KUBECONFIG="$(kind get kubeconfig-path --name="kk")"
kubectl cluster-info
☁ ~ export KUBECONFIG="$(kind get kubeconfig-path --name="kk")"
☁ ~ kubectl cluster-info
Kubernetes master is running at https://localhost:30339
KubeDNS is running at https://localhost:30339/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
5、查看node、pod
☁ ~ kubectl get node
NAME STATUS ROLES AGE VERSION
kk-control-plane Ready master 63s v1.13.4
kk-worker Ready <none> 47s v1.13.4
☁ ~ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-86c58d9df4-7dq7v 1/1 Running 0 67s
coredns-86c58d9df4-p54w6 1/1 Running 0 67s
etcd-kk-control-plane 1/1 Running 0 6s
kube-apiserver-kk-control-plane 1/1 Running 0 19s
kube-proxy-2qzl7 1/1 Running 0 61s
kube-proxy-8g2ds 1/1 Running 0 66s
weave-net-8zkr4 2/2 Running 1 61s
weave-net-pjg2t 2/2 Running 1 66s
6、查看容器(节点)
[root@tjwq01-userdns02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
146e66a17125 kindest/node:v1.13.4 "/usr/local/bin/entr…" 3 minutes ago Up 3 minutes 16621/tcp, 127.0.0.1:16621->6443/tcp mo-control-plane
0e7c81a8cd22 kindest/node:v1.13.4 "/usr/local/bin/entr…" 3 minutes ago Up 3 minutes mo-worker
7、登录worker节点
[root@tjwq01-userdns02 ~]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5c7588df-r7frw 1/1 Running 0 3m57s 10.32.0.2 mo-worker <none> <none>
[root@tjwq01-userdns02 ~]# docker exec -it 0e7c81a8cd22 bash
root@mo-worker:/# ls
bin build etc kind lib64 mnt proc run srv tmp var
boot dev home lib media opt root sbin sys usr
8、查看配置文件
root@mo-worker:/etc/kubernetes# ls
bootstrap-kubelet.conf kubelet.conf pki
9、访问nginx服务
root@mo-worker:/etc/kubernetes# curl 10.0.238.21
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>