使用nginx統一代理dashboard,grafana,Prometheus二級目錄訪問
阿新 • • 發佈:2018-11-22
k8s上的這些管理工具必不可少,可以統一在nginx下的二級目錄下。
ingress是好,但我們不方便使用內部域名,相信麼。。。:)
一,prometheus改造
在prometheus的deployment中傳遞一下—web.external-url引數。如下所示:
- name: prometheus
image: xxx/3rd_part/prometheus:v2.4.3
imagePullPolicy: IfNotPresent
args:
- '--storage.tsdb.path=/prometheus/data/'
- '--storage.tsdb.retention=1d'
- '--config.file=/etc/prometheus/prometheus.yaml'
- '--web.enable-lifecycle'
- '--web.external-url=/prometheus'
ports:
- name: webui
containerPort: 9090
resources:
requests:
cpu: 400m
memory: 500M
# limits:
# cpu: 500m
# memory: 500M
二,grafana改造
在grafana的deployment中,env環境變數更新GF_SERVER_ROOT_URL的值。如下所示:
containers:
- name: grafana
image: xxx/3rd_part/grafana/grafana:5.3.1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
name: grafana
env:
- name: GF_SECURITY_ADMIN_USER
value: [usr]
- name: GF_SECURITY_ADMIN_PASSWORD
value: [pwd]
- name: GF_SERVER_ROOT_URL
value: "%(protocol)s://%(domain)s:%(http_port)s/grafana"
resources:
limits:
cpu: 100m
memory: 256Mi
requests:
cpu: 100m
memory: 256Mi
三,dashboard改造
dashboard不再使用443埠,使用最簡單的方式來達到目的。其yaml檔案如下所示:
# ------------------- Dashboard Service Account ------------------- #
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
---
# ------------------- Dashboard Deployment ------------------- #
kind: Deployment
apiVersion: apps/v1beta2
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
replicas: 1
revisionHistoryLimit: 3
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
spec:
containers:
- name: kubernetes-dashboard
image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3
ports:
- containerPort: 9090
protocol: TCP
livenessProbe:
httpGet:
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
serviceAccountName: kubernetes-dashboard
# Comment the following tolerations if Dashboard must not be deployed on master
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
---
# ------------------- Dashboard Service ------------------- #
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
type: NodePort
ports:
- port: 80
targetPort: 9090
nodePort: 3xxxxx
selector:
k8s-app: kubernetes-dashboard
四,nginx配置
1, 用htpasswd命令生成包含使用者名稱和加密的密碼檔案nginx_passwd。
2, 將nginx配置和nginx_passwd作成configmap檔案。
apiVersion: v1
kind: ConfigMap
metadata:
name: proxy-nginx
namespace: kube-system
data:
default.conf: |-
upstream prometheus {
server prometheus:9090;
}
upstream grafana {
server monitoring-grafana:80;
}
upstream dashboard {
server [master_ip]:[3xxxx];
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /check {
default_type text/plain;
return 200 "serving is ok!";
}
location /status {
stub_status on;
access_log off;
}
location /prometheus {
proxy_pass http://prometheus;
proxy_set_header Host $host;
}
location /grafana {
proxy_pass http://grafana;
rewrite ^/grafana/(.*) /$1 break;
proxy_set_header Host $host;
}
location /dashboard {
auth_basic "Password please";
auth_basic_user_file /etc/nginx/conf.d/nginx_passwd;
proxy_pass http://dashboard;
rewrite ^/dashboard/(.*) /$1 break;
proxy_set_header Host $host;
}
# redirect server error pages to the static page /50x.html
# chengang from k8s config map file
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
nginx_passwd: |-
user:${password}
3, 製作nginx的deployment和service檔案。
掛載了nginx的configmap檔案。在其中加了一個[3xxxx]埠,這就是其它應用的入口。
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: proxy-nginx
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
k8s-app: proxy-nginx
spec:
containers:
- name: nginx
image: xxx/official_hub/nginx:1.13-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx/conf.d
volumes:
- name: nginx-conf
configMap:
name: proxy-nginx
nodeSelector:
node-role.kubernetes.io/master: ""
tolerations:
- key: "node-role.kubernetes.io/master"
effect: "NoSchedule"
---
apiVersion: v1
kind: Service
metadata:
name: proxy-nginx
namespace: kube-system
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: [3xxxx]
selector:
k8s-app: proxy-nginx
五,測試URL
http://[master_ip:3xxxx]/grafana
http://[master_ip:3xxxx]/prometheus
http://[master_ip:3xxxx]/dashboard