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

     1  ## Phabricator example
     2  
     3  This example shows how to build a simple multi-tier web application using Kubernetes and Docker.
     4  
     5  The example combines a web frontend and an external service that provides MySQL database. We use CloudSQL on Google Cloud Platform in this example, but in principle any approach to running MySQL should work.
     6  
     7  ### Step Zero: Prerequisites
     8  
     9  This example assumes that you have a basic understanding of kubernetes [services](https://kubernetes.io/docs/user-guide/services.md) and that you have forked the repository and [turned up a Kubernetes cluster](https://kubernetes.io/docs/getting-started-guides/):
    10  
    11  ```sh
    12  $ cd kubernetes
    13  $ cluster/kube-up.sh
    14  ```
    15  
    16  ### Step One: Set up Cloud SQL instance
    17  
    18  Follow the [official instructions](https://cloud.google.com/sql/docs/getting-started) to set up Cloud SQL instance.
    19  
    20  In the remaining part of this example we will assume that your instance is named "phabricator-db", has IP 1.2.3.4, is listening on port 3306 and the password is "1234".
    21  
    22  ### Step Two: Authenticate phabricator in Cloud SQL
    23  
    24  In order to allow phabricator to connect to your Cloud SQL instance you need to run the following command to authorize all your nodes within a cluster:
    25  
    26  ```bash
    27  NODE_NAMES=`kubectl get nodes | cut -d" " -f1 | tail -n+2`
    28  NODE_IPS=`gcloud compute instances list $NODE_NAMES | tr -s " " | cut -d" " -f 5 | tail -n+2`
    29  gcloud sql instances patch phabricator-db --authorized-networks $NODE_IPS
    30  ```
    31  
    32  Otherwise you will see the following logs:
    33  
    34  ```bash
    35  $ kubectl logs phabricator-controller-02qp4
    36  [...]
    37  Raw MySQL Error: Attempt to connect to root@1.2.3.4 failed with error
    38  #2013: Lost connection to MySQL server at 'reading initial communication packet', system error: 0.
    39  
    40  ```
    41  
    42  ### Step Three: Turn up the phabricator
    43  
    44  To start Phabricator server use the file [`examples/phabricator/phabricator-controller.json`](phabricator-controller.json) which describes a [replication controller](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/) with a single [pod](https://kubernetes.io/docs/user-guide/pods.md) running an Apache server with Phabricator PHP source:
    45  
    46  <!-- BEGIN MUNGE: EXAMPLE phabricator-controller.json -->
    47  
    48  ```json
    49  {
    50    "kind": "ReplicationController",
    51    "apiVersion": "v1",
    52    "metadata": {
    53      "name": "phabricator-controller",
    54      "labels": {
    55        "name": "phabricator"
    56      }
    57    },
    58    "spec": {
    59      "replicas": 1,
    60      "selector": {
    61        "name": "phabricator"
    62      },
    63      "template": {
    64        "metadata": {
    65          "labels": {
    66            "name": "phabricator"
    67          }
    68        },
    69        "spec": {
    70          "containers": [
    71            {
    72              "name": "phabricator",
    73              "image": "fgrzadkowski/example-php-phabricator",
    74              "ports": [
    75                {
    76                  "name": "http-server",
    77                  "containerPort": 80
    78                }
    79              ],
    80              "env": [
    81                {
    82                  "name": "MYSQL_SERVICE_IP",
    83                  "value": "1.2.3.4"
    84                },
    85                {
    86                  "name": "MYSQL_SERVICE_PORT",
    87                  "value": "3306"
    88                },
    89                {
    90                  "name": "MYSQL_PASSWORD",
    91                  "value": "1234"
    92                }
    93              ]
    94            }
    95          ]
    96        }
    97      }
    98    }
    99  }
   100  ```
   101  
   102  [Download example](phabricator-controller.json?raw=true)
   103  <!-- END MUNGE: EXAMPLE phabricator-controller.json -->
   104  
   105  Create the phabricator pod in your Kubernetes cluster by running:
   106  
   107  ```sh
   108  $ kubectl create -f examples/phabricator/phabricator-controller.json
   109  ```
   110  
   111  **Note:** Remember to substitute environment variable values in json file before create replication controller.
   112  
   113  Once that's up you can list the pods in the cluster, to verify that it is running:
   114  
   115  ```sh
   116  kubectl get pods
   117  ```
   118  
   119  You'll see a single phabricator pod. It will also display the machine that the pod is running on once it gets placed (may take up to thirty seconds):
   120  
   121  ```
   122  NAME                           READY     STATUS    RESTARTS   AGE
   123  phabricator-controller-9vy68   1/1       Running   0          1m
   124  ```
   125  
   126  If you ssh to that machine, you can run `docker ps` to see the actual pod:
   127  
   128  ```sh
   129  me@workstation$ gcloud compute ssh --zone us-central1-b kubernetes-node-2
   130  
   131  $ sudo docker ps
   132  CONTAINER ID        IMAGE                             COMMAND     CREATED       STATUS      PORTS   NAMES
   133  54983bc33494        fgrzadkowski/phabricator:latest   "/run.sh"   2 hours ago   Up 2 hours          k8s_phabricator.d6b45054_phabricator-controller-02qp4.default.api_eafb1e53-b6a9-11e4-b1ae-42010af05ea6_01c2c4ca
   134  ```
   135  
   136  (Note that initial `docker pull` may take a few minutes, depending on network conditions.  During this time, the `get pods` command will return `Pending` because the container has not yet started )
   137  
   138  ### Step Four: Turn up the phabricator service
   139  
   140  A Kubernetes 'service' is a named load balancer that proxies traffic to one or more containers. The services in a Kubernetes cluster are discoverable inside other containers via *environment variables*. Services find the containers to load balance based on pod labels.  These environment variables are typically referenced in application code, shell scripts, or other places where one node needs to talk to another in a distributed system.  You should catch up on [kubernetes services](https://kubernetes.io/docs/user-guide/services.md) before proceeding.
   141  
   142  The pod that you created in Step Three has the label `name=phabricator`. The selector field of the service determines which pods will receive the traffic sent to the service.
   143  
   144  Use the file [`examples/phabricator/phabricator-service.json`](phabricator-service.json):
   145  
   146  <!-- BEGIN MUNGE: EXAMPLE phabricator-service.json -->
   147  
   148  ```json
   149  {
   150    "kind": "Service",
   151    "apiVersion": "v1",
   152    "metadata": {
   153      "name": "phabricator"
   154    },
   155    "spec": {
   156      "ports": [
   157        {
   158          "port": 80,
   159          "targetPort": "http-server"
   160        }
   161      ],
   162      "selector": {
   163        "name": "phabricator"
   164      },
   165      "type": "LoadBalancer"
   166    }
   167  }
   168  ```
   169  
   170  [Download example](phabricator-service.json?raw=true)
   171  <!-- END MUNGE: EXAMPLE phabricator-service.json -->
   172  
   173  To create the service run:
   174  
   175  ```sh
   176  $ kubectl create -f examples/phabricator/phabricator-service.json
   177  phabricator
   178  ```
   179  
   180  To play with the service itself, find the external IP of the load balancer:
   181  
   182  ```console
   183  $ kubectl get services
   184  NAME          LABELS                                    SELECTOR           IP(S)         PORT(S)
   185  kubernetes    component=apiserver,provider=kubernetes   <none>             10.0.0.1      443/TCP
   186  phabricator   <none>                                    name=phabricator   10.0.31.173   80/TCP
   187  $ kubectl get services phabricator -o json | grep ingress -A 4
   188              "ingress": [
   189                  {
   190                      "ip": "104.197.13.125"
   191                  }
   192              ]
   193  ```
   194  
   195  and then visit port 80 of that IP address.
   196  
   197  **Note**: Provisioning of the external IP address may take few minutes.
   198  
   199  **Note**: You may need to open the firewall for port 80 using the [console][cloud-console] or the `gcloud` tool. The following command will allow traffic from any source to instances tagged `kubernetes-node`:
   200  
   201  ```sh
   202  $ gcloud compute firewall-rules create phabricator-node-80 --allow=tcp:80 --target-tags kubernetes-node
   203  ```
   204  
   205  ### Step Six: Cleanup
   206  
   207  To turn down a Kubernetes cluster:
   208  
   209  ```sh
   210  $ cluster/kube-down.sh
   211  ```
   212  
   213  
   214  <!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
   215  [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/examples/phabricator/README.md?pixel)]()
   216  <!-- END MUNGE: GENERATED_ANALYTICS -->