• kubernetes集群应用部署实例


    今天,我们将要带来入门hello world示例,它是一个web留言板应用,基于PHP+Redis的两层分布式架构的web应用,前端PHP web网站通过访问后端Redis数据库完成用户留言的查询和添加功能,具备读写分离能力,留言板内容是从redis中查询到的,首页中添加留言并提交后,留言会被添加到redis中。

    有三个前端节点:php-frontend,对网站的访问进行负载均衡

    有两个redis后端节点:一个redis-master和两个redis-slave,两个redis-slave从redis-master进行同步数据

    php-frontend进行了读写分离,即在写入的时候写入主库,而读取的时候从从库读取。

    客户通过客户端访问的时候,访问前端相应的地址即可。

     

    整体的架构是这个样子:

    1.创建redis-master Pod和服务

    先定义RC来创建pod,然后定义与之关联的service。

    为redis-master服务新建一个名为redis-master-controller.yaml的replicationcontroller定义文件,内容为:

    [root@uat-app01 etc]# mkdir /etc/k8s_yaml
    [root@uat-app01 k8s_yaml]# vim /etc/k8s_yaml/redis-master-controller.yaml
    apiVersion: v1    #指定api版本号
    kind: ReplicationController    #创建资源的类型:这里为ReplicationController
    metadata:    #资源元数据
      name: redis-master    #资源名称
      labels:     #资源标签
        name: redis-master    #标签名
    spec:     #容器的详细定义
      replicas: 1    #副本数量:这里为1
      selector:     #RC通过spec.selector来筛选要控制的Pod
        name: redis-master
      template:    # pod的定义
        metadata:  # pod元数据
          labels:    #pod标签
            name: redis-master
        spec:     #指定资源内容
          containers:    #容器
          - name: master    #容器名
            image: kubeguide/redis-master    #使用的镜像
            ports:     #容器开放对外的端口号:这里为6379
              - containerPort: 6379

    创建好文件后,执行如下命令:

    [root@uat-app01 k8s_yaml]# kubectl create -f /etc/k8s_yaml/redis-master-controller.yaml 
    replicationcontroller "redis-master" created

    查看刚才新建的RC信息:

    [root@uat-app01 k8s_yaml]# kubectl get rc
    NAME           DESIRED   CURRENT   READY     AGE
    redis-master   1         1         0         48s

    查看pods信息:

    [root@uat-app01 k8s_yaml]# kubectl get pods
    NAME                 READY     STATUS              RESTARTS   AGE
    redis-master-2qftt   0/1       ContainerCreating   0          1m

    查看pod redis-master-2qftt 信息:

    [root@uat-app01 k8s_yaml]# kubectl describe pod redis-master-2qftt
    Name:        redis-master-2qftt
    Namespace:    default
    Node:        uat-ucs02.insightcredit/192.168.1.47
    Start Time:    Wed, 20 Dec 2017 15:27:07 +0800
    Labels:        name=redis-master
    Status:        Pending
    IP:        
    Controllers:    ReplicationController/redis-master
    Containers:
      master:
        Container ID:        
        Image:            kubeguide/redis-master
        Image ID:            
        Port:            6379/TCP
        State:            Waiting
          Reason:            ContainerCreating
        Ready:            False
        Restart Count:        0
        Volume Mounts:        <none>
        Environment Variables:    <none>
    Conditions:
      Type        Status
      Initialized     True 
      Ready     False 
      PodScheduled     True 
    No volumes.
    QoS Class:    BestEffort
    Tolerations:    <none>
    Events:
      FirstSeen    LastSeen    Count    From                    SubObjectPath    Type        Reason        Message
      ---------    --------    -----    ----                    -------------    --------    ------        -------
      3m        3m        1    {default-scheduler }                    Normal        Scheduled    Successfully assigned redis-master-2qftt to uat-ucs02.insightcredit
      3m        18s        5    {kubelet uat-ucs02.insightcredit}            Warning        FailedSync    Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.  details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"
    
      2m    7s    10    {kubelet uat-ucs02.insightcredit}        Warning    FailedSync    Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image "registry.access.redhat.com/rhel7/pod-infrastructure:latest""
    
    

    这里看到是发生了异常的:

    在pull registry.access.redhat.com/rhel7/pod-infrastructure:latest 镜像时出现问题,问题原因是 /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt文件不存在

    尝试去pull这个镜像,发现缺失文件/etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt

    [root@uat-app01 k8s_yaml]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
    Trying to pull repository registry.access.redhat.com/rhel7/pod-infrastructure ... 
    open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory
    
    

    查看该缺失的文件,发现为软连接,缺少名叫rhsm的依赖,查找关于rhsm的依赖包

    经过查阅发现,该缺少的rhsm包为:python-rhsm

    使用yum安装

    [root@uat-ucs01 kubernetes]# yum install python-rhsm.x86_64 0:1.19.10-1.el7_4

    安装后,该文件存在,问题解决

    创建于redis-master pod相关联的service,文件内容如下:

    [root@uat-app01 k8s_yaml]# vi /etc/k8s_yaml/redis-master-service.yaml
    
    apiVersion: v1
    kind: Service
    metadata: 
      name: redis-master
      labels:
        name: redis-master
    spec:
      ports:
      - port: 6379    #服务监听的端口号
        targetPort: 6379    #需要转发到后端pod的端口号,就是容器对外开放的端口号
      selector:
        name: redis-master

    创建service:

    [root@uat-app01 k8s_yaml]# kubectl create -f /etc/k8s_yaml/redis-master-service.yaml 
    service "redis-master" created

    查看新建的service:

    [root@uat-app01 k8s_yaml]# kubectl get services
    NAME           CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
    kubernetes     10.254.0.1      <none>        443/TCP    5h
    redis-master   10.254.227.87   <none>        6379/TCP   1m

    2.创建redis-slave Pod和服务

    为redis-slave服务新建一个名为redis-slave-controller.yaml的replicationcontroller定义文件,内容为:

    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: redis-slave
      labels:
        name: redis-slave
    spec:
      replicas: 2
      selector:
        name: redis-slave
      template:
        metadata:
          labels:
            name: redis-slave
        spec: 
          containers:
          - name: slave
            image: kubeguide/guestbook-redis-slave
            env:
            - name: GET_HOSTS_FROM
              value: env
            ports:
            - containerPort: 6379

    创建好文件后,执行如下命令:

    [root@uat-app01 k8s_yaml]# kubectl create -f /etc/k8s_yaml/redis-slave-controller.yaml 
    replicationcontroller "redis-slave" created

    查看刚才新建的RC信息:

    [root@uat-app01 k8s_yaml]# kubectl get rc redis-slave
    NAME          DESIRED   CURRENT   READY     AGE
    redis-slave   2         2         0         39s

    配置文件redis-salve-service.yaml内容如下:

    [root@uat-app01 k8s_yaml]# vim /etc/k8s_yaml/redis-slave-service.yaml 
    
    apiVersion: v1
    kind: Service
    metadata:
      name: redis-slave
      labels:
        name: redis-slave
    spec:
      ports:
      - port: 6379
      selector:
        name: redis-slave

    创建service:

    [root@uat-app01 k8s_yaml]# kubectl create -f redis-slave-service.yaml
    service "redis-slave" created

    检查service:

    [root@uat-app01 k8s_yaml]# kubectl get service
    NAME           CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
    kubernetes     10.254.0.1      <none>        443/TCP    5h
    redis-master   10.254.227.87   <none>        6379/TCP   29m
    redis-slave    10.254.71.156   <none>        6379/TCP   35s

    3.创建fronted pod和服务

    创建frontend的rc

    [root@uat-app01 k8s_yaml]#  vi /etc/k8s_yaml/frontend-controller.yaml
    
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: frontend
      labels:
        name: frontend
    spec:
      replicas: 3
      selector:
        name: frontend
      template:
        metadata:
          labels:
            name: frontend
        spec:
          containers:
          - name: frontend
            image: kubeguide/guestbook-php-frontend
            env: 
            - name: GET_HOSTS_FROM
              value: env
            ports:
            - containerPort: 80

    创建rc

    [root@uat-app01 k8s_yaml]#  kubectl create -f /etc/k8s_yaml/frontend-controller.yaml
    replicationcontroller "frontend" created

    创建frontend的service,前端的service是需要外部访问的,所以进行如下配置

    [root@uat-app01 k8s_yaml]# vi /etc/k8s_yaml/frontend-service.yaml
    
    apiVersion: v1
    kind: Service
    metadata:
      name: frontend
      labels:
        name: frontend
    spec:
      type: NodePort    #外部访问端口形式为:通过node端口形式进行访问
      ports:
      - port: 80    #服务监听的端口号
        nodePort: 30001    #node上开放的外部端口
      selector:
        name: frontend
    [root@uat-app01 k8s_yaml]# kubectl create -f /etc/k8s_yaml/frontend-service.yaml
    service "frontend" created

    创建好以上rc、pod、service后查看pod情况

    [root@uat-app01 ~]# kubectl get pods
    NAME                 READY     STATUS    RESTARTS   AGE
    frontend-3bv0p       1/1       Running   0          15h
    frontend-kmth2       1/1       Running   0          15h
    frontend-vltkp       1/1       Running   0          15h
    redis-master-f59zp   1/1       Running   0          15h
    redis-slave-6rxl8    1/1       Running   0          15h
    redis-slave-j4jz0    1/1       Running   0          15h

    在浏览器中输入任意运行frontend的pod的ip地址加上我们定义好的node port 30001

    我这里是192.168.1.40:30001

    留言板出现了,快来提交我们的留言吧~

    hello world

    kubernetes的hello world实例到此完成啦。

    转载:http://www.louisvv.com/archives/1179.html

  • 相关阅读:
    django计数器: form collections import Counter
    issubclass使用
    Python rpartition() 方法
    try...except...else高级用法
    FBV及CBV区别
    类属性及对象属性
    判断类或对象方法
    django md5
    @cached_property
    js Array 中的 map, filter 和 reduce
  • 原文地址:https://www.cnblogs.com/linyouyi/p/10691060.html
Copyright © 2020-2023  润新知