在这个例子中,会在Kubernetes集群中部署这样一个应用:
它是一个PHP网站,并同时运行3个副本来保证高可用;
这个PHP网站在Redis里存储了一个数据,不定期进行读写;
这个Redis服务由1个Matser节点和2个Slave节点组成高可用集群,读请求由Slave处理,写请求则交给Master。
操作系统版本:CentOS Linux release 7.2.1511 (Core)
内核版本:3.10.0-327.36.2.el7.x86_64
Docker版本:1.10.3
Kubernetes版本:v1.2.0
Kubernets集群由1个Master节点和3个Minion节点组成:
192.168.80.159 kube-master
192.168.80.160 kube-minion1
192.168.80.161 kube-minion2
192.168.80.162 kube-minion3
说明:实例中用到的docker镜像均已pull至本地,并添加到私有仓库中。
首先使用ReplicationController创建一个Redis Master Pod,然后创建该Pod的Service。
redis-master-rc.json
资源文件如下:
{
"kind":"ReplicationController",
"apiVersion":"v1",
"metadata":{
"name":"redis-master",
"labels":{
"name":"redis-master"
}
},
"spec":{
"replicas":1,
"selector":{
"name":"redis-master"
},
"template":{
"metadata":{
"labels":{
"name":"redis-master" }
},
"spec":{
"containers":[
{
"name":"master",
"image":"192.168.80.128:5000/redis",
"ports":[ { "containerPort":6379 } ] }
]
}
}
}
}
redis-master-service.json
资源文件如下:
{
"kind":"Service",
"apiVersion":"v1",
"metadata":{
"name":"redis-master",
"labels":{
"name":"redis-master"
}
},
"spec":{
"ports": [
{
"port":10001,
"targetPort":6379
}
],
"selector":{
"name":"redis-master"
}
}
}
kubectl create -f redis-master-rc.json
kubectl create -f redis-master-service.json
# kubectl get pods,rc,services -o wide
NAME READY STATUS RESTARTS AGE NODE
redis-master-pe80n 1/1 Running 0 1h kube-minion1
NAME DESIRED CURRENT AGE CONTAINER(S) IMAGE(S) SELECTOR
redis-master 1 1 17h master 192.168.80.128:5000/redis name=redis-master
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes 10.254.0.1 443/TCP 23d
redis-master 10.254.155.212 6379/TCP 17h name=redis-master
首先使用ReplicationController创建2个Redis Slave Pod,然后创建该Pod组的Service。
redis-slave-rc.json
资源文件如下:
{
"kind":"ReplicationController",
"apiVersion":"v1",
"metadata":{
"name":"redis-slave",
"labels":{
"name":"redis-slave"
}
},
"spec":{
"replicas":2,
"selector":{
"name":"redis-slave"
},
"template":{
"metadata":{
"labels":{
"name":"redis-slave" }
},
"spec":{
"containers":[
{
"name":"slave",
"image":"192.168.80.128:5000/kubernetes/redis-slave",
"ports":[ { "containerPort":6379 } ] }
]
}
}
}
}
redis-slave-service.json
资源文件如下:
{
"kind":"Service",
"apiVersion":"v1",
"metadata":{
"name":"redis-slave",
"labels":{
"name":"redis-slave"
}
},
"spec":{
"ports": [
{
"port":10001
"targetPort":6379
}
],
"selector":{
"name":"redis-slave"
}
}
}
kubectl create -f redis-slave-rc.json
kubectl create -f redis-slave-service.json
# kubectl get pods,rc,services -o wide
NAME READY STATUS RESTARTS AGE NODE
redis-master-pe80n 1/1 Running 0 1h kube-minion1
redis-slave-t4n6v 1/1 Running 0 1h kube-minion1
redis-slave-vhyt0 1/1 Running 0 1h kube-minion1
NAME DESIRED CURRENT AGE CONTAINER(S) IMAGE(S) SELECTOR
redis-master 1 1 17h master 192.168.80.128:5000/redis name=redis-master
redis-slave 2 2 17h slave 192.168.80.128:5000/kubernetes/redis-slave name=redis-slave
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes 10.254.0.1 443/TCP 23d
redis-master 10.254.155.212 6379/TCP 17h name=redis-master
redis-slave 10.254.236.218 6379/TCP 17h name=redis-slave
同样首先使用ReplicationController创建3个frontend Pod副本,然后创建该Pod组的Service。
frontend-rc.json
资源文件如下:
{
"kind":"ReplicationController",
"apiVersion":"v1",
"metadata":{
"name":"frontend",
"labels":{
"name":"frontend"
}
},
"spec":{
"replicas":3,
"selector":{
"name":"frontend"
},
"template":{
"metadata":{
"labels":{
"name":"frontend" }
},
"spec":{
"containers":[
{
"name":"php-redis",
"image":"192.168.80.128:5000/kubernetes/example-guestbook-php-redis",
"ports":[ { "containerPort":80 } ] }
]
}
}
}
}
frontend-service.json
资源文件如下:
{
"kind":"Service",
"apiVersion":"v1",
"metadata":{
"name":"frontend",
"labels":{
"name":"frontend"
}
},
"spec":{
"type":"NodePort",
"ports": [
{
"port":80,
"nodePort":30001
}
],
"selector":{
"name":"frontend"
}
}
}
kubectl create -f frontend-rc.json
kubectl create -f frontend-service.json
# kubectl get pods,rc,services -o wide
NAME READY STATUS RESTARTS AGE NODE
frontend-bu4np 1/1 Running 0 1h kube-minion3
frontend-dnuh6 1/1 Running 0 1h kube-minion1
frontend-j9pkp 1/1 Running 0 1h kube-minion2
redis-master-pe80n 1/1 Running 0 2h kube-minion1
redis-slave-t4n6v 1/1 Running 0 2h kube-minion1
redis-slave-vhyt0 1/1 Running 0 2h kube-minion1
NAME DESIRED CURRENT AGE CONTAINER(S) IMAGE(S) SELECTOR
frontend 3 3 1h php-redis 192.168.80.128:5000/kubernetes/example-guestbook-php-redis name=frontend
redis-master 1 1 17h master 192.168.80.128:5000/redis name=redis-master
redis-slave 2 2 17h slave 192.168.80.128:5000/kubernetes/redis-slave name=redis-slave
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
frontend 10.254.158.175 nodes 80/TCP 1h name=frontend
kubernetes 10.254.0.1 443/TCP 23d
redis-master 10.254.155.212 6379/TCP 17h name=redis-master
redis-slave 10.254.236.218 6379/TCP 17h name=redis-slave
根据上面获取的Pod信息,php前端分别在3台minion节点上运行,因此可通过这3个minion访问Guestbook。