k8s给pod添加hosts
###
1. 背景
线上待办功能一直不通,发现正式环境的待办系统域名无法访问,需要配置hosts。因为应用部署在云上,需使用k8s给pod添加域名IP映射。
2. 实战过程
使用yaml方式创建的pod,使用hostAliases添加域名IP映射,yaml内容如下: apiVersion: apps/v1 kind: Deployment metadata: name: smart-pod namespace: default spec: replicas: 1 selector: matchLabels: app: smart-pod template: metadata: labels: app: smart-pod spec: hostAliases: - ip: "10.236.9.220" hostnames: - "intf2.portal.unicom.local" containers: - name: smart-hr image: www.docker.com/images/smart-pod:latest ports: - name: smart-pod-port containerPort: 8090 protocol: TCP
3.修改yaml后,重新启动pod
# 查看pod kubectl get pods # 删除pod kubectl delete -f smart-pod.yaml # 创建pod kubectl create -f smart-pod.yaml # 查看pod kubectl get pods
4.查看hosts是否添加成功
kubectl exec smart-pod-name -- cat /etc/hosts
OK, 搞定
###