k8s入门系列之guestbook快速部署
k8s集群以及一些扩展插件已经安装完毕,本篇文章介绍一下如何在k8s集群上快速部署guestbook应用。
•实验环境为集群:master(1)+node(4),详细内容参考《k8s入门系列之集群安装篇》
•guestbook应用示例介绍:
应用包含2部分:web前端和redis(1主2从),用户通过web前端提交数据,写入到redis-master上,然后通过读取同步到redis-slave上的数据展示给用户。
提示:所有的定义文件都存放在master端,所有的操作也都是在master端操作。
实验:
1.配置启动redis-master服务
•编辑redis-master-rc.yml定义文件
[root@master guestbook]# cat redis-master-rc.yml
kind: ReplicationController 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: docker.gaoxiaobang.com/kubernetes/redis ports: - containerPort: 6379
•创建Pod
[root@master guestbook]# kubectl create -f redis-master-rc.yml replicationcontroller "redis-master" created
•查看rc(ReplicationController)
[root@master guestbook]# kubectl get rc -l name=redis-master NAME DESIRED CURRENT AGE redis-master 1 1 29s
•查看Pod
[root@master guestbook]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE NODE
redis-master-7tt3s 1/1 Running 0 15m 192.168.30.21
Kubernetes中Service中起到了负载均衡器的作用,通过Proxy和Selector决定服务请求传递给后端提供服务的Pod,对外提供固定的IP,这样的话Redis Master Pod迁移变化也不会影响。
•编辑redis-master-svc.yml定义文件
[root@master guestbook]# cat redis-master-svc.yml
apiVersion: v1 kind: Service metadata: name: redis-master labels: name: redis-master spec: ports: - port: 6379 targetPort: 6379 selector: name: redis-master
•创建Service
[root@master guestbook]# kubectl create -f redis-master-svc.yml
•查看Service
[root@master guestbook]# kubectl get svc redis-master NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis-master 10.254.59.45 <none> 6379/TCP 3m
2.配置启动redis-slave服务
•编辑redis-slave-rc.yml定义文件
[root@master guestbook]# cat redis-slave-rc.yml
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: worker image: docker.gaoxiaobang.com/kubernetes/redis-slave ports: - containerPort: 6379
•创建Pod
[root@master guestbook]# kubectl create -f redis-slave-rc.yml replicationcontroller "redis-slave" created
•查看rc
[root@master guestbook]# kubectl get rc redis-slave NAME DESIRED CURRENT AGE redis-slave 2 2 1m
•查看Pod
[root@master guestbook]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE NODE redis-master-7tt3s 1/1 Running 0 15m 192.168.30.21 redis-slave-drxok 1/1 Running 0 15m 192.168.30.23 redis-slave-h43yo 1/1 Running 0 15m 192.168.30.21
•编辑redis-slave-svc.yml定义文件
[root@master guestbook]# cat redis-slave-svc.yml
apiVersion: v1 kind: Service metadata: name: redis-slave labels: name: redis-slave spec: ports: - port: 6379 targetPort: 6379 selector: name: redis-slave
•创建Service
[root@master guestbook]# kubectl create -f redis-slave-svc.yml service "redis-slave" created
•查看Service
[root@master guestbook]# kubectl get svc redis-slave NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis-slave 10.254.172.53 <none> 6379/TCP 19m
3.配置启动web端服务
•编辑guestbook-web-rc.yml定义文件
[root@master guestbook]# cat guestbook-web-rc.yml
apiVersion: v1 kind: ReplicationController metadata: name: guestbook-web labels: name: guestbook-web spec: replicas: 2 selector: name: guestbook-web template: metadata: labels: name: guestbook-web spec: containers: - name: php-redis image: docker.gaoxiaobang.com/kubernetes/guestbook-web ports: - containerPort: 80
•创建Pod
[root@master guestbook]# kubectl create -f guestbook-web-rc.yml replicationcontroller "guestbook-web" created
•查看rc
[root@master guestbook]# kubectl get rc guestbook-web NAME DESIRED CURRENT AGE guestbook-web 2 2 18m
•查看Pod
[root@master guestbook]# kubectl get pods NAME READY STATUS RESTARTS AGE guestbook-web-1un8o 1/1 Running 0 34m guestbook-web-iyxpg 1/1 Running 0 34m gxb-web-rc-afi56 1/1 Running 0 10d redis-master-7tt3s 1/1 Running 0 38m redis-slave-drxok 1/1 Running 0 37m redis-slave-h43yo 1/1 Running 0 37m
•编辑guestbook-web-svc.yml定义文件
[root@master guestbook]# cat guestbook-web-svc.yml
apiVersion: v1 kind: Service metadata: name: guestbook-web labels: name: guestbook-web spec: type: NodePort ports: - port: 80 selector: name: guestbook-web
•创建Service
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:520) to serve traffic. See http://releases.k8s.io/release-1.2/docs/user-guide/services-firewalls.md for more details. service "guestbook-web" created
*提示:在集群所有node节点上都开启了520端口,访问任何一个node的520端口都可以。例如:192.168.30.21:520
•查看Service
[root@master guestbook]# kubectl get svc guestbook-web NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE guestbook-web 10.254.225.216 nodes 80/TCP 34m
4.访问guestbook,如下图
参考文章:
http://dockone.io/article/542