1.安装ingress
注意minikube版本需要>=1.23,早期版本的docker版ingress运行有bug
执行命令
minikube addons enable ingress
查看所有插件状态
minikube addons list
查看ingress-nginx-controller的pod状态,新版本是查看ingress-nginx命名空间,早期版本是查看kube-system命名空间
kubectl get pods -n ingress-nginx
2.部署demo
2.1创建pod和service
创建文件whoami-service.yaml,内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: whoami-deployment
labels:
app: whoami
spec:
replicas: 3
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: jwilder/whoami
ports:
- containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: whoami-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8000
nodePort: 30080
selector:
应用yaml文件
kubectl apply -f whoami-service.yaml
查看deployment,pod,service状态
# 查看 Deployment
kubectl get deployments
# 查看 Pod
kubectl get pods
# 查看 service
kubectl get service
#查看到的service状态时,LoadBalancer类型的EXTERNAL-IP状态为pending不影响运行结果
service状态pending解决
执行命令
minikube tunnel
2.2部署ingress
创建文件whoami-ingress.yaml
内容如下
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: whoami-ingress
spec:
rules:
- host: a.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: whoami-service
port:
number: 80
这里host指定域名,如果需要通过ip访问host:后不填域名就行。
应用yaml文件
kubectl apply -f whoami-ingress.yaml
查看ingress状态
kubectl get ingress
加hosts,将前面host配置的域名加到系统的/etc/hosts文件
echo "$(minikube ip) a.example.com" | sudo tee -a /etc/hosts
执行本地访问
curl a.example.com
默认情况会返回deployment名称
运行结果如图