在Kubernetes(简称K8S)中,可以通过Ingress来实现工作负载同一域名双网站的部署。在这篇文章中,我将介绍如何在K8S中实现这样一个方案,并向刚入行的同事详细解释每一步需要做什么以及使用的代码示例。

### 流程概述

以下是实现"K8S工作负载同一域名双网站"的流程概述:

| 步骤 | 描述 |
|-------------------|-------------------------------------|
| 步骤一:创建两个Deployment | 创建两个Deployment,分别代表两个网站 |
| 步骤二:创建两个Service | 创建两个Service来暴露Deployment的服务 |
| 步骤三:创建Ingress | 创建Ingress资源来将域名路由到不同的Service |


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

#### 步骤一:创建两个Deployment

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

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

#### 步骤二:创建两个Service

```yaml
# website-one-service.yaml
apiVersion: v1
kind: Service
metadata:
name: website-one
spec:
selector:
app: website-one
ports:
- protocol: TCP
port: 80
targetPort: 80

---
# website-two-service.yaml
apiVersion: v1
kind: Service
metadata:
name: website-two
spec:
selector:
app: website-two
ports:
- protocol: TCP
port: 80
targetPort: 80
```

#### 步骤三:创建Ingress

```yaml
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: mydomain.com
http:
paths:
- path: /website-one
pathType: Prefix
backend:
service:
name: website-one
port:
number: 80
- path: /website-two
pathType: Prefix
backend:
service:
name: website-two
port:
number: 80
```

在上面的示例中,我们创建了两个Deployment(代表两个不同的网站),两个Service来暴露Deployment的服务,以及一个Ingress来将域名路由到不同的Service上。

通过这种方式,我们可以在K8S中实现工作负载同一域名双网站的部署。希望这篇文章对你有所帮助!如果有任何问题,欢迎随时向我提问。