k8s~為服務添加sidecar邊斗
sidecar這個(ge)詞一般指帶有跨(kua)斗(dou)(dou)的摩(mo)托車,在二戰時候(hou)小日本(ben)開著很多(duo)這種(zhong)摩(mo)托車,它(ta)在原有基礎上(shang)添加了一個(ge)跨(kua)斗(dou)(dou),之后(hou)就可以多(duo)載一個(ge)人,而(er)對于原來的兩(liang)輪摩(mo)托車沒有什(shen)么影響,把跨(kua)斗(dou)(dou)拆了也(ye)是可以的,對原來的事物沒有本(ben)質上(shang)的破壞,只(zhi)(zhi)是擴展了新的功能,這與軟件開發里(li)的OCP原則(ze)很像,在服務網格的istio里(li)也(ye)有這個(ge)概(gai)念,它(ta)把這種(zhong)組件叫(jiao)“sidecar”,在istio里(li)sidecar也(ye)只(zhi)(zhi)是一個(ge)概(gai)念,具體是由envoy來實現的。
具體fluentd功能的sidecar
我們的容器部署到k8s里,通過k8s來管理我們的容器,實現對容器的生命周期管理,服務發現管理,多副本管理等等;而我們把這些容器可以理解為一個個的微服務,而這些服務的日志一般先記錄在本地,然后推到elasticsearch里,而日志收集工具我們可以選擇fluent,Filebeat,Logstash等等。
添加fluentd的sidecar
添加fluentd.config配置
<source>
type tail
format json
path /var/log/*.log
pos_file /var/log/log.pos
tag saas # 這個tag對應match.logstash_prefix,之后在kibana的索引配置里可以找到
</source>
<match **>
@id elasticsearch
@type elasticsearch
@log_level debug
index_name fluentd
type_name fluentd
host elasticsearch.elk
port 9200
include_tag_key true
tag_key @log_name
logstash_format true
logstash_prefix saas
flush_interval 10s
</match>
服務的部署文件添加sidecar
kind: Service
apiVersion: v1
metadata:
name: hello-world
namespace: saas
spec:
selector:
app: hello-world
type: ClusterIP
ports:
- protocol: TCP
targetPort: 9001
port: 80
---
# 構建反射代理
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: hello-world-ingress
namespace: saas
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
tls:
- hosts:
- www.abc.com
secretName: saas-tls
rules:
- host: www.abc.com
http:
paths:
- backend:
serviceName: hello-world
servicePort: 9001
- path: /dotnet
backend:
serviceName: dotnet-hello
servicePort: 80
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: hello-world-deployment
namespace: saas
labels:
app: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: 172.17.0.22:8888/saas/hello-world:latest
imagePullPolicy: Always
ports:
- containerPort: 9001
env:
- name: spring.profiles.active
value: prod
volumeMounts:
- name: varlog
mountPath: /var/log
- name: fluent-sidecar
image: registry.cn-beijing.aliyuncs.com/k8s-mqm/fluentd-elasticsearch:v2.1.0
env:
- name: FLUENTD_ARGS
value: -c /etc/fluentd-config/fluentd.conf
volumeMounts:
- name: varlog
mountPath: /var/log
- name: config-volume
mountPath: /etc/fluentd-config
volumes:
- name: varlog
emptyDir: {}
- name: config-volume
configMap:
name: fluentd-config
當你(ni)的hello-world部署到(dao)k8s之后(hou),在有日(ri)志記(ji)錄時(shi)它(ta)會寫到(dao)/var/logs目錄,而fluentd這個sidecar因為是與容器花(hua)用的磁盤,所以它(ta)也可以讀(du)到(dao)日(ri)志的內容,然后(hou)把(ba)日(ri)志發到(dao)elasticsearch里。