参考MiniKube安装准备环境。
EFK,集中式的日志监控平台。Elasticsearch, Fluentd, Kibana三个组件的首字母构成它的名字。
如果要对K8S的Pod,容器采集日志,那么我们需要在K8S集群的每个节点上都部署一个fluentd的pod。发布形式为DaemonSet。
fluentd负责采集节点上所有容器产生的日志。具体要采集哪个日志,可以通过fluentd的配置文件进行配置。
fluentd采集好日志后,会定期传到elasticsearch集群的端点,elasticsearch会将日志集中存储并且根据配置建立反向索引。
然后开发,运维人员可以通过Kibana界面,集中的查询和分析日志。
如果要对fluentd的监控日志目标进行配置,可以通过configmap这种方式来实现,运维人员可以通过configmap可以定制fluentd的日志采集配置文件,然后发布到K8S环境。
fluentd的Pod可以通过持久卷的形式挂载configmap中的配置文件。就是说,可以通过configmap,运维人员可以动态发布和更新fluentd的配置文件。这种更新可以实时生效。需要重启fluentd的Pod。
实际企业生产环境,kibana和elasticsearch不会部署在K8S当中。另外,在实际生产环境中,企业级日志分析平台EFK架构中,在fluentd与elasticsearch之间,还有一套kafka队列来做缓冲,在数据量大的时候,可以解决ES性能跟不上的问题。ES建反向索引是比较耗资源的,而且比较慢,数据量大的时候,ES可能会跟不上。有了kafka缓冲之后,后台ES,Kibana升级维护就会比较容易。
DaemonSet是与ReplicaSet对应的概念。不同之处是daemonSet会在K8S所有节点上,都部署一个pod。
K8S DaemonSet机制会保证,每个节点上都有一个pod可用。如果一个Pod挂了,daemonSet会自动重启。
MiniKube本身是有EFK Addon的。但是本实验打算有发布文件的形式搞一搞。
可以用以下命令查看:
minikube addons list
[ryan@lab3 ~]$ minikube addons list
|-----------------------------|----------|--------------|
| ADDON NAME | PROFILE | STATUS |
|-----------------------------|----------|--------------|
| ambassador | minikube | disabled |
| csi-hostpath-driver | minikube | disabled |
| dashboard | minikube | enabled ? |
| default-storageclass | minikube | enabled ? |
| efk | minikube | disabled |
| freshpod | minikube | disabled |
| gcp-auth | minikube | disabled |
文件:ns.yml
apiVersion: v1
kind: Namespace
metadata:
name: logging
文件:elastic.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: elasticsearch
namespace: logging
spec:
selector:
matchLabels:
component: elasticsearch
template:
metadata:
labels:
component: elasticsearch
spec:
containers:
- name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:6.8.13
env:
- name: discovery.type
value: single-node
ports:
- containerPort: 9200
name: http
protocol: TCP
resources:
limits:
cpu: 500m
memory: 2Gi
requests:
cpu: 500m
memory: 2Gi
---
apiVersion: v1
kind: Service
metadata:
name: elasticsearch
namespace: logging
labels:
service: elasticsearch
spec:
type: NodePort
selector:
component: elasticsearch
ports:
- port: 9200
targetPort: 9200
nodePort: 31200
文件:kibana.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: kibana
namespace: logging
spec:
selector:
matchLabels:
run: kibana
template:
metadata:
labels:
run: kibana
spec:
containers:
- name: kibana
image: docker.elastic.co/kibana/kibana:6.8.13
env:
- name: ELASTICSEARCH_URL
value: http://elasticsearch:9200
- name: XPACK_SECURITY_ENABLED
value: "false"
ports:
- containerPort: 5601
name: http
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: kibana
namespace: logging
labels:
service: kibana
spec:
type: NodePort
selector:
run: kibana
ports:
- port: 5601
targetPort: 5601
nodePort: 31601
文件:fluentd-daemonset.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
namespace: kube-system
labels:
k8s-app: fluentd-logging
version: v1
kubernetes.io/cluster-service: "true"
spec:
selector:
matchLabels:
k8s-app: fluentd-logging
version: v1
template:
metadata:
labels:
k8s-app: fluentd-logging
version: v1
kubernetes.io/cluster-service: "true"
spec:
serviceAccount: fluentd
serviceAccountName: fluentd
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd
image: fluent/fluentd-kubernetes-daemonset:v1-debian-elasticsearch
env:
- name: FLUENT_ELASTICSEARCH_HOST
value: "elasticsearch.logging"
- name: FLUENT_ELASTICSEARCH_PORT
value: "9200"
- name: FLUENT_ELASTICSEARCH_SCHEME
value: "http"
- name: FLUENT_UID
value: "0"
- name: FLUENTD_SYSTEMD_CONF
value: disable
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
文件:fluentd-rbac.yml
rbac: role based access control
apiVersion: v1
kind: ServiceAccount
metadata:
name: fluentd
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: fluentd
namespace: kube-system
rules:
- apiGroups:
- ""
resources:
- pods
- namespaces
verbs:
- get
- list
- watch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: fluentd
roleRef:
kind: ClusterRole
name: fluentd
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: fluentd
namespace: kube-system
准备好5个文件之后,准备发布。
[ryan@lab3 efk_deploy]$ ll
total 20
-rw-rw-r--. 1 ryan ryan 964 Jan 6 01:48 elastic.yml
-rw-rw-r--. 1 ryan ryan 1657 Jan 6 01:50 fluentd-daemonset.yml
-rw-rw-r--. 1 ryan ryan 600 Jan 6 01:51 fluentd.rbac.yml
-rw-rw-r--. 1 ryan ryan 809 Jan 6 01:48 kibana.yml
-rw-rw-r--. 1 ryan ryan 57 Jan 6 01:47 ns.yml
发布命名空间
kubectl apply -f ns.yml
查看命名空间
kubectl get ns
发布elasticsearch,需要等一段时间才能就绪。
kubectl apply -f elastic.yml
查看刚发布的pod,在命名空间logging里面
kubectl get all -n logging
发布成功,看到status running.
[ryan@lab3 efk_deploy]$ kubectl get all -n logging
NAME READY STATUS RESTARTS AGE
pod/elasticsearch-848b5b7585-dgh8l 1/1 Running 0 15m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/elasticsearch NodePort 10.99.78.88 <none> 9200:31200/TCP 15m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/elasticsearch 1/1 1 1 15m
NAME DESIRED CURRENT READY AGE
replicaset.apps/elasticsearch-848b5b7585 1 1 1 15m
可以用下面命令查看minikube的service list。
minikube service list
发布Kibana,需要等一段时间才能就绪。
kubectl apply -f kibana.yml
查看成功:
[ryan@lab3 efk_deploy]$ kubectl get all -n logging
NAME READY STATUS RESTARTS AGE
pod/elasticsearch-848b5b7585-dgh8l 1/1 Running 0 36m
pod/kibana-5c7df47d47-dpjbc 1/1 Running 0 7m18s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/elasticsearch NodePort 10.99.78.88 <none> 9200:31200/TCP 36m
service/kibana NodePort 10.99.146.170 <none> 5601:31601/TCP 7m18s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/elasticsearch 1/1 1 1 36m
deployment.apps/kibana 1/1 1 1 7m18s
NAME DESIRED CURRENT READY AGE
replicaset.apps/elasticsearch-848b5b7585 1 1 1 36m
replicaset.apps/kibana-5c7df47d47 1 1 1 7m18s
发布fluentd K8S角色权限绑定
kubectl apply -f fluentd-rbac.yml
继续发布fluentd-daemonset
kubectl apply -f fluentd-daemonset.yml
查看POD状态:
kubectl get po -n kube-system
[ryan@lab3 efk_deploy]$ kubectl get po -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-54d67798b7-2rdv2 1/1 Running 0 12h
etcd-minikube 1/1 Running 0 12h
fluentd-6jrkj 1/1 Running 0 2m19s
kube-apiserver-minikube 1/1 Running 0 12h
kube-controller-manager-minikube 1/1 Running 0 12h
kube-proxy-qwd7c 1/1 Running 0 12h
kube-scheduler-minikube 1/1 Running 0 12h
storage-provisioner 1/1 Running 1 12h
minikube查看minikube node开启的服务端口:
可以发现,minikube node已经在监听服务端口了,kibana应该是可以访问了,但是centos没有装桌面gui怎么办。
minikube service list
[ryan@lab3 conf]$ minikube service list
|----------------------|---------------------------|--------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|----------------------|---------------------------|--------------|---------------------------|
| default | kubernetes | No node port |
| kube-system | kube-dns | No node port |
| kubernetes-dashboard | dashboard-metrics-scraper | No node port |
| kubernetes-dashboard | kubernetes-dashboard | No node port |
| logging | elasticsearch | 9200 | http://192.168.49.2:31200 |
| logging | kibana | 5601 | http://192.168.49.2:31601 |
结构化:
我的centos虚拟机的IP是192.168.44.133。
centos上面装的minikube node的IP是192.168.49.2。
我们发布了nodeport service给kibana,ES容器,是为了将容器的服务端口映射到node上,所以在centos上应该是可以访问,192.168.49.2:31601的。但是由于centos没有桌面,你验证不了。:)如下图所示。
如果想要远程访问,需要配置nginx。不然在centos上装桌面,往下看看吧。:)
由于当前的环境太虚拟了,如下图所示:
我用nginx做了代理,然后在笔记本上可以访问了。请参考我下面的笔记,不用谢。
Nginx安装
Nginx配置
成功后,如下图所示。