参考文档:

https://blog.csdn.net/weixin_39246554/article/details/129334116

https://zhuanlan.zhihu.com/p/555130712?utm_id=0

https://blog.csdn.net/star_apple/article/details/124215035

直接安装:

minikube addons enable ingress

注意minikube版本需要>=1.23,早期版本的docker版ingress运行有bug

然后安装一直报错,估计是镜像的问题:

1[zhangcong@master ~]$ kubectl get pods -n ingress-nginx 
2NAME                                        READY   STATUS              RESTARTS   AGE
3ingress-nginx-admission-create-vpvsd        0/1     ImagePullBackOff    0          73s
4ingress-nginx-admission-patch-kxg7j         0/1     ImagePullBackOff    0          73s
5ingress-nginx-controller-6b55b8874b-np758   0/1     ContainerCreating   0          73s

只能使用国内镜像仓库来安装:

(1)下载部署文件

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/cloud/deploy.yaml

(2)修改上面的部署文件,替换镜像

将:

image: registry.k8s.io/ingress-nginx/controller:v1.5.1@sha256:4ba73c697770664c1e00e9f968de14e08f606ff961c76e5d7033a4a9c593c629

替换为:

image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.5.1

将(有2处):

image: registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20220916-gd32f8c343@sha256:39c5b2e3310dc4264d638ad28d9d1d96c4cbb2b2dcfb52368fe4e3c63f61e10f

替换为:

image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v20220916-gd32f8c343

(3)部署

 1[zhangcong@master ~]$ kubectl apply -f ingress.yaml
 2namespace/ingress-nginx created
 3serviceaccount/ingress-nginx created
 4serviceaccount/ingress-nginx-admission created
 5role.rbac.authorization.k8s.io/ingress-nginx created
 6role.rbac.authorization.k8s.io/ingress-nginx-admission created
 7clusterrole.rbac.authorization.k8s.io/ingress-nginx configured
 8clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission configured
 9rolebinding.rbac.authorization.k8s.io/ingress-nginx created
10rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
11clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx configured
12clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission configured
13configmap/ingress-nginx-controller created
14service/ingress-nginx-controller created
15service/ingress-nginx-controller-admission created
16deployment.apps/ingress-nginx-controller created
17job.batch/ingress-nginx-admission-create created
18job.batch/ingress-nginx-admission-patch created
19ingressclass.networking.k8s.io/nginx configured
20validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission configured

以下为测试 ingress

创建namespace

1$ kubectl create ns test

创建文件 vi whoami-service.yaml,内容如下:

 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  name: whoami-deployment
 5  namespace: test
 6  labels:
 7    app: whoami
 8spec:
 9  replicas: 3
10  selector:
11    matchLabels:
12      app: whoami
13  template:
14    metadata:
15      labels:
16        app: whoami
17    spec:
18      containers:
19        - name: whoami
20          image: jwilder/whoami
21          ports:
22            - containerPort: 8000
23---
24apiVersion: v1
25kind: Service
26metadata:
27  name: whoami-service
28  namespace: test
29spec:
30  type: ClusterIP
31  ports:
32    - port: 80
33      targetPort: 8000
34  selector:
35    app: whoami

应用yaml文件

kubectl apply -f whoami-service.yaml

查看 Deployment

kubectl get deployments -n test

查看 Pod和日志

kubectl get pods -n ingress-nginx

kubectl logs -n ingress-nginx -f ingress-nginx-controller-f57c5b4d9-cjscg

查看 service

kubectl get service -n test

部署ingress

创建文件 vi whoami-ingress.yaml,内容如下:

 1apiVersion: networking.k8s.io/v1
 2kind: Ingress
 3metadata:
 4  name: whoami-ingress
 5  namespace: test
 6  annotations:
 7    kubernetes.io/ingress.class: nginx
 8spec:
 9  rules:
10  - host: whoami.com
11    http:
12      paths:
13        - path: /
14          pathType: Prefix
15          backend:
16            service:
17              name: whoami-service
18              port:
19                number: 80

这里host指定域名,如果需要通过ip访问host:后不填域名就行。

应用yaml文件

kubectl apply -f whoami-ingress.yaml

查看ingress状态

kubectl get ingress

查看服务状态:

1[zhangcong@master k8s]$ kubectl get service -A
2NAMESPACE       NAME                                 TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
3test            kubernetes                           ClusterIP      10.96.0.1        <none>        443/TCP                      46h
4test            whoami-service                       ClusterIP      10.99.75.202     <none>        80/TCP                       16m
5ingress-nginx   ingress-nginx-controller             LoadBalancer   10.107.94.254    <pending>     80:31166/TCP,443:31665/TCP   73m
6ingress-nginx   ingress-nginx-controller-admission   ClusterIP      10.104.198.151   <none>        443/TCP                      73m
7kube-system     kube-dns                             ClusterIP      10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP       46h

加hosts,将前面host配置的域名加到系统的/etc/hosts文件

echo “$(minikube ip) whoami.com” | sudo tee -a /etc/hosts

执行本地访问

curl whoami.com:31166