在中国搭建Kubernetes环境并运行guestbook应用
为了创建Kubernetes验证环境,将在VirtualBox的虚拟机上构建Kubernetes集群。
环境
-
- OS X El Capitan
-
- VirtualBox 5.0.8
-
- Vagrant 1.7.4
- Kubernetes 1.1.1
建立步骤
最新版本在官方存储库中。在这里我们将使用1.1.1版本进行推进。
$ wget https://github.com/kubernetes/kubernetes/releases/download/v1.1.1/kubernetes.tar.gz
$ tar zxvf kubernetes.tar.gz
Kubernetes 的自动构建脚本位于 ./cluster/kube-up.sh 中,但默认构建位置是在 GCE(Google Compute Engine),为了改为 vagrant,并且预先指定 Minion 的数量。
$ cd kubernetes
$ export KUBERNETES_PROVIDER=vagrant # vagrantを利用してvirtualbox上に構築
$ export NUM_MINIONS=2 # Minionの数
$ cluster/kube-up.sh # しばらくまつ
...
Done, listing cluster services:
Kubernetes master is running at https://10.245.1.2
Heapster is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/heapster
KubeDNS is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/kube-dns
KubeUI is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/kube-ui
Grafana is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/monitoring-grafana
InfluxDB is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/monitoring-influxdb
確保环境构建完成后,确保kubectl命令可正常运行。
$ cluster/kubectl.sh get pods # まだ何もない
NAME READY STATUS RESTARTS AGE
$ cluster/kubectl.sh get services
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
kubernetes 10.247.0.1 <none> 443/TCP <none> 8m
留言簿的执行
在kubernetes/examples文件夹中执行名为guestbook的示例程序。
增加 Redis 主节点
$ cluster/kubectl.sh create -f examples/guestbook/redis-master-controller.yaml
replicationcontroller "redis-master-controller" created
$ cluster/kubectl.sh create -f examples/guestbook/redis-master-service.yaml
service "redis-master" created
$ cluster/kubectl.sh get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS AGE
redis-master master redis name=redis-master 1 4m
$ cluster/kubectl.sh get pods
NAME READY STATUS RESTARTS AGE
redis-master-svui5 1/1 Running 0 4m
$ cluster/kubectl.sh get services
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
kubernetes 10.247.0.1 <none> 443/TCP <none> 10h
redis-master 10.247.189.209 <none> 6379/TCP name=redis-master 20s
添加Redis从服务器
$ cluster/kubectl.sh create -f examples/guestbook/redis-slave-controller.yaml
replicationcontroller "redis-slave" created
$ cluster/kubectl.sh create -f examples/guestbook/redis-slave-service.yaml
service "redis-slave" created
$ cluster/kubectl.sh get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS AGE
redis-master master redis name=redis-master 1 7m
redis-slave worker gcr.io/google_samples/gb-redisslave:v1 name=redis-slave 2 59s
$ cluster/kubectl.sh get pods
NAME READY STATUS RESTARTS AGE
redis-master-svui5 1/1 Running 0 8m
redis-slave-67g25 1/1 Running 0 1m
redis-slave-vsuge 1/1 Running 0 1m
$ cluster/kubectl.sh get services
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
kubernetes 10.247.0.1 <none> 443/TCP <none> 10h
redis-master 10.247.189.209 <none> 6379/TCP name=redis-master 2m
redis-slave 10.247.105.127 <none> 6379/TCP name=redis-slave 1m
添加前端
在添加前端时,需要设置NodePort以使Minion内部以及主机Mac可以访问。
$ vi examples/guestbook/frontend-service.yaml
# 下記のように2行追加する
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
name: frontend
spec:
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
# type: LoadBalancer
type: NodePort # ADD
ports:
# the port that this service should serve on
- port: 80
nodePort: 30301 # ADD
selector:
name: frontend
$ cluster/kubectl.sh create -f examples/guestbook/frontend-controller.yaml
replicationcontroller "frontend" created
$ cluster/kubectl.sh create -f examples/guestbook/frontend-service.yaml
You have exposed your service on an external port on all nodes in your
cluster. If you want to expose this service to the external internet, you may
need to set up firewall rules for the service port(s) (tcp:30301) to serve traffic.
See http://releases.k8s.io/release-1.1/docs/user-guide/services-firewalls.md for more details.
service "frontend" created
$ cluster/kubectl.sh get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS AGE
frontend php-redis gcr.io/google_samples/gb-frontend:v3 name=frontend 3 1m
redis-master master redis name=redis-master 1 14m
redis-slave worker gcr.io/google_samples/gb-redisslave:v1 name=redis-slave 2 7m
$ cluster/kubectl.sh get pods
NAME READY STATUS RESTARTS AGE
frontend-b8fay 1/1 Running 0 3m
frontend-en9xd 1/1 Running 0 3m
frontend-fu8mh 1/1 Running 0 3m
redis-master-svui5 1/1 Running 0 16m
redis-slave-67g25 1/1 Running 0 9m
redis-slave-vsuge 1/1 Running 0 9m
$ cluster/kubectl.sh get services
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
frontend 10.247.5.231 nodes 80/TCP name=frontend 55s
kubernetes 10.247.0.1 <none> 443/TCP <none> 11h
redis-master 10.247.189.209 <none> 6379/TCP name=redis-master 8m
redis-slave 10.247.105.127 <none> 6379/TCP name=redis-slave 7m
考试
在主机上执行命令kubectl get nodes,查找Minion的IP地址,然后在主机的浏览器中访问”http://[通过上述命令找到的Minion IP地址]:30301″。
$ cluster/kubectl.sh get nodes
NAME LABELS STATUS AGE
10.245.1.3 kubernetes.io/hostname=10.245.1.3 Ready 11h
10.245.1.4 kubernetes.io/hostname=10.245.1.4 Ready 10h
当显示出类似下面这样的画面并可以发布评论时,就算完成了。