k8s sidecar开发-webhook开发

7 篇文章 0 订阅

1.首先需要申请一个secret,用来在进行webhook的时候apiserver访问我们的webhook服务器的时候进行证书认证。

[ -z ${service} ] && service=logcar-service
[ -z ${secret} ] && secret=logcar-secret
[ -z ${namespace} ] && namespace=kube-system

if [ ! -x "$(command -v openssl)" ]; then
    echo "openssl not found"
    exit 1
fi

csrName=${service}.${namespace}
tmpdir=$(mktemp -d)
echo "creating certs in tmpdir ${tmpdir} "

cat <<EOF >> ${tmpdir}/csr.conf
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn

[ dn ]
C = CN
ST = HangZhou
L = LA
O = Personal
OU = King
CN = ${service}.${namespace}.svc

[ req_ext ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = ${service}.${namespace}.svc

[ v3_ext ]
authorityKeyIdentifier=keyid,issuer:always
basicConstraints=CA:FALSE
keyUsage=keyEncipherment,dataEncipherment
extendedKeyUsage=serverAuth,clientAuth
subjectAltName=@alt_names
EOF

openssl req -x509 -nodes -new -sha256 -days 3650 -newkey rsa:2048 -subj "/CN=${service}.${namespace}.svc" \
  -keyout ${tmpdir}/ca.key \
  -out ${tmpdir}/ca.crt
openssl genrsa -out ${tmpdir}/server.key 2048
openssl req -new -key ${tmpdir}/server.key -out ${tmpdir}/server.csr -config ${tmpdir}/csr.conf
openssl x509 -req -in ${tmpdir}/server.csr -CA ${tmpdir}/ca.crt -CAkey ${tmpdir}/ca.key \
  -CAcreateserial -out ${tmpdir}/server.crt -days 3650 \
  -extensions v3_ext -extfile ${tmpdir}/csr.conf

kubectl create secret generic ${secret} -n ${namespace} \
  --from-file=${tmpdir}/ca.crt \
  --from-file=${tmpdir}/server.key \
  --from-file=${tmpdir}/server.crt

2.接着是一些webhook需要使用到的serviceAccount,rbac和service。

apiVersion: v1
kind: Service
metadata:
  name: logcar-service
  namespace: kube-system
spec:
  selector:
    app: webhook
  ports:
    - port: 18090
      targetPort: 18090
  type: NodePort
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sidecar-sa
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: sidecar-role
rules:
  - apiGroups: [""]
    resources: ["pods","nodes","configmaps","secret","serviceaccounts"]
    verbs: ["get","list","watch","update","create"]
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get","list","watch","update","create"]
  - apiGroups: ["batch"]
    resources: ["job"]
    verbs: ["get","list","watch","update","create"]
  - apiGroups: ["rbac.authorization.k8s.io"]
    resources: ["roles","rolebindings"]
    verbs: ["get","list","watch","update","create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: sidecar-bind
subjects:
  - kind: ServiceAccount
    name: sidecar-sa
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: sidecar-role
  apiGroup: rbac.authorization.k8s.io

3.接着就可以添加webhook了。

apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
  name: webhook-config
  namespace: kube-system
  labels:
    app: webhook
webhooks:
  - name: ${service}.${namespace}.svc
    clientConfig:
      service:
        name: logcar-service
        namespace: kube-system
        path: "/sidecar-mutating"
        port: 18090
      caBundle: $(kubectl get secret ${secret} -n ${namespace} -o jsonpath='{.data.ca\.crt}')
    rules:
      - apiGroups: [ "apps" ]
        apiVersions: [ "v1" ]
        operations: [ "CREATE","UPDATE" ]
        resources: [ "deployments" ]
        scope: "*"
      - apiGroups: [ "" ]
        apiVersions: [ "v1" ]
        operations: [ "CREATE","UPDATE" ]
        resources: [ "pods" ]
        scope: "*"
      - apiGroups: [ "batch" ]
        apiVersions: [ "v1" ]
        operations: [ "CREATE","UPDATE" ]
        resources: [ "jobs" ]
        scope: "*"
    admissionReviewVersions: ["v1", "v1beta1"]
    sideEffects: None
    timeoutSeconds: 10

4.接着编写webhook的代码。

func (l *LogCarWebhook) Run() {
	mux := http.NewServeMux()
    //只要把webhookconfig里面的地址写进去就可以
	mux.HandleFunc("/sidecar-mutating", l.Inject)

	fmt.Println("sidecar running in https:0.0.0.0:18090")
	err := http.ListenAndServeTLS(":18090", l.config.CertFile, l.config.KeyFile, mux)
	if err != nil {
		panic(err)
	}
}

 5.最后把代码部署上去就可以了。需要注意这里要把前面的secret mount到pod里面,https服务起来的时候要使用。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: logcar-webhook
  namespace: kube-system
  labels:
    app: webhook
spec:
  replicas: 1
  template:
    metadata:
      name: logcar-pod
      labels:
        app: webhook
    spec:
      containers:
        - name: webhook-app
          image: yh960124/logcarwebhook
          imagePullPolicy: Always
          volumeMounts:
            - name: webhook-certs
              mountPath: /etc/webhook/certs
              readOnly: true
          ports:
            - containerPort: 18090
              protocol: TCP
      volumes:
        - name: webhook-certs
          secret:
              secretName: ${secret}
      serviceAccountName: sidecar-sa
  selector:
    matchLabels:
      app: webhook

点击查看所有代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
prometheus-webhook-dingtalk 是一个用于将 Prometheus 监控告警消息发送到钉钉的 Webhook 工具。它可以帮助你将 Prometheus 监控告警通过钉钉机器人发送到指定的群组或用户。 你可以通过以下步骤来配置和使用 prometheus-webhook-dingtalk: 1. 安装 prometheus-webhook-dingtalk:你可以使用 Go 工具链来安装 prometheus-webhook-dingtalk,运行以下命令: ``` go get github.com/timonwong/prometheus-webhook-dingtalk/cmd/dingtalk ``` 2. 创建钉钉机器人:在钉钉中创建一个自定义机器人,并获取到它的 Webhook 地址,用于将告警消息发送到指定的群组或用户。 3. 创建配置文件:在 prometheus-webhook-dingtalk 的配置文件中,你需要指定钉钉机器人的 Webhook 地址以及其他相关参数。你可以创建一个名为 config.yml 的配置文件,并将以下示例内容填入: ```yaml listen: 0.0.0.0:8060 dingtalk: webhook: https://oapi.dingtalk.com/robot/send?access_token=your_webhook_token ``` 4. 启动 prometheus-webhook-dingtalk:运行以下命令来启动 prometheus-webhook-dingtalk: ``` dingtalk -config.file=config.yml ``` 5. 配置 Prometheus:在 Prometheus 的配置文件中,添加以下内容来指定告警消息的接收端: ```yaml receivers: - name: 'dingtalk' webhook_configs: - url: 'http://prometheus-webhook-dingtalk:8060/dingtalk/webhook' ``` 6. 重新启动 Prometheus:确保 Prometheus 已经重新加载了配置文件,并重启 Prometheus 服务。 现在,当 Prometheus 监控触发告警时,prometheus-webhook-dingtalk 将会将告警消息发送到钉钉机器人的 Webhook 地址,从而通知到指定的群组或用户。 请注意,以上步骤仅为一般示例,实际操作可能会因环境和需求而有所不

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值