k8sdeployment部署nginx
时间: 2023-08-29 19:13:30 浏览: 73
可以通过以下步骤使用 Kubernetes Deployment 部署 nginx:
1. 编写 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:1.16.1
ports:
- containerPort: 80
```
2. 执行以下命令创建 Deployment:
```
kubectl apply -f nginx-deployment.yaml
```
3. 等待部署完成,可以执行以下命令查看 Deployment 状态:
```
kubectl get deployments
```
4. 创建 Service,使得外部可以访问 nginx:
```
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
type: ClusterIP
```
5. 执行以下命令创建 Service:
```
kubectl apply -f nginx-service.yaml
```
6. 等待 Service 创建完成,可以执行以下命令查看 Service 状态:
```
kubectl get services
```
7. 通过 Service 的 Cluster IP 访问 nginx,可以执行以下命令获取 Cluster IP:
```
kubectl get services nginx-service
```
8. 在浏览器中访问 Cluster IP 即可访问 nginx。
阅读全文