使用示例
最后,让我们进入实战模式,这里跑1个最简单的单机示例(所有组件运行在1台机器上),旨在买通基本流程。
搭建环境
第1步,我们需要Kuberntes各组件的2进制可履行文件。有以下两种方式获得:
git clone https://github.com/GoogleCloudPlatform/kubernetes.git
cd kubernetes/build
./release.sh
wget https://storage.googleapis.com/kubernetes/binaries.tar.gz
自己编译源码需要先安装好golang,编译完以后在kubernetes/_output/release-tars文件夹下可以得到打包文件。直接下载的方式不需要安装其他软件,但可能得不到最新的版本。
第2步,我们还需要etcd的2进制可履行文件,通过以下方式获得:
wget https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz
tar xvf etcd-v0.4.6-linux-amd64.tar.gz
第3步,就能够启动各个组件了:
etcd
cd etcd-v0.4.6-linux-amd64
./etcd
apiserver
./apiserver
-address=127.0.0.1
-port=8080
-portal_net="172.0.0.0/16"
-etcd_servers=http://127.0.0.1:4001
-machines=127.0.0.1
-v=3
-logtostderr=false
-log_dir=./log
scheduler
./scheduler -master 127.0.0.1:8080
-v=3
-logtostderr=false
-log_dir=./log
controller-manager
./controller-manager -master 127.0.0.1:8080
-v=3
-logtostderr=false
-log_dir=./log
kubelet
./kubelet
-address=127.0.0.1
-port=10250
-hostname_override=127.0.0.1
-etcd_servers=http://127.0.0.1:4001
-v=3
-logtostderr=false
-log_dir=./log
创建pod
搭好了运行环境后,就能够提交pod了。首先编写pod描写文件,保存为redis.json:
{
"id": "redis",
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "redis",
"containers": [{
"name": "redis",
"image": "dockerfile/redis",
"imagePullPolicy": "PullIfNotPresent",
"ports": [{
"containerPort": 6379,
"hostPort": 6379
}]
}]
}
},
"labels": {
"name": "redis"
}
}
然后,通过命令行工具kubecfg提交:
./kubecfg -c redis.json create /pods
提交完后,通过kubecfg查看pod状态:
# ./kubecfg list /pods
ID Image(s) Host Labels Status
---------- ---------- ---------- ---------- ----------
redis dockerfile/redis 127.0.0.1/ name=redis Running
Status是Running表示pod已在容器里运行起来了,可以用"docker ps"命令来查看容器信息:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae83d1e4b1ec dockerfile/redis:latest "redis-server /etc/r 19 seconds ago Up 19 seconds k8s_redis.caa18858_redis.default.etcd_1414684622_1b43fe35
创建replicationController
{
"id": "redisController",
"apiVersion": "v1beta1",
"kind": "ReplicationController",
"desiredState": {
"replicas": 1,
"replicaSelector": {"name": "redis"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "redisController",
"containers": [{
"name": "redis",
"image": "dockerfile/redis",
"imagePullPolicy": "PullIfNotPresent",
"ports": [{
"containerPort": 6379,
"hostPort": 6379
}]
}]
}
},
"labels": {"name": "redis"}
}},
"labels": {"name": "redis"}
}
然后,通过命令行工具kubecfg提交:
./kubecfg -c redisController.json create /replicationControllers
提交完后,通过kubecfg查看replicationController状态:
# ./kubecfg list /replicationControllers
ID Image(s) Selector Replicas
---------- ---------- ---------- ----------
redisController dockerfile/redis name=redis 1
同时,1个pod也将被自动创建出来,即便我们故意删除该pod,replicationController也将保证创建1个新pod。