一看必会系列:k8s 练习2 使用k8s部署php&redis组成的留言系统

No Comments Kubernetes

k8s-master 192.168.10.68
k8s-node1  192.168.10.71

本文将演示使用kubernetes系统基于 kubeguide相关的镜像文件创建基于php和redis的留言板系统
容器功能实现

前端 frontend 是php  从后端读取数据并展示
后端是 redis (一主 2从)提供数据

在线下载相关的镜像文件,并纳入本地仓库统一管理

# docker pull kubeguide/redis-master
# docker pull kubeguide/guestbook-php-frontend
# docker pull kubeguide/guestbook-redis-slave
# docker tag kubeguide/redis-master reg.jdccie.com/redis-master
# docker tag kubeguide/guestbook-php-frontend reg.jdccie.com/guestbook-php-frontend
# docker tag kubeguide/guestbook-redis-slave reg.jdccie.com/guestbook-redis-slave

#要称登陆 registry仓库
docker login -u 用户 -p密码 reg.jdccie.wang
# docker push reg.jdccie.com/redis-master
# docker push reg.jdccie.com/guestbook-php-frontend
# docker push reg.jdccie.com/guestbook-redis-slave

文件结构
[root@k8s-master yaml]# tree redis_web/
redis_web/
├── frontend-rc.yaml
├── frontend-svc.yaml
├── redis-master-rc.yaml
├── redis-master-svc.yaml
├── redis-slave-rc.yaml
└── redis-slave-svc.yaml

[root@k8s-master yaml]# vim /root/login.sh
[root@k8s-master yaml]# cat redis_web/*

├── frontend-rc.yaml 配置

apiVersion: v1
kind: ReplicationController
metadata:
  name: frontend-rc
  labels:
    name: frontend-pod-lb
spec:
  replicas: 3
  selector:
    name: frontend-pod-lb
  template:
    metadata:
      labels:
        name: frontend-pod-lb
    spec:
     containers:
     – name: frontend-name
       image: reg.ccie.wang/test/guestbook-php-frontend:latest
       ports:
       – containerPort: 80
       env:
       – name: GET_HOSTS_FROM
         value: "env"
        
├── frontend-svc.yaml配置
apiVersion: v1                                                                             
kind: Service
metadata:                                                                                  
  name: frontend-svc                                                                              
  labels:
    name: frontend-pod-lb
spec:                                                                                      
  type: NodePort
  ports:
    – port: 80
      nodePort: 30011
  selector:
    name: frontend-pod-lb

├── redis-master-rc.yaml配置

apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-master-rc
  labels:
    names: redis-master-lb
spec:
  replicas: 1
  selector:
    name: redis-master-lb
  template:
    metadata:
      labels:
        name: redis-master-lb
    spec:
     containers:
     – name: master
       image: kubeguide/redis-master
       ports:
       – containerPort: 6379
      
├── redis-master-svc.yaml 配置
apiVersion: v1                                                                             
kind: Service
metadata:                                                                                  
  name: redis-master                                                                              
  labels:
    name: redis-master-lb
spec:                                                                                      
  ports:
    – port: 6379
      targetPort: 6379
  selector:
    name: redis-master-lb
   
├── redis-slave-rc.yaml 配置
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-slave-rc
  labels:
    names: redis-slave-lb
spec:
  replicas: 2
  selector:
    name: redis-slave-lb
  template:
    metadata:
      labels:
        name: redis-slave-lb
    spec:
     containers:
     – name: slave
       image: reg.ccie.wang/test/guestbook-redis-slave:latest
       env:
       – name: GET_HOSTS_FROM
         value: env
       ports:
       – containerPort: 6379
      
└── redis-slave-svc.yaml 配置

apiVersion: v1                                                                             
kind: Service
metadata:                                                                                  
  name: redis-slave                                                                              
  labels:
    name: redis-slave-lb
spec:                                                                                      
  ports:
    – port: 6379
  selector:
    name: redis-slave-lb

创建相应容器
redis-master
  940  kubectl create -f redis-master-rc.yaml
  941  kubectl create -f redis-master-svc.yaml
redis-slave
  948  kubectl create -f redis-slave-rc.yaml
  958  kubectl create -f redis-slave-svc.yaml
frontend
1011  kubectl create -f frontend-rc.yaml
1030  kubectl create -f frontend-svc.yaml

删除容器
kubectl delete -f 对应配置
记得验证 kubectl get rc,pod,svc

验证rc,pod,svc启动是否正常
[root@k8s-master yaml]# kubectl get rc,pod,svc

#DESIRED/CURRENT/READY 三者数量一至并与配置文件一样即为正常

------中间广告---------

NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/frontend-rc       3         3         3       81m
replicationcontroller/redis-master-rc   1         1         1       17h
replicationcontroller/redis-slave-rc    2         2         2       17h

#所有 Running 即正常

NAME                        READY   STATUS    RESTARTS   AGE
pod/frontend-rc-2h62f       1/1     Running   0          81m
pod/frontend-rc-5dwk2       1/1     Running   0          81m
pod/frontend-rc-dmxp8       1/1     Running   0          81m
pod/redis-master-rc-jrrgx   1/1     Running   0          17h
pod/redis-slave-rc-f9svq    1/1     Running   0          6h12m
pod/redis-slave-rc-p6kbq    1/1     Running   0          17h

#确认type port是否与配置一致
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/frontend-svc   NodePort    10.100.151.156   <none>        80:30011/TCP   81m
service/redis-master   ClusterIP   10.100.215.236   <none>        6379/TCP       17h
service/redis-slave    ClusterIP   10.100.178.103   <none>        6379/TCP       17h

查看redis-master是同步是否正常
grep -A 5 "关键字" #显示关键字后的5行

[root@k8s-master yaml]# kubectl exec redis-master-rc-jrrgx redis-cli info |grep -A 5 "Replication"
# Replication
role:master
connected_slaves:2   #说明有两个slave且正常
slave0:ip=10.244.1.108,port=6379,state=online,offset=82615,lag=0
slave1:ip=10.244.1.110,port=6379,state=online,offset=82615,lag=0
master_repl_offset:82615

查看slave的环境变量是否拿到 MASTER信息
[root@k8s-master yaml]# kubectl exec redis-slave-rc-f9svq env |grep MASTER
REDIS_MASTER_PORT_6379_TCP=tcp://10.100.215.236:6379
REDIS_MASTER_PORT_6379_TCP_ADDR=10.100.215.236
REDIS_MASTER_PORT=tcp://10.100.215.236:6379
REDIS_MASTER_SERVICE_PORT=6379
REDIS_MASTER_PORT_6379_TCP_PROTO=tcp
REDIS_MASTER_SERVICE_HOST=10.100.215.236
REDIS_MASTER_PORT_6379_TCP_PORT=6379
[root@k8s-master yaml]#

 

查看frontend 运行在哪个node
[root@k8s-master yaml]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
frontend-rc-px989       1/1     Running   0          145m    10.244.1.114   k8s-node1   <none>           <none>
frontend-rc-q5mk4       1/1     Running   0          145m    10.244.1.115   k8s-node1   <none>           <none>
frontend-rc-qh466       1/1     Running   0          145m    10.244.1.116   k8s-node1   <none>           <none>
redis-master-rc-jrrgx   1/1     Running   0          16h     10.244.1.107   k8s-node1   <none>           <none>
redis-slave-rc-f9svq    1/1     Running   0          4h44m   10.244.1.110   k8s-node1   <none>           <none>
redis-slave-rc-p6kbq    1/1     Running   0          16h     10.244.1.108   k8s-node1   <none>           <none>
[root@k8s-master yaml]#

验证服务,使用刚查看的 node1及IP进行测试,能访问,能提交数据进行测试即可
[root@k8s-node1 ~]# curl 192.168.10.69:30011
<html ng-app="redis">
  <head>
    <title>Guestbook</title>
    <link rel="stylesheet" href="bootstrap.min.css">
    <script src="angular.min.js"></script>
    <script src="controllers.js"></script>
    <script src="ui-bootstrap-tpls.js"></script>
  </head>

最后可以进redis进行数据验证和 http界面上是否一致

1.进入redis-master容器内
[root@k8s-master yaml]#  kubectl exec -it redis-master-rc-jrrgx /bin/bash
2.进入redis
[ root@redis-master-rc-jrrgx:/data ]$ redis-cli    
3 查看所有keys
127.0.0.1:6379> keys *
1) "messages"
4. 获取key  "messages" 对应的值
127.0.0.1:6379> get messages
"Hello World!,hhh,gegrgr"
127.0.0.1:6379> get messages
"Hello World!,hhh,gegrgr,frr"  #增加为正常
127.0.0.1:6379>

 

———-报错

[root@k8s-master yaml]# kubectl logs frontend-rc-2h62f
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 10.244.1.118. Set the ‘ServerName’ directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 10.244.1.118. Set the ‘ServerName’ directive globally to suppress this message
[Tue Mar 26 08:34:03.926249 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.12 configured — resuming normal operations
[Tue Mar 26 08:34:03.926296 2019] [core:notice] [pid 1] AH00094: Command line: ‘apache2 -D FOREGROUND’

解决进服务器
[root@k8s-master yaml]# kubectl exec -it frontend-rc-2h62f /bin/bash
root@frontend-rc-2h62f:/var/www/html#
root@frontend-rc-2h62f:/var/www/html#
root@frontend-rc-2h62f:/var/www/html# find / -name httpd.conf
root@frontend-rc-2h62f:/var/www/html#

不管他没有影响,想解决加个Servername IP 就行了

发表评论