美文网首页
Kubernetes 示例 - 部署 GuestBook

Kubernetes 示例 - 部署 GuestBook

作者: SlowGO | 来源:发表于2018-11-26 14:41 被阅读17次

    目标

    部署一个 GuestBook 示例,是 php+redis 环境,我们需要部署3个东西:

    1. frontend 前端php应用
    2. redis master
    3. redis slave

    其中每一个都包括 ReplicationController 和 Service,先写配置文件,然后运行起来。

    redis master

    (1)ReplicationController

    redis-master-controller.yml

    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: redis:3.0.7-alpine
            ports:
            - containerPort: 6379
    

    启动:

    $ kubectl create -f redis-master-controller.yaml
    replicationcontroller/redis-master created
    

    查看当前controller和pod状况:

    $ kubectl get rc
    NAME           DESIRED   CURRENT   READY   AGE
    redis-master   1         1         1       54s
    
    $ kubectl get pods
    NAME                 READY   STATUS    RESTARTS   AGE
    redis-master-hr8wf   1/1     Running   0          89s
    

    Service

    redis-master-service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: redis-master
      labels:
        name: redis-master
    spec:
      ports:
        # the port that this service should serve on
      - port: 6379
        targetPort: 6379
      selector:
        name: redis-master
    

    启动:

    $ kubectl create -f redis-master-service.yaml
    service/redis-master created
    

    查看当前service状况:

    $ kubectl get services
    NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
    kubernetes     ClusterIP   10.152.183.1     <none>        443/TCP    45h
    redis-master   ClusterIP   10.152.183.209   <none>        6379/TCP   37s
    
    
    $ kubectl describe services redis-master
    Name:              redis-master
    Namespace:         default
    Labels:            name=redis-master
    Annotations:       <none>
    Selector:          name=redis-master
    Type:              ClusterIP
    IP:                10.152.183.209
    Port:              <unset>  6379/TCP
    TargetPort:        6379/TCP
    Endpoints:         10.1.1.2:6379
    Session Affinity:  None
    Events:            <none>
    

    redis slave

    (1)ReplicationController

    redis-slave-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: worker
            image: gcr.io/google_samples/gb-redisslave:v1
            env:
            - name: GET_HOSTS_FROM
              # value: dns
              # 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
    

    启动:

    $ kubectl create -f redis-slave-controller.yaml
    replicationcontroller/redis-slave created
    

    查看rc:

    $ kubectl get rc
    NAME           DESIRED   CURRENT   READY   AGE
    redis-master   1         1         1       6m34s
    redis-slave    2         2         2       20s
    

    Service

    redis-slave-service.yaml

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

    启动:

    $ kubectl create -f redis-slave-service.yaml
    

    查看 service:

    $ kubectl get services
    NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
    kubernetes     ClusterIP   10.152.183.1     <none>        443/TCP    2d
    redis-master   ClusterIP   10.152.183.209   <none>        6379/TCP   139m
    redis-slave    ClusterIP   10.152.183.168   <none>        6379/TCP   35s
    

    frontend

    (1)ReplicationController

    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: php-redis
            image: gcr.io/google_samples/gb-frontend:v3
            env:
            - name: GET_HOSTS_FROM
              # value: dns
              # 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
    

    启动:

    kubectl create -f frontend-controller.yaml
    

    查看rc和pod:

    $ kubectl get rc
    NAME           DESIRED   CURRENT   READY   AGE
    frontend       3         3         0       34s
    redis-master   1         1         1       144m
    redis-slave    2         2         2       137m
    
    $ microk8s.kubectl get pods
    NAME                 READY   STATUS    RESTARTS   AGE
    frontend-fb8bs       1/1     Running   0          55s
    frontend-pc88k       1/1     Running   0          55s
    frontend-sgk4m       1/1     Running   0          55s
    redis-master-hr8wf   1/1     Running   0          144m
    redis-slave-62w7b    1/1     Running   0          138m
    redis-slave-shhsv    1/1     Running   0          138m
    

    Service

    frontend-service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: frontend
      labels:
        name: 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: 30080
      selector:
        name: frontend
    

    启动:

    kubectl create -f frontend-service.yaml
    

    查看 service:

    $ kubectl get services
    NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
    frontend       NodePort    10.152.183.73    <none>        80:30080/TCP   6s
    kubernetes     ClusterIP   10.152.183.1     <none>        443/TCP        2d
    redis-master   ClusterIP   10.152.183.209   <none>        6379/TCP       144m
    redis-slave    ClusterIP   10.152.183.168   <none>        6379/TCP       5m30s
    

    访问

    获取 frontend 的 NodePort:

    kubectl describe service frontend | grep NodePort
    Type:                     NodePort
    NodePort:                 <unset>  30080/TCP
    

    访问 <ip>: 30080

    相关文章

      网友评论

          本文标题:Kubernetes 示例 - 部署 GuestBook

          本文链接:https://www.haomeiwen.com/subject/ltjbqqtx.html