Can host aliases be assigned to deployments in Kubernetes? If so, how?
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
hostAliases
is part of the PodSpec
, which is what you also find in Deployment under spec.template.spec
in your Deployment so you can easily use it in the same way in your Deployments Pod spec template as you do for Pod it self.