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信息
