大网数据广告活动
服务器运维问答

Can host aliases be assigned to deployments in Kubernetes? If so, how?

· 52CE 我爱测

This article describes how to assign host aliases to pods in kubernetes, is there anyway to do it for a deployment and not for a pod as such? Any other suggestions to add host entries in kubernetes to provide a first line of host…

问题原文

原帖链接 · Ulukai · CC BY-SA 3.0

This article describes how to assign host aliases to pods in kubernetes, is there anyway to do it for a deployment and not for a pod as such?

Any other suggestions to add host entries in kubernetes to provide a first line of host name resolution (before checking a server like 8.8.8.8) would be welcomed as an answer as well.

已采纳解答

原帖链接 · neilH · CC BY-SA 3.0

Yes this is possible. All you need to do is follow the same advice you were for a pod specification, but rather than applying it to a YAML file for pods, you apply it to a YAML file for a deployment. For example, if you are already running a deployment you can edit the current deployment by issuing the following command.

$ kubectl edit deployment DEPLOYMENT_NAME

This will allow you to access edit mode of the currently running deployment in YAML format.

You need to add the 'hostAliases' section in the deployments 'template: spec' field which allows you to configure the template for the pod/containers. So to demonstrate this visually, here is the YAML for a deployment I am running in my project that I can edit by running the command I mentioned above:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "6"
  creationTimestamp: 2018-01-30T14:42:48Z
  generation: 7
  labels:
    app: nginx-site-app
  name: nginx-site
  namespace: default
  resourceVersion: "778922"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-site
  uid: dc4535333d-05cb-11e8-b5c0-7878748e0178
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-site-app
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx-site-app
    spec:
      containers:
      - image: gcr.io/myprojectid/tuneup-nginx:latest
        imagePullPolicy: Always
        name: nginx-container
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: 2018-01-30T14:55:28Z
    lastUpdateTime: 2018-01-30T14:55:28Z
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  observedGeneration: 7
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

If I want to add 'hostAliases' to the pods within this deployment, I need to add this information to the pod template spec section as demonstrated below (notice it is in line with 'containers' (***important- it's worth noting that there are two 'spec' sections within my file- I don't want to add it to the first spec section, but rather the template spec section which defines the pod template):

     spec:
       containers:
       - image: gcr.io/development-project-192309/tuneup-nginx:latest
         imagePullPolicy: Always
         name: nginx-container
         ports:
         - containerPort: 80
           protocol: TCP
       hostAliases:
       - ip: 127.0.0.1
         hostnames:
         - myadded.examplehostname

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-01-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。