Docker Kubernetes hostPort 代理转发

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、浏览器测试

 

posted @ 2018-11-26 09:56  kevin.Xiang  阅读(4965)  评论(2编辑  收藏  举报