一台虚拟机创建k8s多节点集群

一、安装docker环境

这里使用ubuntu系统

apt update
apt install snapd
snap install docker

cat >> ~/.bashrc << EOF
export PATH=/snap/bin:$PATH
EOF

source ~/.bashrc

二、下载kind

https://github.com/kubernetes-sigs/kind/releases下载最新版本的kind

wget https://github.com/kubernetes-sigs/kind/releases/download/v0.5.1/kind-linux-amd64
chmod +x kind-linux-amd64
mv kind-linux-amd64 /usr/local/bin/kind

三、初始化k8s集群

  1. 创建配置文件
cat > kind-config.yaml << EOF
kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
EOF
  1. 创建集群
kind create cluster --config kind-config.yaml  --name k8s
  1. 测试集群
snap install kubectl
export KUBECONFIG="$(kind get kubeconfig-path --name="k8s")"

kubectl cluster-info

或者登录到control节点(docker容器)

kind get kubeconfig-path --name="k8s"
cp /root/.kube/kind-config-k8s /root
sed -i -e "s/34891/8443/g" /root/kind-config-k8s
docker cp /root/kind-config-k8s k8s-control-plane:/root/.kube/

docker exec -it k8s-control-plane bash
export KUBECONFIG=/root/.kube/kind-config-k8s
kubectl cluster-info

推荐阅读更多精彩内容