If DNS or other related settings are inappropriate, you can use hostAliases to overwrite the resolution of the hostname at the pod level when adding entries to the /etc/hosts file of the pod.
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
Parameter |
Mandatory |
Description |
---|---|---|
apiVersion |
Yes |
API version number |
kind |
Yes |
Type of the object to be created |
metadata |
Yes |
Metadata definition of a resource object |
name |
Yes |
Name of a pod |
spec |
Yes |
Detailed description of the pod. For details, see Table 2. |
Parameter |
Mandatory |
Description |
---|---|---|
hostAliases |
Yes |
Host alias |
containers |
Yes |
For details, see Table 3. |
kubectl create -f hostaliases-pod.yaml
If information similar to the following is displayed, the pod is created.
pod/hostaliases-pod created
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
docker ps |grep hostaliases-pod
docker exec -ti Container ID /bin/sh