做鏡像時候忘記expose端口了, 或者要做一個通用的鏡像, expose端口不固定, 又要在k8s環境里跑並暴漏服務訪問,怎么破?
實際上: yaml的
ports:
- containerPort: 8000
相當於 docker run --expose
構建無expose的鏡像
[root@n1 pyhttp]# cat Dockerfile
from ubuntu:14.04
workdir /
#expose 8000
entrypoint ["python3", "-m", "http.server"]
docker build -t pyhttp .
啟動后訪問
docker run --expose 8000 -p 8000:8000 -itd pyhttp
pyhttp在k8s環境中跑
[root@n1 pyhttp]# cat pyhttp.yaml
apiVersion: v1
kind: Pod
metadata:
name: pyhttp
labels:
name: pyhttp
spec:
containers:
- name: pyhttp
image: pyhttp
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8000
[root@n1 pyhttp]# cat pyhttp-svc.yaml
apiVersion: v1
kind: Pod
metadata:
name: pyhttp
labels:
name: pyhttp
spec:
containers:
- name: pyhttp
image: pyhttp
imagePullPolicy: IfNotPresent
觀察pyhttp的pod和svc狀態如下
啟動一個curl鏡像驗證svc
kubectl run -it --rm --restart=Never curl --image=appropriate/curl sh