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

     1  ## Java EE Application using WildFly and MySQL
     2  
     3  The following document describes the deployment of a Java EE application using [WildFly](http://wildfly.org) application server and MySQL database server on Kubernetes. The sample application source code is at: https://github.com/javaee-samples/javaee7-simple-sample.
     4  
     5  ### Prerequisites
     6  
     7  https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/prereqs.md
     8  
     9  ### Start MySQL Pod
    10  
    11  In Kubernetes a [_Pod_](https://kubernetes.io/docs/user-guide/pods.md) is the smallest deployable unit that can be created, scheduled, and managed. It's a collocated group of containers that share an IP and storage volume.
    12  
    13  Here is the config for MySQL pod: [mysql-pod.yaml](mysql-pod.yaml)
    14  
    15  <!-- BEGIN MUNGE: mysql-pod.yaml -->
    16  <!-- END MUNGE: EXAMPLE -->
    17  
    18  Create the MySQL pod:
    19  
    20  ```sh
    21  kubectl create -f examples/javaee/mysql-pod.yaml
    22  ```
    23  
    24  Check status of the pod:
    25  
    26  ```sh
    27  kubectl get -w po
    28  NAME        READY     STATUS    RESTARTS   AGE
    29  mysql-pod   0/1       Pending   0          4s
    30  NAME        READY     STATUS    RESTARTS   AGE
    31  mysql-pod   0/1       Running   0          44s
    32  mysql-pod   1/1       Running   0         44s
    33  ```
    34  
    35  Wait for the status to `1/1` and `Running`.
    36  
    37  ### Start MySQL Service
    38  
    39  We are creating a [_Service_](https://kubernetes.io/docs/user-guide/services.md) to expose the TCP port of the MySQL server. A Service distributes traffic across a set of Pods. The order of Service and the targeted Pods does not matter. However Service needs to be started before any other Pods consuming the Service are started.
    40  
    41  In this application, we will use a Kubernetes Service to provide a discoverable endpoints for the MySQL endpoint in the cluster.  MySQL service target pods with the labels `name: mysql-pod` and `context: docker-k8s-lab`.
    42  
    43  Here is definition of the MySQL service: [mysql-service.yaml](mysql-service.yaml)
    44  
    45  <!-- BEGIN MUNGE: mysql-service.yaml -->
    46  <!-- END MUNGE: EXAMPLE -->
    47  
    48  Create this service:
    49  
    50  ```sh
    51  kubectl create -f examples/javaee/mysql-service.yaml
    52  ```
    53  
    54  Get status of the service:
    55  
    56  ```sh
    57  kubectl get -w svc
    58  NAME            LABELS                                    SELECTOR                                IP(S)          PORT(S)
    59  kubernetes      component=apiserver,provider=kubernetes   <none>                                  10.247.0.1     443/TCP
    60  mysql-service   context=docker-k8s-lab,name=mysql-pod     context=docker-k8s-lab,name=mysql-pod   10.247.63.43   3306/TCP
    61  ```
    62  
    63  If multiple services are running, then it can be narrowed by specifying labels:
    64  
    65  ```sh
    66  kubectl get -w po -l context=docker-k8s-lab,name=mysql-pod
    67  NAME        READY     STATUS    RESTARTS   AGE
    68  mysql-pod   1/1       Running   0          4m
    69  ```
    70  
    71  This is also the selector label used by service to target pods.
    72  
    73  When a Service is run on a node, the kubelet adds a set of environment variables for each active Service. It supports both Docker links compatible variables and simpler `{SVCNAME}_SERVICE_HOST` and `{SVCNAME}_SERVICE_PORT` variables, where the Service name is upper-cased and dashes are converted to underscores.
    74  
    75  Our service name is ``mysql-service'' and so ``MYSQL_SERVICE_SERVICE_HOST'' and ``MYSQL_SERVICE_SERVICE_PORT'' variables are available to other pods. This host and port variables are then used to create the JDBC resource in WildFly.
    76  
    77  ### Start WildFly Replication Controller
    78  
    79  WildFly is a lightweight Java EE 7 compliant application server. It is wrapped in a Replication Controller and used as the Java EE runtime.
    80  
    81  In Kubernetes a [_Replication Controller_](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/) is responsible for replicating sets of identical pods. Like a _Service_ it has a selector query which identifies the members of it's set.  Unlike a service it also has a desired number of replicas, and it will create or delete pods to ensure that the number of pods matches up with it's desired state.
    82  
    83  Here is definition of the MySQL service: [wildfly-rc.yaml](wildfly-rc.yaml).
    84  
    85  <!-- BEGIN MUNGE: wildfly-rc.yaml -->
    86  <!-- END MUNGE: EXAMPLE -->
    87  
    88  Create this controller:
    89  
    90  ```sh
    91  kubectl create -f examples/javaee/wildfly-rc.yaml
    92  ```
    93  
    94  Check status of the pod inside replication controller:
    95  
    96  ```sh
    97  kubectl get po
    98  NAME               READY     STATUS    RESTARTS   AGE
    99  mysql-pod          1/1       Running   0          1h
   100  wildfly-rc-w2kk5   1/1       Running   0          6m
   101  ```
   102  
   103  ### Access the application
   104  
   105  Get IP address of the pod:
   106  
   107  ```sh
   108  kubectl get -o template po wildfly-rc-w2kk5 --template={{.status.podIP}}
   109  10.246.1.23
   110  ```
   111  
   112  Log in to node and access the application:
   113  
   114  ```sh
   115  vagrant ssh node-1
   116  Last login: Thu Jul 16 00:24:36 2015 from 10.0.2.2
   117  [vagrant@kubernetes-node-1 ~]$ curl http://10.246.1.23:8080/employees/resources/employees/
   118  <?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><employee><id>1</id><name>Penny</name></employee><employee><id>2</id><name>Sheldon</name></employee><employee><id>3</id><name>Amy</name></employee><employee><id>4</id><name>Leonard</name></employee><employee><id>5</id><name>Bernadette</name></employee><employee><id>6</id><name>Raj</name></employee><employee><id>7</id><name>Howard</name></employee><employee><id>8</id><name>Priya</name></employee></collection>
   119  ```
   120  
   121  ### Delete resources
   122  
   123  All resources created in this application can be deleted:
   124  
   125  ```sh
   126  kubectl delete -f examples/javaee/mysql-pod.yaml
   127  kubectl delete -f examples/javaee/mysql-service.yaml
   128  kubectl delete -f examples/javaee/wildfly-rc.yaml
   129  ```
   130  
   131  
   132  <!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
   133  [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/examples/javaee/README.md?pixel)]()
   134  <!-- END MUNGE: GENERATED_ANALYTICS -->