• 使用Redis部署 PHP 留言板应用


    使用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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    查看日志

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    创建 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    查看服务:

    controlplane $ kubectl get service -o wide
    NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE     SELECTOR
    kubernetes     ClusterIP   10.96.0.1                443/TCP    7d17h   
    redis-leader   ClusterIP   10.111.244.137           6379/TCP   2m6s    app=redis,role=leader,tier=backend
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    设置 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    查看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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    创建 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    设置并公开留言板前端

    现在你有了一个为 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    创建前端服务

    应用的 Redis 服务只能在 Kubernetes 集群中访问,因为服务的默认类型是 ClusterIPClusterIP 为服务指向的 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    通过 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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    本地访问:

    controlplane $ curl  http://localhost:8080
    "redis">
      
        Guestbook<<span class="token operator">/</span>title>
        <link rel=<span class="token string">"stylesheet"</span> href=<span class="token string">"//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"</span>>
        <script src=<span class="token string">"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"</span>><<span class="token operator">/</span>script>
        <script src=<span class="token string">"controllers.js"</span>><<span class="token operator">/</span>script>
        <script src=<span class="token string">"https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.6/ui-bootstrap-tpls.js"</span>><<span class="token operator">/</span>script>
      <<span class="token operator">/</span>head>
      <body ng-controller=<span class="token string">"RedisCtrl"</span>>
        <div style=<span class="token string">"width: 50%; margin-left: 20px"</span>>
          <h2>Guestbook<<span class="token operator">/</span>h2>
        <form>
        <fieldset>
        <input ng-model=<span class="token string">"msg"</span> placeholder=<span class="token string">"Messages"</span> <span class="token keyword">class</span>=<span class="token string">"form-control"</span> <span class="token function">type</span>=<span class="token string">"text"</span> name=<span class="token string">"input"</span>><br>
        <button <span class="token function">type</span>=<span class="token string">"button"</span> <span class="token keyword">class</span>=<span class="token string">"btn btn-primary"</span> ng-click=<span class="token string">"controller.onRedis()"</span>>Submit<<span class="token operator">/</span>button>
        <<span class="token operator">/</span>fieldset>
        <<span class="token operator">/</span>form>
        <div>
          <div ng-repeat=<span class="token string">"msg in messages track by <span class="token variable">$index</span>"</span>>
            <span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>msg<span class="token punctuation">}</span><span class="token punctuation">}</span>
          <<span class="token operator">/</span>div>
        <<span class="token operator">/</span>div>
        <<span class="token operator">/</span>div>
      <<span class="token operator">/</span>body>
    <<span class="token operator">/</span>html>
    <div class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://1000bd.com/contentImg/2022/06/27/191644837.png" alt="" title=""></span></div><ul class="pre-numbering" style="opacity: 0.161109;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li></ul></pre>
                    </div>
                        </div>
                    </li>
    
                    <li class="list-group-item ul-li">
    
                        <b>相关阅读:</b><br>
                        <nobr>
    <a href="/Article/Index/875329">ERP为化工企业的采购决策提供技术支持</a>                            <br />
    <a href="/Article/Index/804654">二叉堆------小根堆</a>                            <br />
    <a href="/Article/Index/802002">如何做网站推广-外链为皇</a>                            <br />
    <a href="/Article/Index/1092838">java毕业设计家庭理财记账系统(附源码、数据库)</a>                            <br />
    <a href="/Article/Index/739468">单例模式学习笔记</a>                            <br />
    <a href="/Article/Index/1435378">cmake 教程</a>                            <br />
    <a href="/Article/Index/1301751">linux命令</a>                            <br />
    <a href="/Article/Index/1056725">线程的学习5</a>                            <br />
    <a href="/Article/Index/1447075">第八章:枚举</a>                            <br />
    <a href="/Article/Index/1281864">压缩算法---以golang/snappy为例</a>                            <br />
                        </nobr>
                    </li>
                    <li class="list-group-item from-a mb-2">
                        原文地址:https://blog.csdn.net/weixin_51882166/article/details/134037204
                    </li>
    
                </ul>
            </div>
    
            <div class="col-lg-4 col-sm-12">
                <ul class="list-group" style="word-break:break-all;">
                    <li class="list-group-item ul-li-bg" aria-current="true">
                        最新文章
                    </li>
                    <li class="list-group-item ul-li">
                        <nobr>
    <a href="/Article/Index/1484446">攻防演习之三天拿下官网站群</a>                            <br />
    <a href="/Article/Index/1515268">数据安全治理学习——前期安全规划和安全管理体系建设</a>                            <br />
    <a href="/Article/Index/1759065">企业安全 | 企业内一次钓鱼演练准备过程</a>                            <br />
    <a href="/Article/Index/1485036">内网渗透测试 | Kerberos协议及其部分攻击手法</a>                            <br />
    <a href="/Article/Index/1877332">0day的产生 | 不懂代码的"代码审计"</a>                            <br />
    <a href="/Article/Index/1887576">安装scrcpy-client模块av模块异常,环境问题解决方案</a>                            <br />
    <a href="/Article/Index/1887578">leetcode hot100【LeetCode 279. 完全平方数】java实现</a>                            <br />
    <a href="/Article/Index/1887512">OpenWrt下安装Mosquitto</a>                            <br />
    <a href="/Article/Index/1887520">AnatoMask论文汇总</a>                            <br />
    <a href="/Article/Index/1887496">【AI日记】24.11.01 LangChain、openai api和github copilot</a>                            <br />
                        </nobr>
                    </li>
                </ul>
    
                <ul class="list-group pt-2" style="word-break:break-all;">
                    <li class="list-group-item ul-li-bg" aria-current="true">
                        热门文章
                    </li>
                    <li class="list-group-item ul-li">
                        <nobr>
    <a href="/Article/Index/888177">十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!</a>                            <br />
    <a href="/Article/Index/797680">奉劝各位学弟学妹们,该打造你的技术影响力了!</a>                            <br />
    <a href="/Article/Index/888183">五年了,我在 CSDN 的两个一百万。</a>                            <br />
    <a href="/Article/Index/888179">Java俄罗斯方块,老程序员花了一个周末,连接中学年代!</a>                            <br />
    <a href="/Article/Index/797730">面试官都震惊,你这网络基础可以啊!</a>                            <br />
    <a href="/Article/Index/797725">你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法</a>                            <br />
    <a href="/Article/Index/797702">心情不好的时候,用 Python 画棵樱花树送给自己吧</a>                            <br />
    <a href="/Article/Index/797709">通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!</a>                            <br />
    <a href="/Article/Index/797716">13 万字 C 语言从入门到精通保姆级教程2021 年版</a>                            <br />
    <a href="/Article/Index/888192">10行代码集2000张美女图,Python爬虫120例,再上征途</a>                            <br />
                        </nobr>
                    </li>
                </ul>
    
            </div>
        </div>
    </div>
    <!-- 主体 -->
    
    
        <!--body结束-->
        <!--这里是footer模板-->
        
        <!--footer-->
    <nav class="navbar navbar-inverse navbar-fixed-bottom">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="text-muted center foot-height">
                        Copyright © 2022 侵权请联系<a href="mailto:2656653265@qq.com">2656653265@qq.com</a>   
                        <a href="https://beian.miit.gov.cn/" target="_blank">京ICP备2022015340号-1</a>
                    </div>
                    <div style="width:300px;margin:0 auto; padding:0px 5px;">
                        <a href="/regex.html">正则表达式工具</a>
                        <a href="/cron.html">cron表达式工具</a>
                        <a href="/pwdcreator.html">密码生成工具</a>
                    </div>
                    <div style="width:300px;margin:0 auto; padding:5px 0;">
                        <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11010502049817" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;">
                        <img src="" style="float:left;" /><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">京公网安备 11010502049817号</p></a>
                    </div>
                </div>
            </div>
        </div>
      
    </nav>
    <!--footer-->
    
        <!--footer模板结束-->
    
        <script src="/js/plugins/jquery/jquery.js"></script>
        <script src="/js/bootstrap.min.js"></script>
    
        <!--这里是scripts模板-->
        
    
        
     
    
    
        <!--scripts模板结束-->
    
    </body>
    </html>