rmdir.cn

kubectl expose

暴露服务给外部访问

语法

kubectl expose <资源类型> <资源名> --port=<端口> [选项]

说明

为 Deployment、Pod 或 ReplicaSet 创建 Service,使其可以通过集群内或外部网络访问。支持 ClusterIP、NodePort、LoadBalancer 等类型。

参数

  • --port必填

    Service 端口

  • --target-port

    目标 Pod 端口

  • --type

    Service 类型:ClusterIP/NodePort/LoadBalancer

  • --name

    Service 名称

示例

暴露 Deployment

创建 ClusterIP 类型的 Service

kubectl expose deploy nginx --port=80 --target-port=80

创建 NodePort

以 NodePort 方式暴露服务

kubectl expose deploy nginx --port=80 --type=NodePort