Kubernetes5-集群上搭建基於redis和docker的留言薄


一、簡介

  1、環境依舊是kubernetes之前文章的架構

  2、需要docker的鏡像

    1)php-forntend web 前端鏡像

      docker.io-kubeguide-guestbook-php-frontend.tar

    2)redis master

      docker.io-kubeguide-redis-master.tar

    3)redis slave

      docker.io-kubeguide-guestbook-redis-slave.tar

    web前端通過javascript redis api 和redis master交互,可以在阿里上或自行下載

 

  3、架構示意圖

  4、底層網絡架構圖

  5、補充kubernetes系統架構圖

 

 二、開始部署

  1、上傳導入鏡像

    先下載相關鏡像

      互聯網自行下載,或者直接docker pull

    docker load導入

[root@node1 k8s]# docker load -i docker.io-kubeguide-redis-master.tar               #---導入redis-master
[root@node1 k8s]# docker load -i docker.io-kubeguide-guestbook-php-frontend.tar         #---導入guestbook-php-frontend
[root@node1 k8s]# docker load -i docker.io-kubeguide-guestbook-redis-slave.tar          #---導入guestbook-redis-slave

 

  2、創建redis master deployment配置文件

      回顧:deployment作用,是描述想要的目標狀態是什么

    1)創建vim redis-master-deployment.yaml 


 [root@master k8s]# cd /etc/kubernetes/yaml/

[root@master k8s]# vim redis-master-deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis-master
  # these labels can be applied automatically 
  # from the labels in the pod template if not set              #---如果沒有設置,這些標簽可以自動應用於POD模板中的標簽
  # labels:                                           
  #   app: redis
  #   role: master
  #   tier: backend
spec:
  # this replicas value is default
  # modify it according to your case
  replicas: 1                                    #---此副本值,根據情況修改
  # selector can be applied automatically 
  # from the labels in the pod template if not set              #---如果沒有設置
  # selector:
  #   matchLabels:
  #     app: guestbook
  #     role: master
  #     tier: backend
  template:                                     #---模板,如果沒有設置,選擇自動使用從此模板中默認的標簽
    metadata:
      labels:
        app: redis
        role: master
        tier: backend
    spec:
      containers:
      - name: master
        image: docker.io/kubeguide/redis-master:latest           #---自行修改,鏡像地址
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379
                                                     

    2)啟動這個deployment

[root@master yaml]# kubectl create -f /etc/kubernetes/yaml/redis-master-deployment.yaml                 
deployment "redis-master" created
[root@master yaml]# kubectl get deployment                                              
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx          2         2         2            2           21h
redis-master   1         1         1            1           4s
[root@master yaml]# kubectl get deployment 
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx          2         2         2            2           21h
redis-master   1         1         1            1           6s

  3、創建redis master service配置文件

    1)編輯redis-master-service.yaml

[root@master yaml]# vim /etc/kubernetes/yaml/redis-master-service.yaml 

apiVersion: v1
kind: Service
metadata:                                       #---metadata.name定義了service的服務名,spec.selector確定了pod對應到本服務  
  name: redis-master
  labels:                                        #---這里的定義表面擁有redis-master標簽的pod屬於redis-master服務
    app: redis
    role: master
    tier: backend
spec:
  ports:
    # the port that this service should serve on
  - port: 6379                                    #---ports部分中targetport屬性用來確定提供該服務的容器所暴露(expose)的端口號,port定義server的虛擬端口
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend

    2)啟動這個service

[root@master yaml]# kubectl create -f /etc/kubernetes/yaml/redis-master-service.yaml 
service "redis-master" created
[root@master yaml]# kubectl get svc
NAME           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes     10.254.0.1     <none>        443/TCP        14d
nginx          10.254.8.125   <nodes>       80:31011/TCP   9d
redis-master   10.254.16.93   <none>        6379/TCP 6s
[root@master yaml]# 

  4、創建redis slave deployment配置文件

    1)創建redis-salve-deployment

[root@master yaml]# vim redis-slave-deployment.yaml 

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis-slave
  # these labels can be applied automatically
  # from the labels in the pod template if not set
  # labels:
  #   app: redis
  #   role: slave
  #   tier: backend
spec:
  # this replicas value is default
  # modify it according to your case
  replicas: 2
  # selector can be applied automatically
  # from the labels in the pod template if not set
  # selector:
  #   matchLabels:
  #     app: guestbook
  #     role: slave
  #     tier: backend
  template:
    metadata:
      labels:
        app: redis
        role: slave
        tier: backend
    spec:
      containers:
      - name: slave
        image: docker.io/kubeguide/guestbook-redis-slave:latest
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: env
          # If your cluster config does not include a dns service, then to
          # instead access an environment variable to find the master
          # service's host, comment out the 'value: dns' line above, and
          # uncomment the line below.
          # value: env
        ports:
        - containerPort: 6379

    2)啟動redis-slave-deployment

[root@master yaml]# kubectl create -f /etc/kubernetes/yaml/redis-slave-deployment.yaml 
deployment "redis-slave" created
[root@master yaml]# kubectl get deploy                                   #---查看deployment
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx          2         2         2            2           22h
redis-master   1         1         1            1           11m
redis-slave    2         2         2            2           7s
[root@master yaml]# kubectl get pods                                    #---查看pods
NAME                            READY     STATUS    RESTARTS   AGE
nginx-2187705812-n5wsr          1/1       Running   1          22h
nginx-2187705812-zngv0          1/1       Running   1          22h
redis-master-3671804942-vjx86   1/1       Running   0          30m
redis-slave-2377017994-9nk3l    1/1       Running   0          19m
redis-slave-2377017994-vfd4b    1/1       Running   0          19m
[root@master yaml]# kubectl get pods -o wide                                #---查看pod 詳細信息,可以看到一個master兩個slave
NAME                            READY     STATUS    RESTARTS   AGE       IP             NODE
nginx-2187705812-n5wsr          1/1       Running   1          22h       10.255.101.2   node2
nginx-2187705812-zngv0          1/1       Running   1          22h       10.255.58.2    node1
redis-master-3671804942-vjx86   1/1       Running   0          31m       10.255.58.3    node1
redis-slave-2377017994-9nk3l    1/1       Running   0          19m       10.255.101.4   node2
redis-slave-2377017994-vfd4b    1/1       Running   0          19m       10.255.58.4 node1
[root@master yaml]# 

 

 

  5、創建slave service 配置文件

    1)創建redis-slave-service.yaml 

[root@master yaml]# vim /etc/kubernetes/yaml/redis-slave-service.yaml 

apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    app: redis
    role: slave
    tier: backend
spec:
  ports:
    # the port that this service should serve on
  - port: 6379
  selector:
    app: redis
    role: slave
    tier: backend
                                                 

    2)啟動這service

[root@master yaml]# kubectl create -f /etc/kubernetes/yaml/redis-slave-service.yaml 
service "redis-slave" created
[root@master yaml]# kubectl get svc -o wide
NAME           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE       SELECTOR
kubernetes     10.254.0.1      <none>        443/TCP        14d       <none>
nginx          10.254.8.125    <nodes>       80:31011/TCP   9d        name=nginx
redis-master   10.254.16.93    <none>        6379/TCP       32m       app=redis,role=master,tier=backend
redis-slave    10.254.22.156   <none>        6379/TCP       10s       app=redis,role=slave,tier=backend
[root@master yaml]# 

 

  6、創建fronteng guestbook deployment配置文件

    這個是一個簡單的PHP服務,用來和master service (寫請求),slave service(讀請求)交互

    1)創建編輯frontend-deployment.yaml 

[root@master yaml]# vim frontend-deployment.yaml 

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: frontend
  # these labels can be applied automatically
  # from the labels in the pod template if not set
  # labels:
  #   app: guestbook
  #   tier: frontend
spec:
  # this replicas value is default
  # modify it according to your case
  replicas: 3
  # selector can be applied automatically
  # from the labels in the pod template if not set
  # selector:
  #   matchLabels:
  #     app: guestbook
  #     tier: frontend
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: docker.io/kubeguide/guestbook-php-frontend:latest
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: env
          # If your cluster config does not include a dns service, then to
          # instead access environment variables to find service host
          # info, comment out the 'value: dns' line above, and uncomment the
          # line below.
          # value: env
        ports:
        - containerPort: 80

    2)啟動這個deployment

      可以看到三個frontend,一個master,兩個slave

[root@master yaml]# kubectl create -f frontend-deployment.yaml 
deployment "frontend" created
[root@master yaml]# kubectl get deploy
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
frontend       3         3         3            3           10s
nginx          2         2         2            2           22h
redis-master   1         1         1            1           48m
redis-slave    2         2         2            2 37m
[root@master yaml]# kubectl get pods -o wide          
NAME                            READY     STATUS    RESTARTS   AGE       IP             NODE
frontend-1186687533-0g08q       1/1       Running   0          24s       10.255.58.5    node1
frontend-1186687533-fxf29       1/1       Running   0          24s       10.255.101.5   node2
frontend-1186687533-q38fx       1/1       Running   0          24s       10.255.101.6 node2
nginx-2187705812-n5wsr          1/1       Running   1          22h       10.255.101.2   node2
nginx-2187705812-zngv0          1/1       Running   1          22h       10.255.58.2    node1
redis-master-3671804942-vjx86   1/1       Running   0          49m       10.255.58.3    node1
redis-slave-2377017994-9nk3l    1/1       Running   0          37m       10.255.101.4   node2
redis-slave-2377017994-vfd4b    1/1       Running   0          37m       10.255.58.4 node1
[root@master yaml]# 

  7、創建frontend guestbook service 配置文件

    1)創建frontend-service.yaml 

[root@master yaml]# vim frontend-service.yaml 

apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # if your cluster supports it, uncomment the following to automatically create
  # an external load-balanced IP for the frontend service.
  # type: LoadBalancer
  type: NodePort
  ports:
    # the port that this service should serve on
  - port: 80
    nodePort: 30001
  selector:
    app: guestbook
    tier: frontend

    2)啟動這個service

[root@master yaml]# kubectl create -f /etc/kubernetes/yaml/frontend-service.yaml 
service "frontend" created
[root@master yaml]# kubectl get svc -o wide
NAME           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE       SELECTOR
frontend 10.254.129.194   <nodes>       80:30001/TCP  4m        app=guestbook,tier=frontend
kubernetes     10.254.0.1       <none>        443/TCP        14d       <none>
nginx          10.254.8.125     <nodes>       80:31011/TCP   9d        name=nginx
redis-master   10.254.16.93     <none>        6379/TCP       52m       app=redis,role=master,tier=backend
redis-slave    10.254.22.156    <none>        6379/TCP       20m       app=redis,role=slave,tier=backend
[root@master yaml]# 

    3)node1 查看端口

[root@node1 k8s]# netstat -antup |grep 30001
tcp6       0      0 :::30001                :::*                    LISTEN      1110/kube-proxy     

三、驗證

  1、訪問web端

    http://192.168.216.53:30001/

 

參考:互聯網內容

總結流程:准備deployment和service配置文件》導入鏡像》啟動deployment和service

kubernetes系列文章請見:

部署:請見Kubernetes2-K8s的集群部署

kubectl:請見Kubernetes3-kubectl管理Kubernetes容器平台-1 

kubectl:請見Kubernetes3-kubectl管理Kubernetes容器平台-2  

Kubernetes Dashboard web界面:請見Kubernetes4-web管理界面

 

轉載請注明出處:https://www.cnblogs.com/zhangxingeng/p/11847953.html

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM