k8s e2e的一些测试镜像(一)

测试镜像的代码和DockerFile一般位于kubernetes/test/images

1.serve_hostname

路径:gcr.io/google_containers/serve_hostname:v1.4
功能:通过flag控制使用tcp、udp、http模式返回hostname

使用
使用docker命令的方式

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

TCP

# 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的镜像来完成

HTTP

# 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:
启动命令需要改变一下

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

自己用go简单写个clientudp_client.go

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 
", 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
", data)
}
 

执行udp发包:

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

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

原文地址:https://www.cnblogs.com/opama/p/6012855.html