这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
// GetObjByObjKey b. 根据对象的key返回(演示Store.Get方法)
func GetObjByObjKey(c *gin.Context) {
rawObj, exists, err := INDEXER.GetByKey(ObjKey(c))
if err != nil {
c.String(500, fmt.Sprintf("b. get pod failed, %v", err))
} else if !exists {
c.String(500, fmt.Sprintf("b. get empty pod, %v", err))
} else {
if v, ok := rawObj.(*v1.Pod); ok {
c.JSON(200, v)
} else {
c.String(500, "b. convert interface to pod failed")
}
}
}
// 用于提供基本功能的路由组
basicGroup := r.Group("/basic")
// a. 查询指定语言的所有对象的key(演示2. IndexKeys方法)
basicGroup.GET("get_obj_keys_by_language_name", basic.GetObjKeysByLanguageName)
// b. 返回对象的key,返回对应的对象(演示Store.GetByKey方法)
basicGroup.GET("get_obj_by_obj_key", basic.GetObjByObjKey)
var ClientSet *kubernetes.Clientset
var once sync.Once
func initIndexer() {
log.Println("开始初始化Indexer")
var kubeconfig *string
// 试图取到当前账号的家目录
if home := homedir.HomeDir(); home != "" {
// 如果能取到,就把家目录下的.kube/config作为默认配置文件
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
// 如果取不到,就没有默认配置文件,必须通过kubeconfig参数来指定
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
// 加载配置文件
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// 用clientset类来执行后续的查询操作
ClientSet, err = kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
...
// GetObjByObjKey 远程请求,根据指定key查询pod对象
func GetObjByObjKey(c *gin.Context) {
rawObj, err := basic.ClientSet.
CoreV1().
Pods(basic.NAMESPACE).
Get(c, c.DefaultQuery("pod_name", ""), metav1.GetOptions{})
if err != nil {
c.String(500, fmt.Sprintf("g. get pod failed, %v", err))
} else {
c.JSON(200, rawObj)
}
}
remoteGroup := r.Group("/remote")
// g. 使用clientset远程查询
remoteGroup.GET("get_obj_by_obj_key_remote_query", remote.GetObjByObjKey)
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const res = http.get(`http://${__ENV.MY_HOSTNAME}/remote/get_obj_by_obj_key_remote_query?pod_name=${__ENV.POD_NAME}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
}
docker run \
--rm \
-i \
loadimpact/k6 \
run \
--duration 60s \
--vus 10 \
-e MY_HOSTNAME=192.168.50.76:18080 \
-e POD_NAME=nginx-deployment-696cc4bc86-2rqcg \
- < remote.js
docker run \ // docker运行容器
--rm \ // 等当前控制台结束时删除该容器(相当于一次性任务)
-i \ // 保持STDIN打开
loadimpact/k6 \ // 镜像名
run \ // 容器中执行的命令,即启动k6的命令
--duration 60s \ // k6的参数:压测时长60秒
--vus 10 \ // k6的参数:并发数为10
-e MY_HOSTNAME=192.168.50.76:18080 \ // remote.js脚本中用到的参数,压测服务的IP和端口
-e POD_NAME=nginx-deployment-696cc4bc86-2rqcg \ // remote.js脚本中用到的参数,pod名称
- < remote.js // k6压测脚本名称
✓ is status 200
✓ body size is > 0
checks.........................: 100.00% ✓ 638 ✗ 0
data_received..................: 1.5 MB 25 kB/s
data_sent......................: 53 kB 857 B/s
http_req_blocked...............: avg=119.67µs min=927ns med=2.63µs max=4.2ms p(90)=4.96µs p(95)=7.16µs
http_req_connecting............: avg=115.35µs min=0s med=0s max=4.13ms p(90)=0s p(95)=0s
http_req_duration..............: avg=1.9s min=15.62ms med=1.99s max=2.11s p(90)=2.01s p(95)=2.03s
{ expected_response:true }...: avg=1.9s min=15.62ms med=1.99s max=2.11s p(90)=2.01s p(95)=2.03s
http_req_failed................: 0.00% ✓ 0 ✗ 319
http_req_receiving.............: avg=244.4µs min=23.86µs med=115.38µs max=8.72ms p(90)=343.48µs p(95)=674.31µs
http_req_sending...............: avg=18.34µs min=4.59µs med=12.75µs max=209.44µs p(90)=25.43µs p(95)=35.07µs
http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...............: avg=1.9s min=15.51ms med=1.99s max=2.11s p(90)=2.01s p(95)=2.03s
http_reqs......................: 319 5.159646/s
iteration_duration.............: avg=1.9s min=19.58ms med=1.99s max=2.11s p(90)=2.01s p(95)=2.03s
iterations.....................: 319 5.159646/s
vus............................: 5 min=5 max=10
vus_max........................: 10 min=10 max=10
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const res = http.get(`http://${__ENV.MY_HOSTNAME}/basic/get_obj_by_obj_key?obj_key=${__ENV.OBJ_KEY}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
}
docker run \
--rm \
-i \
loadimpact/k6 \
run \
--duration 60s \
--vus 10 \
-e MY_HOSTNAME=192.168.50.76:18080 \
-e OBJ_KEY=indexer-tutorials/nginx-deployment-696cc4bc86-2rqcg \
- < indexer.js
✓ is status 200
✓ body size is > 0
checks.........................: 100.00% ✓ 119236 ✗ 0
data_received..................: 288 MB 4.8 MB/s
data_sent......................: 10 MB 168 kB/s
http_req_blocked...............: avg=4.67µs min=343ns med=3.13µs max=4.81ms p(90)=5.55µs p(95)=6.56µs
http_req_connecting............: avg=633ns min=0s med=0s max=4.4ms p(90)=0s p(95)=0s
http_req_duration..............: avg=9.87ms min=3.15ms med=9.03ms max=238.05ms p(90)=13.56ms p(95)=15.55ms
{ expected_response:true }...: avg=9.87ms min=3.15ms med=9.03ms max=238.05ms p(90)=13.56ms p(95)=15.55ms
http_req_failed................: 0.00% ✓ 0 ✗ 59618
http_req_receiving.............: avg=321.15µs min=8.16µs med=70.93µs max=62.14ms p(90)=478.85µs p(95)=2.07ms
http_req_sending...............: avg=19.72µs min=2.29µs med=14.48µs max=2.82ms p(90)=25.42µs p(95)=37.31µs
http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...............: avg=9.53ms min=3.07ms med=8.69ms max=237.72ms p(90)=13.13ms p(95)=15.04ms
http_reqs......................: 59618 993.440775/s
iteration_duration.............: avg=10.05ms min=3.24ms med=9.21ms max=238.67ms p(90)=13.75ms p(95)=15.73ms
iterations.....................: 59618 993.440775/s
vus............................: 10 min=10 max=10
vus_max........................: 10 min=10 max=10