【问题标题】:Setting the correct hostname in Kubernetes/Nginx for the client在 Kubernetes/Nginx 中为客户端设置正确的主机名
【发布时间】:2018-08-03 13:59:19
【问题描述】:

我正在使用在 Kubernetes 上运行的 dockerized 微服务架构和 Nginx,并且遇到了主机名问题。如何正确地将主机名添加到 Kubernetes(或者 Nginx)?

问题:当名为 admin 的微服务 A 尝试与名为 session 的微服务 B 通信时,admin 记录以下错误并且未到达 session

{ Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's 
altnames: Host: session. is not in the cert's altnames: DNS:*.example.com, example.com
at Object.checkServerIdentity (tls.js:225:17)
at TLSSocket.onConnectSecure (_tls_wrap.js:1051:27)
at TLSSocket.emit (events.js:160:13)
at TLSSocket._finishInit (_tls_wrap.js:638:8)
  reason: 'Host: session. is not in the cert\'s altnames: 
DNS:*.example.com, example.com',
  host: 'session',
  cert:
   { subject: { OU: 'Domain Control Validated', CN: 
'*.example.com' },
     issuer: ...

针对此错误,我尝试更新 kubernetes config yaml 文件中的主机名失败(基于this)。请参阅下面添加的hostname

apiVersion: apps/v1
kind: Deployment
metadata:
  name: session
  namespace: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: session
      component: demo
  template:
    metadata:
      labels:
        app: session
        component: demo
    spec:
      hostname: session.example.com . ----> added host name here
      imagePullSecrets:
        - name: docker-secret 
      containers:
      - name: session
       ...

但是,当我尝试在 Kubernetes 中应用这个更新的配置文件时,会出现一个错误,我不能使用句点。如果我不能使用句点,并且主机名是*.example.com(即session.example.com),那么应该在哪里/如何更新主机名。

The Deployment "session" is invalid: spec.template.spec.hostname: 
Invalid value: "session.example.com": a DNS-1123 label must 
consist of lower case alphanumeric characters or '-', and must start and 
end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex 
used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')

同时,nginx 配置文件中的服务器名称确实更新为session.example.com

upstream session {
  server 127.0.0.1:3000;
  keepalive 32;
}

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name "session.example.com";  ---> updated for hostname 

  ssl_certificate      /etc/ssl/nginx/certificate.pem;
  ssl_certificate_key  /etc/ssl/nginx/key.pem;

  location / {
      proxy_pass http://session/;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
  }
}


server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name "session.example.com";    ---> updated for hostname 

  return 301 https://$host$request_uri;
}

您建议如何解决此问题?我的目标是让adminsession 成功通信。

【问题讨论】:

    标签: docker nginx kubernetes ssl-certificate microservices


    【解决方案1】:

    你可以使用kubernetes自己的dns。

    https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

    因此您可以使用 pod dns 访问您的 pod;

    启用后,Pod 会被分配一条 DNS A 记录,格式为

    “pod-ip-address.my-namespace.pod.cluster.local”

    您可以使用服务

    my-svc.my-namespace.svc.cluster.local

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2016-10-22
      • 2020-03-31
      • 1970-01-01
      • 2021-04-28
      • 2018-04-21
      相关资源
      最近更新 更多