暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

如何使用Kind 快速构建 Kubernetes 集群

202
Kind是什么

kind is a tool for running local Kubernetes clusters using Docker container “nodes”.kind was primarily designed for testing Kubernetes itself, but may be used for local development or CI. 

Kind 用途

其实就是Kubernetes in Docker 的技术,这种技术有什么用,能用于生产环境吗?这种技术主要用在如果你没有很多服务器供你安装k8s时,你又需要一个k8s开发环境进行联调、学习k8s技术、快速构建环境验证等,此时你就可以使用一台安装了Docker的服务器,使用kind命令来安装k8s环境,并且如果你单机性能足够好的话,跑多套k8s集群没有任何问题。至于能不能用于生产,不建议生产环境使用。具体的可参见官网 https://kind.sigs.k8s.io/

安装部署

1. 部署安装工具包

apt install -y net-tools tcpdump chrony bridge-utils tree wget iftop ethtool curl
复制

2. 关闭swap

sed -ri 's/.*swap.*/#&/' etc/fstab
swapoff -a

复制

3. 修改内核参数

# vi etc/sysctl.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
fs.inotify.max_user_instances=1024
fs.inotify.max_user_watches=524288
fs.file-max=524288
# vim etc/security/limits.conf
root soft nofile 655350
root hard nofile 655350
* soft nofile 655350
* hard nofile 655350

复制

4. 添加阿里的repo

apt-get update && apt-get install -y apt-transport-https

apt upgrade -y
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
tee /etc/apt/sources.list.d/kubernetes.list <<-'EOF'
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update

复制

5. 安装Docker并配置启动

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
mkdir -p /etc/docker
cat <<EOF > /etc/docker/daemon.json
{
  "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
systemctl enable docker

复制

6. 部署kind 工具

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.18.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

复制

kind 使用

 1. 简单配置文件

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: testkind1
networking:
  disableDefaultCNI: true
  podSubnet: "10.244.0.0/16"
  serviceSubnet: "10.96.0.0/12"
  apiServerAddress: "0.0.0.0"
  apiServerPort: 6543
  kubeProxyMode: "ipvs"
containerdConfigPatches:
- |-
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]
    endpoint = ["https://registry.aliyuncs.com/google_containers"]
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]
    endpoint = ["https://registry.aliyuncs.com/google_containers"]
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."quay.io"]
    endpoint = ["https://quay-mirror.aliyuncs.com"]
nodes:
- role: control-plane
- role: worker
- role: worker

复制

官网:https://kind.sigs.k8s.io/docs/user/configuration/

2. 部署命令

kind create cluster --config ./b.yaml --verbosity 4 --retain  --wait 20m

--verbosity:显示日志部署过程
--retain:默认部署失败的话,会docker容器会直接消失,这个可以设置下,方便部署失败时查看;

--wait 20m:k8s部署时间默认等待Ready 4m0s,如果下载镜像慢的话,有可能会失败,这里设置20m,但要注意一个问题,如果你配置文件中指定了disableDefaultCNI: true,不使用默认插件的话,这个如果再设置了--wait 20m,那么就会等待很长时间。如下图

此时可以通过手动部署下网络插件
docker exec -it testkind1-control-plane /bin/bash kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
复制
指定安装k8s的版本:kind create cluster --config ./b.yaml  --retain --image=kindest/node:v1.26.3 --wait 10m

3. 查看kind创建的集群

root@k8s-test:~/kind# kind get clusters |grep test
testkind1
root@k8s-test:~/kind#

复制

4. 查看集群配置并导出

root@k8s-test:~/kind# kind get kubeconfig --name testkind1 > /root/.kube/config
root@k8s-test:~/kind#

复制

或者

root@k8s-test:~/kind# kind export kubeconfig --name testkind1
Set kubectl context to "kind-testkind1"
root@k8s-test:~/kind#

复制

5. 查看集群node节点列表

root@k8s-test:~/kind# kind get nodes --name testkind1
testkind1-control-plane
testkind1-worker
testkind1-worker2
root@k8s-test:~/kind#

复制

6. 解决kind创建集群中无法下载镜像问题

kind load docker-image registry.k8s.io/metrics-server/metrics-server:v0.6.3 --name <集群名称>
复制

7. 解决kind创建集群的master节点无法调度

kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name |grep control-plane
kubectl taint nodes $controller_node node-role.kubernetes.io/master:NoSchedule-
kubectl get nodes -owide

复制

总结

本文主要总结下kind的简单使用,方便快速创建一套可用集群进行测试或学习。

 

文章转载自Linux点滴运维实践,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论