使用hostalias为容器额外定义dns
准备个容器
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
labels:
app: busybox
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: centos:latest
ports:
- name: http
containerPort: 8080
protocol: TCP
command: ["/bin/sh"]
args: ["-c","sleep 1000"]
让我们看看他默认状态下访问百度是什么样的
[root@master ~]# kubectl exec -it busybox-54b6dd484b-9pc22 -c busybox -- sh
sh-4.4# ping baidu.com
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=51 time=9.05 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=2 ttl=51 time=9.05 ms
好,让我们nslookup一下阿里云的ip,挑选其中一个
nslookup aliyun.com
Server: 100.100.2.136
Address: 100.100.2.136#53
Non-authoritative answer:
Name: aliyun.com
Address: 106.11.249.99
加上一点小小的魔法
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
labels:
app: busybox
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
hostAliases:
- ip: "106.11.249.99"
hostnames:
- "baidu.com"
containers:
- name: busybox
image: centos:latest
ports:
- name: http
containerPort: 8080
protocol: TCP
command: ["/bin/sh"]
args: ["-c","sleep 1000"]
再进去看看,乐
[root@master ~]# kubectl exec -it busybox-7995d79d96-pnm2f -c busybox -- sh
sh-4.4# ping baidu.com
PING baidu.com (106.11.249.99) 56(84) bytes of data.
64 bytes from baidu.com (106.11.249.99): icmp_seq=1 ttl=48 time=26.7 ms
64 bytes from baidu.com (106.11.249.99): icmp_seq=2 ttl=48 time=26.7 ms
当你想要给你的应用配置某个dns,但是又不想直接修改coredns以影响整个集群的时候,你就可以使用这个
评论已关闭