Kubernetes replica Testing

Here are some useful references
https://cloud.google.com/kubernetes-engine/docs/quickstart
https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app
https://kubernetes.io/blog/2016/07/autoscaling-in-kubernetes/

if you happen to share a project billing account with your friend, and you see error of no permission creating a bucket when you first push your image, do gcloud auth login

So testing effects of kubernetes replicas on running time
I am using google’s demo code

// [START all]
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func main() {
	// register hello function to handle all requests
	mux := http.NewServeMux()
	mux.HandleFunc("/", hello)

	// use PORT environment variable, or default to 8080
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	// start the web server on port and accept requests
	log.Printf("Server listening on port %s", port)
	log.Fatal(http.ListenAndServe(":"+port, mux))
}

// hello responds to the request with a plain-text "Hello, world" message.
func hello(w http.ResponseWriter, r *http.Request) {
	log.Printf("Serving request: %s", r.URL.Path)
	host, _ := os.Hostname()
	fmt.Fprintf(w, "Hello, world!\n")
	fmt.Fprintf(w, "Version: 1.0.0\n")
	fmt.Fprintf(w, "Hostname: %s\n", host)

  sum := 0
  for i := 0; i < 10000000000; i++ {
      sum += i
  }   
  fmt.Println("hello bobo")

}

// [END all]
# build the image and push it
docker build -t gcr.io/fuckctc/hello-app:v3
docker push gcr.io/fuckctc/hello-app:v3
# now create the kubenetes service
# the code runs 30s
kubectl create deployment hello-server3 --image=gcr.io/fuckctc/hello-app:v3
kubectl expose deployment hello-server3 --type LoadBalancer   --port 80 --target-port 8080

and we have our testing code

import time
from multiprocessing import Pool
import requests
def helper(url):
    return requests.get(url)

def main():
    t0 = time.time()
    pool = Pool(7)
    ### replace the ip with your service ip
    pool.map(helper, ['http://34.66.86.134/']*7)
    print(time.time() - t0)


if __name__ == "__main__":
    main()
# the code runs 17s
kubectl scale deployment hello-server2 --replicas=3

as I only have 2 nodes in the cluster, the running time reduce to ~17s
one caveat here is, the testing code has to be computing expensive, if you use sleep function, the server may simply get around with thread. which will be difficult to test