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
修改yaml后,重新启动pod
# 查看pod
kubectl get pods
# 删除pod
kubectl delete -f smart-pod.yaml
# 创建pod
kubectl create -f smart-pod.yaml
# 查看pod
kubectl get pods
查看hosts是否添加成功
kubectl exec smart-pod-name -- cat /etc/hosts
OK, 搞定
3. 引用
Kubernetes HostAliases添加其他主机别名到Pod
k8s之Deployment 资源详解
每天5分钟|轻松掌握开发工作必会的k8s-deployment与rs
K8S中 yaml 文件详解
k8s中pod常见的启动和运行错误
K8S Pod ImagePullBackOff
查看kubernets上的image信息