使用Redis部署 PHP 留言板应用
- 启动 Redis 领导者(Leader)
- 启动两个 Redis 跟随者(Follower)
- 公开并查看前端服务
- 清理
启动 Redis 数据库
创建 Redis Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: leader
tier: backend
spec:
containers:
- name: leader
image: "docker.io/redis:6.0.5"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
查看日志:
controlplane $ kubectl logs -f deployment/redis-leader
1:C 25 Oct 2023 07:40:52.913 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 25 Oct 2023 07:40:52.913 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 25 Oct 2023 07:40:52.913 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 25 Oct 2023 07:40:52.915 * Running mode=standalone, port=6379.
1:M 25 Oct 2023 07:40:52.915 # Server initialized
1:M 25 Oct 2023 07:40:52.915 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 25 Oct 2023 07:40:52.915 * Ready to accept connections
创建 Redis 领导者服务
apiVersion: v1
kind: Service
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: leader
tier: backend
查看服务:
controlplane $ kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d17h <none>
redis-leader ClusterIP 10.111.244.137 <none> 6379/TCP 2m6s app=redis,role=leader,tier=backend
设置 Redis 跟随者
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
replicas: 2
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: follower
tier: backend
spec:
containers:
- name: follower
image: gcr.io/google_samples/gb-redis-follower:v2
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
查看Pod:
controlplane $ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-follower-5bdd6fffcb-5tt8q 1/1 Running 0 29s
redis-follower-5bdd6fffcb-klr45 1/1 Running 0 29s
redis-leader-6cc46676d8-8rdsj 1/1 Running 0 7m14s
创建 Redis 跟随者服务
Guestbook 应用需要与 Redis 跟随者通信以读取数据。
apiVersion: v1
kind: Service
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
ports:
# 此服务应使用的端口
- port: 6379
selector:
app: redis
role: follower
tier: backend
设置并公开留言板前端
现在你有了一个为 Guestbook 应用配置的 Redis 存储处于运行状态, 接下来可以启动 Guestbook 的 Web 服务器了。 与 Redis 跟随者类似,前端也是使用 Kubernetes Deployment 来部署的。
Guestbook 应用使用 PHP 前端。该前端被配置成与后端的 Redis 跟随者或者领导者服务通信,具体选择哪个服务取决于请求是读操作还是写操作。 前端对外暴露一个 JSON 接口,并提供基于 jQuery-Ajax 的用户体验。
创建 Guestbook 前端 Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
app: guestbook
tier: frontend
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v5
env:
- name: GET_HOSTS_FROM
value: "dns"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 80
创建前端服务
应用的
Redis
服务只能在 Kubernetes 集群中访问,因为服务的默认类型是 ClusterIP。ClusterIP
为服务指向的 Pod 集提供一个 IP 地址。这个 IP 地址只能在集群中访问。
如果你希望访客能够访问你的 Guestbook,你必须将前端服务配置为外部可见的, 以便客户端可以从 Kubernetes 集群之外请求服务。 然而即便使用了
ClusterIP
,Kubernetes 用户仍可以通过kubectl port-forward
访问服务。
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
ports:
# 此服务应使用的端口
- port: 80
selector:
app: guestbook
tier: frontend
通过 kubectl port-forward
查看前端服务
#端口转发
controlplane $ kubectl port-forward svc/frontend 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080
本地访问:
controlplane $ curl http://localhost:8080
<html ng-app="redis">
<head>
<title>Guestbook</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="controllers.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.6/ui-bootstrap-tpls.js"></script>
</head>
<body ng-controller="RedisCtrl">
<div style="width: 50%; margin-left: 20px">
<h2>Guestbook</h2>
<form>
<fieldset>
<input ng-model="msg" placeholder="Messages" class="form-control" type="text" name="input"><br>
<button type="button" class="btn btn-primary" ng-click="controller.onRedis()">Submit</button>
</fieldset>
</form>
<div>
<div ng-repeat="msg in messages track by $index">
{{msg}}
</div>
</div>
</div>
</body>
</html>