Reviewed-by: Eotvos, Oliver <oliver.eotvos@t-systems.com> Co-authored-by: Dong, Qiu Jian <qiujiandong1@huawei.com> Co-committed-by: Dong, Qiu Jian <qiujiandong1@huawei.com>
16 KiB
Using hostAliases to Configure /etc/hosts in a Pod
Scenario
If DNS or other related settings are inappropriate, you can use hostAliases to overwrite the resolution of the host name at the pod level when adding entries to the /etc/hosts file of the pod.
Procedure
- Use kubectl to connect to the cluster.
- Create the hostaliases-pod.yaml file.
vi hostaliases-pod.yaml
The field in bold in the YAML file indicates the image name and tag. You can replace the example value as required.
apiVersion: v1 kind: Pod metadata: name: hostaliases-pod spec: hostAliases: - ip: 127.0.0.1 hostnames: - foo.local - bar.local - ip: 10.1.2.3 hostnames: - foo.remote - bar.remote containers: - name: cat-hosts image: tomcat:9-jre11-slim lifecycle: postStart: exec: command: - cat - /etc/hosts imagePullSecrets: - name: default-secret
Table 1 pod field description Parameter
Mandatory/Optional
Description
apiVersion
Mandatory
API version number
kind
Mandatory
Type of the object to be created
metadata
Mandatory
Metadata definition of a resource object
name
Mandatory
Name of a pod
spec
Mandatory
Detailed description of the pod. For details, see Table 2.
Table 2 spec field description Parameter
Mandatory/Optional
Description
hostAliases
Mandatory
Host alias
containers
Mandatory
For details, see Table 3.
- Create a pod.
kubectl create -f hostaliases-pod.yaml
If information similar to the following is displayed, the pod is created.
pod/hostaliases-pod created
- Query the pod status.
kubectl get pod hostaliases-pod
If the pod is in the Running state, the pod is successfully created.
NAME READY STATUS RESTARTS AGE hostaliases-pod 1/1 Running 0 16m
- Check whether the hostAliases functions properly.
docker ps |grep hostaliases-pod
docker exec -ti Container ID /bin/sh