k8s官方案例练习-使用 Redis 部署 PHP 留言板应用程序

[[email protected] guestbook]# cat redis-master-deployment.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: redis-master
  labels:
    app: redis
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: 192.168.9.14/public/redis  # or just image: redis
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379
[[email protected] guestbook]# kubectl apply -f redis-master-deployment.yaml
deployment.apps/redis-master created

查看

[[email protected] guestbook]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
kuard-5cd647675b-65cwg          1/1     Running   0          2d1h
kuard-5cd647675b-65r9c          1/1     Running   0          2d1h
kuard-5cd647675b-f9r9f          1/1     Running   0          2d1h
nginx                           1/1     Running   0          47h
nginx-78b75497b7-rpt8t          1/1     Running   0          24h
redis-master-79c5f44c84-5wq2n   1/1     Running   0          39s

运行以下命令查看 Redis 主节点 Pod 中的日志

[[email protected] guestbook]# kubectl logs -f redis-master-79c5f44c84-5wq2n
1:C 22 Jan 2020 08:20:24.108 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 22 Jan 2020 08:20:24.108 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 22 Jan 2020 08:20:24.108 # 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 22 Jan 2020 08:20:24.110 * Running mode=standalone, port=6379.
1:M 22 Jan 2020 08:20:24.110 # 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:M 22 Jan 2020 08:20:24.110 # Server initialized
1:M 22 Jan 2020 08:20:24.110 # 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 22 Jan 2020 08:20:24.111 * Ready to accept connections

创建 Redis 主节点的服务

[[email protected] guestbook]# ls
redis-master-deployment.yaml  redis-master-service.yaml
[[email protected] guestbook]# cat redis-master-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
    role: master
    tier: backend
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend

创建 Redis 主节点的服务

[[email protected] guestbook]# kubectl apply -f redis-master-service.yaml
service/redis-master created
[[email protected] guestbook]# kubectl get service
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kuard          ClusterIP   10.110.133.234   <none>        80/TCP     2d2h
kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP    2d2h
redis-master   ClusterIP   10.102.97.125    <none>        6379/TCP   9s

启动 Redis 从节点

[[email protected] guestbook]# ls
redis-master-deployment.yaml  redis-master-service.yaml  redis-slave-deployment.yaml
[[email protected] guestbook]# cat redis-slave-deployment.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: redis-slave
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: slave
      tier: backend
  replicas: 2
  template:
    metadata:
      labels:
        app: redis
        role: slave
        tier: backend
    spec:
      containers:
      - name: slave
        image: registry.cn-hangzhou.aliyuncs.com/lina/gb-redisslave:v1
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # Using `GET_HOSTS_FROM=dns` requires your cluster to
          # provide a dns service. As of Kubernetes 1.3, DNS is a built-in
          # service launched automatically. However, if the cluster you are using
          # does not have a built-in DNS service, you can instead
          # access an environment variable to find the master
          # service‘s host. To do so, comment out the ‘value: dns‘ line above, and
          # uncomment the line below:
          # value: env
        ports:
        - containerPort: 6379
[[email protected] guestbook]# kubectl apply -f redis-slave-deployment.yaml
deployment.apps/redis-slave created
[[email protected] guestbook]# kubectl get pods
NAME                            READY   STATUS         RESTARTS   AGE
kuard-5cd647675b-65cwg          1/1     Running        0          2d2h
kuard-5cd647675b-65r9c          1/1     Running        0          2d2h
kuard-5cd647675b-f9r9f          1/1     Running        0          2d2h
nginx                           1/1     Running        0          47h
nginx-78b75497b7-rpt8t          1/1     Running        0          24h
redis-master-79c5f44c84-5wq2n   1/1     Running        0          20m
redis-slave-784476f99-hngj8     0/1     ErrImagePull   0          6s
redis-slave-784476f99-pvl7p     0/1     ErrImagePull   0          6s

创建 Redis 从节点的 Service

[[email protected] guestbook]# ls
redis-master-deployment.yaml  redis-master-service.yaml  redis-slave-deployment.yaml  redis-slave-service.yaml
[[email protected] guestbook]# cat redis-slave-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    app: redis
    role: slave
    tier: backend
spec:
  ports:
  - port: 6379
  selector:
    app: redis
    role: slave
    tier: backend
[[email protected] guestbook]# kubectl apply -f redis-slave-service.yaml
service/redis-slave created
[[email protected] guestbook]# kubectl get services
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kuard          ClusterIP   10.110.133.234   <none>        80/TCP     2d2h
kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP    2d2h
redis-master   ClusterIP   10.102.97.125    <none>        6379/TCP   22m
redis-slave    ClusterIP   10.110.55.92     <none>        6379/TCP   5s
[[email protected] guestbook]# kubectl get services -o wide
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE    SELECTOR
kuard          ClusterIP   10.110.133.234   <none>        80/TCP     2d2h   app=kuard
kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP    2d2h   <none>
redis-master   ClusterIP   10.102.97.125    <none>        6379/TCP   22m    app=redis,role=master,tier=backend
redis-slave    ClusterIP   10.110.55.92     <none>        6379/TCP   11s    app=redis,role=slave,tier=backend

设置并公开留言板前端

[[email protected] guestbook]# cat frontend-deployment.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: frontend
  labels:
    app: guestbook
spec:
  selector:
    matchLabels:
      app: guestbook
      tier: frontend
  replicas: 3
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: registry.cn-hangzhou.aliyuncs.com/paul-xiong/gb-frontend:v4
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # Using `GET_HOSTS_FROM=dns` requires your cluster to
          # provide a dns service. As of Kubernetes 1.3, DNS is a built-in
          # service launched automatically. However, if the cluster you are using
          # does not have a built-in DNS service, you can instead
          # access an environment variable to find the master
          # service‘s host. To do so, comment out the ‘value: dns‘ line above, and
          # uncomment the line below:
          # value: env
        ports:
        - containerPort: 80

查看运行

[[email protected] guestbook]# kubectl apply -f frontend-deployment.yaml
deployment.apps/frontend created

[[email protected] guestbook]# kubectl get pods -l app=guestbook -l tier=frontend
NAME                        READY   STATUS              RESTARTS   AGE
frontend-696c6dcdb4-4jdkd   0/1     ContainerCreating   0          34s
frontend-696c6dcdb4-glzgb   0/1     ContainerCreating   0          34s
frontend-696c6dcdb4-gsn7x   0/1     ContainerCreating   0          34s

创建前端服务

[[email protected] guestbook]# cat frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# comment or delete the following line if you want to use a LoadBalancer
type: NodePort
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
# type: LoadBalancer
ports:
- port: 80
selector:
app: guestbook
tier: frontend

[[email protected] guestbook]# vi frontend-service.yaml
[[email protected] guestbook]# vi frontend-service.yaml
[[email protected] guestbook]#  kubectl apply -f frontend-service.yaml
service/frontend created
[[email protected] guestbook]#   kubectl get services
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
frontend       NodePort    10.111.192.148   <none>        80:30351/TCP   6s
kuard          ClusterIP   10.110.133.234   <none>        80/TCP         2d2h
kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP        2d2h
redis-master   ClusterIP   10.102.97.125    <none>        6379/TCP       56m
redis-slave    ClusterIP   10.110.55.92     <none>        6379/TCP       34m

通过 NodePort 查看前端服务

[[email protected] guestbook]# kubectl get service frontend
NAME       TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
frontend   NodePort   10.111.192.148   <none>        80:30351/TCP   99s

扩展 Web 前端

[[email protected] guestbook]# kubectl scale deployment frontend --replicas=5
deployment.apps/frontend scaled
[[email protected] guestbook]#  kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
frontend-696c6dcdb4-8lpls       1/1     Running   0          2m15s
frontend-696c6dcdb4-djf5w       1/1     Running   0          6s
frontend-696c6dcdb4-gnn2r       1/1     Running   0          2m15s
frontend-696c6dcdb4-rmzw7       1/1     Running   0          2m15s
frontend-696c6dcdb4-s7gmb       1/1     Running   0          6s
[[email protected] guestbook]# kubectl scale deployment frontend --replicas=2
deployment.apps/frontend scaled
[[email protected] guestbook]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
frontend-696c6dcdb4-gnn2r       1/1     Running   0          2m54s
frontend-696c6dcdb4-rmzw7       1/1     Running   0          2m54s

清理:

kubectl delete deployment -l app=redis
  kubectl delete service -l app=redis
  kubectl delete deployment -l app=guestbook
  kubectl delete service -l app=guestbook

原文地址:https://www.cnblogs.com/jackluo/p/12229207.html

时间: 2024-10-29 17:28:42

k8s官方案例练习-使用 Redis 部署 PHP 留言板应用程序的相关文章

k8s官方案例练习-使用 ConfigMap 来配置 Redis

使用 ConfigMap 来配置 Redis [[email protected] ~]# mkdir kustomization [[email protected] ~]# cd kustomization/ [[email protected] kustomization]# vi kustomization.yaml [[email protected] kustomization]# kubectl version Client Version: version.Info{Major:

k8s官方案例练习-公开外部 IP 地址以访问集群中应用程序

先准备镜像: [[email protected] node]# ls Dockerfile server.js [[email protected] node]# cat Dockerfile FROM node:6.14.2 EXPOSE 8080 COPY server.js . CMD node server.js [[email protected] node]# cat server.js var http = require('http'); var handleRequest =

Exchange 2016部署实施案例篇-03.Exchange部署篇(中)

上一章<Exchange 2016部署实施案例篇-03.Exchange部署篇(上)>我们对部署Exchange Server 2016的先决条件做了简单的讲解,接下来我们进入先决条件准备工作. 先简单说下环境: 服务器名称 IP地址 系统 作用 ADSrv01 192.168.1.10 Win2016 GC(已部署完成) ADSrv02 192.168.1.20 Win2016 GC(已部署完成) EXSrv01 192.168.1.50 Win2016 MailBox(未安装) EXSrv

Unity 官方案例 Roll a ball

终于找到免费科学上网的方法了,虽然 速度一般,但对于学习unity官方视频来说还行. 废话不多的说,这次学习官方案例 Roll a ball,就是控制小球在平台上吃食物,这么一个简单的游戏.对于unity的一些基本操作,我就不在博客中介绍了,所以还请各位朋友先学会unity的基本操作,几乎只需要知道unity的面板有几块,分别操作什么,怎么创建物体,脚本,以及给物体添加组件,就可以学习本博客了. 好,现在开始.先创建一个3D项目,名字就叫做 Roll a ball, 在Assert 目录下创建几

redis部署手册

一.redis介绍 1.redis简介 REmote DIctionary Server(Redis)是一个基于key-value键值对的持久化数据库存储系统.redis和memcached缓存服务很像,但是redis支持的数据存储类型更丰富,包括string(字符串).list(链表).set(集合)和zset(有序集合)等: 这些数据类型都支持push/pop.add/remove及取交集.并集和差集及更丰富的操作,而且这些操作都是原子性的:在此基础上,redis支持各种不同方式的排序,与m

mutiprocessing 同步类型,如锁,条件和队列官方案例:

官方文档:https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing 1. 同步类型,如锁,条件和队列官方案例: # # A test file for the `multiprocessing` package # # Copyright (c) 2006-2008, R Oudkerk # All rights reserved. # import time, sys, random from Q

Docker(三)——应用部署(MySQL部署,Nginx部署,Redis部署)

一.MySQL部署 1.拉取mysql5.7镜像 docker pull mysql:5.7 2.创建容器,启动mysql5.7 (建议别用最新版本的mysql) docker run -id --name=mysql1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 -p  代表端口映射,格式为    宿主机映射端口:容器运行端口 -e  代表添加环境变量  MYSQL_ROOT_PASSWORD 是root用户的登录密码 设置好之后,

基于Bluemix快速构建部署一款Java小程序——微博影响力分析器

一.前言 近年来IT界风云际会,各种新概念如大数据.云平台.虚拟化等,如雨后春笋般层出不穷.而云平台,可以说是近些年来引领时代新潮的边缘概念之一,各大厂商竞相推出各种云产品,抢占云市场高地.近期,IT百年老厂IBM也推出了其花费重金打造的基于CloudFoundry开放标准的云平台Bluemix.本文就旨在从一位普通Java开发者角度,尝鲜Bluemix云平台及其提供的Cloudant服务,快速构建并部署一款Java小程序"微博影响力分析器". 二."微博影响力分析器&quo

如何使用Docker部署一个Go Web应用程序

熟悉Docker如何提升你在构建.测试并部署Go Web应用程序的方式,并且理解如何使用Semaphore来持续部署. 简介 大多数情况下Go应用程序被编译成单个二进制文件,web应用程序则会包括模版和配置文件.而当一个项目中有很多文件的时候,由于很多文件没有同步就会导致错误的发生并且产生很多的问题. 在本教程中,你将学习如何使用Docker部署一个Go web应用程序,并且认识到Docker将如何改进你的开发工作流及部署流程.各种规模的团队都会从这里所介绍的设置中受益. 目标 在本文结束后,你