使用Kubernetes创建PHP留言板系统
本文将演示使用kubernetes系统基于 kubeguide相关的镜像文件创建基于php和redis的留言板系统,因为系统资源有限,kubernetes集群只有一个master节点和slave节点。
一、环境介绍
操作系统版本:CentOS linux 7.2 64bit
Master节点:192.168.115.5/24 vm1
Slave节点1:192.168.115.6/24 vm2
访问流程图如下:
二、在线下载相关的镜像文件,并纳入本地仓库统一管理
# docker pull kubeguide/redis-master
# docker pull kubeguide/guestbook-php-frontend
# docker pull kubeguide/guestbook-redis-slave
# docker tag kubeguide/redis-master registry.fjhb.cn/redis-master
# docker tag kubeguide/guestbook-php-frontend registry.fjhb.cn/guestbook-php-frontend
# docker tag kubeguide/guestbook-redis-slave registry.fjhb.cn/guestbook-redis-slave
# docker push registry.fjhb.cn/redis-master
# docker push registry.fjhb.cn/guestbook-php-frontend
# docker push registry.fjhb.cn/guestbook-redis-slave
# curl -u yang:123 -XGET https://registry.fjhb.cn/v2/_catalog
# docker images |grep fjhb
三、配置文件准备
1、创建redis-master rc和service配置文件
# cat redis-master-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-master
spec:
replicas: 1
selector:
name: redis-master
template:
metadata:
name: redis-master
labels:
name: redis-master
spec:
containers:
- name: redis-master
image: registry.fjhb.cn/redis-master
ports:
- containerPort: 6379# cat redis-master-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
name: redis-master
spec:
ports:
- port: 6379
targetPort: 6379
selector:
name: redis-master
2、创建redis-slave的rc和service配置文件
# cat redis-slave-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-slave
spec:
replicas: 2
selector:
name: redis-slave
template:
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
containers:
- name: redis-slave
image: registry.fjhb.cn/guestbook-redis-slave
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 6379# cat redis-slave-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
ports:
- port: 6379
selector:
name: redis-slave
3、创建frontend的rc和service配置文件
# cat frontend-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
labels:
name: frontend
spec:
replicas: 3
selector:
name: frontend
template:
metadata:
labels:
name: frontend
spec:
containers:
- name: frontend
image: registry.fjhb.cn/guestbook-php-frontend
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 80# cat frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
name: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort: 30001
selector:
name: frontend
四、在master节点上创建rc 和 sevices
# kubectl create -f redis-master-controller.yaml
# kubectl create -f redis-slave-controller.yaml
# kubectl create -f frontend-controller.yaml
# kubectl create -f redis-master-service.yaml
# kubectl create -f redis-slave-service.yaml
# kubectl create -f frontend-service.yaml
# kubectl get rc
# kubectl get svc
# kubectl get pod
# kubectl describe pod redis-slave-gsk1p
无法创建pod的原因主要是无法从本地仓库中拉取镜像,即使本地已经存在了这个镜像,也是报这个错误。因为kubernetes的imagePullPolicy获取镜像策略默认值为Always
我们本地仓库的nginx配置了basic验证,故而报错如下
Error syncing pod, skipping: failed to "StartContainer" for "redis-slave" with ErrImagePull: "unauthorized: authentication required"
关闭nginx basic认证之后,再尝试一次,果然没有问题。
在Kubernetes中使用私有仓库并支持basic认证的最终解决方案是去配置kubernetes的serviceaccount,关于详细的配置将在后续的文章中进行介绍。
六、测试
1、查看redis master节点的redis集群信息
# kubectl exec redis-master-9993n redis-cli info |grep -A 5 "Replication"
# kubectl exec redis-slave-gnq41 redis-cli info |grep -A 5 "Replication"
# kubectl exec redis-slave-nvscp redis-cli info |grep -A 5 "Replication"
2、web测试
通过firebug插件可以看到是php+apache在提供web服务
七、 rc和service的删除
# kubectl delete -f frontend-controller.yaml
# kubectl delete -f redis-master-controller.yaml
# kubectl delete -f redis-slave-controller.yaml
# kubectl delete -f redis-slave-service.yaml
# kubectl delete -f redis-master-service.yaml
# kubectl delete -f frontend-service.yaml