github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/meteor/README.md (about)

     1  Meteor on Kubernetes
     2  ====================
     3  
     4  This example shows you how to package and run a
     5  [Meteor](https://www.meteor.com/) app on Kubernetes.
     6  
     7  Get started on Google Compute Engine
     8  ------------------------------------
     9  
    10  Meteor uses MongoDB, and we will use the `GCEPersistentDisk` type of
    11  volume for persistent storage. Therefore, this example is only
    12  applicable to [Google Compute
    13  Engine](https://cloud.google.com/compute/). Take a look at the
    14  [volumes documentation](https://kubernetes.io/docs/user-guide/volumes.md) for other options.
    15  
    16  First, if you have not already done so:
    17  
    18  1. [Create](https://cloud.google.com/compute/docs/quickstart) a
    19  [Google Cloud Platform](https://cloud.google.com/) project.
    20  2. [Enable
    21  billing](https://developers.google.com/console/help/new/#billing).
    22  3. Install the [gcloud SDK](https://cloud.google.com/sdk/).
    23  
    24  Authenticate with gcloud and set the gcloud default project name to
    25  point to the project you want to use for your Kubernetes cluster:
    26  
    27  ```sh
    28  gcloud auth login
    29  gcloud config set project <project-name>
    30  ```
    31  
    32  Next, start up a Kubernetes cluster:
    33  
    34  ```sh
    35  wget -q -O - https://get.k8s.io | bash
    36  ```
    37  
    38  Please see the [Google Compute Engine getting started
    39  guide](https://kubernetes.io/docs/getting-started-guides/gce.md) for full
    40  details and other options for starting a cluster.
    41  
    42  Build a container for your Meteor app
    43  -------------------------------------
    44  
    45  To be able to run your Meteor app on Kubernetes you need to build a
    46  Docker container for it first. To do that you need to install
    47  [Docker](https://www.docker.com) Once you have that you need to add 2
    48  files to your existing Meteor project `Dockerfile` and
    49  `.dockerignore`.
    50  
    51  `Dockerfile` should contain the below lines. You should replace the
    52  `ROOT_URL` with the actual hostname of your app.
    53  
    54  ```
    55  FROM chees/meteor-kubernetes
    56  ENV ROOT_URL http://myawesomeapp.com
    57  ```
    58  
    59  The `.dockerignore` file should contain the below lines. This tells
    60  Docker to ignore the files on those directories when it's building
    61  your container.
    62  
    63  ```
    64  .meteor/local
    65  packages/*/.build*
    66  ```
    67  
    68  You can see an example meteor project already set up at:
    69  [meteor-gke-example](https://github.com/Q42/meteor-gke-example). Feel
    70  free to use this app for this example.
    71  
    72  > Note: The next step will not work if you have added mobile platforms
    73  > to your meteor project. Check with `meteor list-platforms`
    74  
    75  Now you can build your container by running this in
    76  your Meteor project directory:
    77  
    78  ```
    79  docker build -t my-meteor .
    80  ```
    81  
    82  Pushing to a registry
    83  ---------------------
    84  
    85  For the [Docker Hub](https://hub.docker.com/), tag your app image with
    86  your username and push to the Hub with the below commands. Replace
    87  `<username>` with your Hub username.
    88  
    89  ```
    90  docker tag my-meteor <username>/my-meteor
    91  docker push <username>/my-meteor
    92  ```
    93  
    94  For [Google Container
    95  Registry](https://cloud.google.com/tools/container-registry/), tag
    96  your app image with your project ID, and push to GCR. Replace
    97  `<project>` with your project ID.
    98  
    99  ```
   100  docker tag my-meteor gcr.io/<project>/my-meteor
   101  gcloud docker -- push gcr.io/<project>/my-meteor
   102  ```
   103  
   104  Running
   105  -------
   106  
   107  Now that you have containerized your Meteor app it's time to set up
   108  your cluster. Edit [`meteor-controller.json`](meteor-controller.json)
   109  and make sure the `image:` points to the container you just pushed to
   110  the Docker Hub or GCR.
   111  
   112  We will need to provide MongoDB a persistent Kubernetes volume to
   113  store its data. See the [volumes documentation](https://kubernetes.io/docs/user-guide/volumes.md) for
   114  options. We're going to use Google Compute Engine persistent
   115  disks. Create the MongoDB disk by running:
   116  
   117  ```
   118  gcloud compute disks create --size=200GB mongo-disk
   119  ```
   120  
   121  Now you can start Mongo using that disk:
   122  
   123  ```
   124  kubectl create -f examples/staging/meteor/mongo-pod.json
   125  kubectl create -f examples/staging/meteor/mongo-service.json
   126  ```
   127  
   128  Wait until Mongo is started completely and then start up your Meteor app:
   129  
   130  ```
   131  kubectl create -f examples/staging/meteor/meteor-service.json
   132  kubectl create -f examples/staging/meteor/meteor-controller.json
   133  ```
   134  
   135  Note that [`meteor-service.json`](meteor-service.json) creates a load balancer, so
   136  your app should be available through the IP of that load balancer once
   137  the Meteor pods are started. We also created the service before creating the rc to
   138  aid the scheduler in placing pods, as the scheduler ranks pod placement according to
   139  service anti-affinity (among other things). You can find the IP of your load balancer
   140  by running:
   141  
   142  ```
   143  kubectl get service meteor --template="{{range .status.loadBalancer.ingress}} {{.ip}} {{end}}"
   144  ```
   145  
   146  You will have to open up port 80 if it's not open yet in your
   147  environment. On Google Compute Engine, you may run the below command.
   148  
   149  ```
   150  gcloud compute firewall-rules create meteor-80 --allow=tcp:80 --target-tags kubernetes-node
   151  ```
   152  
   153  What is going on?
   154  -----------------
   155  
   156  Firstly, the `FROM chees/meteor-kubernetes` line in your `Dockerfile`
   157  specifies the base image for your Meteor app. The code for that image
   158  is located in the `dockerbase/` subdirectory. Open up the `Dockerfile`
   159  to get an insight of what happens during the `docker build` step. The
   160  image is based on the Node.js official image. It then installs Meteor
   161  and copies in your apps' code. The last line specifies what happens
   162  when your app container is run.
   163  
   164  ```sh
   165  ENTRYPOINT MONGO_URL=mongodb://$MONGO_SERVICE_HOST:$MONGO_SERVICE_PORT /usr/local/bin/node main.js
   166  ```
   167  
   168  Here we can see the MongoDB host and port information being passed
   169  into the Meteor app. The `MONGO_SERVICE...` environment variables are
   170  set by Kubernetes, and point to the service named `mongo` specified in
   171  [`mongo-service.json`](mongo-service.json). See the [environment
   172  documentation](https://kubernetes.io/docs/concepts/containers/container-environment-variables/) for more details.
   173  
   174  As you may know, Meteor uses long lasting connections, and requires
   175  _sticky sessions_. With Kubernetes you can scale out your app easily
   176  with session affinity. The
   177  [`meteor-service.json`](meteor-service.json) file contains
   178  `"sessionAffinity": "ClientIP"`, which provides this for us. See the
   179  [service
   180  documentation](https://kubernetes.io/docs/user-guide/services.md#virtual-ips-and-service-proxies) for
   181  more information.
   182  
   183  As mentioned above, the mongo container uses a volume which is mapped
   184  to a persistent disk by Kubernetes. In [`mongo-pod.json`](mongo-pod.json) the container
   185  section specifies the volume:
   186  
   187  ```json
   188  {
   189          "volumeMounts": [
   190            {
   191              "name": "mongo-disk",
   192              "mountPath": "/data/db"
   193            }
   194  ```
   195  
   196  The name `mongo-disk` refers to the volume specified outside the
   197  container section:
   198  
   199  ```json
   200  {
   201      "volumes": [
   202        {
   203          "name": "mongo-disk",
   204          "gcePersistentDisk": {
   205            "pdName": "mongo-disk",
   206            "fsType": "ext4"
   207          }
   208        }
   209      ],
   210  ```
   211  
   212  
   213  <!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
   214  [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/examples/meteor/README.md?pixel)]()
   215  <!-- END MUNGE: GENERATED_ANALYTICS -->