【问题标题】:Getting prometheus/grafana and k3s to work together让 prometheus/grafana 和 k3s 一起工作
【发布时间】:2021-09-27 18:53:42
【问题描述】:

学习 kubernetes 我已经使用 4 个 Raspberry PI 为自己构建了一个裸机集群,并使用 k3s 设置了它:

# curl -sfL https://get.k3s.io | sh -

添加了节点等,一切都出现了,我可以看到所有节点,几乎一切都按预期工作。

我想监控 PI,所以我用 helm 安装了 kube-prometheus-stack:

$ kubectl create namespace monitoring
$ helm install prometheus --namespace monitoring prometheus-community/kube-prometheus-stack

现在一切看起来都很棒:

$ kubectl get pods --all-namespaces 
NAMESPACE     NAME                                                     READY   STATUS      RESTARTS   AGE
kube-system   helm-install-traefik-crd-s8zw5                           0/1     Completed   0          5d21h
kube-system   helm-install-traefik-rc9f2                               0/1     Completed   1          5d21h
monitoring    prometheus-prometheus-node-exporter-j85rw                1/1     Running     10         28h
kube-system   metrics-server-86cbb8457f-mvbkl                          1/1     Running     12         5d21h
kube-system   coredns-7448499f4d-t7sp8                                 1/1     Running     13         5d21h
monitoring    prometheus-prometheus-node-exporter-mmh2q                1/1     Running     9          28h
monitoring    prometheus-prometheus-node-exporter-j4k4c                1/1     Running     10         28h
monitoring    alertmanager-prometheus-kube-prometheus-alertmanager-0   2/2     Running     10         28h
kube-system   svclb-traefik-zkqd6                                      2/2     Running     6          19h
monitoring    prometheus-prometheus-node-exporter-bft5t                1/1     Running     10         28h
kube-system   local-path-provisioner-5ff76fc89d-g8tm6                  1/1     Running     12         5d21h
kube-system   svclb-traefik-jcxd2                                      2/2     Running     28         5d21h
kube-system   svclb-traefik-mpbjm                                      2/2     Running     22         5d21h
kube-system   svclb-traefik-7kxtw                                      2/2     Running     20         5d21h
monitoring    prometheus-grafana-864598fd54-9548l                      2/2     Running     10         28h
kube-system   traefik-65969d48c7-9lh9m                                 1/1     Running     3          19h
monitoring    prometheus-prometheus-kube-prometheus-prometheus-0       2/2     Running     10         28h
monitoring    prometheus-kube-state-metrics-76f66976cb-m8k2h           1/1     Running     6          28h
monitoring    prometheus-kube-prometheus-operator-5c758db547-zsv4s     1/1     Running     6          28h

服务都在那里:

$ kubectl get services --all-namespaces
NAMESPACE     NAME                                                 TYPE           CLUSTER-IP      EXTERNAL-IP                                                   PORT(S)                        AGE
default       kubernetes                                           ClusterIP      10.43.0.1       <none>                                                        443/TCP                        5d21h
kube-system   kube-dns                                             ClusterIP      10.43.0.10      <none>                                                        53/UDP,53/TCP,9153/TCP         5d21h
kube-system   metrics-server                                       ClusterIP      10.43.80.65     <none>                                                        443/TCP                        5d21h
kube-system   prometheus-kube-prometheus-kube-proxy                ClusterIP      None            <none>                                                        10249/TCP                      28h
kube-system   prometheus-kube-prometheus-kube-scheduler            ClusterIP      None            <none>                                                        10251/TCP                      28h
monitoring    prometheus-kube-prometheus-operator                  ClusterIP      10.43.180.73    <none>                                                        443/TCP                        28h
kube-system   prometheus-kube-prometheus-coredns                   ClusterIP      None            <none>                                                        9153/TCP                       28h
kube-system   prometheus-kube-prometheus-kube-etcd                 ClusterIP      None            <none>                                                        2379/TCP                       28h
kube-system   prometheus-kube-prometheus-kube-controller-manager   ClusterIP      None            <none>                                                        10252/TCP                      28h
monitoring    prometheus-kube-prometheus-alertmanager              ClusterIP      10.43.195.99    <none>                                                        9093/TCP                       28h
monitoring    prometheus-prometheus-node-exporter                  ClusterIP      10.43.171.218   <none>                                                        9100/TCP                       28h
monitoring    prometheus-grafana                                   ClusterIP      10.43.20.165    <none>                                                        80/TCP                         28h
monitoring    prometheus-kube-prometheus-prometheus                ClusterIP      10.43.207.29    <none>                                                        9090/TCP                       28h
monitoring    prometheus-kube-state-metrics                        ClusterIP      10.43.229.14    <none>                                                        8080/TCP                       28h
kube-system   prometheus-kube-prometheus-kubelet                   ClusterIP      None            <none>                                                        10250/TCP,10255/TCP,4194/TCP   28h
monitoring    alertmanager-operated                                ClusterIP      None            <none>                                                        9093/TCP,9094/TCP,9094/UDP     28h
monitoring    prometheus-operated                                  ClusterIP      None            <none>                                                        9090/TCP                       28h
kube-system   traefik                                              LoadBalancer   10.43.20.17     192.168.76.200,192.168.76.201,192.168.76.202,192.168.76.203   80:31131/TCP,443:31562/TCP     5d21h

命名空间:

$ kubectl get namespaces 
NAME              STATUS   AGE
kube-system       Active   5d21h
default           Active   5d21h
kube-public       Active   5d21h
kube-node-lease   Active   5d21h
monitoring        Active   28h

但我无法访问 grafana 服务。

我想很公平,让我们定义一个 Ingress,但它不起作用:

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: grafana-ingress
  namespace: monitoring
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: prometheus-grafana
            port:
              number: 80

我不知道为什么它没有进入服务,我也看不出问题出在哪里,尽管我了解容器等(我首先让所有东西都在 docker swarm 上运行),但我不知道真的知道在哪里,如果在任何地方,它会显示在日志中。

在过去的几天里,我尝试了各种各样的事情,终于找到了有关名称空间和调用服务问题的提示,以及称为“type: ExternalName”的东西。

我从集群内的一个 pod 中检查 curl,它正在“监控”名称空间内传递数据,但 traefik 无法到达那里,甚至可能看到它?

查看 Traefik 文档后,我发现了有关 namespaces 的内容,但我不知道从哪里开始找到提到的内容:

providers:
  kubernetesCRD:
    namespaces:

我假设 k3s 已将其正确设置为一个空数组,因为我在他们的网站上找不到任何东西告诉我如何处理他们的“klipper-lb”和“traefik”组合。

我终于尝试用外部名称定义另一个服务:

---
apiVersion: v1
  kind: Service
  metadata:
    name: grafana-named
    namespace: kube-system
  spec:
    type: ExternalName
    externalName: prometheus-grafana.monitoring.svc.cluster.local
    ports:
    - name: service
      protocol: TCP
      port: 80
      targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
  metadata:
    name: grafana-ingress
    namespace: kube-system
    annotations:
      kubernetes.io/ingress.class: traefik
  spec:
    rules:
    - http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: grafana-named
            port:
              number: 80

2-3 天后,我已经尝试了所有我能想到的东西,在阳光下搜索了所有内容,但我无法从内部集群节点外部访问 grafana。

我不知道如何使用 k3s 进行任何工作。我在我的主 PC 上安装了 Lens,几乎可以看到那里的所有内容,但我认为缺少的指标信息也需要 Ingress 或类似的东西。

我必须做些什么才能让 traefik 完成我认为基本上是它的工作,将传入请求路由到后端服务?

【问题讨论】:

    标签: kubernetes namespaces grafana traefik-ingress k3s


    【解决方案1】:

    我提交了bug report on github 并且其中的一个人(再次感谢 brandond)为我指明了正确的方向。

    网络层使用flannel来处理“in cluster”的网络。其默认实现称为“vxlan”,使用虚拟以太网适配器似乎更复杂。

    根据我的要求(阅读:让集群正常工作),解决方案是将实现更改为“host-gw”。

    这是通过在控制器的 k3s.service 选项中添加“--flannel-backend=host-gw”来完成的。

    $ sudo systemctl edit k3s.service
    ### Editing /etc/systemd/system/k3s.service.d/override.conf
    ### Anything between here and the comment below will become the new contents of the file
    
    [Service]
    ExecStart=
    ExecStart=/usr/local/bin/k3s \
        server \
          '--flannel-backend=host-gw'
    
    ### Lines below this comment will be discarded
    

    第一个 "ExecStart=" 清除现有的默认启动命令,使其可以被第二个替换。

    现在一切正常,我终于可以继续学习 K8s 了。

    我可能会在某个时候重新激活“vxlan”并弄清楚这一点。

    【讨论】:

    • 看起来我在同一点上挣扎。但是更改 flannel-backend 似乎对我不起作用。您是否获得了其他见解?
    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多