k8s e2e的一些測試鏡像(一)

測試鏡像的代碼和DockerFile通常位於kubernetes/test/imagesnode

1.serve_hostname

路徑:gcr.io/google_containers/serve_hostname:v1.4
功能:經過flag控制使用tcp、udp、http模式返回hostname mysql

使用
使用docker命令的方式sql

docker run  -p 9376:9376 gcr.io/google_containers/serve_hostname:v1.4 -tcp=true -udp=false -http=false
 

TCPdocker

# telnet 127.0.0.1 9376 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. ff640c2df975 Connection closed by foreign host.

這個是TCP短鏈接,長鏈接的測試能夠找個mysql的鏡像來完成api

 

 

HTTPcurl

# curl 127.0.0.1:9376 ff640c2df975 root@opama-HP-EliteBook-8560w:/home/opama/workspace/k8s/src/k8s.io/kubernetes# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ff640c2df975 gcr.io/google_containers/serve_hostname:v1.4   "/serve_hostname -tcp"   15 minutes ago      Up 15 minutes       0.0.0.0:9376->9376/tcp   modest_kowalevski


UDP:
啓動命令須要改變一下socket

docker run  -p 9376:9376/udp gcr.io/google_containers/serve_hostname:v1.4 -tcp=false -udp=true -http=false    
 

本身用go簡單寫個clientudp_client.gotcp

package main import ( "fmt"
    "net"
    "os" ) func main() { // 獲取輸入
    input := os.Args[1] // 建立鏈接
    socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{ IP: net.IPv4(127, 0, 0, 1),//modify
        Port: 9376,//modify
 }) if err != nil { fmt.Println("鏈接失敗!", err) return } defer socket.Close() // 發送數據
    fmt.Printf("發送數據:%s \n", input) senddata := []byte(input) _, err = socket.Write(senddata) if err != nil { fmt.Println("發送數據失敗!", err) return } // 接收數據
    data := make([]byte, 4096) read, remoteAddr, err := socket.ReadFromUDP(data) if err != nil { fmt.Println("讀取數據失敗!", err) return } fmt.Println(read, remoteAddr) fmt.Printf("%s\n", data) }
 

執行udp發包:測試

# go run udp_client.go 13 127.0.0.1:9376 3a323826364f

ui

go build -ldflags "-s -w" ./udp_client 11111
 

YAML樣例

apiVersion: extensions/v1beta1 kind: Deployment metadata: name: servehostname-deployment labels: name: servehostname spec: replicas: 1 selector: matchLabels: name: servehostname template: metadata: labels: name: servehostname spec: containers: - image: gcr.io/google_containers/serve_hostname:v1.4 args: ["-tcp=true","-http=false","-udp=false"] imagePullPolicy: IfNotPresent name: servehostname ports: - containerPort: 9376 protocol: TCP resources: {} terminationMessagePath: /dev/termination-log
 

若想使用env的方式,能夠修改原有serve_hostname.go文件

func getEnvValueBool(name string,orin bool) bool{ if  os.Getenv(name) == "true" { return true } if  os.Getenv(name) == "false" { return false } return orin } func getEnvValueInt(name string,orin int) int{ if  os.Getenv(name) != "" { value, _ := strconv.Atoi(os.Getenv(name)) return value } return orin } func main() { flag.Parse() *doTCP = getEnvValueBool("mytcp",*doTCP) *doUDP = getEnvValueBool("myudp",*doUDP) *doHTTP = getEnvValueBool("myhttp",*doHTTP) *port = getEnvValueInt("myport",*port

 

 

2.clusterapi-tester


路徑:gcr.io/google_containers/clusterapi-tester:1.0

 

功能:在pod裏使用serviceAccount訪問apiserver,主要邏輯:

1.從pod的環境變量裏獲取KUBERNETES_SERVICE_HOSTKUBERNETES_SERVICE_PORT
2.獲取/var/run/secrets/kubernetes.io/serviceaccount/下的token
3.帶token訪問https://host+port
4.去作list node,list service等操做,訪問成功則起一個8080端口,設置的健康檢查成功,失敗則健康檢查失敗

 

使用

YAML樣例

apiVersion: v1
kind: Pod
metadata:
  name: clusterapi-tester
spec:
  containers:
  - image: gcr.io/google_containers/clusterapi-tester:1.0
    name: clusterapi-tester
    readinessProbe:
      httpGet:
        path: /healthz
        port: 8080
        scheme: HTTP
      initialDelaySeconds: 10
      timeoutSeconds: 5
      failureThreshold: 3
      periodSeconds: 10
      successThreshold: 1
  restartPolicy: OnFailure

 

實際使用時可去容器裏查看env,這裏local起的訪問不通KUBERNETES_PORT

# docker exec -it fd0eb9f6d3a6 /bin/sh
/ # env
KUBERNETES_PORT=tcp://10.0.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=clusterapi-tester
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
PWD=/
KUBERNETES_SERVICE_HOST=10.0.0.1
/ # ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1): 56 data bytes

訪問失敗,pod重啓

  6m    30s     6       {kubelet 127.0.0.1}     spec.containers{clusterapi-tester}      Normal  Pulled          Container image "gcr.io/google_containers/clusterapi-tester:1.0" already present on machine
  30s   30s     1       {kubelet 127.0.0.1}     spec.containers{clusterapi-tester}      Normal  Created         Created container with docker id aa9a0151abd8
  30s   30s     1       {kubelet 127.0.0.1}     spec.containers{clusterapi-tester}      Normal  Started         Started container with docker id aa9a0151abd8
  5m    7s      13      {kubelet 127.0.0.1}     spec.containers{clusterapi-tester}      Warning Unhealthy       Readiness probe failed: Get http://172.17.0.2:8080/healthz: dial tcp 172.17.0.2:8080: getsockopt: connection refused

對於serviceAccount的使用能夠見http://www.jianshu.com/p/415c5fc6ddcf

相關文章
相關標籤/搜索