在k8s上运行实例Guestbook

解决拉取不到谷歌k8s.gcr.io仓库镜像的问题
  之前,已经搭建好k8s集群,为方便使用在ali CES服务器搭建了k8s solo.此次,在
k8s上运行实例Guestbook.
  • 创建rc&service文件

    • redis-master-controller.yaml

      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
      
    • 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
      • redis-slave-controller.yaml
      apiVersion: v1
      kind: ReplicationController
      metadata:
        name: redis-slave
      spec:
        replicas: 2
        selector:
          name: redis-slave
        template:
          metadata:
            name: redis-slave
            labels:
              name: redis-slave
          spec:
            containers:
           - name: redis-slave
             image: kubeguide/guestbook-redis-slave
             env:
           - name: GET_HOSTS_FROM
             value: env
           ports:
           - containerPort: 6379
      • redis-slave-service.yaml
      apiVersion: v1
      kind: Service
      metadata:
        name: redis-slave
        labels:
          name: redis-slave
      spec:
        ports:
        - port: 6379
        selector: 
          name: redis-slave
      • 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
      
      
      • frontend-service.yaml
      apiVersion: v1
      kind: Service
      metadata:
        name: frontend
        labels:
          name: frontend
      spec:
        type: NodePort
        ports:
        - port: 80
          nodePort: 30001
        selector:
          name: frontend
  • 创建、删除rc&service

    
    #创建rc&service
    
    kubectl create -f redis-master-controller.yaml
    kubectl create -f redis-master-service.yaml
    kubectl create -f redis-slave-controller.yaml
    kubectl create -f redis-slave-service.yaml
    kubectl create -f frontend-controller.yaml
    kubectl create -f frontend-service.yaml
    
    
    #删除rc&service
    
    kubectl delete -f redis-slave-controller.yaml 
    kubectl delete -f redis-slave-service.yaml 
    kubectl delete -f redis-master-controller.yaml 
    kubectl delete -f redis-master-service.yaml 
    kubectl delete -f frontend-controller.yaml 
    kubectl delete -f frontend-service.yaml 
    
    #或者
    
    kubectl delete rc --all
    kubectl delete service --all
  • 查看rs&service

    root@liu:~/demo# kubectl get rc
    NAME           DESIRED   CURRENT   READY     AGE
    frontend       3         3         3         4h
    redis-master   1         1         1         5h
    redis-slave    2         2         2         4h
    
    root@liu:~/demo# kubectl get service
    NAME           TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)        AGE
    frontend       NodePort    169.169.10.4      <none>        80:30001/TCP   4h
    kubernetes     ClusterIP   169.169.0.1       <none>        443/TCP        5h
    redis-master   ClusterIP   169.169.224.109   <none>        6379/TCP       4h
    redis-slave    ClusterIP   169.169.38.248    <none>        6379/TCP       4h
  • 查看pod

    root@liu:~# kubectl get pods
    NAME                 READY     STATUS              RESTARTS   AGE
    frontend-5n2p2       0/1       ContainerCreating   0          31s
    frontend-hrxbc       0/1       ContainerCreating   0          31s
    frontend-l67mj       0/1       ContainerCreating   0          31s
    redis-master-dnck9   0/1       ContainerCreating   0          31s
    redis-slave-4sw5p    0/1       ContainerCreating   0          31s
    redis-slave-sc6bd    0/1       ContainerCreating   0          31s
  • Q:拉取不到k8s.gcr.io/pause,pod一直处于创建状态

      创建好rc,查看pod,发现pod处于创建状态,查看日志发现没有配置clusterDNS,  
    启动了default配置,但是要下载pause:3.1镜像.由于GFW,拉取不到pause:3.1.  
    更烦的是,无论采用代理还是阿里云容器加速,都拉取不到该镜像.
    
    • 查看pod日志

      kubectl describe pod redis-master
  • A:拉取别人克隆的镜像,并要修改成谷歌镜像的容器名

    
    #这是国内,别人建的仓库
    
    docker pull anjia0532/pause:3.1
    
    #k8s监测镜像k8s.gcr.io/pause:3.1,不存在会拉取
    
    
    #修改后,查看pod状态,逐渐变为running
    
    docker tag da86e6ba6ca1 k8s.gcr.io/pause:3.1
    

    running
    done

  • 测试用例

      由于实例在阿里云上,要添加安全组,才能从外部网访问,这里分析GuestBook,  
    通过curl的方式实现与服务的交互.
    
    • Guestbook页面的处理逻辑

        redis中的存储着一个key-value,key=massages,value=留言1,留言2,  
      访问页面,查redis key=massages,得到value以","分隔的留言串.控制器  
      分割字符串,得到一个个留言,显示到页面
        在页面上填写留言,并提交.控制器,将留言追加到留言串上,写入redis.
      并得到"updated"响应.
      
    • Guestbook页面
      guest
    • curl方式留言

      
      #获取留言信息
      
      root@liu:~/demo# curl "127.0.0.1:30001/guestbook.php?cmd=get&key=messages"
      {"data": "hello,hi"}
      
      
      #填写留言
      
      root@liu:~/demo# curl "127.0.0.1:30001/guestbook.php?cmd=set&key=messages&value=hello,hi,test"
      {"message": "Updated"}
      
      
      #更新后的留言
      
      root@liu:~/demo# curl "127.0.0.1:30001/guestbook.php?cmd=get&key=messages"
      {"data": "hello,hi,test"}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐