pods之间的通信

Communication between pods

我目前正在设置 sentry.io 但我在 openshift 3.11 中设置它时遇到问题

我得到 pods 运行 sentry 本身,postgresqlredismemcache 但根据日志消息,它们是不能一起交流。

sentry.exceptions.InvalidConfiguration: Error 111 connecting to 127.0.0.1:6379. Connection refused.

我是否需要像 docker 那样创建一个网络,或者 pods(都在同一个命名空间中)是否应该默认能够相互通信?我获得了整个项目的管理员权限,因此我还可以使用控制台,而不仅仅是 Web 界面。

祝福

编辑: 为哨兵及其服务添加部署配置,为了简单起见,为 postgres 配置和服务添加部署配置。我还用关键字 BLANK 删除了一些不必要的信息,如果我说得太过分了,请告诉我,我会查一下的。

sentry 的部署配置:

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  annotations:
    openshift.io/generated-by: OpenShiftWebConsole
  creationTimestamp: BLANK
  generation: 20
  labels:
    app: sentry
  name: sentry
  namespace: test
  resourceVersion: '506667843'
  selfLink: BLANK
  uid: BLANK
spec:
  replicas: 1
  selector:
    app: sentry
    deploymentconfig: sentry
  strategy:
    activeDeadlineSeconds: 21600
    resources: {}
    rollingParams:
      intervalSeconds: 1
      maxSurge: 25%
      maxUnavailable: 25%
      timeoutSeconds: 600
      updatePeriodSeconds: 1
    type: Rolling
  template:
    metadata:
      annotations:
        openshift.io/generated-by: OpenShiftWebConsole
      creationTimestamp: null
      labels:
        app: sentry
        deploymentconfig: sentry
    spec:
      containers:
        - env:
            - name: SENTRY_SECRET_KEY
              value: Iamsosecret
            - name: C_FORCE_ROOT
              value: '1'
            - name: SENTRY_FILESTORE_DIR
              value: /var/lib/sentry/files/data
          image: BLANK
          imagePullPolicy: Always
          name: sentry
          ports:
            - containerPort: 9000
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /var/lib/sentry/files
              name: sentry-1
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
        - emptyDir: {}
          name: sentry-1
  test: false
  triggers:
    - type: ConfigChange
    - imageChangeParams:
        automatic: true
        containerNames:
          - sentry
        from:
          kind: ImageStreamTag
          name: 'sentry:latest'
          namespace: catcloud
        lastTriggeredImage: BLANK
      type: ImageChange
status:
  availableReplicas: 1
  conditions:
    - lastTransitionTime: BLANK
      lastUpdateTime: BLANK
      message: Deployment config has minimum availability.
      status: 'True'
      type: Available
    - lastTransitionTime: BLANK
      lastUpdateTime: BLANK
      message: replication controller "sentry-19" successfully rolled out
      reason: NewReplicationControllerAvailable
      status: 'True'
      type: Progressing
  details:
    causes:
      - type: ConfigChange
    message: config change
  latestVersion: 19
  observedGeneration: 20
  readyReplicas: 1
  replicas: 1
  unavailableReplicas: 0
  updatedReplicas: 1

sentry服务:

apiVersion: v1
kind: Service
metadata:
  annotations:
    openshift.io/generated-by: OpenShiftWebConsole
  creationTimestamp: BLANK
  labels:
    app: sentry
  name: sentry
  namespace: test
  resourceVersion: '505555608'
  selfLink: BLANK
  uid: BLANK
spec:
  clusterIP: BLANK
  ports:
    - name: 9000-tcp
      port: 9000
      protocol: TCP
      targetPort: 9000
  selector:
    deploymentconfig: sentry
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

postgresql 的部署配置:

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  annotations:
    openshift.io/generated-by: OpenShiftWebConsole
  creationTimestamp: BLANK
  generation: 10
  labels:
    app: postgres
    type: backend
  name: postgres
  namespace: test
  resourceVersion: '506664185'
  selfLink: BLANK
  uid: BLANK
spec:
  replicas: 1
  selector:
    app: postgres
    deploymentconfig: postgres
    type: backend
  strategy:
    activeDeadlineSeconds: 21600
    resources: {}
    rollingParams:
      intervalSeconds: 1
      maxSurge: 25%
      maxUnavailable: 25%
      timeoutSeconds: 600
      updatePeriodSeconds: 1
    type: Rolling
  template:
    metadata:
      annotations:
        openshift.io/generated-by: OpenShiftWebConsole
      creationTimestamp: null
      labels:
        app: postgres
        deploymentconfig: postgres
        type: backend
    spec:
      containers:
        - env:
            - name: PGDATA
              value: /var/lib/postgresql/data/sql
            - name: POSTGRES_HOST_AUTH_METHOD
              value: trust
            - name: POSTGRESQL_USER
              value: sentry
            - name: POSTGRESQL_PASSWORD
              value: sentry
            - name: POSTGRESQL_DATABASE
              value: sentry
          image: BLANK
          imagePullPolicy: Always
          name: postgres
          ports:
            - containerPort: 5432
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: volume-uirge
              subPath: sql
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        runAsUser: 2000020900
      terminationGracePeriodSeconds: 30
      volumes:
        - name: volume-uirge
          persistentVolumeClaim:
            claimName: postgressql
  test: false
  triggers:
    - type: ConfigChange
    - imageChangeParams:
        automatic: true
        containerNames:
          - postgres
        from:
          kind: ImageStreamTag
          name: 'postgres:latest'
          namespace: catcloud
        lastTriggeredImage: BLANK
      type: ImageChange
status:
  availableReplicas: 1
  conditions:
    - lastTransitionTime: BLANK
      lastUpdateTime: BLANK
      message: Deployment config has minimum availability.
      status: 'True'
      type: Available
    - lastTransitionTime: BLANK
      lastUpdateTime: BLANK
      message: replication controller "postgres-9" successfully rolled out
      reason: NewReplicationControllerAvailable
      status: 'True'
      type: Progressing
  details:
    causes:
      - type: ConfigChange
    message: config change
  latestVersion: 9
  observedGeneration: 10
  readyReplicas: 1
  replicas: 1
  unavailableReplicas: 0
  updatedReplicas: 1

服务配置postgresql:

apiVersion: v1
kind: Service
metadata:
  annotations:
    openshift.io/generated-by: OpenShiftWebConsole
  creationTimestamp: BLANK
  labels:
    app: postgres
    type: backend
  name: postgres
  namespace: catcloud
  resourceVersion: '506548841'
  selfLink: /api/v1/namespaces/catcloud/services/postgres
  uid: BLANK
spec:
  clusterIP: BLANK
  ports:
    - name: 5432-tcp
      port: 5432
      protocol: TCP
      targetPort: 5432
  selector:
    deploymentconfig: postgres
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
默认情况下,

Pods(即使在同一个命名空间中)无法直接 相互交谈。您需要创建一个 Service 以允许一个 pod 接收来自另一个 pod 的连接。通常,一个 pod 通过另一个 pod 的服务连接到另一个 pod,如下图所示:

连接信息看起来像 <servicename>:<serviceport>(例如 elasticsearch-master:9200)而不是 localhost:port

您可以阅读 https://kubernetes.io/docs/concepts/services-networking/service/ 以获取有关服务的更多信息。

N.B: localhost:port 仅适用于同一 pod 内的容器 运行 相互连接,就像 nginx 如何连接到 gravitee-mgmt-api 和 gravitee-mgmt-ui 在我上面的插图中。

对于 pods localhost127.0.0.1 之间的通信不起作用。

使用

获取任何 pod 的 IP
kubectl describe podname

在另一个 pod 中使用该 IP 与上面的 pod 通信。

由于如果重新创建 Pod,Pod IP 会发生变化,因此理想情况下,您应该特别使用 kubernetes service 类型 clusterIP 集群内 pods 之间的通信。

对我来说,您似乎没有正确配置 sentry,这意味着您没有向 sentry pod 提供凭据以连接到 PostgreSQL pod 和 redis pod。

env:
    - name: SENTRY_SECRET_KEY
      valueFrom:
        secretKeyRef:
          name: sentry-sentry
          key: sentry-secret
    - name: SENTRY_DB_USER
      value: "sentry"
    - name: SENTRY_DB_NAME
      value: "sentry"
    - name: SENTRY_DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: sentry-postgresql
          key: postgres-password
    - name: SENTRY_POSTGRES_HOST
      value: sentry-postgresql
    - name: SENTRY_POSTGRES_PORT
      value: "5432"
    - name: SENTRY_REDIS_PASSWORD
      valueFrom:
        secretKeyRef:
          name: sentry-redis
          key: redis-password
    - name: SENTRY_REDIS_HOST
      value: sentry-redis
    - name: SENTRY_REDIS_PORT
      value: "6379"
    - name: SENTRY_EMAIL_HOST
      value: "smtp"
    - name: SENTRY_EMAIL_PORT
      value: "25"
    - name: SENTRY_EMAIL_USER
      value: ""
    - name: SENTRY_EMAIL_PASSWORD
      valueFrom:
        secretKeyRef:
          name: sentry-sentry
          key: smtp-password
    - name: SENTRY_EMAIL_USE_TLS
      value: "false"
    - name: SENTRY_SERVER_EMAIL
      value: "sentry@sentry.local"

有关更多信息,您可以参考他们配置哨兵的地方

https://github.com/maty21/sentry-kubernetes/blob/master/sentry.yaml