From 80fceb629b498cef565cc73dcb86c7215a469b45 Mon Sep 17 00:00:00 2001 From: Irvi Firqotul Aini Date: Mon, 26 Mar 2018 14:12:39 +0700 Subject: [PATCH 1/6] port to kubernetes 1.9 --- README.md | 81 ++++++------------- deploy.sh | 12 +++ docker-image/Dockerfile | 2 +- docker-image/locust-tasks/requirements.txt | 12 +-- ...ler.yaml => locust-master-deployment.yaml} | 19 ++--- ...ler.yaml => locust-worker-deployment.yaml} | 18 ++--- 6 files changed, 56 insertions(+), 88 deletions(-) create mode 100755 deploy.sh rename kubernetes-config/{locust-master-controller.yaml => locust-master-deployment.yaml} (76%) rename kubernetes-config/{locust-worker-controller.yaml => locust-worker-deployment.yaml} (74%) diff --git a/README.md b/README.md index 3a757d6..eec2db8 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,9 @@ Before deploying the `locust-master` and `locust-worker` controllers, update eac - name: TARGET_HOST value: http://PROJECT-ID.appspot.com -### Update Controller Docker Image (Optional) +### Build Locust Docker Image -The `locust-master` and `locust-worker` controllers are set to use the pre-built `locust-tasks` Docker image, which has been uploaded to the [Google Container Registry](http://gcr.io) and is available at `gcr.io/cloud-solutions-images/locust-tasks`. If you are interested in making changes and publishing a new Docker image, refer to the following steps. +To build and publish Locust contoller Docker image, refer to the following steps. First, [install Docker](https://docs.docker.com/installation/#installation) on your platform. Once Docker is installed and you've made changes to the `Dockerfile`, you can build, tag, and upload the image using the following steps: @@ -49,7 +49,7 @@ First, [install Docker](https://docs.docker.com/installation/#installation) on y **Note:** you are not required to use the Google Container Registry. If you'd like to publish your images to the [Docker Hub](https://hub.docker.com) please refer to the steps in [Working with Docker Hub](https://docs.docker.com/userguide/dockerrepos/). -Once the Docker image has been rebuilt and uploaded to the registry you will need to edit the controllers with your new image location. Specifically, the `spec.template.spec.containers.image` field in each controller controls which Docker image to use. +Once the Docker image has been built and uploaded to the registry you will need to edit the deployments with your new image location. Specifically, the `spec.template.spec.containers.image` field in each deployment controls which Docker image to use. If you uploaded your Docker image to the Google Container Registry: @@ -61,96 +61,61 @@ If you uploaded your Docker image to the Docker Hub: **Note:** the image location includes the `latest` tag so that the image is pulled down every time a new Pod is launched. To use a Kubernetes-cached copy of the image, remove `:latest` from the image location. +Please note that image `gcr.io/cloud-solutions-images/locust-tasks:latest` isn't available anymore, so you may need to create your own image. + ### Deploy Kubernetes Cluster -First create the [Google Container Engine](http://cloud.google.com/container-engine) cluster using the `gcloud` command as shown below. +First create the [Google kubernetes Engine](https://cloud.google.com/kubernetes-engine/) cluster using the `gcloud` command as shown below. -**Note:** This command defaults to creating a three node Kubernetes cluster (not counting the master) using the `n1-standard-1` machine type. Refer to the [`gcloud alpha container clusters create`](https://cloud.google.com/sdk/gcloud/reference/alpha/container/clusters/create) documentation information on specifying a different cluster configuration. +**Note:** This command defaults to creating a three node Kubernetes cluster (not counting the master) using the `n1-standard-1` machine type. Refer to the [`gcloud alpha container clusters create`](https://cloud.google.com/sdk/gcloud/reference/container/clusters/create) documentation information on specifying a different cluster configuration. - $ gcloud alpha container clusters create CLUSTER-NAME + $ gcloud container clusters create [CLUSTER-NAME] After a few minutes, you'll have a working Kubernetes cluster with three nodes (not counting the Kubernetes master). Next, configure your system to use the `kubectl` command: - $ kubectl config use-context gke_PROJECT-ID_ZONE_CLUSTER-NAME + $ gcloud container clusters get-credentials [CLUSTER-NAME] -**Note:** the output from the previous `gcloud` cluster create command will contain the specific `kubectl config` command to execute for your platform/project. ### Deploy locust-master -Now that `kubectl` is setup, deploy the `locust-master-controller`: +Now that `kubectl` is setup, deploy the `kubernetes configuration`: - $ kubectl create -f locust-master-controller.yaml + $ ./deploy.sh [PROJECT_ID] -To confirm that the Replication Controller and Pod are created, run the following: +To confirm that the deployment and Pod are created, run the following: - $ kubectl get rc + $ kubectl get deployments $ kubectl get pods -l name=locust,role=master -Next, deploy the `locust-master-service`: - - $ kubectl create -f locust-master-service.yaml - -This step will expose the Pod with an internal DNS name (`locust-master`) and ports `8089`, `5557`, and `5558`. As part of this step, the `type: LoadBalancer` directive in `locust-master-service.yaml` will tell Google Container Engine to create a Google Compute Engine forwarding-rule from a publicly avaialble IP address to the `locust-master` Pod. To view the newly created forwarding-rule, execute the following: - - $ gcloud compute forwarding-rules list - -### Deploy locust-worker +This step will expose the Pod with an internal DNS name (`locust-master`) and ports `8089`, `5557`, and `5558`. As part of this step, the `type: LoadBalancer` directive in `locust-master-service.yaml` will tell Google Container Engine to create a Google Compute Engine forwarding-rule from a publicly avaialble IP address to the `locust-master` Pod. To see the the service IP address ('LoadBalancer'), issue the below command: + + $ kubectl get services locust-master -Now deploy `locust-worker-controller`: - - $ kubectl create -f locust-worker-controller.yaml - -The `locust-worker-controller` is set to deploy 10 `locust-worker` Pods, to confirm they were deployed run the following: +The `locust-worker-deployment` is set to deploy 10 `locust-worker` Pods, to confirm they were deployed run the following: $ kubectl get pods -l name=locust,role=worker -To scale the number of `locust-worker` Pods, issue a replication controller `scale` command. +To scale the number of `locust-worker` Pods, issue a deployment `scale` command. - $ kubectl scale --replicas=20 replicationcontrollers locust-worker + $ kubectl scale --replicas=20 deployment locust-worker To confirm that the Pods have launched and are ready, get the list of `locust-worker` Pods: $ kubectl get pods -l name=locust,role=worker -**Note:** depending on the desired number of `locust-worker` Pods, the Kubernetes cluster may need to be launched with more than 3 compute engine nodes and may also need a machine type more powerful than n1-standard-1. Refer to the [`gcloud alpha container clusters create`](https://cloud.google.com/sdk/gcloud/reference/alpha/container/clusters/create) documentation for more information. - -### Setup Firewall Rules - -The final step in deploying these controllers and services is to allow traffic from your publicly accessible forwarding-rule IP address to the appropriate Container Engine instances. - -The only traffic we need to allow externally is to the Locust web interface, running on the `locust-master` Pod at port `8089`. First, get the target tags for the nodes in your Kubernetes cluster using the output from `kubectl get nodes`: - - $ kubectl get nodes - NAME LABELS STATUS - gke-ws-0e365264-node-4pdw kubernetes.io/hostname=gke-ws-0e365264-node-4pdw Ready - gke-ws-0e365264-node-jdcz kubernetes.io/hostname=gke-ws-0e365264-node-jdcz Ready - gke-ws-0e365264-node-kp3d kubernetes.io/hostname=gke-ws-0e365264-node-kp3d Ready - -The target tag is the node name prefix up to `-node` and is formatted as `gke-CLUSTER-NAME-[...]-node`. For example, if your node name is `gke-mycluster-12345678-node-abcd`, the target tag would be `gke-mycluster-12345678-node`. - -Now to create the firewall rule, execute the following: - - $ gcloud compute firewall-rules create FIREWALL-RULE-NAME --allow=tcp:8089 --target-tags gke-CLUSTER-NAME-[...]-node +**Note:** depending on the desired number of `locust-worker` Pods, the Kubernetes cluster may need to be launched with more than 3 compute engine nodes and may also need a machine type more powerful than n1-standard-1. Refer to the [`gcloud container clusters create`](https://cloud.google.com/sdk/gcloud/reference/container/clusters/create) documentation for more information. ## Execute Tests -To execute the Locust tests, navigate to the IP address of your forwarding-rule (see above) and port `8089` and enter the number of clients to spawn and the client hatch rate then start the simulation. +To execute the Locust tests, navigate to the IP address of your locust-master-service LoadBalancer (see above) and port `8089` and enter the number of clients to spawn and the client hatch rate then start the simulation. ## Deployment Cleanup To teardown the workload simulation cluster, use the following steps. First, delete the Kubernetes cluster: - $ gcloud alpha container clusters delete CLUSTER-NAME - -Next, delete the forwarding rule that forwards traffic into the cluster. - - $ gcloud compute forwarding-rules delete FORWARDING-RULE-NAME - -Finally, delete the firewall rule that allows incoming traffic to the cluster. - - $ gcloud compute firewall-rules delete FIREWALL-RULE-NAME + $ gcloud container clusters delete CLUSTER-NAME -To delete the sample web application, visit the [Google Cloud Console](https://console.developers.google.com). +To delete the sample web application, visit the [Google Cloud Console](https://console.cloud.google.com). ## License diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..0e57d5a --- /dev/null +++ b/deploy.sh @@ -0,0 +1,12 @@ +#!/bin/bash -xe + +PROJECT_ID=$1 + +if [[ -z "${PROJECT_ID}" ]]; then + exit 1 +fi + +sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-master-deployment.yaml +sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-worker-deployment.yaml + +kubectl apply -f kubernetes-config \ No newline at end of file diff --git a/docker-image/Dockerfile b/docker-image/Dockerfile index f0db361..02067de 100644 --- a/docker-image/Dockerfile +++ b/docker-image/Dockerfile @@ -34,4 +34,4 @@ EXPOSE 5557 5558 8089 RUN chmod 755 /locust-tasks/run.sh # Start Locust using LOCUS_OPTS environment variable -ENTRYPOINT ["/locust-tasks/run.sh"] +CMD ["/bin/bash", "-c", "/locust-tasks/run.sh"] diff --git a/docker-image/locust-tasks/requirements.txt b/docker-image/locust-tasks/requirements.txt index 3684955..9368bcd 100644 --- a/docker-image/locust-tasks/requirements.txt +++ b/docker-image/locust-tasks/requirements.txt @@ -1,11 +1 @@ -Flask==0.10.1 -gevent==1.0.1 -greenlet==0.4.5 -itsdangerous==0.24 -Jinja2==2.7.3 -locustio==0.7.2 -MarkupSafe==0.23 -msgpack-python==0.4.6 -pyzmq==14.5.0 -requests==2.6.2 -Werkzeug==0.10.4 +locustio==0.8.1 \ No newline at end of file diff --git a/kubernetes-config/locust-master-controller.yaml b/kubernetes-config/locust-master-deployment.yaml similarity index 76% rename from kubernetes-config/locust-master-controller.yaml rename to kubernetes-config/locust-master-deployment.yaml index fa4468c..f3afbc4 100644 --- a/kubernetes-config/locust-master-controller.yaml +++ b/kubernetes-config/locust-master-deployment.yaml @@ -13,8 +13,8 @@ # limitations under the License. -kind: ReplicationController -apiVersion: v1 +apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1. For version 1.9 use apps/v1 +kind: Deployment metadata: name: locust-master labels: @@ -23,8 +23,9 @@ metadata: spec: replicas: 1 selector: - name: locust - role: master + matchLabels: + name: locust + role: master template: metadata: labels: @@ -33,14 +34,14 @@ spec: spec: containers: - name: locust - image: gcr.io/cloud-solutions-images/locust-tasks:latest + image: gcr.io/$projectId/locust-tasks:latest + command: [ "/bin/bash", "-c", "--" ] + args: [ "/locust-tasks/run.sh" ] env: - name: LOCUST_MODE - key: LOCUST_MODE value: master - name: TARGET_HOST - key: TARGET_HOST - value: http://workload-simulation-webapp.appspot.com + value: $targetHost ports: - name: loc-master-web containerPort: 8089 @@ -50,4 +51,4 @@ spec: protocol: TCP - name: loc-master-p2 containerPort: 5558 - protocol: TCP + protocol: TCP \ No newline at end of file diff --git a/kubernetes-config/locust-worker-controller.yaml b/kubernetes-config/locust-worker-deployment.yaml similarity index 74% rename from kubernetes-config/locust-worker-controller.yaml rename to kubernetes-config/locust-worker-deployment.yaml index 883c450..03bc3c7 100644 --- a/kubernetes-config/locust-worker-controller.yaml +++ b/kubernetes-config/locust-worker-deployment.yaml @@ -13,8 +13,8 @@ # limitations under the License. -kind: ReplicationController -apiVersion: v1 +apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1. For version 1.9 use apps/v1 +kind: Deployment metadata: name: locust-worker labels: @@ -23,8 +23,9 @@ metadata: spec: replicas: 10 selector: - name: locust - role: worker + matchLabels: + name: locust + role: worker template: metadata: labels: @@ -33,14 +34,13 @@ spec: spec: containers: - name: locust - image: gcr.io/cloud-solutions-images/locust-tasks:latest + image: gcr.io/$projectId/locust-tasks:latest + command: [ "/bin/bash", "-c", "--" ] + args: [ "/locust-tasks/run.sh" ] env: - name: LOCUST_MODE - key: LOCUST_MODE value: worker - name: LOCUST_MASTER - key: LOCUST_MASTER value: locust-master - name: TARGET_HOST - key: TARGET_HOST - value: http://workload-simulation-webapp.appspot.com + value: $targetHost \ No newline at end of file From 5308d92b1c408e4dba6a5e3a0d6843a7dfe7930c Mon Sep 17 00:00:00 2001 From: Irvi Firqotul Aini Date: Thu, 5 Apr 2018 00:45:15 +0700 Subject: [PATCH 2/6] using configmap to store python executable, so that we don't need to rebuild dockerimage if we change the tasks --- {docker-image/locust-tasks => config}/tasks.py | 0 deploy.sh | 1 + docker-image/locust-tasks/run.sh | 2 +- kubernetes-config/locust-master-deployment.yaml | 9 ++++++++- kubernetes-config/locust-worker-deployment.yaml | 9 ++++++++- 5 files changed, 18 insertions(+), 3 deletions(-) rename {docker-image/locust-tasks => config}/tasks.py (100%) mode change 100644 => 100755 mode change 100644 => 100755 docker-image/locust-tasks/run.sh diff --git a/docker-image/locust-tasks/tasks.py b/config/tasks.py old mode 100644 new mode 100755 similarity index 100% rename from docker-image/locust-tasks/tasks.py rename to config/tasks.py diff --git a/deploy.sh b/deploy.sh index 0e57d5a..fa43d15 100755 --- a/deploy.sh +++ b/deploy.sh @@ -9,4 +9,5 @@ fi sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-master-deployment.yaml sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-worker-deployment.yaml +kubectl create configmap locust-tasks-configuration --from-file=config/tasks.py kubectl apply -f kubernetes-config \ No newline at end of file diff --git a/docker-image/locust-tasks/run.sh b/docker-image/locust-tasks/run.sh old mode 100644 new mode 100755 index ba5f684..18d399d --- a/docker-image/locust-tasks/run.sh +++ b/docker-image/locust-tasks/run.sh @@ -16,7 +16,7 @@ LOCUST="/usr/local/bin/locust" -LOCUS_OPTS="-f /locust-tasks/tasks.py --host=$TARGET_HOST" +LOCUS_OPTS="-f /locust-tasks/script/tasks.py --host=$TARGET_HOST" LOCUST_MODE=${LOCUST_MODE:-standalone} if [[ "$LOCUST_MODE" = "master" ]]; then diff --git a/kubernetes-config/locust-master-deployment.yaml b/kubernetes-config/locust-master-deployment.yaml index f3afbc4..719bf2f 100644 --- a/kubernetes-config/locust-master-deployment.yaml +++ b/kubernetes-config/locust-master-deployment.yaml @@ -51,4 +51,11 @@ spec: protocol: TCP - name: loc-master-p2 containerPort: 5558 - protocol: TCP \ No newline at end of file + protocol: TCP + volumeMounts: + - name: locust-tasks-configuration + mountPath: /locust-tasks/script + volumes: + - name: locust-tasks-configuration + configMap: + name: locust-tasks-configuration \ No newline at end of file diff --git a/kubernetes-config/locust-worker-deployment.yaml b/kubernetes-config/locust-worker-deployment.yaml index 03bc3c7..a69e045 100644 --- a/kubernetes-config/locust-worker-deployment.yaml +++ b/kubernetes-config/locust-worker-deployment.yaml @@ -43,4 +43,11 @@ spec: - name: LOCUST_MASTER value: locust-master - name: TARGET_HOST - value: $targetHost \ No newline at end of file + value: $targetHost + volumeMounts: + - name: locust-tasks-configuration + mountPath: /locust-tasks/script + volumes: + - name: locust-tasks-configuration + configMap: + name: locust-tasks-configuration \ No newline at end of file From 25f1a069ccf5509a7ce64441b70f5b35e0a5c807 Mon Sep 17 00:00:00 2001 From: Irvi Firqotul Aini Date: Sat, 14 Apr 2018 14:49:37 +0700 Subject: [PATCH 3/6] using env variable in deployment --- README.md | 2 +- deploy.sh | 11 +++++++---- kubernetes-config/environment-variable.yaml | 6 ++++++ kubernetes-config/locust-master-deployment.yaml | 7 ++++--- kubernetes-config/locust-worker-deployment.yaml | 7 ++++--- 5 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 kubernetes-config/environment-variable.yaml diff --git a/README.md b/README.md index eec2db8..1aa926d 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ After a few minutes, you'll have a working Kubernetes cluster with three nodes ( Now that `kubectl` is setup, deploy the `kubernetes configuration`: - $ ./deploy.sh [PROJECT_ID] + $ ./deploy.sh [TARGET_HOST] [IMAGE_NAME] To confirm that the deployment and Pod are created, run the following: diff --git a/deploy.sh b/deploy.sh index fa43d15..e63697a 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,13 +1,16 @@ #!/bin/bash -xe -PROJECT_ID=$1 +IMAGE_NAME=$1 +TARGET_HOST=$2 -if [[ -z "${PROJECT_ID}" ]]; then +if [[ -z "${IMAGE_NAME}" && -z "${TARGET_HOST}" ]]; then exit 1 fi -sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-master-deployment.yaml -sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-worker-deployment.yaml +sed -i s/\$targetHost/${TARGET_HOST}/g kubernetes-config/environment-variable.yaml +sed -i s/\$imageName/${IMAGE_NAME}/g kubernetes-config/locust-master-deployment.yaml +sed -i s/\$imageName/${IMAGE_NAME}/g kubernetes-config/locust-worker-deployment.yaml +kubectl apply -f kubernetes-config --dry-run kubectl create configmap locust-tasks-configuration --from-file=config/tasks.py kubectl apply -f kubernetes-config \ No newline at end of file diff --git a/kubernetes-config/environment-variable.yaml b/kubernetes-config/environment-variable.yaml new file mode 100644 index 0000000..065440d --- /dev/null +++ b/kubernetes-config/environment-variable.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: environment-variable +data: + TARGET_HOST: $targetHost diff --git a/kubernetes-config/locust-master-deployment.yaml b/kubernetes-config/locust-master-deployment.yaml index 719bf2f..a133b20 100644 --- a/kubernetes-config/locust-master-deployment.yaml +++ b/kubernetes-config/locust-master-deployment.yaml @@ -34,14 +34,15 @@ spec: spec: containers: - name: locust - image: gcr.io/$projectId/locust-tasks:latest + image: $imageName command: [ "/bin/bash", "-c", "--" ] args: [ "/locust-tasks/run.sh" ] env: - name: LOCUST_MODE value: master - - name: TARGET_HOST - value: $targetHost + envFrom: + - configMapRef: + name: environment-variable ports: - name: loc-master-web containerPort: 8089 diff --git a/kubernetes-config/locust-worker-deployment.yaml b/kubernetes-config/locust-worker-deployment.yaml index a69e045..39535f7 100644 --- a/kubernetes-config/locust-worker-deployment.yaml +++ b/kubernetes-config/locust-worker-deployment.yaml @@ -34,7 +34,7 @@ spec: spec: containers: - name: locust - image: gcr.io/$projectId/locust-tasks:latest + image: $imageName command: [ "/bin/bash", "-c", "--" ] args: [ "/locust-tasks/run.sh" ] env: @@ -42,8 +42,9 @@ spec: value: worker - name: LOCUST_MASTER value: locust-master - - name: TARGET_HOST - value: $targetHost + envFrom: + - configMapRef: + name: environment-variable volumeMounts: - name: locust-tasks-configuration mountPath: /locust-tasks/script From c28a21d63562ec125f7c71ac5bedd1308c75408f Mon Sep 17 00:00:00 2001 From: Irvi Firqotul Aini Date: Sat, 14 Apr 2018 14:49:37 +0700 Subject: [PATCH 4/6] using env variable in deployment --- README.md | 2 +- deploy.sh | 11 +++++++---- kubernetes-config/environment-variable.yaml | 6 ++++++ kubernetes-config/locust-master-deployment.yaml | 7 ++++--- kubernetes-config/locust-worker-deployment.yaml | 7 ++++--- 5 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 kubernetes-config/environment-variable.yaml diff --git a/README.md b/README.md index eec2db8..1aa926d 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ After a few minutes, you'll have a working Kubernetes cluster with three nodes ( Now that `kubectl` is setup, deploy the `kubernetes configuration`: - $ ./deploy.sh [PROJECT_ID] + $ ./deploy.sh [TARGET_HOST] [IMAGE_NAME] To confirm that the deployment and Pod are created, run the following: diff --git a/deploy.sh b/deploy.sh index fa43d15..e63697a 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,13 +1,16 @@ #!/bin/bash -xe -PROJECT_ID=$1 +IMAGE_NAME=$1 +TARGET_HOST=$2 -if [[ -z "${PROJECT_ID}" ]]; then +if [[ -z "${IMAGE_NAME}" && -z "${TARGET_HOST}" ]]; then exit 1 fi -sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-master-deployment.yaml -sed -i "s/\$targetHost/http:\/\/${PROJECT_ID}.appspot.com/g;s/\$projectId/${PROJECT_ID}/g" kubernetes-config/locust-worker-deployment.yaml +sed -i s/\$targetHost/${TARGET_HOST}/g kubernetes-config/environment-variable.yaml +sed -i s/\$imageName/${IMAGE_NAME}/g kubernetes-config/locust-master-deployment.yaml +sed -i s/\$imageName/${IMAGE_NAME}/g kubernetes-config/locust-worker-deployment.yaml +kubectl apply -f kubernetes-config --dry-run kubectl create configmap locust-tasks-configuration --from-file=config/tasks.py kubectl apply -f kubernetes-config \ No newline at end of file diff --git a/kubernetes-config/environment-variable.yaml b/kubernetes-config/environment-variable.yaml new file mode 100644 index 0000000..065440d --- /dev/null +++ b/kubernetes-config/environment-variable.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: environment-variable +data: + TARGET_HOST: $targetHost diff --git a/kubernetes-config/locust-master-deployment.yaml b/kubernetes-config/locust-master-deployment.yaml index 719bf2f..a133b20 100644 --- a/kubernetes-config/locust-master-deployment.yaml +++ b/kubernetes-config/locust-master-deployment.yaml @@ -34,14 +34,15 @@ spec: spec: containers: - name: locust - image: gcr.io/$projectId/locust-tasks:latest + image: $imageName command: [ "/bin/bash", "-c", "--" ] args: [ "/locust-tasks/run.sh" ] env: - name: LOCUST_MODE value: master - - name: TARGET_HOST - value: $targetHost + envFrom: + - configMapRef: + name: environment-variable ports: - name: loc-master-web containerPort: 8089 diff --git a/kubernetes-config/locust-worker-deployment.yaml b/kubernetes-config/locust-worker-deployment.yaml index a69e045..39535f7 100644 --- a/kubernetes-config/locust-worker-deployment.yaml +++ b/kubernetes-config/locust-worker-deployment.yaml @@ -34,7 +34,7 @@ spec: spec: containers: - name: locust - image: gcr.io/$projectId/locust-tasks:latest + image: $imageName command: [ "/bin/bash", "-c", "--" ] args: [ "/locust-tasks/run.sh" ] env: @@ -42,8 +42,9 @@ spec: value: worker - name: LOCUST_MASTER value: locust-master - - name: TARGET_HOST - value: $targetHost + envFrom: + - configMapRef: + name: environment-variable volumeMounts: - name: locust-tasks-configuration mountPath: /locust-tasks/script From b551971de2c6926cc8e7b67e9cd716aa434766b6 Mon Sep 17 00:00:00 2001 From: Irvi Firqotul Aini Date: Sat, 28 Jul 2018 21:47:36 -0700 Subject: [PATCH 5/6] enable deploy using python script --- README.md | 17 +++++++-- .../environment-variable.yaml | 3 +- .../locust-master-deployment.yaml | 5 ++- .../locust-master-service.yaml | 1 + .../locust-worker-deployment.yaml | 5 ++- substitute.py | 38 +++++++++++++++++++ 6 files changed, 61 insertions(+), 8 deletions(-) rename {kubernetes-config => k8s}/environment-variable.yaml (60%) rename {kubernetes-config => k8s}/locust-master-deployment.yaml (94%) rename {kubernetes-config => k8s}/locust-master-service.yaml (97%) rename {kubernetes-config => k8s}/locust-worker-deployment.yaml (94%) create mode 100644 substitute.py diff --git a/README.md b/README.md index 1aa926d..896f19e 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,20 @@ After a few minutes, you'll have a working Kubernetes cluster with three nodes ( ### Deploy locust-master -Now that `kubectl` is setup, deploy the `kubernetes configuration`: - - $ ./deploy.sh [TARGET_HOST] [IMAGE_NAME] +Now that `kubectl` is setup, deploy the `k8s`. + +please add tasks.py on config folder and create the following configmap: + +``` +kubectl create configmap locust-tasks-configuration --from-file=config/tasks.py --namespace load-test +# image name will always the same +# just change the url +python substitute.py --project-id --image-name locust-tasks --image-tag --target-url +kubectl apply -f k8s/environment-variable.yaml +kubectl apply -f k8s/locust-master-deployment.yaml +kubectl apply -f k8s/locust-worker-deployment.yaml +kubectl apply -f k8s/locust-master-service.yaml +``` To confirm that the deployment and Pod are created, run the following: diff --git a/kubernetes-config/environment-variable.yaml b/k8s/environment-variable.yaml similarity index 60% rename from kubernetes-config/environment-variable.yaml rename to k8s/environment-variable.yaml index 065440d..9e53017 100644 --- a/kubernetes-config/environment-variable.yaml +++ b/k8s/environment-variable.yaml @@ -2,5 +2,6 @@ apiVersion: v1 kind: ConfigMap metadata: name: environment-variable + namespace: load-test data: - TARGET_HOST: $targetHost + TARGET_HOST: $targetUrl diff --git a/kubernetes-config/locust-master-deployment.yaml b/k8s/locust-master-deployment.yaml similarity index 94% rename from kubernetes-config/locust-master-deployment.yaml rename to k8s/locust-master-deployment.yaml index a133b20..e1b8a93 100644 --- a/kubernetes-config/locust-master-deployment.yaml +++ b/k8s/locust-master-deployment.yaml @@ -17,6 +17,7 @@ apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1. For version 1. kind: Deployment metadata: name: locust-master + namespace: load-test labels: name: locust role: master @@ -34,7 +35,7 @@ spec: spec: containers: - name: locust - image: $imageName + image: $appImage command: [ "/bin/bash", "-c", "--" ] args: [ "/locust-tasks/run.sh" ] env: @@ -59,4 +60,4 @@ spec: volumes: - name: locust-tasks-configuration configMap: - name: locust-tasks-configuration \ No newline at end of file + name: locust-tasks-configuration diff --git a/kubernetes-config/locust-master-service.yaml b/k8s/locust-master-service.yaml similarity index 97% rename from kubernetes-config/locust-master-service.yaml rename to k8s/locust-master-service.yaml index 033460b..27f3506 100644 --- a/kubernetes-config/locust-master-service.yaml +++ b/k8s/locust-master-service.yaml @@ -17,6 +17,7 @@ kind: Service apiVersion: v1 metadata: name: locust-master + namespace: load-test labels: name: locust role: master diff --git a/kubernetes-config/locust-worker-deployment.yaml b/k8s/locust-worker-deployment.yaml similarity index 94% rename from kubernetes-config/locust-worker-deployment.yaml rename to k8s/locust-worker-deployment.yaml index 39535f7..d91fc21 100644 --- a/kubernetes-config/locust-worker-deployment.yaml +++ b/k8s/locust-worker-deployment.yaml @@ -17,6 +17,7 @@ apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1. For version 1. kind: Deployment metadata: name: locust-worker + namespace: load-test labels: name: locust role: worker @@ -34,7 +35,7 @@ spec: spec: containers: - name: locust - image: $imageName + image: $appImage command: [ "/bin/bash", "-c", "--" ] args: [ "/locust-tasks/run.sh" ] env: @@ -51,4 +52,4 @@ spec: volumes: - name: locust-tasks-configuration configMap: - name: locust-tasks-configuration \ No newline at end of file + name: locust-tasks-configuration diff --git a/substitute.py b/substitute.py new file mode 100644 index 0000000..de0dc4a --- /dev/null +++ b/substitute.py @@ -0,0 +1,38 @@ +import argparse + + +def replace_in_file(filename, target, replacement): + with open(filename) as f: + lines = [line.rstrip() for line in f.readlines()] + + with open(filename, 'w+') as f: + for line in lines: + f.write(line.replace(target, replacement) + '\n') + +def change_target_url(target_url): + replace_in_file('k8s/environment-variable.yaml', '$targetUrl', + target_url) + +def change_image(project_id, image_name, image_tag): + replace_in_file('k8s/locust-master-deployment.yaml', '$appImage', + 'gcr.io/{project_id}/{image_name}:{image_tag}'.format( + project_id=project_id, + image_name=image_name, + image_tag=image_tag)) + replace_in_file('k8s/locust-worker-deployment.yaml', '$appImage', + 'gcr.io/{project_id}/{image_name}:{image_tag}'.format( + project_id=project_id, + image_name=image_name, + image_tag=image_tag)) + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("--project-id") + parser.add_argument("--image-name") + parser.add_argument("--image-tag") + parser.add_argument("--target-url") + result = parser.parse_args() + + change_image(result.project_id, result.image_name, + result.image_tag) + change_target_url(result.target_url) From fd04621568ef963c7380f1d4c267777c5b7515af Mon Sep 17 00:00:00 2001 From: Irvi Firqotul Aini Date: Thu, 4 Oct 2018 21:47:25 +0700 Subject: [PATCH 6/6] remove unecessary script --- deploy.sh | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100755 deploy.sh diff --git a/deploy.sh b/deploy.sh deleted file mode 100755 index e63697a..0000000 --- a/deploy.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -xe - -IMAGE_NAME=$1 -TARGET_HOST=$2 - -if [[ -z "${IMAGE_NAME}" && -z "${TARGET_HOST}" ]]; then - exit 1 -fi - -sed -i s/\$targetHost/${TARGET_HOST}/g kubernetes-config/environment-variable.yaml -sed -i s/\$imageName/${IMAGE_NAME}/g kubernetes-config/locust-master-deployment.yaml -sed -i s/\$imageName/${IMAGE_NAME}/g kubernetes-config/locust-worker-deployment.yaml - -kubectl apply -f kubernetes-config --dry-run -kubectl create configmap locust-tasks-configuration --from-file=config/tasks.py -kubectl apply -f kubernetes-config \ No newline at end of file