本文简要讲述了如何使用Vagrant一键搭建Kubernetes集群。本项目有针对国内网络进行优化的版本。请在开始前先安装VirtualBox与Vagrant。
简要说明
本项目默认安装具有一个master及两个worker的Kubernetes集群。使用的IP地址为:
1 2 3
| 10.0.0.80 master 10.0.0.81 worker1 10.0.0.82 worker2
|
如需要修改worker个数、IP地址或者Kubernetes版本,所需修改的地方将在后文说明。
版本说明
本项目使用的 VirtualBox 及 Vagrant 版本:
1 2
| VirtualBox: 6.1.20 r143896 (Qt5.6.2) Vagrant: 2.2.16
|
本项目使用的 Vagrant Box 为 ubuntu/bionic64
。
本项目搭建的集群版本:
1 2 3 4 5 6 7
| kube-apiserver: v1.21.1 kube-proxy: v1.21.1 kube-controller-manager: v1.21.1 kube-scheduler: v1.21.1 pause: 3.4.1 coredns: v1.8.0 etcd: 3.4.13-0
|
使用方法
搭建集群
GitHub版本
1 2 3
| git clone https://github.com/bambrow/vagrant-k8s-cluster.git cd vagrant-k8s-cluster vagrant up
|
Gitee版本(针对国内网络优化)
1 2 3
| git clone https://gitee.com/bambrow/vagrant-k8s-cluster-cn.git cd vagrant-k8s-cluster-cn vagrant up
|
连接节点
1 2 3
| vagrant ssh master vagrant ssh worker1 vagrant ssh worker2
|
如果一切启动顺利,可以进入master节点查看状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| $ kubectl get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME master Ready control-plane,master 48m v1.21.1 10.0.0.80 <none> Ubuntu 18.04.5 LTS 4.15.0-147-generic docker://20.10.12 worker1 Ready <none> 7m28s v1.21.1 10.0.0.81 <none> Ubuntu 18.04.5 LTS 4.15.0-147-generic docker://20.10.12 worker2 Ready <none> 21m v1.21.1 10.0.0.82 <none> Ubuntu 18.04.5 LTS 4.15.0-147-generic docker://20.10.12
$ kubectl -n kube-system get all -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/calico-kube-controllers-6b9fbfff44-4jpvc 1/1 Running 0 50m 10.244.219.66 master <none> <none> pod/calico-node-p4qb2 1/1 Running 0 23m 10.0.0.82 worker2 <none> <none> pod/calico-node-tcsxt 1/1 Running 0 9m47s 10.0.0.81 worker1 <none> <none> pod/calico-node-wl74r 1/1 Running 0 50m 10.0.0.80 master <none> <none> pod/coredns-558bd4d5db-78sjf 1/1 Running 0 50m 10.244.219.67 master <none> <none> pod/coredns-558bd4d5db-jft6t 1/1 Running 0 50m 10.244.219.65 master <none> <none> pod/etcd-master 1/1 Running 0 50m 10.0.0.80 master <none> <none> pod/kube-apiserver-master 1/1 Running 0 50m 10.0.0.80 master <none> <none> pod/kube-controller-manager-master 1/1 Running 8 50m 10.0.0.80 master <none> <none> pod/kube-proxy-h4vnv 1/1 Running 0 9m47s 10.0.0.81 worker1 <none> <none> pod/kube-proxy-jgd5h 1/1 Running 1 23m 10.0.0.82 worker2 <none> <none> pod/kube-proxy-pznsv 1/1 Running 0 50m 10.0.0.80 master <none> <none> pod/kube-scheduler-master 1/1 Running 3 50m 10.0.0.80 master <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 50m k8s-app=kube-dns
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR daemonset.apps/calico-node 3 3 3 3 3 kubernetes.io/os=linux 50m calico-node docker.io/calico/node:v3.21.2 k8s-app=calico-node daemonset.apps/kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 50m kube-proxy k8s.gcr.io/kube-proxy:v1.21.1 k8s-app=kube-proxy
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR deployment.apps/calico-kube-controllers 1/1 1 1 50m calico-kube-controllers docker.io/calico/kube-controllers:v3.21.2 k8s-app=calico-kube-controllers deployment.apps/coredns 2/2 2 2 50m coredns k8s.gcr.io/coredns/coredns:v1.8.0 k8s-app=kube-dns
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR replicaset.apps/calico-kube-controllers-6b9fbfff44 1 1 1 50m calico-kube-controllers docker.io/calico/kube-controllers:v3.21.2 k8s-app=calico-kube-controllers,pod-template-hash=6b9fbfff44 replicaset.apps/coredns-558bd4d5db 2 2 2 50m coredns k8s.gcr.io/coredns/coredns:v1.8.0 k8s-app=kube-dns,pod-template-hash=558bd4d5db
|
暂停与启动集群
如需暂停或启动(包括销毁后重建)某个节点,可以使用vagrant halt|up master|worker1|worker2
。
销毁集群
如需销毁某个节点,可以使用vagrant destroy -f master|worker1|worker2
。
特殊设置
修改worker个数
打开Vagrantfile
,修改如下设置:
Vagrantfile1 2 3 4 5 6 7
| ... - (1..2).each do |i| + (1..[number]).each do |i| config.vm.define "worker#{i}" do |worker| worker.vm.box = "ubuntu/bionic64" worker.vm.hostname = "worker#{i}" ...
|
将 [number]
改为你需要的数目即可。请注意,在修改完毕后需要在hosts设置处添加或删除对应的行。例如,添加worker3
后需要做如下修改:
Vagrantfile1 2 3 4 5 6
| ... echo "10.0.0.80 master" >> /etc/hosts echo "10.0.0.81 worker1" >> /etc/hosts echo "10.0.0.82 worker2" >> /etc/hosts + echo "10.0.0.83 worker3" >> /etc/hosts ...
|
修改IP地址
打开Vagrantfile
,修改如下设置:
Vagrantfile1 2 3
| echo "10.0.0.80 master" >> /etc/hosts echo "10.0.0.81 worker1" >> /etc/hosts echo "10.0.0.82 worker2" >> /etc/hosts
|
把三个IP设置为你想要的IP即可。然后,修改下面的master.vm.network
与worker.vm.network
。请注意,worker.vm.network
的IP必须是连续的。
此外,还需要修改master.sh
中的MASTER_IP
。
修改Kubernetes版本
首先,修改common.sh
中的KUBERNETES_VERSION
。其次,需要修改master.sh
中的KUBE_VERSION
。
如果你使用的是针对国内网络优化的Gitee版本,需要注意以下情况:
由于镜像的image版本问题,对于Kubernetes v1.21.1,coredns
的tag有错误,因此在master.sh
中有修复tag的语句:
master.sh1 2 3 4
| COREDNS_VERSION=1.8.0 sudo docker pull registry.aliyuncs.com/google_containers/coredns:$COREDNS_VERSION sudo docker tag registry.aliyuncs.com/google_containers/coredns:$COREDNS_VERSION registry.aliyuncs.com/google_containers/coredns/coredns:v$COREDNS_VERSION
|
如果使用其他版本的Kubernetes,这个问题可能不会出现,因此可以按需要修改COREDNS_VERSION
或直接注释掉这几行即可。