Prometheus接入Kubernetes错误

avatar 2020年5月28日18:30:16 评论 1,601 次浏览

Kubernetes已经上线了,准备做Prometheus接入,我们使用的是每个Kubernetes都有一套自己的Prometheus,然后使用Grafana接入所有的Prometheus。这样做的好处是可以根据项目进行区分,在以后的多项目中不会容易达到一个瓶颈。下面是我接入Prometheus时遇到的两个问题以及解决方法。

第一个问题

Kubernetes版本的问题

[root@www.wulaoer.org 2.0]# kubectl apply -f prometheus.yaml 
serviceaccount/prometheus-serviceaccount unchanged
clusterrole.rbac.authorization.k8s.io/prometheus unchanged
clusterrolebinding.rbac.authorization.k8s.io/prometheus unchanged
service/prometheus unchanged
error: unable to recognize "prometheus.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"

问题的原因是因为,Kubernetes已经从v1.16.2版本中Deployment 已经被extensions/v1beta1 弃用,所以我们直接替换成apps/v1即可。

apiVersion: extensions/v1beta1
kind: Deployment
替换成
apiVersion: apps/v1
kind: Deployment

可以看一下我的Kubernetes版本

[root@www.wulaoer.org ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:18:23Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:09:08Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}

第二个问题

spec的selector的配置问题,下面是我创建Prometheus时报的错误。

[root@www.wulaoer.org 2.0]# kubectl apply -f prometheus.yaml 
serviceaccount/prometheus-serviceaccount unchanged
clusterrole.rbac.authorization.k8s.io/prometheus unchanged
clusterrolebinding.rbac.authorization.k8s.io/prometheus unchanged
error: error validating "prometheus.yaml": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false

解决方法,就是在你的spec下加一个selector,selector下的app名称必须要和定义的程序名称一致。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: prometheus-deployment
  name: prometheus
  namespace: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus

我是直接在replicas下插入的seector,重新创建问题解决。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: