实例:Guestbook留言板系统将通过Pod、RC、Service等资源对象搭建完成,成功启动后在网页中显示一条“Hello World”留言。其系统架构是一个基于PHP+Redis的分布式Web应用,前端PHP Web网站通过访问后端的Redis来完成用户留言的查询和添加功能。同时Redis以Master+Slave的模式进行部署,实现数据的读写分离能力。
架构图:
一、创建redis-master的RC定义文件
vim redis-master-controller.yaml
apiVersion: v1 kind: ReplicationController #表明这是一个RC metadata: name: redis-master labels: name: redis-master spec: replicas: 1 selector: #spec.selector是RC的Pod选择器,监控和管理拥有这些标签(Label)的Pod实例,确保当前集群上始终有且仅有replicas个Pod实例在运行,这里设置replicas=1表示只运行一个名为redis-master的Pod实例,当集群中运行的Pod数量小于replicas时,RC会根据spec.template段定义的Pod模块来生成一个新的Pod实例,lebels属性指定了该Pod的标签,这里的labels必须匹配RC的spec.selector name: redis-master template: metadata: labels: name: redis-master spec: containers: - name: master image: kubeguide/redis-master ports: - containerPort: 6379
kubectl create -f redis-master-controller.yaml
2、创建与之关联的Service(服务)定义文件
vim redis-master-service.yaml
apiVersion: v1 kind: Service metadata: name: redis-master #metadata.name是Service的服务名 labels: name: redis-master spec: ports: - port: 6379 #port属性定义的是Service的虚拟端口号 targetPort: 6379 #targetPort属性指定后端Pod内容器应用监听的端口号 selector: #spec.selector确定了选择哪些Pod name: redis-master
kubectl create -f redis-master-service.yaml
3、创建redis-slave RC定义文件
vim redis-slave-controller.yaml
apiVersion: v1 kind: ReplicationController 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: kubeguide/guestbook-redis-slave env: - name: GET_HOSTS_FROM value: env ports: - containerPort: 6379
4、创建redis-slave Service文件
vim redis-slave-service.yaml
apiVersion: v1 kind: Service metadata: name: redis-slave labels: name: redis-slave spec: ports: - port: 6379 selector: name: redis-slave
5、创建frontend RC
vim 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: kubeguide/guestbook-php-frontend env: - name: GET_HOSTS_FROM value: env ports: - containerPort: 80
6、创建frontend Service
vim frontend-service.yaml
apiVersion: v1 kind: Service metadata: name: frontend labels: name: frontend spec: type: NodePort ports: - port: 80 nodePort: 30001 selector: name: frontend
7、通过浏览器访问frontend页面