This scenario explains how to launch a simple, multi-tier web application using Kubernetes and Docker. The Guestbook example application stores notes from guests in Redis via JavaScript API calls. Redis contains a master (for storage), and a replicated set of redis 'slaves'.

Core Concepts

The following core concepts will be covered during this scenario. These are the foundations of understanding Kubernetes.

  • Pods

  • Replication Controllers

  • Services

  • NodePorts

Step 1 - Start Kubernetes

To start we need a running Kubernetes cluster. The details of this are explained in the Launch Kubernetes cluster scenario.

Start a single-node cluster using the helper script. The helper script will launch the API, Master, a Proxy and DNS discovery. The Web App uses DNS Discovery to find the Redis slave to store data.

launch.sh

Health Check

Check everything is up using the following health Check:

kubectl cluster-info

kubectl get nodes

If the node returns NotReady then it is still waiting. Wait a couple of seconds before retrying.

Step 2 - Redis Master Controller

The first stage of launching the application is to start the Redis Master. A Kubernetes service deployment has, at least, two parts. A replication controller and a service.

The replication controller defines how many instances should be running, the Docker Image to use, and a name to identify the service. Additional options can be utilized for configuration and discovery. Use the editor above to view the YAML definition.

If Redis were to go down, the replication controller would restart it on an active node.

Create Replication Controller

In this example, the YAML defines a redis server called redis-master using the official redis running port 6379.

The kubectl create command takes a YAML definition and instructs the master to start the controller.

kubectl create -f redis-master-controller.yaml

What's running?

The above command created a Replication Controller. The Replication

kubectl get rc

All containers described as Pods. A pod is a collection of containers that makes up a particular application, for example Redis. You can view this using kubectl

kubectl get pods

Step 3 - Redis Master Service

The second part is a service. A Kubernetes service is a named load balancer that proxies traffic to one or more containers. The proxy works even if the containers are on different nodes.

Services proxy communicate within the cluster and rarely expose ports to an outside interface.

When you launch a service it looks like you cannot connect using curl or netcat unless you start it as part of Kubernetes. The recommended approach is to have a LoadBalancer service to handle external communications.

Create Service

The YAML defines the name of the replication controller, redis-master, and the ports which should be proxied.

kubectl create -f redis-master-service.yaml

List & Describe Services

kubectl get services

kubectl describe services redis-master

Step 4 - Replication Slave Pods

In this example we'll be running Redis Slaves which will have replicated data from the master. More details of Redis replication can be found at http://redis.io/topics/replication

As previously described, the controller defines how the service runs. In this example we need to determine how the service discovers the other pods. The YAML represents the GET_HOSTS_FROM property as DNS. You can change it to use Environment variables in the yaml but this introduces creation-order dependencies as the service needs to be running for the environment variable to be defined.

Start Redis Slave Controller

In this case, we'll be launching two instances of the pod using the image kubernetes/redis-slave:v2. It will link to redis-mastervia DNS.

kubectl create -f redis-slave-controller.yaml

List Replication Controllers

kubectl get rc

Step 5 - Redis Slave Service

As before we need to make our slaves accessible to incoming requests. This is done by starting a service which knows how to communicate with redis-slave.

Because we have two replicated pods the service will also provide load balancing between the two nodes.

Start Redis Slave Service

kubectl create -f redis-slave-service.yaml

kubectl get services

Step 6 - Frontend Replicated Pods

With the data services started we can now deploy the web application. The pattern of deploying a web application is the same as the pods we've deployed before.

Launch Frontend

The YAML defines a service called frontend that uses the image _gcr.io/googlesamples/gb-frontend:v3. The replication controller will ensure that three pods will always exist.

kubectl create -f frontend-controller.yaml

List controllers and pods

kubectl get rc

kubectl get pods

PHP Code

The PHP code uses HTTP and JSON to communicate with Redis. When setting a value requests go to redis-master while read data comes from the redis-slave nodes.

Step 7 - Guestbook Frontend Service

To make the frontend accessible we need to start a service to configure the proxy.

Start Proxy

The YAML defines the service as a NodePort. NodePort allows you to set well-known ports that are shared across your entire cluster. This is like -p 80:80 in Docker.

In this case, we define our web app is running on port 80 but we'll expose the service on 30080.

kubectl create -f frontend-service.yaml

kubectl get services

Step 8 - Access Guestbook Frontend

With all controllers and services defined Kubernetes will start launching them as Pods. A pod can have different states depending on what's happening. For example, if the Docker Image is still being downloaded then the Pod will have a pendingstate as it's not able to launch. Once ready the status will change to running.

View Pods Status

You can view the status using the following command:

kubectl get pods

Find NodePort

If you didn't assign a well-known NodePort then Kubernetes will assign an available port randomly. You can find the assigned NodePort using kubectl.

kubectl describe service frontend | grep NodePort

View UI

Once the Pod is in running state you will be able to view the UI via port 30080. Use the URL to view the pagehttps://2886795360-30080-ollie02.environments.katacoda.com

Under the covers the PHP service is discovering the Redis instances via DNS. You now have a working multi-tier application deployed on Kubernetes.

We'll discuss NodePort in future scenarios.

Kubernetes - Deploy Guestbook example on Kubernetes的更多相关文章

  1. centos7下kubernetes(5。部署kubernetes dashboard)

    基于WEB的dashboard,用户可以用kubernetes dashboard部署容器话的应用,监控应用的状态,执行故障排查任务以及管理kubernetes各种资源. 在kubernetes da ...

  2. 【Kubernetes学习之二】Kubernetes集群安装

    环境 centos 7 Kubernetes有三种安装方式:yum.二进制.kubeadm,这里演示kubeadm. 一.准备工作1.软件版本 软件 版本 kubernetes v1.15.3 Cen ...

  3. Kubernetes 实战 —— 02. 开始使用 Kubernetes 和 Docker

    创建.运行及共享容器镜像 P23 运行容器 P24 运行 P24 可以运行 Docker 客户端可执行文件来执行各种 Docker 命令.例如:可以试着从 Docker Hub 的公共镜像仓库拉取.运 ...

  4. kubernetes进阶(02)kubernetes的node

    一.Node概念 Node是Pod真正运行的主机,可以物理机,也可以是虚拟机. 为了管理Pod,每个Node节点上至少要运行container runtime(比如docker或者rkt). kube ...

  5. kubernetes入门(04)kubernetes的核心概念(1)

    一.ReplicationController/ReplicaSet 在Kubernetes集群中,ReplicationController能够确保在任意时刻,指定数量的Pod副本正在运行.如果Po ...

  6. 高可用Kubernetes集群-15. 部署Kubernetes集群统一日志管理

    参考文档: Github:https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/fluentd-elasticsear ...

  7. Kubernetes - Deploy Containers Using YAML

    In this scenario, you'll learn how to use Kubectl to create and launch Deployments, Replication Cont ...

  8. kubernetes入门(05)kubernetes的核心概念(2)

    一.使用 kubectl run 创建 pod(容器) 命令 kubectl run类似于 docker run,可以方便的创建一个容器(实际上创建的是一个由deployment来管理的Pod): 等 ...

  9. 高可用Kubernetes集群-14. 部署Kubernetes集群性能监控平台

    参考文档: Github介绍:https://github.com/kubernetes/heapster Github yaml文件: https://github.com/kubernetes/h ...

随机推荐

  1. JS数据结构学习之排序

    在看<>这本书中关于排序这一章的时候,我试着用javascript语言来重写里面几个经典的排序方法,包括冒泡排序.快速排序.选择排序.插入排序还有希尔排序. 一.冒泡排序 冒泡排序算是排序 ...

  2. USACO 1.4.2 Mother's Mil 母亲的牛奶(DFS)

    Description 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装 ...

  3. 软件工程第七周psp

    1.PSP表格 类别 任务 开始时间 结束时间 中断时间 delta时间 立会 汇报昨天的成绩,分配任务,部署计划 10月27日18:00 10月27日18:36 0 36分钟 准备工作 查阅有关资料 ...

  4. 记录 C++ STL 中 一些好用的函数--持续更新 (for_each,transform,count_if,find_if)

    在日常的编程中,有这么几种操作还是比较常见的: 把一组数据都赋值成一个数,在一组数据中查找一个数,统计一组数据中符合条件的数等等. 一般的写法可以用循环,没有什么是循环不能搞定的.假如在这里怎么用介绍 ...

  5. 让程序运行更加面向用户——电梯V2.1

    电梯V2.1 GitHub仓库地址 Problem 为程序添加命令行参数(自行利用搜索引擎进行学习). 写成 .cpp .h 文件分离的形式(大多数同学已经达到). 继续完善函数分离.模块化思想. 要 ...

  6. Java 将数字转为16进制,然后转为字符串类型

    public class ArrayTest3 { public static void main(String[] args){ System.out.println(toHex(60)); } / ...

  7. CSS中px和em属性的特点与区别

    详解px和em的特点和区别象素px是我们在定义CSS中经常用到的尺寸大小单位,而em在国外网站中经常被使用,px和em之间究竟有什么区别和特点呢?◆px像素(Pixel),相对长度单位.像素px是相对 ...

  8. 第22章 软件安装:源码与Tarball

    开放源码的软件安装与升级简介 什么是开放源码.编译程序与可执行文件 开放源码:程序代码,写给人类看的程序语言 编译程序:将源码编译成机器能看得懂的语言 可执行文件:经过编译变成二进制程序后机器看得懂可 ...

  9. 树莓派两用优盘制作(FAT32存储+EXT树莓派系统)

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:树莓派两用优盘制作(FAT32存储+EXT树莓派系统)     本文地址:http://tec ...

  10. 微信小程序组件 自定义单选

    <view class='userperson'> <view class='f30 flexca'>请选择您的注册身份</view> <view class ...