k8s1.26.1集群搭建+prometheus+grafana
安装containerd和kubeadm,kubelet,kubectl
/etc/yum.repo.d/docker-ce.repo内容如下:
[docker-ce-stable] name=Docker CE Stable - $basearch baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/stable enabled=1 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-stable-debuginfo] name=Docker CE Stable - Debuginfo $basearch baseurl=https://download.docker.com/linux/centos/$releasever/debug-$basearch/stable enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-stable-source] name=Docker CE Stable - Sources baseurl=https://download.docker.com/linux/centos/$releasever/source/stable enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-test] name=Docker CE Test - $basearch baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/test enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-test-debuginfo] name=Docker CE Test - Debuginfo $basearch baseurl=https://download.docker.com/linux/centos/$releasever/debug-$basearch/test enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-test-source] name=Docker CE Test - Sources baseurl=https://download.docker.com/linux/centos/$releasever/source/test enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-nightly] name=Docker CE Nightly - $basearch baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/nightly enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-nightly-debuginfo] name=Docker CE Nightly - Debuginfo $basearch baseurl=https://download.docker.com/linux/centos/$releasever/debug-$basearch/nightly enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg [docker-ce-nightly-source] name=Docker CE Nightly - Sources baseurl=https://download.docker.com/linux/centos/$releasever/source/nightly enabled=0 gpgcheck=1 gpgkey=https://download.docker.com/linux/centos/gpg
/etc/yum.repo.d/kubernetes.repo内容如下:
[kubernetes] name = kubernetes baseurl = https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled = 1 gpgcheck = 1 gpgkey = https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
yum install kubelet kubeadm kubectl containerd
containerd自动生成配置文件
containerd config default > /etc/containerd/config.toml
重启containerd生效
systemctl daemon-reload && systemctl restart containerd
vim /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock image-endpoint: unix:///run/containerd/containerd.sock timeout: 10 debug: false
创建集群
kubeadm init --control-plane-endpoint "192.168.1.101:6443" --upload-certs
下载calico部署清单,然后部署
wget -c https://raw.githubusercontent.com/projectcalico/calico/master/manifests/calico.yaml wget -c https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/tigera-operator.yaml wget -c https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/custom-resources.yaml
如果出现calico一直停留在init:0/3,可以删除一下目录
/etc/cni/ /var/lib/cni /opt/cni
等待pod状态都变成running
下载kube-prometheus
wget -c https://github.com/prometheus-operator/kube-prometheus/archive/refs/tags/v0.12.0.tar.gz
部署文件
kubectl create -f manifests/setup
kubectl create -f manifests/
等到pod running
配置端口转发(因为calico网络策略的缘故无法nodeport端口访问)
nohup kubectl --address 0.0.0.0 --namespace monitoring port-forward svc/grafana 3000 > nohupcmd.out 2>&1 &
或者删除grafana策略 再去kubectl edit svc grafana -n monitoring修改为nodeport
kubectl delete networkpolicy grafana -n monitoring
查看pod状态
[root@ceph2 ~]# kubectl get pods -n monitoring NAME READY STATUS RESTARTS AGE alertmanager-main-0 2/2 Running 1 (36m ago) 36m alertmanager-main-1 2/2 Running 0 30m alertmanager-main-2 2/2 Running 1 (36m ago) 36m blackbox-exporter-6495c95d8f-jgmwg 3/3 Running 0 71m grafana-5698c77fd7-fpghq 1/1 Running 0 38m kube-state-metrics-7cc68994c-d8k94 3/3 Running 0 66m node-exporter-dn8tx 2/2 Running 0 71m node-exporter-s7n2b 2/2 Running 0 71m node-exporter-xw59r 2/2 Running 0 71m prometheus-adapter-779df64887-fkgjz 1/1 Running 0 71m prometheus-adapter-779df64887-zzsnk 1/1 Running 0 59m prometheus-k8s-0 2/2 Running 0 32m prometheus-k8s-1 2/2 Running 0 36m prometheus-operator-8d5b96fc9-rdvjh 2/2 Running 0 36m
http://master主机IP:3000
配置grafana模板 13105
配置alert报警,如下:
1.在/etc/grafana/grafana.ini配置文件中增加邮件服务器配置
apiVersion: v1 kind: Secret metadata: labels: app.kubernetes.io/component: grafana app.kubernetes.io/name: grafana app.kubernetes.io/part-of: kube-prometheus app.kubernetes.io/version: 9.3.6 name: grafana-config namespace: monitoring stringData: grafana.ini: | [date_formats] default_timezone = UTC [smtp] enabled = true host = smtp.126.com:25 user = xxxx_dc@126.com password = #授权码 skip_verify = true from_address = xxxx_dc@126.com from_name = Grafana type: Opaque
2.配置grafana
其中报警模板内容如下:
{{ define "myalert" }} [{{.Status}}] {{ .Labels.alertname }} Labels: {{ range .Labels.SortedPairs }} {{ .Name }}: {{ .Value }} {{ end }} {{ if gt (len .Annotations) 0 }} Annotations: {{ range .Annotations.SortedPairs }} {{ .Name }}: {{ .Value }} {{ end }} {{ end }} {{ if gt (len .SilenceURL ) 0 }} Silence alert: {{ .SilenceURL }} {{ end }} {{ if gt (len .DashboardURL ) 0 }} Go to dashboard: {{ .DashboardURL }} {{ end }} {{ end }} {{ define "mymessage" }} {{ if gt (len .Alerts.Firing) 0 }} {{ len .Alerts.Firing }} firing: {{ range .Alerts.Firing }} {{ template "myalert" .}} {{ end }} {{ end }} {{ if gt (len .Alerts.Resolved) 0 }} {{ len .Alerts.Resolved }} resolved: {{ range .Alerts.Resolved }} {{ template "myalert" .}} {{ end }} {{ end }} {{ end }}