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

1、簡介

  一、環境依舊是kubernetes以前文章的架構

  二、須要docker的鏡像

    1)php-forntend web 前端鏡像

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

    2)redis master

      docker.io-kubeguide-redis-master.tarphp

    3)redis slave

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

    web前端經過javascript redis api 和redis master交互,能夠在阿里上或自行下載html

 

  三、架構示意圖

  四、底層網絡架構圖

  五、補充kubernetes系統架構圖

 

 2、開始部署

  一、上傳導入鏡像

    先下載相關鏡像前端

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

    docker load導入node

[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

 

  二、建立redis master deployment配置文件

      回顧:deployment做用,是描述想要的目標狀態是什麼nginx

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


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

[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

  三、建立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]# 

  四、建立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]# 

 

 

  五、建立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]# 

 

  六、建立fronteng guestbook deployment配置文件

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

    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,兩個slavedocker

[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]# 

  七、建立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     

3、驗證

  一、訪問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管理界面

 

轉載請註明出處:http://www.javashuo.com/article/p-fokhlxsl-e.html

相關文章
相關標籤/搜索