Kubernetes示例Guestbook
Guestbook的文档,根据kubernetes的不同版本而不同,具体的版本信息:[url]releases.k8s.io[/url].kubernetes发现Service有2种方式:环境变量,DNS[b]环境变量[/b]服务可用被之前的容器发现。[b]DNS[/b]使用cluster's DNS service, 所有的pod基于Service name自动解析到serv...
·
Guestbook的文档,根据kubernetes的不同版本而不同,具体的版本信息:[url]releases.k8s.io[/url].
kubernetes发现Service有2种方式:环境变量,DNS
[b]环境变量[/b]
服务可用被之前的容器发现。
[b]DNS[/b]
使用cluster's DNS service, 所有的pod基于Service name自动解析到service。这种方式,需要安装cluster DNS
本例使用环境变量env的方式,无需安装cluster DNS。因此在创建之前,需要修改examples/guestbook/redis-slave-deployment.yaml 和 examples/guestbook/frontend-deployment.yaml中变量GET_HOSTS_FROM 的值,由dns变更为env
[size=x-large]1 准备工作[/size]
需要准备一套kubernetes的运行环境,因此我们通过kubectl来检查集群的情况,如果没安装,请参考之前的帖子
[img]http://dl2.iteye.com/upload/attachment/0119/8640/2e73c28f-cb07-365b-890c-dd7accb14ab3.png[/img]
[size=x-large]2 创建redis master[/size]
本例中的redis master并不是高可用的
[size=large]2.1 定义Deployment[/size]
定义文件redis-master-deployment.yaml:
[img]http://dl2.iteye.com/upload/attachment/0119/8644/ee144bf7-20ac-3950-831c-0cfd112c7d21.png[/img]
[size=large]2.2 定义Service[/size]
定义文件redis-master-service.yaml:
[img]http://dl2.iteye.com/upload/attachment/0119/8648/1b873a6a-44d7-37a1-b99d-a3562f9daba8.png[/img]
[size=large]2.3 创建Service[/size]
# kubectl create -f redis-master-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8659/339ebdf3-d57d-3c80-b6a0-e0ebcfb44f5d.png[/img]
[size=large]2.4 查看Service[/size]
# kubectl get service
[img]http://dl2.iteye.com/upload/attachment/0119/8662/42aa21dc-5b0f-3c23-b3a4-3f9b0b09558b.png[/img]
[size=large]2.5 创建Deployment[/size]
#kubectl create -f redis-master-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8668/b7437dad-3205-3c13-b6ee-abf290327f0f.png[/img]
[size=large]2.6 查看Deployment[/size]
# kubectl get deployments
[img]http://dl2.iteye.com/upload/attachment/0119/8670/3d9707fc-a98c-3dcd-baf3-1d03fe9cbc9c.png[/img]
# kubectl get pods
[img]http://dl2.iteye.com/upload/attachment/0119/8672/cc4bc9f2-18b5-3ae1-8f4e-069a13d8ff7a.png[/img]
[size=x-large]3 创建redis slave[/size]
定义两个slave pod
[size=large]3.1 定义Deployment[/size]
[img]http://dl2.iteye.com/upload/attachment/0119/8674/c6fea0c8-2dd1-3ffe-870f-082ea585ade1.png[/img]
由于没安装cluster DNS,先修改GET_HOSTS_FROM为env
[img]http://dl2.iteye.com/upload/attachment/0119/8676/7321e7fa-c96a-383e-9215-5f4eac484e46.png[/img]
[size=large]3.2 定义Service[/size]
定义文件redis-slave-service.yaml:
[img]http://dl2.iteye.com/upload/attachment/0119/8678/c6587a95-b74d-389f-bef5-0f0a9c0e8724.png[/img]
[size=large]3.3 创建Service[/size]
# kubectl create -f redis-slave-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8681/9a9b271d-bfbd-3411-8b4c-c0fe8bd400e4.png[/img]
[size=large]3.4 创建Deployment[/size]
# kubectl create -f redis-slave-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8683/731a028d-e98f-37ee-89dd-6df90f7fe71d.png[/img]
[size=large]3.5 构建image[/size]
# docker build -t gcr.io/google_samples/gb-redisslave:v1 .
[img]http://dl2.iteye.com/upload/attachment/0119/8685/d32dd882-af58-36bf-b584-79e84f7adfaf.png[/img]
[size=large]3.6 验证[/size]
# kubectl get services
# kubectl get deployments
# kubectl get pods
[img]http://dl2.iteye.com/upload/attachment/0119/8687/d49abbe5-8639-3df5-8d43-eebd57d95ba2.png[/img]
[size=x-large]4 创建frontend[/size]
[size=large]4.1 定义Deployment[/size]
定义文件frontend-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8694/d7d96f2e-df78-3c3c-b4ba-18e30dc814e9.png[/img]
[size=large]4.2 定义Service[/size]
定义文件frontend-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8696/372d2b91-c759-3f72-b011-b43002688fa1.png[/img]
[size=large]4.3 创建Service[/size]
# kubectl create -f frontend-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8698/a0dad68f-9c8c-3f25-8259-ca273818db6d.png[/img]
[size=large]4.4 创建Deployment[/size]
# kubectl create -f frontend-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8700/c38d4268-579a-35ab-8af6-d55e2189352f.png[/img]
[size=large]4.5 构建image[/size]
[img]http://dl2.iteye.com/upload/attachment/0119/8702/0ffaeff1-68fb-3787-bda9-e4bc17271d32.png[/img]
# docker build -t gcr.io/google-samples/gb-frontend:v4 .
[size=large]4.6 验证[/size]
# kubectl get services
# kubectl get deployments
# kubectl get pods -L tier
查找label为tier的所有pod
[img]http://dl2.iteye.com/upload/attachment/0119/8704/afe73793-6d18-36af-889c-41fb747b020c.png[/img]
php的代码:
[img]http://dl2.iteye.com/upload/attachment/0119/8706/ec821f2c-f998-3d9b-aff7-530162a73467.png[/img]
[size=x-large]5 外部访问guestbook[/size]
kubernetes支持2种方式暴露一个服务到外部:NodePort和LoadBalancer
NodePort:
修改front-service.yaml, 设置type为NodePort,这样kubernetes会为其随机分配一个端口(默认:30000-32767)
如果希望指定nodePort的端口号,而不是系统自动分配。设置spec.ports[*].nodePort的值为自定义的数字,当然这样需要自己保障不要和其他端口冲突
[img]http://dl2.iteye.com/upload/attachment/0119/8708/18e6dd9e-364a-3f1b-99b8-d6f55da90d26.png[/img]
重启frontend的service
[img]http://dl2.iteye.com/upload/attachment/0119/8710/98b4d112-463c-331b-b977-1b70cd91552b.png[/img]
重新创建Service成功以后,我们再次查看所有的Service,发现frontend的EXTERNAL-IP更新为nodes
[img]http://dl2.iteye.com/upload/attachment/0119/8712/b059cd6b-4e18-3c60-9f49-21628ab6a977.png[/img]
在外部用浏览器访问的效果
[img]http://dl2.iteye.com/upload/attachment/0119/8714/4a66211e-bc17-370f-a6f5-b10945322f13.png[/img]
[size=x-large]6 清理[/size]
使用label标签,快速查找相关的资源,并且删除
# kubectl delete deployments,services -l "app in (redis, guestbook)"
[img]http://dl2.iteye.com/upload/attachment/0119/8716/d7c4985a-da86-34e9-bb2c-32486a998a96.png[/img]
参考:
[url]https://github.com/kubernetes/kubernetes/tree/master/examples/guestbook#guestbook-example[/url]
kubernetes发现Service有2种方式:环境变量,DNS
[b]环境变量[/b]
服务可用被之前的容器发现。
[b]DNS[/b]
使用cluster's DNS service, 所有的pod基于Service name自动解析到service。这种方式,需要安装cluster DNS
本例使用环境变量env的方式,无需安装cluster DNS。因此在创建之前,需要修改examples/guestbook/redis-slave-deployment.yaml 和 examples/guestbook/frontend-deployment.yaml中变量GET_HOSTS_FROM 的值,由dns变更为env
[size=x-large]1 准备工作[/size]
需要准备一套kubernetes的运行环境,因此我们通过kubectl来检查集群的情况,如果没安装,请参考之前的帖子
# kubectl cluster-info
[img]http://dl2.iteye.com/upload/attachment/0119/8640/2e73c28f-cb07-365b-890c-dd7accb14ab3.png[/img]
[size=x-large]2 创建redis master[/size]
本例中的redis master并不是高可用的
[size=large]2.1 定义Deployment[/size]
定义文件redis-master-deployment.yaml:
[img]http://dl2.iteye.com/upload/attachment/0119/8644/ee144bf7-20ac-3950-831c-0cfd112c7d21.png[/img]
[size=large]2.2 定义Service[/size]
定义文件redis-master-service.yaml:
[img]http://dl2.iteye.com/upload/attachment/0119/8648/1b873a6a-44d7-37a1-b99d-a3562f9daba8.png[/img]
[size=large]2.3 创建Service[/size]
# kubectl create -f redis-master-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8659/339ebdf3-d57d-3c80-b6a0-e0ebcfb44f5d.png[/img]
[size=large]2.4 查看Service[/size]
# kubectl get service
[img]http://dl2.iteye.com/upload/attachment/0119/8662/42aa21dc-5b0f-3c23-b3a4-3f9b0b09558b.png[/img]
[size=large]2.5 创建Deployment[/size]
#kubectl create -f redis-master-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8668/b7437dad-3205-3c13-b6ee-abf290327f0f.png[/img]
[size=large]2.6 查看Deployment[/size]
# kubectl get deployments
[img]http://dl2.iteye.com/upload/attachment/0119/8670/3d9707fc-a98c-3dcd-baf3-1d03fe9cbc9c.png[/img]
# kubectl get pods
[img]http://dl2.iteye.com/upload/attachment/0119/8672/cc4bc9f2-18b5-3ae1-8f4e-069a13d8ff7a.png[/img]
[size=x-large]3 创建redis slave[/size]
定义两个slave pod
[size=large]3.1 定义Deployment[/size]
[img]http://dl2.iteye.com/upload/attachment/0119/8674/c6fea0c8-2dd1-3ffe-870f-082ea585ade1.png[/img]
由于没安装cluster DNS,先修改GET_HOSTS_FROM为env
[img]http://dl2.iteye.com/upload/attachment/0119/8676/7321e7fa-c96a-383e-9215-5f4eac484e46.png[/img]
[size=large]3.2 定义Service[/size]
定义文件redis-slave-service.yaml:
[img]http://dl2.iteye.com/upload/attachment/0119/8678/c6587a95-b74d-389f-bef5-0f0a9c0e8724.png[/img]
[size=large]3.3 创建Service[/size]
# kubectl create -f redis-slave-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8681/9a9b271d-bfbd-3411-8b4c-c0fe8bd400e4.png[/img]
[size=large]3.4 创建Deployment[/size]
# kubectl create -f redis-slave-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8683/731a028d-e98f-37ee-89dd-6df90f7fe71d.png[/img]
[size=large]3.5 构建image[/size]
# docker build -t gcr.io/google_samples/gb-redisslave:v1 .
[img]http://dl2.iteye.com/upload/attachment/0119/8685/d32dd882-af58-36bf-b584-79e84f7adfaf.png[/img]
[size=large]3.6 验证[/size]
# kubectl get services
# kubectl get deployments
# kubectl get pods
[img]http://dl2.iteye.com/upload/attachment/0119/8687/d49abbe5-8639-3df5-8d43-eebd57d95ba2.png[/img]
[size=x-large]4 创建frontend[/size]
[size=large]4.1 定义Deployment[/size]
定义文件frontend-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8694/d7d96f2e-df78-3c3c-b4ba-18e30dc814e9.png[/img]
[size=large]4.2 定义Service[/size]
定义文件frontend-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8696/372d2b91-c759-3f72-b011-b43002688fa1.png[/img]
[size=large]4.3 创建Service[/size]
# kubectl create -f frontend-service.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8698/a0dad68f-9c8c-3f25-8259-ca273818db6d.png[/img]
[size=large]4.4 创建Deployment[/size]
# kubectl create -f frontend-deployment.yaml
[img]http://dl2.iteye.com/upload/attachment/0119/8700/c38d4268-579a-35ab-8af6-d55e2189352f.png[/img]
[size=large]4.5 构建image[/size]
[img]http://dl2.iteye.com/upload/attachment/0119/8702/0ffaeff1-68fb-3787-bda9-e4bc17271d32.png[/img]
# docker build -t gcr.io/google-samples/gb-frontend:v4 .
[size=large]4.6 验证[/size]
# kubectl get services
# kubectl get deployments
# kubectl get pods -L tier
查找label为tier的所有pod
[img]http://dl2.iteye.com/upload/attachment/0119/8704/afe73793-6d18-36af-889c-41fb747b020c.png[/img]
php的代码:
[img]http://dl2.iteye.com/upload/attachment/0119/8706/ec821f2c-f998-3d9b-aff7-530162a73467.png[/img]
[size=x-large]5 外部访问guestbook[/size]
kubernetes支持2种方式暴露一个服务到外部:NodePort和LoadBalancer
NodePort:
修改front-service.yaml, 设置type为NodePort,这样kubernetes会为其随机分配一个端口(默认:30000-32767)
如果希望指定nodePort的端口号,而不是系统自动分配。设置spec.ports[*].nodePort的值为自定义的数字,当然这样需要自己保障不要和其他端口冲突
[img]http://dl2.iteye.com/upload/attachment/0119/8708/18e6dd9e-364a-3f1b-99b8-d6f55da90d26.png[/img]
重启frontend的service
[img]http://dl2.iteye.com/upload/attachment/0119/8710/98b4d112-463c-331b-b977-1b70cd91552b.png[/img]
重新创建Service成功以后,我们再次查看所有的Service,发现frontend的EXTERNAL-IP更新为nodes
[img]http://dl2.iteye.com/upload/attachment/0119/8712/b059cd6b-4e18-3c60-9f49-21628ab6a977.png[/img]
在外部用浏览器访问的效果
[img]http://dl2.iteye.com/upload/attachment/0119/8714/4a66211e-bc17-370f-a6f5-b10945322f13.png[/img]
[size=x-large]6 清理[/size]
使用label标签,快速查找相关的资源,并且删除
# kubectl delete deployments,services -l "app in (redis, guestbook)"
[img]http://dl2.iteye.com/upload/attachment/0119/8716/d7c4985a-da86-34e9-bb2c-32486a998a96.png[/img]
参考:
[url]https://github.com/kubernetes/kubernetes/tree/master/examples/guestbook#guestbook-example[/url]
更多推荐
已为社区贡献1条内容
所有评论(0)