使用Redis部署 PHP 留言板应用

使用Redis部署 PHP 留言板应用

  • 启动 Redis 领导者(Leader)
  • 启动两个 Redis 跟随者(Follower)
  • 公开并查看前端服务
  • 清理

启动 Redis 数据库

创建 Redis Deployment

yaml 复制代码
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

查看日志:

powershell 复制代码
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 领导者服务

yaml 复制代码
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

查看服务:

powershell 复制代码
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 跟随者

yaml 复制代码
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:

powershell 复制代码
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 跟随者通信以读取数据。

yaml 复制代码
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

yaml 复制代码
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 集群中访问,因为服务的默认类型是 ClusterIPClusterIP 为服务指向的 Pod 集提供一个 IP 地址。这个 IP 地址只能在集群中访问。
如果你希望访客能够访问你的 Guestbook,你必须将前端服务配置为外部可见的, 以便客户端可以从 Kubernetes 集群之外请求服务。 然而即便使用了 ClusterIP,Kubernetes 用户仍可以通过 kubectl port-forward 访问服务。

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  ports:
    # 此服务应使用的端口
  - port: 80
  selector:
    app: guestbook
    tier: frontend

通过 kubectl port-forward 查看前端服务

powershell 复制代码
#端口转发
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

本地访问:

powershell 复制代码
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>
相关推荐
罗政38 分钟前
PDF 批量合并工具:本地 AI 自动排序、识别正文日期与合同编号
数据库·pdf·php
SelectDB1 小时前
宽表元数据膨胀怎么解?Doris Segment V3 对比 Parquet、Lance
大数据·数据库·数据分析
爱网络爱Linux1 小时前
Linux proc 目录安全加固
linux·运维·rhce·rhca·红帽认证·proc 目录安全加固
曹牧1 小时前
Oracle:快速定位最新数据
数据库·oracle
MindUp1 小时前
企业网盘权限模型解析:多层级访问控制与审计能力选型指南
java·linux·运维
Zhu7582 小时前
在Docker环境部署ApacheGuacamole,对接PG数据库
数据库·docker·容器
持力行2 小时前
D-BUS会话总线
运维·网络
kdxiaojie2 小时前
Linux 驱动研究 —— I2C (5)
linux·运维·笔记·学习
Leighteen2 小时前
Redis与MySQL双写一致性:从问题根源到工程落地
数据库·redis·mysql
独自归家的兔2 小时前
2026年7月2日 Docker 容器化部署 PaddleOCR-VL 实战(OpenAI 协议兼容 HTTP 调用)
运维·docker·容器