一看必會系列:k8s 練習2 使用k8s部署php&redis組成的留言系統

來源:本站原創 Kubernetes 超過561 views圍觀 0條評論

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

文件結構
[[email protected] 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

[[email protected] yaml]# vim /root/login.sh
[[email protected] 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啟動是否正常
[[email protected] 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行

[[email protected] 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信息
[[email protected] 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
[[email protected] yaml]#

 

查看frontend 運行在哪個node
[[email protected] 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>
[[email protected] yaml]#

驗證服務,使用剛查看的 node1及IP進行測試,能訪問,能提交數據進行測試即可
[[email protected] ~]# 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容器內
[[email protected] yaml]#  kubectl exec -it redis-master-rc-jrrgx /bin/bash
2.進入redis
[ [email protected]:/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>

 

———-報錯

[[email protected] 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’

解決進服務器
[[email protected] yaml]# kubectl exec -it frontend-rc-2h62f /bin/bash
[email protected]:/var/www/html#
[email protected]:/var/www/html#
[email protected]:/var/www/html# find / -name httpd.conf
[email protected]:/var/www/html#

不管他沒有影響,想解決加個Servername IP 就行了

文章出自:CCIE那點事 http://www.qdxgqk.live/ 版權所有。本站文章除注明出處外,皆為作者原創文章,可自由引用,但請注明來源。 禁止全文轉載。
本文鏈接:http://www.qdxgqk.live/?p=4112轉載請注明轉自CCIE那點事
如果喜歡:點此訂閱本站
  • 相關文章
  • 為您推薦
  • 各種觀點
?
?
萌宠夺宝游戏