可以将主机别名分配给 Kubernetes 中的部署吗?如果是这样,如何?

Ulu*_*kai 8 hosts internal-dns kubernetes google-kubernetes-engine

这篇文章描述了如何在 kubernetes 中为 pod 分配主机别名,无论如何都可以为部署而不是为 pod 那样做?

在 kubernetes 中添加主机条目以提供主机名解析的第一行(在检查像 8.8.8.8 这样的服务器之前)的任何其他建议也将作为答案受到欢迎。

nei*_*ilH 19

是的,这是可能的。您需要做的就是遵循与 pod 规范相同的建议,但不是将其应用于 pod 的 YAML 文件,而是将其应用于部署的 YAML 文件。例如,如果您已经在运行部署,则可以通过发出以下命令来编辑当前部署。

$ kubectl 编辑部署 DEPLOYMENT_NAME

这将允许您以 YAML 格式访问当前运行的部署的编辑模式。

您需要在部署的“模板:规范”字段中添加“hostAliases”部分,它允许您为 pod/容器配置模板。因此,为了直观地演示这一点,这里是我在项目中运行的部署的 YAML,我可以通过运行我上面提到的命令进行编辑:

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
Run Code Online (Sandbox Code Playgroud)

如果我想在此部署中的 pod 中添加“hostAliases”,我需要将此信息添加到 pod 模板规范部分,如下所示(注意它与“容器”一致(***重要 - 值得注意的是我的文件中有两个“规范”部分 - 我不想将它添加到第一个规范部分,而是定义 pod 模板的模板规范部分):

     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
Run Code Online (Sandbox Code Playgroud)


Rad*_*nka 5

hostAliases是 的一部分PodSpec,您也可以在 Deployment 下的 Deployment 中找到它spec.template.spec,因此您可以在 Deployments Pod 规范模板中以与 Pod 本身相同的方式轻松使用它。