### 实现K8S Service对外暴露端口号

#### 流程步骤

| 步骤 | 操作描述 |
|------|--------------------------------|
| 1 | 创建一个Deployment |
| 2 | 创建一个Service |
| 3 | 为Service指定外部暴露端口号 |

#### 详细操作步骤及代码示例

1. **创建一个Deployment**

首先,我们需要创建一个Deployment,这里我们以一个简单的Nginx应用为例,使用以下yaml文件创建:

```yaml
# nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
```

使用以下命令创建Deployment:

```bash
kubectl apply -f nginx-deployment.yaml
```

2. **创建一个Service**

接下来,我们需要创建一个Service来暴露Deployment中的Pod,使用以下yaml文件创建Service:

```yaml
# nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
```

使用以下命令创建Service:

```bash
kubectl apply -f nginx-service.yaml
```

3. **为Service指定外部暴露端口号**

最后,我们需要为Service指定一个外部暴露端口号,可以通过NodePort类型的Service实现:

```yaml
# nginx-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30000
type: NodePort
```

使用以下命令创建NodePort类型的Service:

```bash
kubectl apply -f nginx-nodeport.yaml
```

现在,你的Nginx应用就可以通过集群节点的`NodeIP:NodePort`访问,比如`192.168.1.100:30000`。

通过以上步骤,你已经成功实现了K8S Service对外暴露端口号的功能。希望这篇文章对你有所帮助!
```

通过以上步骤,你已经成功实现了K8S Service对外暴露端口号的功能。希望这篇文章对你有所帮助! 如果有任何疑问或者问题,欢迎随时联系我,我会尽力帮助你解决问题。祝你在K8S的学习和实践中取得成功!