Demo repo to create k8s clusters on local machine using Ansible and Vagrant. 演示如何一键构建k8s集群,满足你蠢蠢欲动的小心灵。
Please note that current version of Vagrant (2.2.6) only supports Virtualbox version up to 6.0.x, please do not install Virtualbox 6.1.
If you install Ansible on your host machine is not an option, please check Windows User section.
Install requirements from Ansible Galaxy.
> ansible-galaxy install -r requirements.yml
Start Vms using Vagrant
> vagrant up
Install kubernetes clusters using Ansible playbook.
> ansible-playbook -i inventory kubernetes.yml
Connect to k8s cluster using config
> export KUBECONFIG=kubeconf/k8s-master-01_admin.conf
> kubectl get nodes
Copy the granted token from generated dashboard_token.log
cat dashboard_token.log | grep '^token'
Run proxy for the Dashboard
> kubectl proxy
Open the Dashboard UI and login using the token.
/proxy/">http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https/proxy/
> ansible-playbook -i inventory dashboard.yml
> ansible-playbook -i inventory dashboard_token.yml
Because Ansible does not support Windows host, we can use one of the nodes of the k8s clusters as the Ansible host.
I’ve configured Vagrant to provision the kubernetes using the master node as the Ansible host.
So please use the following orders to startup the clusters.
Start Vms using Vagrant
> vagrant up
Provision kuberneters using Vagrant provision.
> vagrant provision --provision-with kubernetes
Use your k8s clusters.
You can also run Ansible playbook after ssh into the master node, the project folder will be automatically mounted to /vagrant
, and Ansible should already be installed.
> vagrant ssh k8s-master-01
> cd /vagrant
> ansible-playbook -i inventory dashboard_token.yml
vboxnet1
of virtualbox:
> sudo route add -net 192.168.10 -interface vboxnet1
# check route
> netstat -nr
注意1:当前 Vagrant 版本(2.2.6)不支持 Virtualbox 6.1,请安装6.0.x版本的 VirtualBox
注意2:Ansible 不支持使用 Windows 做主机,如果你是 Windows 用户,可以不用装,当然你是 Macos 或者 Linux 用户也可以不用装,我都帮你们在 Vagrant Provision 里配置好了。
使用 Vagrant 起虚拟机:
> vagrant up
国内网络环境用户、不能科学上网的国内用户,我特地为大家准备了使用阿里云镜像的加速;如果你能科学上网或者身处国外网络环境,请跳过此步骤:
> vagrant provision --provision-with aliyun_mirror
安装 Kubernetes
> vagrant provision --provision-with kubernetes
狗定,大家都是资深开发/运维人员了,自己研究吧。
vagrant up
慢、超时报错等使用此链接下载ubuntu的box
https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20200115.0.0/providers/virtualbox.box
将下载的box添加到本地 Vagrant box ,记得自行替换路径
> vagrant box add --name ubuntu/bionic64 C:\\迅雷下载\\virtualbox.box