Linux command
kubectl-expose 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Expose deployment
kubectl expose deployment [name] --port=[80]
Expose with type
kubectl expose deployment [name] --port=[80] --type=[LoadBalancer]
Expose pod
kubectl expose pod [pod-name] --port=[8080] --target-port=[80]
Expose with name
kubectl expose deployment [name] --port=[80] --name=[service-name]
Expose NodePort
kubectl expose deployment [name] --port=[80] --type=[NodePort]
说明
kubectl expose creates a Kubernetes Service that provides stable network access to a set of pods managed by a deployment, replica set, pod, or other resource. It automatically generates the service configuration by inspecting the target resource's labels and port definitions, creating a selector-based service without requiring a manual manifest. The command supports four service types: ClusterIP (default) for internal cluster access, NodePort for exposing the service on each node's IP at a static port, LoadBalancer for provisioning an external load balancer through the cloud provider, and ExternalName for mapping to a DNS name. The `--port` flag sets the service's listening port while `--target-port` specifies the container port to forward traffic to. This approach is convenient for quick service creation during development or prototyping. For production environments, defining services declaratively in manifest files alongside the workload definitions provides better reproducibility and version control.
参数
- --port _PORT_
- Service port.
- --target-port _PORT_
- Container port.
- --type _TYPE_
- Service type (ClusterIP, NodePort, LoadBalancer).
- --name _NAME_
- Service name.
- --help
- Display help information.
FAQ
What is the kubectl-expose command used for?
kubectl expose creates a Kubernetes Service that provides stable network access to a set of pods managed by a deployment, replica set, pod, or other resource. It automatically generates the service configuration by inspecting the target resource's labels and port definitions, creating a selector-based service without requiring a manual manifest. The command supports four service types: ClusterIP (default) for internal cluster access, NodePort for exposing the service on each node's IP at a static port, LoadBalancer for provisioning an external load balancer through the cloud provider, and ExternalName for mapping to a DNS name. The `--port` flag sets the service's listening port while `--target-port` specifies the container port to forward traffic to. This approach is convenient for quick service creation during development or prototyping. For production environments, defining services declaratively in manifest files alongside the workload definitions provides better reproducibility and version control.
How do I run a basic kubectl-expose example?
Run `kubectl expose deployment [name] --port=[80]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --port _PORT_ do in kubectl-expose?
Service port.