0%

Kubernetes-HostAliases

k8s里增加hosts

https://kubernetes.io/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/

k8s上微服务之间可以通过service的域名来互相访问。域名的解析是一般是通过在集群中的kube-dns来完成的。

如需向Pod的/etc/hosts文件添加条目实现解析需求时,可以使用Pod Spec中的HostAliases字段添加自定义条目。

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
service/networking/hostaliases-pod.yaml 

apiVersion: v1
kind: Pod
metadata:
name: hostaliases-pod
spec:
restartPolicy: Never
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: busybox
command:
- cat
args:
- "/etc/hosts"

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
apiVersion: apps/v1beta2
kind: Deployment
metadata:
labels:
app: vcg-gateway
name: vcg-gateway
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: vcg-gateway
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: vcg-gateway
spec:
containers:
- env:
- name: aliyun_logs_vcg-gateway
value: stdout
image: >-
registry-vpc.cn-beijing.aliyuncs.com/vcg/vcg-gateway:pro-20190423162116
imagePullPolicy: Always
name: vcg-gateway
resources:
limits:
cpu: '1'
memory: 4Gi
requests:
cpu: 500m
memory: 1000Mi
dnsPolicy: ClusterFirst
hostAliases:
- hostnames:
- www.vcg.com
ip: 39.97.197.135
- hostnames:
- www1.visualchina.com
- www1.vcg.com
ip: 39.97.21.16
imagePullSecrets:
- name: registry-vpc.cn-beijing.aliyuncs.com
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30