Kubernetes上手指南之Vagrant部署Kubernetes

想要尝试一下Kubernetes,最简单的方式,就是用Vagrant了。

首先,安装VagrantVirtualBox

第二步,运行命令:

1
2
export KUBERNETES_PROVIDER=vagrant
curl -sS https://get.k8s.io | bash

第一行设置KUBERNETES_PROVIDER环境变量,不设置的话,默认是Google Cloud Engine (GCE)作为运行平台。

https://get.k8s.io是一段脚本,下载最新的Kubernetes稳定版本,存储到本地,名字为Kubernetes.tar.gz,然后解压缩,存储到Kubernetes目录中,并执行创建集群命令。

如果不想通过这个脚本来执行,那到Kubernetes的GitHub上手动下载软件包,然后解压缩,效果是一样的。

第三步,启动Kubernetes集群,在Kubernetes目录下,运行命令:

1
./cluster/kube-up.sh

这个脚本会动态修改Vagrantfile,然后自动虚拟机。如果直接运行vagrant up,会得到不能直接调用vagrant up的提示,这是因为下载的Kubernetes修改了Vagrantfile

需要提一句,默认情况下,kube-up.sh创建的集群是一个master节点和一个minion节点,如果你的机器资源足够(每个节点内存占用1G),可以多起几个minion节点,试验下Kubernetes的调度以及自动重生功能。要想修改启动的minion数目,需要运行脚本:

1
2
export NUM_MINIONS=2
./cluster/kube-up.sh

整个启动时间会比较长,大概做了这么几件事:

  • 如果是初次启动,下载Vagrant的虚拟机Box;
  • 用下载好的Box启动master虚拟机;
  • 安装SaltStack,这是个配置管理工具,用来自动安装所需的环境;
  • 配置环境,也就是执行Provision过程;
  • 启动minion虚拟机;
  • 安装SaltStack;
  • 配置环境;
  • 启动下一个minion虚拟机,直到满足NUM_MINIONS的数目;
  • 最后,还有个validation的过程

如果最后的最后,你看到了这样的输出:

1
2
3
4
Each machine instance has been created/updated.
Now waiting for the Salt provisioning process to complete on each machine.
This can take some time based on your network, disk, and cpu speed.
It is possible for an error to occur during Salt provision of cluster and this could loop forever.

恭喜你,部署成功了……90%。还有最后一步:

1
2
3
Validating master
.....Validating minion-1
....................................................................................................................................................................................Validating minion-2

这里可能会持续很长时间,看到很多个.,不要着急,多等一会儿,就好了。

最后完全建立成功的标志,是看到这样的输出:

1
2
3
4
5
6
7
Cluster validation succeeded
Done, listing cluster services:
current-context: "vagrant"
Running: ./cluster/../cluster/vagrant/../../cluster/../cluster/../cluster/vagrant/../../cluster/../platforms/darwin/amd64/kubectl cluster-info
Kubernetes master is running at https://10.245.1.2
kube-dns is running at https://10.245.1.2/api/v1beta1/proxy/services/kube-dns/ (note the trailing slash)

第四步,小试牛刀。运行几个命令看看:

  • 列出当前的minion节点
1
2
3
4
5
6
$ ./cluster/kubectl.sh get minions
current-context: "vagrant"
Running: ./cluster/../cluster/vagrant/../../cluster/../platforms/darwin/amd64/kubectl get minions
NAME LABELS STATUS
10.245.1.3 <none> Ready
10.245.1.4 <none> Ready
  • 列出当前运行的pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
./cluster/kubectl.sh get pods
current-context: "vagrant"
Running: ./cluster/../cluster/vagrant/../../cluster/../platforms/darwin/amd64/kubectl get pods
POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS CREATED MESSAGE
etcd-server-kubernetes-master kubernetes-master/ <none> Running About an hour
etcd-container gcr.io/google_containers/etcd:2.0.9 Running About an hour
kube-apiserver-kubernetes-master kubernetes-master/ <none> Running About an hour
kube-apiserver gcr.io/google_containers/kube-apiserver:692646611b85d87665ed3f889122fcab Running About an hour
kube-controller-manager-kubernetes-master kubernetes-master/ <none> Running About an hour
kube-controller-manager gcr.io/google_containers/kube-controller-manager:6bed564d3d6609bcb623d810ac5252cd Running About an hour
kube-dns-iji04 10.246.2.2 10.245.1.4/10.245.1.4 k8s-app=kube-dns,kubernetes.io/cluster-service=true,name=kube-dns Running 52 minutes
etcd quay.io/coreos/etcd:v2.0.3 Running 45 minutes
kube2sky gcr.io/google_containers/kube2sky:1.1 Running 45 minutes
skydns gcr.io/google_containers/skydns:2015-03-11-001 Running 46 minutes
kube-scheduler-kubernetes-master kubernetes-master/ <none> Running About an hour
kube-scheduler gcr.io/google_containers/kube-scheduler:373ae04d624d71637cebd9e6caa70621 Running About an hour

Kubernetes的一些调度服务,比如etcd,api server之类的,也都运行在container中,被自己管起来了。不知道这算不算一种自举。

  • 列出所有service
1
2
3
4
5
6
7
./cluster/kubectl.sh get services
current-context: "vagrant"
Running: ./cluster/../cluster/vagrant/../../cluster/../platforms/darwin/amd64/kubectl get services
NAME LABELS SELECTOR IP PORT(S)
kube-dns k8s-app=kube-dns,kubernetes.io/cluster-service=true,name=kube-dns k8s-app=kube-dns 10.247.0.10 53/UDP
kubernetes component=apiserver,provider=kubernetes <none> 10.247.0.2 443/TCP
kubernetes-ro component=apiserver,provider=kubernetes <none> 10.247.0.1 80/TCP
  • 启动运行nginx的container,使用repilication controller,并设置使用3个副本:
1
2
3
4
5
6
./cluster/kubectl.sh run-container my-nginx --image=nginx --replicas=3 --port=80
Starting cluster using os distro: debian
current-context: "vagrant"
Running: ./cluster/../cluster/gce/../../cluster/../platforms/darwin/amd64/kubectl run-container my-nginx --image=nginx --replicas=3 --port=80
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
my-nginx my-nginx nginx run-container=my-nginx 3

在master节点上,可以运行命令查看每个节点上都在运行什么container:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[vagrant@kubernetes-master ~]$ sudo salt '*minion*' cmd.run 'docker ps'
kubernetes-minion-1:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2d40b1c7f66 nginx:1 "nginx -g 'daemon of 41 seconds ago Up 41 seconds k8s_my-nginx.9f0e07bf_my-nginx-tvhpp_default_669eabb2-fe36-11e4-b0be-0800278433f5_a2883f3b
c09a83a6c046 gcr.io/google_containers/pause:0.8.0 "/pause" 4 minutes ago Up 4 minutes k8s_POD.d41d03ce_my-nginx-tvhpp_default_669eabb2-fe36-11e4-b0be-0800278433f5_3c2c26dc
kubernetes-minion-2:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a0ab5cb36ee nginx:1 "nginx -g 'daemon of 55 seconds ago Up 55 seconds k8s_my-nginx.9f0e07bf_my-nginx-razzz_default_669f2d09-fe36-11e4-b0be-0800278433f5_80034ad7
e716dfeacb12 nginx:1 "nginx -g 'daemon of 56 seconds ago Up 55 seconds k8s_my-nginx.9f0e07bf_my-nginx-r47w2_default_669eb5e2-fe36-11e4-b0be-0800278433f5_16e340f5
17bd8ca34033 gcr.io/google_containers/pause:0.8.0 "/pause" 4 minutes ago Up 4 minutes k8s_POD.d41d03ce_my-nginx-razzz_default_669f2d09-fe36-11e4-b0be-0800278433f5_5d3d34fd
7c73449eadb0 gcr.io/google_containers/pause:0.8.0 "/pause" 4 minutes ago Up 4 minutes k8s_POD.d41d03ce_my-nginx-r47w2_default_669eb5e2-fe36-11e4-b0be-0800278433f5_bf21f4a1
8c0d817df5bd gcr.io/google_containers/kube2sky:1.1 "/kube2sky -domain=k 58 minutes ago Up 58 minutes k8s_kube2sky.488c1671_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_644cece9
8fbb31306d64 quay.io/coreos/etcd:v2.0.3 "/etcd -listen-clien 58 minutes ago Up 58 minutes k8s_etcd.97d6179f_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_f595dad2
3d0c10eeace3 gcr.io/google_containers/skydns:2015-03-11-001 "/skydns -machines=h 59 minutes ago Up 59 minutes k8s_skydns.d9122cf3_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_31cb0a51
92fdcca71d2b gcr.io/google_containers/pause:0.8.0 "/pause" About an hour ago Up About an hour k8s_POD.48fc0515_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_8b564fff
  • 试试Kubernetes的动态伸缩能力。运行命令:
1
2
3
4
5
./cluster/kubectl.sh resize rc my-nginx --replicas=2
Starting cluster using os distro: debian
current-context: "vagrant"
Running: ./cluster/../cluster/gce/../../cluster/../platforms/darwin/amd64/kubectl resize rc my-nginx --replicas=2
resized

在看看还有多少container在运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
[vagrant@kubernetes-master ~]$ sudo salt '*minion*' cmd.run 'docker ps'
kubernetes-minion-2:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a0ab5cb36ee nginx:1 "nginx -g 'daemon of 8 minutes ago Up 8 minutes k8s_my-nginx.9f0e07bf_my-nginx-razzz_default_669f2d09-fe36-11e4-b0be-0800278433f5_80034ad7
17bd8ca34033 gcr.io/google_containers/pause:0.8.0 "/pause" 12 minutes ago Up 12 minutes k8s_POD.d41d03ce_my-nginx-razzz_default_669f2d09-fe36-11e4-b0be-0800278433f5_5d3d34fd
8c0d817df5bd gcr.io/google_containers/kube2sky:1.1 "/kube2sky -domain=k About an hour ago Up About an hour k8s_kube2sky.488c1671_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_644cece9
8fbb31306d64 quay.io/coreos/etcd:v2.0.3 "/etcd -listen-clien About an hour ago Up About an hour k8s_etcd.97d6179f_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_f595dad2
3d0c10eeace3 gcr.io/google_containers/skydns:2015-03-11-001 "/skydns -machines=h About an hour ago Up About an hour k8s_skydns.d9122cf3_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_31cb0a51
92fdcca71d2b gcr.io/google_containers/pause:0.8.0 "/pause" About an hour ago Up About an hour k8s_POD.48fc0515_kube-dns-iji04_default_eb31d303-fe2d-11e4-b0be-0800278433f5_8b564fff
kubernetes-minion-1:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2d40b1c7f66 nginx:1 "nginx -g 'daemon of 8 minutes ago Up 8 minutes k8s_my-nginx.9f0e07bf_my-nginx-tvhpp_default_669eabb2-fe36-11e4-b0be-0800278433f5_a2883f3b
c09a83a6c046 gcr.io/google_containers/pause:0.8.0 "/pause" 12 minutes ago Up 12 minutes k8s_POD.d41d03ce_my-nginx-tvhpp_default_669eabb2-fe36-11e4-b0be-0800278433f5_3c2c26dc

只有两个nginx:1的container了。这里查看还有那些container运行,也可以在host机上运行kubectl get pods


通过上面几个步骤,应该对Kubernetes有些初步的了解了。这里还有个很重要的概念没有演示,就是service,我们希望运行多个副本的container能够对外提供同一个访问节点,这就是service要干的事。创建service的过程放在下次再讲。

本文参考资源: