本教程介绍如何构建和部署使用Kubernetes和一个简单的,多层次的Web应用程序Guestbook。
实验目标 实验环境需要有一个Kubernetes集群,以及kubectl命令行工具必须配置与集群通信
参考:https://www.linuxidc.com/Linux/2018-03/151479.htm
在此可以检查k8s及相关工具版本:kubectl version
[root@aniu-k8s ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T10:09:24Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T09:42:01Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
下载实验用到的配置文件:
redis-master-deployment.yaml
redis-master-service.yaml
redis-slave-deployment.yaml
redis-slave-service.yaml
frontend-deployment.yaml
frontend-service.yaml
笔者在k8s集群master服务器上创建了/opt/k8s/guestbook目录,下载上面的文件到目录如下:
[root@aniu-k8s ~]# cd /opt/k8s/guestbook/
[root@aniu-k8s guestbook]# ll
total 24
-rw-r--r-- 1 root root 1086 Feb 6 16:28 frontend-deployment.yaml
-rw-r--r-- 1 root root 438 Feb 6 16:29 frontend-service.yaml
-rw-r--r-- 1 root root 561 Feb 6 17:01 redis-master-deployment.yaml
-rw-r--r-- 1 root root 233 Feb 6 17:14 redis-master-service.yaml
-rw-r--r-- 1 root root 1117 Feb 6 16:28 redis-slave-deployment.yaml
-rw-r--r-- 1 root root 209 Feb 6 16:28 redis-slave-service.yaml
留言簿应用程序使用Redis来存储其数据。它将其数据写入Redis主实例,并从多个Redis从实例中读取数据
创建Redis Master部署配置文件清单文件(如下所示)指定运行单个副本Redis master Pod的Deployment Controller。
在下载清单文件的目录中启动一个终端窗口
从redis-master-deployment.yaml文件应用Redis Master部署
[root@aniu-k8s guestbook]# kubectl apply -f redis-master-deployment.yaml Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply deployment "redis-master" configured
redis-master-deployment.yam
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: redis-master
spec:
selector:
matchLabels:
app: redis
role: master
tier: backend
replicas: 1
template:
metadata:
labels:
app: redis
role: master
tier: backend
spec:
containers:
- name: master
image: k8s.gcr.io/redis:e2e # or just image: redis
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
查询Pod的列表以验证Redis Master Pod正在运
[root@aniu-k8s guestbook]# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-585798d8ff-g69wc 1/1 Running 0 28m
运行以下命令查看Redis Master Pod中的日志:
[root@aniu-k8s guestbook]# kubectl logs -f redis-master-585798d8ff-g69wc
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.19 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[1] 06 Feb 09:14:33.096 # Server started, Redis version 2.8.19
[1] 06 Feb 09:14:33.097 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[1] 06 Feb 09:14:33.097 * The server is now ready to accept connections on port 6379
留言簿应用程序需要与Redis主站通信以写入其数据。您需要应用服务将流量代理到Redis主Pod。服务定义访问Pod的策略
应用以下redis-master-service.yaml文件中的Redis Master服务
[root@aniu-k8s guestbook]# kubectl apply -f redis-master-service.yaml
service "redis-master" created
redis-master-service.yaml