K8S是一种流行的容器编排平台,用于管理容器化应用程序的部署、扩展和管理。而Elasticsearch、Fluentd和Kibana(EFK)则是一组流行的开源工具,用于日志收集和分析。在K8S环境中使用EFK可以帮助开发人员和运维团队更好地监控和调试应用程序。本文将详细介绍如何在K8S集群中部署EFK,并将日志输出到Github中进行存储和管理。

### 整体流程:

| 步骤 | 操作 |
|-----------------------|-------------------------------------------|
| 步骤一:部署Elasticsearch | 创建Elasticsearch服务和Pod |
| 步骤二:部署Fluentd | 部署Fluentd DaemonSet |
| 步骤三:部署Kibana | 部署Kibana服务和Pod |
| 步骤四:配置日志输出 | 配置Fluentd将日志输出到Github仓库 |

### 具体步骤及代码示例:

#### 步骤一:部署Elasticsearch

1. 创建Elasticsearch服务和Pod,并暴露端口用于外部访问。

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: elasticsearch
spec:
replicas: 1
selector:
matchLabels:
app: elasticsearch
template:
metadata:
labels:
app: elasticsearch
spec:
containers:
- name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:7.9.3
ports:
- containerPort: 9200
---
apiVersion: v1
kind: Service
metadata:
name: elasticsearch
spec:
selector:
app: elasticsearch
ports:
- port: 9200
```

#### 步骤二:部署Fluentd

2. 部署Fluentd DaemonSet,确保每个节点都有Fluentd实例用于日志收集。

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: fluentd-config
data:
fluent.conf: |

@type stdout

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
spec:
selector:
matchLabels:
app: fluentd
template:
metadata:
labels:
app: fluentd
spec:
containers:
- name: fluentd
image: fluent/fluentd
volumeMounts:
- name: fluentd-config
mountPath: /fluentd/etc/fluent.conf
subPath: fluent.conf
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
```

#### 步骤三:部署Kibana

3. 部署Kibana服务和Pod,并设置访问方式。

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: kibana
spec:
replicas: 1
selector:
matchLabels:
app: kibana
template:
metadata:
labels:
app: kibana
spec:
containers:
- name: kibana
image: docker.elastic.co/kibana/kibana:7.9.3
ports:
- containerPort: 5601
---
apiVersion: v1
kind: Service
metadata:
name: kibana
spec:
selector:
app: kibana
ports:
- port: 5601
```

#### 步骤四:配置日志输出到Github

4. 在Fluentd配置文件中添加输出日志到Github的配置。

```yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-github
spec:
selector:
matchLabels:
app: fluentd-github
template:
metadata:
labels:
app: fluentd-github
spec:
containers:
- name: fluentd
image: fluent/fluentd
volumeMounts:
- name: fluentd-config
mountPath: /fluentd/etc/fluent.conf
subPath: fluent.conf
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
```

以上就是在K8S集群中部署EFK,并将日志输出到Github的完整流程。通过上述步骤的操作和代码示例,可以帮助小白快速实现这一目标。希望这篇科普文章对你有所帮助!