• Kubernetes实践--hello world 示例


    本文所说的Hello world是一个web留言板应用,并且是基于PHP+Redis的两层分布式架构的web应用,前端PHP web网站通过访问后端Redis数据库完成用户留言的查询和添加功能,具备读写分离能力,留言板内容是从redis中查询到的,首页中添加刘潭并提交之后留言会被添加到redis中。

    Redis层采用一个Master与两个salve,Master负责写操作,slave负责读操作,PHPweb层启动三个实例组成集群,对网站的访问进行负载均衡。 
    本文的例子首选需要将Docker环境和k8s环境搭建完毕,这个在前一节中已经做了讲解,有需要的可以参考前一节内容部署一下Docker和k8s环境。 
    实验环境: Centos7.2 64位,虚拟机。 
    下面进行实例的具体操作。

    1. 创建redis-master Pod和服务

    先定义RC来创建pod,然后定义与之关联的service。 
    为redis-master服务新建一个名为redis-master-controller.yaml的RC定义文件,内容为:

    
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: redis-master
      labels:
        name: redis-master
    spec:
      replicas: 1
      selector:
        name: redis-master
      template:
        metadata:
         labels:
           name: redis-master
        spec:
          containers:
          - name: master
            image: kubeguide/redis-master
            ports:
            - containerPort: 6379

    新建好文件之后执行如下命令:

    
    kuberctl create -f redis-master-controller.yaml

    删除:将create换为delete即可。 
    查看刚刚新建的redis-master:

    
    kuberctl get rc

    检查pod信息:

    
    kuberctl get pods

    解决kubectl get pods时 No resources found.问题

    1. 使用yum直接安装etcd, kubectl,按顺序启动如下服务:etcd, docker, kube-apiserver, kube-controller-manager, kube-scheduler, kubelet, kube-proxy.
    2. 创建了mysql-rc.yaml.
    3. 执行kubectl create -f mysql-rc.yaml,反馈正常
    4. 执行kubectl get rc, 反馈正常
    5. 执行kubectl get pods,显示no resources found.
    6. 解决方法:
    7. 1、$ vi /etc/kubernetes/apiserver
    8. 2、找到这一行 "KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota",去掉ServiceAccount,保存退出。
    9. 3、重新启动kube-apiserver服务即可
    10. 此时执行kubectl get pods, no resources found问题得以解决

    下面新建与上面建的pod相关联的service,service对应的文件redis-master-service.yaml,内容如下:

    
    apiVersion: v1
    kind: Service
    metadata:
      name: redis-master
      labels:
        name: redis-master
    spec:
      ports:
      - port: 6379
        targetPort: 6379
      selector:
        name: redis-master

    创建service:

    
    kubectl create -f redis-master-service.yaml 

    查看新建的service:

    
    kubectl get services

    2. redis-salve Pod和服务

    步骤和redis-master的一样。 
    新建文件redis-salve-controller.yaml,内容如下:

    
    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

    运行kubectl create命令:

    
    kubectl create -f redis-salve-controller.yaml

    查看RC:

    
    kubectl get rc

    查看pod:

    
    kubectl get pods

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

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

    创建service:

    
    kubectl create -f redis-slave-service.yaml

    检查service:

    
    kubectl get services;

    3. 创建frontend Pod和服务

    文件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

    执行命令kubectl create:

    
    kubectl create -f frontend-controller.yaml

    检查RC:

    
    kunectl get rc

    检查pod:

    
    kubectl get pods

    文件frontend-service.yaml内容如下:

    
    apiVersion: v1
    kind: Service
    metadata:
      name: frontend
      labels:
        name: frontend
    spec:
      type: NodePort
      ports:
      - port: 80
        nodePort: 30001
      selector:
        name: frontend

    创建服务:

    
    kubectl create -f frontend-service.yaml

    检查service:

    
    kubectl get services;

    4. 访问网站

    如我的虚拟机IP为xx.xx.xx.xx
    则浏览器访问: xx.xx.xx.xx:30001 
    成功的话会出现简单的留言板界面,并可进行留言commit。

  • 相关阅读:
    0841. Keys and Rooms (M)
    0478. Generate Random Point in a Circle (M)
    0714. Best Time to Buy and Sell Stock with Transaction Fee (M)
    0535. Encode and Decode TinyURL (M)
    1721. Swapping Nodes in a Linked List (M)
    0823. Binary Trees With Factors (M)
    1461. Check If a String Contains All Binary Codes of Size K (M)
    0623. Add One Row to Tree (M)
    1332. Remove Palindromic Subsequences (E)
    0706. Design HashMap (E)
  • 原文地址:https://www.cnblogs.com/liyongsan/p/9116167.html
Copyright © 2020-2023  润新知