在Kubernetes集群中使用Redis部署PHP留言簿应用程序(3)

[root@aniu-k8s guestbook]# kubectl get pods
NAME                            READY    STATUS              RESTARTS  AGE
redis-master-585798d8ff-g69wc  1/1      Running            0          3m
redis-slave-865486c9df-tjvfn    0/1      ContainerCreating  0          27s
redis-slave-865486c9df-x76gb    1/1      Running

创建Redis Slave服务

留言簿应用程序需要与Redis Slave进行通信以读取数据。为了使Redis Slave可以被发现,需要建立一个服务。服务为一组Pod提供透明的负载平衡。

应用以下redis-slave-service.yaml文件中的Redis从服务

[root@aniu-k8s guestbook]# kubectl apply -f redis-slave-service.yaml
service "redis-slave" created

redis-slave-service.yam

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

查询服务列表以验证Redis Slave服务是否正在运行

[root@aniu-k8s guestbook]# kubectl get services
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP  PORT(S)    AGE
kubernetes    ClusterIP  10.96.0.1        <none>        443/TCP    6d
redis-master  ClusterIP  10.109.193.22    <none>        6379/TCP  2m
redis-slave    ClusterIP  10.101.252.227  <none>        6379/TCP  21s

设置和公开留言簿前端

留言簿应用程序有一个Web前端,用于使用PHP编写的HTTP请求。它被配置为连接到redis-master服务的写请求和redis-slave服务的读请求

创建留言簿前端部署

从以下frontend-deployment.yaml文件应用前端部署:

[root@aniu-k8s guestbook]# kubectl apply -f frontend-deployment.yaml
deployment "frontend" created

frontend-deployment.yam

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: frontend
spec:
  selector:
    matchLabels:
      app: guestbook
      tier: frontend
  replicas: 3
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google-samples/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
          # 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

查询Pod的列表以验证三个前端副本正在运行:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a1568808b388a24ca27f73cde970bf87.html