Docker Kubernetes hostPort 代理轉發
hostPort:
- 1. 類似docker -p 映射宿主級端口到容器。
- 2. 容器所在的主機暴露端口轉發到指定容器中。
- 3. hostport是通過代理轉發的。
環境:
- 系統:Centos 7.4 x64
- Docker版本:18.09.0
- Kubernetes版本:v1.8
- 管理節點:192.168.1.79
- 工作節點:192.168.1.78
- 工作節點:192.168.1.77
1、創建yaml文件
vim hostport.yaml
apiVersion: v1 kind: Pod metadata: name: nginx-pod2 labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 ports: - name: http containerPort: 80 hostIP: 0.0.0.0 hostPort: 89 protocol: TCP - name: https containerPort: 443 hostIP: 0.0.0.0 hostPort: 443 protocol: TCP

# 指定api版本 apiVersion: v1 # 指定需要創建的資源對象 kind: Pod metadata: # 源數據、可以寫name,命名空間,對象標簽 name: nginx-pod2 # 指定標簽 labels: # 標簽名 app: nginx # 描述資源相關信息 spec: # 指定容器信息 containers: # 容器名 - name: nginx # 容器鏡像名 image: nginx:1.10 # hostport管理 ports: # 指定http端口名稱 - name: http # 指定容器端口 containerPort: 80 # hsotip監聽IP,可通過哪些宿主級ip訪問 hostIP: 0.0.0.0 # 宿主級暴露端口,它會映射到containerport的容器端口 hostPort: 89 # 指定協議類型 protocol: TCP # 指定https - name: https # 指定容器端口 containerPort: 443 # hsotip監聽IP,可通過哪些宿主級ip訪問 hostIP: 0.0.0.0 # 宿主級暴露端口,它會映射到containerport的容器端口 hostPort: 443 # 指定協議類型 protocol: TCP
注:可代理多個端口,這里代理的容器端口為80與443。
2、創建容器
kubectl create -f hostport.yaml

命令:kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-pod2 1/1 Running 0 1m

命令:ubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-pod2 1/1 Running 0 2m 172.17.2.4 192.168.1.78

命令:netstat -lnpt | grep 89 tcp6 0 0 :::89 :::* LISTEN 13373/docker-proxy 命令:netstat -lnpt | grep 443 tcp6 0 0 :::443 :::* LISTEN 13359/docker-proxy
3、瀏覽器測試