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

Docker create and push image

  1. create a dockerhub account and create repository
    here I call it “backend-server”
sudo docker run -it -d debian --name backend-server /bin/bash 
### setup the container ####
### sudo docker commit <containerID> <dockerhub repository>[:tag]
sudo docker commit backend-server tbjc1magic/backend-server

sudo docker login docker.io
### enter your dockerhub name and passwd ###

sudo docker push tbjc1magic/backend-server


FB phone screen

Welcome to Facebook!

This is just a simple shared plaintext pad, with no execution capabilities.

When you know what language you would like to use for your interview,
simply choose it from the dropdown in the top bar.

Enjoy your interview!

def main():
    print 123
    
# input: 5->6->3 8->4->2
# output: 1->4->0->5

class LinkNode:
    def __init__(self, v):
        self.val =  v
        self.next =  None

# convert linked list to a number
# convert the result to a linked list




def solve(L1, L2):
    
    
    def linkedlist2number(h):
        
        res = 0
        while h:
            res = res*10 + h.val
            h = h.next
            
        return res
        
    def number2linkedlist(n):
        
        dummy = h = LinkNode(None)
        s = str(n)
        L = len(s)
        i = 0
        while i<L:
            h.next = LinkNode(int(s[i])) 
            h = h.next
            i += 1
            
        return dummy.next
    
    n1 = linkedlist2number(L1)
    n2 = linkedlist2number(L2)
    res = number2linkedlist(n1+n2)
    
    return res
    
            
# input: 5->6->3 8->4->2
# output: 1->4->0->5

# linkedlist2number 
# cycle 1
res = 0
h - > 5
res = res*10 + h.val = 5

# cycle 2
res = 5
h -> 6
----
res = 5*10 + 6 = 56

# cycle 3
res = 56
h -> 3
---
res = 56*10 +3 = 563

return 563

# 842
n = 1405

# number2linkedlist
s = str(n) = '1405'
L = len(n) = 4

i = 0..3
dummy = ListNode(None)
h = dummy

# i = 0
node = LinkNode(int(s[i]))
h.next = node
h = h.next

dummy -> 1(head) -> None

# i = 1
int(s[i=1]) = 4
node = ListNode(4)
h.next = node
h = h.next

dummy -> 1 -> 4 (head) -> None
        
....

dummy -> 1 -> 4 -> 0 -> 5 -> None
return dummy.next


aba, abba abax
        
# input: str
# output: bool
        
abax
l, r = 0, len(s)-1 
s[l] == s[r]

if not same:
    s1 = s[:l] + s[l+1:]
    s2 = s[:r] + s[r+1:]
    
    return s1[::-1] == s1 or s2[::-1] == s2



def isPalindromeWith1Deletion(s):
    
    ## O(n)
    l, r = 0, len(s) - 1
    
    while l<r:
        
        if s[l] != s[r]:
            s1 = s[:l] + s[l+1:] # O(n)
            s2 = s[:r] + s[r+1:] # O(n)
            return s1[::-1] == s1 or s2[::-1] == s2 # 2 * O(n) 
        l, r = l+1, r-1
        
    return True

# abax
l = 0 (a), r = 3 (x)

s1 = 'bax'
s2 = 'aba' # True

# abxax
l = 0 (a), r = 4 (x)

s1 = 'bxax' # False
s2 = 'abxa' # False

return False


    
def isPalindromeWith1Deletion(s):
    
    
    ## o(n*n)
    L = len(s)
    for i in range(i):
        r = s[:i] + s[i+1:]
        if r == r[::-1]:
            return True
    
    return False
    
        
        
        
        
    
    

citadel onsite

  1. behavior question by the head
    if a business team comes to you with an urgent request, and you also have your periodic assignment to turn in by the end of the week. How would you handle that?

2. implement binary searcch

3. set vs. list vs. tuple vs. dict in access time and insert time

give you two pandas dataframe, how would you write a reporting function, find the missing key-combo and the key-combo with mismatching values

df1 = pd.DataFrame(data={'k1':[1,2,3], 'v1':[1,2,3]})  
df2 = pd.DataFrame(data={'k1':[2,3,4], 'v1':[2,3.5,4]}) 

def report(df1, df2):
    r1 = pd.melt(df1, id_vars=['k1'], value_vars=['v1'])
    r2 = pd.melt(df2, id_vars=['k1'], value_vars=['v1'])

    res = pd.merge(r1,r2,how='outer', on='k1', indicator=True)
    res['_merge'] = res['_merge'].astype(str)
    mask_mis = res['value_x'] != res['value_y']
    mask_both = res['_merge'] == 'both'
    res.loc[mask_mis&amp;mask_both, '_merge'] = 'mismatch'
    return res

4. how do you think of hedge fund? how do you compare it with gambling?
3 stocks form a portfolio, find the STD/risk of the portfolio using historical data

setup https for wordpress with docker

1. install docker-ce

https://docs.docker.com/install/linux/docker-ce/debian/

2. install docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

3. setup domain name, and link it to the node you are setting up your web server; install letsencrypt on the webserver

git clone https://github.com/certbot/certbot.git
cd certbot
sudo -H ./letsencrypt-auto certonly --standalone -d jianping-lai.com -d www.jianping-lai.com --email tbjc1magic@gmail.com
https://www.linode.com/docs/security/ssl/install-lets-encrypt-to-create-ssl-certificates/

4. install wordpress using docker with the docker-compose.yml below
https://docs.docker.com/compose/wordpress/; and do “sudo docker-compose up -d”

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "80:80"
       - "443:443"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}

5. after start the wordpress docker container,

mkdir -p blog/etc sudo docker cp wordpress_wordpress_1:/etc/apache2 ~/blog/etc/

6. login to the wordpress docker container and do following

a2enmod ssl
service apache2 restart

7. shutdown the container and put the letsencrypt key and certificate combo to the ~/blog/etc/apache2 folder

cd ~/blog/etc/apache2/sites-enabled
sudo cp /etc/letsencrypt/live/jianping-lai.com/*.pem .
sudo ln -s ../sites-available/default-ssl.conf .
# modify the following two lines of default-ssl.conf
SSLCertificateFile      /etc/apache2/sites-enable/fullchain.pem
SSLCertificateKeyFile /etc/apache2/sites-enabled/privkey.pem
cd ~/blog/etc/apache2/mods-enabled
sudo ln -s ../mods-available/socache_shmcb.load .

8. modify the docker-compose.yml file add the following two lines

volumes:
    - /home/tbjc1magic/blog/etc/apache2:/etc/apache2

9. start docker containers again and turn on the ssl redirect plugin

sudo docker-compose up -d