编辑:我直接在Express应用程序中编码了流利的服务IP及其工作状态。如何在不对IP进行编码的情况下使其正常工作?
我在Kubernetes集群上有几个Pod (nodejs + express服务器)。
我想将日志从我的 nodejs容器发送到 Fluentd DeamonSet 。
但是我遇到了这个错误:
Fluentd error Error: connect ECONNREFUSED 127.0.0.1:24224
我正在使用https://github.com/fluent/fluent-logger-node,并且我的配置非常简单:
const logger = require('fluent-logger')
logger.configure('pptr', {
host: 'localhost',
port: 24224,
timeout: 3.0,
reconnectInterval: 600000
});
我的流利的conf文件:
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
# Ignore fluent logs
<label @FLUENT_LOG>
<match fluent.*>
@type null
</match>
</label>
<match pptr.**>
@type elasticsearch
host "#{ENV['FLUENT_ELASTICSEARCH_HOST']}"
port "#{ENV['FLUENT_ELASTICSEARCH_PORT']}"
scheme "#{ENV['FLUENT_ELASTICSEARCH_SCHEME'] || 'http'}"
ssl_verify "#{ENV['FLUENT_ELASTICSEARCH_SSL_VERIFY'] || 'true'}"
user "#{ENV['FLUENT_ELASTICSEARCH_USER']}"
password "#{ENV['FLUENT_ELASTICSEARCH_PASSWORD']}"
reload_connections "#{ENV['FLUENT_ELASTICSEARCH_RELOAD_CONNECTIONS'] || 'true'}"
type_name fluentd
logstash_format true
</match>
这是Fluentd DeamonSet配置文件:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
namespace: kube-system
labels:
k8s-app: fluentd-logging
version: v1
spec:
selector:
matchLabels:
k8s-app: fluentd-logging
version: v1
template:
metadata:
labels:
k8s-app: fluentd-logging
version: v1
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
ports:
- containerPort: 24224
env:
- name: FLUENT_ELASTICSEARCH_HOST
value: "xxx"
- name: FLUENT_ELASTICSEARCH_PORT
value: "xxx"
- name: FLUENT_ELASTICSEARCH_SCHEME
value: "https"
# Option to configure elasticsearch plugin with self signed certs
# ================================================================
- name: FLUENT_ELASTICSEARCH_SSL_VERIFY
value: "true"
# Option to configure elasticsearch plugin with tls
# ================================================================
- name: FLUENT_ELASTICSEARCH_SSL_VERSION
value: "TLSv1_2"
# X-Pack Authentication
# =====================
- name: FLUENT_ELASTICSEARCH_USER
value: "xxx"
- name: FLUENT_ELASTICSEARCH_PASSWORD
value: "xxx"
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: config-volume
mountPath: /fluentd/etc/kubernetes.conf
subPath: kubernetes.conf
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: config-volume
configMap:
name: fluentd-conf
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
我还尝试部署服务并公开 24224 端口:
apiVersion: v1
kind: Service
metadata:
name: fluentd
namespace: kube-system
labels:
app: fluentd
spec:
ports:
- name: "24224"
port: 24224
targetPort: 24224
selector:
k8s-app: fluentd-logging
status:
loadBalancer: {}
最后,我的Express应用(部署)在这里:
apiVersion: apps/v1
kind: Deployment
metadata:
name: puppet
labels:
app: puppet
spec:
replicas: 5
selector:
matchLabels:
app: puppet
template:
metadata:
labels:
app: puppet
spec:
containers:
- name: puppet
image: myrepo/my-image
ports:
- containerPort: 8080
编辑:我直接在Express应用程序中编码了流利的服务IP及其工作状态。如何在不对IP进行编码的情况下使其正常工作?
答案 0 :(得分:1)
关注问题的以下部分:
我想将日志从我的nodejs容器发送到Fluentd DeamonSet。
编辑:我直接在我的express应用中对流利的服务IP进行了编码及其工作..如何在不对IP进行编码的情况下使其工作?
pod和fluentd
服务之间的通信似乎正确(对IP进行硬编码)。这里的问题是他们彼此之间进行交流的方式。
您可以通过服务fluentd
的名称进行通信。例如(从吊舱内部):
curl fluentd:24224
您只能通过服务名称(例如fluentd
)在同一名称空间中与服务进行通信。如果服务在另一个名称空间中,则需要使用其完整的DNS名称。它是模板,示例如下:
service-name.namespace.svc.cluster.local
fluentd.kube-system.svc.cluster.local
您还可以使用类型ExternalName
的服务将服务的完整DNS名称映射到较短的版本,如下所示:
假设(示例):
nginx-namespace
命名空间:
$ kubectl create namespace nginx-namespace
nginx
中有一个Deployment
nginx-namespace
和与之相关的服务:
$ kubectl create deployment nginx --image=nginx --namespace=nginx-namespace
$ kubectl expose deployment nginx --port=80 --type=ClusterIP --namespace=nginx-namespace
nginx
)中的Deployment
default
通信您可以选择与上方的窗格进行通信:
Pod
的IP地址
10.98.132.201
nginx.nginx-namespace.svc.cluster.local
ExternalName
类型的服务指向一个(完整的)DNS服务名称
nginx-service
ExternalName
服务类型的示例:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default # <- the same as the pod communicating with the service
spec:
type: ExternalName
externalName: nginx.nginx-namespace.svc.cluster.local
您可以通过以下任意一种方式将此信息传递给广告连播:
其他资源: