注册运维操作

通过 Operation 自定义资源注册运维操作。

现在让我们将诊断操作注册到集群中。

撰写一个简单的诊断操作

下列是一个通过 HTTP 服务器的形式提供运维操作的简单示例:

  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "time"
  10. "github.com/gorilla/mux"
  11. )
  12. var (
  13. cache = map[string]string{
  14. "a": "1",
  15. "b": "2",
  16. "c": "3",
  17. "d": "4",
  18. }
  19. )
  20. func main() {
  21. address := flag.String("address", "0.0.0.0", "The address on which to advertise.")
  22. port := flag.String("port", "8000", "The port to serve on.")
  23. flag.Parse()
  24. r := mux.NewRouter()
  25. r.HandleFunc("/", handler)
  26. srv := &http.Server{
  27. Handler: r,
  28. Addr: fmt.Sprintf("%s:%s", *address, *port),
  29. WriteTimeout: 30 * time.Second,
  30. ReadTimeout: 30 * time.Second,
  31. }
  32. log.Fatal(srv.ListenAndServe())
  33. }
  34. func handler(w http.ResponseWriter, r *http.Request) {
  35. switch r.Method {
  36. case "POST":
  37. // Parse the request payload into a map[string]string.
  38. body, err := ioutil.ReadAll(r.Body)
  39. if err != nil {
  40. http.Error(w, err.Error(), http.StatusBadRequest)
  41. return
  42. }
  43. parameters := make(map[string]string)
  44. err = json.Unmarshal(body, &parameters)
  45. if err != nil {
  46. http.Error(w, err.Error(), http.StatusBadRequest)
  47. return
  48. }
  49. // Update cache with parameters.
  50. for key, value := range parameters {
  51. log.Printf("Update cache with %s:%s", key, value)
  52. cache[key] = value
  53. }
  54. data, err := json.Marshal(cache)
  55. if err != nil {
  56. http.Error(w, err.Error(), http.StatusInternalServerError)
  57. return
  58. }
  59. // Response with cache.
  60. w.Header().Set("Content-Type", "application/json")
  61. w.Write(data)
  62. default:
  63. http.Error(w, fmt.Sprintf("method %s is not supported", r.Method), http.StatusMethodNotAllowed)
  64. }
  65. }

将诊断程序部署到集群中

通过下列 YAML 文件您可以将该程序部署到 Kubernetes 集群中:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: http-operation
  5. namespace: kubediag
  6. labels:
  7. operation: cache
  8. type: http
  9. spec:
  10. containers:
  11. - name: http-operation
  12. image: hub.c.163.com/kubediag/http-operation:0.2.0
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: http-operation
  5. namespace: kubediag
  6. spec:
  7. selector:
  8. operation: cache
  9. type: http
  10. ports:
  11. - name: http
  12. port: 80
  13. targetPort: 80

创建运行该程序的 Pod 以及用于访问的 Service:

  1. kubectl apply -f https://raw.githubusercontent.com/kubediag/kubediag/master/samples/http-operation/manifests/pod.yaml
  2. kubectl apply -f https://raw.githubusercontent.com/kubediag/kubediag/master/samples/http-operation/manifests/service.yaml

该程序启动了一个 HTTP 服务器并初始化了内容如下的缓存:

  1. {
  2. "a":"1",
  3. "b":"2",
  4. "c":"3",
  5. "d":"4"
  6. }

查看该程序是否部署成功:

  1. $ kubectl get -n kubediag pod http-operation
  2. NAME READY STATUS RESTARTS AGE
  3. http-operation 1/1 Running 0 4s
  4. $ kubectl get -n kubediag service http-operation
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. http-operation ClusterIP 10.96.73.28 <none> 80/TCP 18s

通过 HTTP 请求可以增加或修改缓存中的键值对,HTTP 服务器会返回修改后缓存中的所有数据:

  1. $ curl -X POST --data '{"a":"100","e":"5"}' http://10.96.73.28:80
  2. {"a":"100","b":"2","c":"3","d":"4","e":"5"}

将诊断操作注册到 KubeDiag 中

Operation 中定义了诊断操作的详细信息,通过创建下列 Operation 可以将示例中的操作注册到 KubeDiag 中:

  1. apiVersion: diagnosis.kubediag.org/v1
  2. kind: Operation
  3. metadata:
  4. name: http-operation
  5. spec:
  6. processor:
  7. externalIP: http-operation.kubediag.svc.cluster.local
  8. externalPort: 80
  9. path: /
  10. scheme: http
  11. timeoutSeconds: 30

创建用于注册诊断操作的 Operation:

  1. kubectl apply -f https://raw.githubusercontent.com/kubediag/kubediag/master/samples/http-operation/manifests/operation.yaml

最后修改 July 14, 2021 : Update index doc and some yaml path (15a2a0b)