🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] 现在常用解析域名的方式有两种: 1. 在dns服务器注册一个域名, 2. 在容器的 `/etc/hosts` 上添加域名 在 `kubernetes` 上也是两种方案,只不过第一钟是在 `coredns` 上添加静态解析即可。 ## coreDNS静态解析 ### 修改coredns配置文件 ```shell $ kubectl -n kube-system edit cm coredns # 在 prometheus :9153 字段上面添加下面内容。 # fallthrough 不能省略该字段,否则不会解析其他域名 hosts { 192.168.31.188 www.ecloud.com fallthrough } ``` ### 重启服务 ```shell $ kubectl -n kube-system delete pod -l k8s-app=kube-dns ``` ### 测试 ```shell $ kubectl run busybox --image=busybox:1.24.1 sleep 3600 pod/busybox created $ kubectl exec -it busybox -- nslookup www.ecloud.com Server: 10.183.0.254 Address 1: 10.183.0.254 kube-dns.kube-system.svc.cluster.local Name: www.ecloud.com Address 1: 192.168.31.188 www.ecloud.com ``` > 注意:需要稍等几分钟,再测试测试是否成功 ## hosts添加域名解析 在启动pod的时候,添加hosts文件字段(pod.spec.hostAliases)。 1. deployment清单文件 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: busybox spec: selector: matchLabels: name: busybox template: metadata: labels: name: busybox spec: hostAliases: - hostnames: - todo.ecloud.com ip: 192.168.31.100 containers: - name: busybox image: busybox:1.24.1 imagePullPolicy: IfNotPresent command: - /bin/sh - -c - sleep 3600 ``` 2. 启动deployment ```shell $ kubectl apply -f test.yml deployment.apps/busybox created ``` 3. 验证 ```shell # 查看hosts文件 $ kubectl exec -it busybox-79b94f5dd8-zht64 -- cat /etc/hosts # Kubernetes-managed hosts file. 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet fe00::0 ip6-mcastprefix fe00::1 ip6-allnodes fe00::2 ip6-allrouters 20.0.32.133 busybox-79b94f5dd8-zht64 # Entries added by HostAliases. 192.168.31.100 todo.ecloud.com # ping域名 $ kubectl exec -it busybox-79b94f5dd8-zht64 -- ping -c4 todo.ecloud.com PING todo.ecloud.com (192.168.31.100): 56 data bytes 64 bytes from 192.168.31.100: seq=0 ttl=64 time=0.465 ms 64 bytes from 192.168.31.100: seq=1 ttl=64 time=0.070 ms 64 bytes from 192.168.31.100: seq=2 ttl=64 time=0.090 ms 64 bytes from 192.168.31.100: seq=3 ttl=64 time=0.080 ms --- todo.ecloud.com ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max = 0.070/0.176/0.465 ms ```