#### Step 1: 安装和配置Kubernetes
在开始之前,确保你已经安装好了Docker和kubectl,并且拥有一个可以访问的K8S集群。
1. 安装Minikube
```bash
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
&& sudo install minikube-linux-amd64 /usr/local/bin/minikube
```
2. 启动Minikube
```bash
minikube start
```
3. 部署Kubernetes Dashboard
```bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
```
4. 创建Dashboard管理员用户
```bash
kubectl apply -f dashboard-adminuser.yaml
```
#### Step 2: 创建Deployment
1. 创建Deployment yaml 文件
```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
```
2. 应用Deployment
```bash
kubectl apply -f nginx-deployment.yaml
```
#### Step 3: 创建Service
1. 创建Service yaml 文件
```yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
```
2. 应用Service
```bash
kubectl apply -f nginx-service.yaml
```
#### Step 4: 使用Ingress Controller
1. 安装Nginx Ingress Controller
```bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
```
2. 创建Ingress yaml 文件
```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
```
3. 应用Ingress
```bash
kubectl apply -f nginx-ingress.yaml
```
#### Step 5: 监控和调试
1. 安装Kubernetes Dashboard
```bash
minikube dashboard
```
2. 查看集群状态
```bash
kubectl cluster-info
```
3. 调试Pod
```bash
kubectl exec -it
```
通过以上步骤,你已经实现了一个简单的Kubernetes Deployment,并且通过Service和Ingress暴露了服务。在实际项目中,可以根据需求进行扩展和优化,以实现更加强大和稳定的应用程序部署和管理。
祝你学习顺利,早日成为Kubernetes大神!