在Easypack中提供了多套Kubernetes快速部署的工具,基于Bash脚本和Ansible的,Ansible的方式也有基于K3S和普通的Kubernetes的两种。这篇文章介绍一下普通Kubernetes版本下的Ansible部署工具中如何安装Prometheus进行监控。
事前准备
本文使用Kubernetes 1.17,可参看下文进行快速环境搭建:
[root@host131 ansible]# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
192.168.163.131 Ready <none> 18h v1.17.0 192.168.163.131 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://18.9.7
[root@host131 ansible]#
事前环境确认
[root@host131 ansible]# kubectl get service -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 18h
kube-system kube-dns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP 18h
[root@host131 ansible]# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-59db588569-s8mbs 1/1 Running 0 18h
[root@host131 ansible]# kubectl get deployment -A
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
kube-system coredns 1/1 1 1 18h
[root@host131 ansible]#
[root@host131 ansible]# kubectl get ns
NAME STATUS AGE
default Active 18h
kube-node-lease Active 18h
kube-public Active 18h
kube-system Active 18h
[root@host131 ansible]#
Prometheus安装
配置设定
var_ver_prometheus: v2.15.1
var_prometheus_dir_etc: "{{var_kube_dir_etc}}/prometheus"
var_template_prometheus_rbac_yaml: "rbac.yml"
var_template_prometheus_configmap_yaml: "configmap.yml"
var_template_prometheus_deployment_yaml: "deployment.yml"
var_src_prometheus: "{{var_src_binary}}/images/prometheus.tar"
var_prometheus_node_port: 33308
var_prometheus_namespace: prometheus
一般来说,根据需要进行调节的有如下内容:
- var_prometheus_namespace: 命名空间,比如此处会将SA以及deployment都关联至此命令空间中,可根据需要进行设定,比如default、kube-system等,如果命名空间之前不存在会先行创建,事前存在不会删除重建,只会直接使用
- var_prometheus_node_port:外部访问的端口
- var_ver_prometheus:版本名称
- var_src_prometheus:镜像的tar文件需要放至{{var_src_binary}}/images/,名称为prometheus.tar,注意内容和实际版本需要一致。
安装Prometheus
执行如下命令即可完成Prometheus的安装
[root@host131 ansible]# ansible-playbook prometheus/tests/test.yml
PLAY [localhost] ***********************************************************************************************************************
TASK [prometheus : create dirs for prometheus] *****************************************************************************************
changed: [localhost] => (item=/etc/k8s/prometheus)
TASK [prometheus : create prometheus rbac yaml file] ***********************************************************************************
changed: [localhost]
TASK [prometheus : create prometheus configmap yaml file] ******************************************************************************
changed: [localhost]
TASK [prometheus : create prometheus deployment yaml file] *****************************************************************************
changed: [localhost]
TASK [prometheus : load prometheus image] **********************************************************************************************
changed: [localhost]
TASK [prometheus : create namespace specified when not existed] ************************************************************************
changed: [localhost]
TASK [prometheus : create prometheus service] ******************************************************************************************
changed: [localhost]
TASK [prometheus : confirm prometheus service state] ***********************************************************************************
FAILED - RETRYING: confirm prometheus service state (15 retries left).
changed: [localhost]
PLAY RECAP *****************************************************************************************************************************
localhost : ok=8 changed=8 unreachable=0 failed=0
[root@host131 ansible]#
结果确认
使用kubectl命令可以确认到,namespace、pod、deployment以及service全部生成并正常运行
[root@host131 ansible]# kubectl get ns
NAME STATUS AGE
default Active 18h
kube-node-lease Active 18h
kube-public Active 18h
kube-system Active 18h
prometheus Active 70s
[root@host131 ansible]#
[root@host131 ansible]# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-59db588569-s8mbs 1/1 Running 0 18h
prometheus prometheus-deployment-774dcd78bc-8blrk 1/1 Running 0 76s
[root@host131 ansible]#
[root@host131 ansible]# kubectl get deployments -A
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
kube-system coredns 1/1 1 1 18h
prometheus prometheus-deployment 1/1 1 1 84s
[root@host131 ansible]#
[root@host131 ansible]# kubectl get service -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 18h
kube-system kube-dns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP 18h
prometheus prometheus-service NodePort 10.254.250.72 <none> 8080:33308/TCP 90s
[root@host131 ansible]#
另外,也可以通过33308端口访问Prometheus,确认Prometheus是否已经能够从K8S获取监控数据了, 从/targets页面可以看到相关的连接都在正常运行
Ansible脚本
github地址:https://github.com/liumiaocn/easypack/tree/master/k8s/ansible