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

Posted jackluo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s官方案例练习-使用 Redis 部署 PHP 留言板应用程序相关的知识,希望对你有一定的参考价值。

[root@master01 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
[root@master01 guestbook]# kubectl apply -f redis-master-deployment.yaml
deployment.apps/redis-master created

查看

[root@master01 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 中的日志

[root@master01 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 主节点的服务

[root@master01 guestbook]# ls
redis-master-deployment.yaml  redis-master-service.yaml
[root@master01 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 主节点的服务

[root@master01 guestbook]# kubectl apply -f redis-master-service.yaml
service/redis-master created
[root@master01 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 从节点

[root@master01 guestbook]# ls
redis-master-deployment.yaml  redis-master-service.yaml  redis-slave-deployment.yaml
[root@master01 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
          # services host. To do so, comment out the value: dns line above, and
          # uncomment the line below:
          # value: env
        ports:
        - containerPort: 6379
[root@master01 guestbook]# kubectl apply -f redis-slave-deployment.yaml
deployment.apps/redis-slave created
[root@master01 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

[root@master01 guestbook]# ls
redis-master-deployment.yaml  redis-master-service.yaml  redis-slave-deployment.yaml  redis-slave-service.yaml
[root@master01 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
[root@master01 guestbook]# kubectl apply -f redis-slave-service.yaml
service/redis-slave created
[root@master01 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
[root@master01 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

设置并公开留言板前端

[root@master01 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
          # services host. To do so, comment out the value: dns line above, and
          # uncomment the line below:
          # value: env
        ports:
        - containerPort: 80

查看运行

[root@master01 guestbook]# kubectl apply -f frontend-deployment.yaml
deployment.apps/frontend created

[root@master01 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

创建前端服务

[root@master01 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


[root@master01 guestbook]# vi frontend-service.yaml [root@master01 guestbook]# vi frontend-service.yaml [root@master01 guestbook]# kubectl apply -f frontend-service.yaml service/frontend created [root@master01 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 查看前端服务

[root@master01 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 前端

[root@master01 guestbook]# kubectl scale deployment frontend --replicas=5
deployment.apps/frontend scaled
[root@master01 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
[root@master01 guestbook]# kubectl scale deployment frontend --replicas=2
deployment.apps/frontend scaled
[root@master01 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

以上是关于k8s官方案例练习-使用 Redis 部署 PHP 留言板应用程序的主要内容,如果未能解决你的问题,请参考以下文章

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

k8s+docker:部署留言板php+redis

使用docker部署多个本地redis

配置Redis主从服务器(有图)

在 k8s 中创建 redis 部署时退避重启失败的容器

在k8s中创建redis部署时,退回重启失败的容器