如何为我的 kubernetes 部署和服务创建 ingress-nginx?

How to create ingress-nginx for my kubernetes deployment and service?

我可以使用 LoadBalancer 服务类型访问我的 django 应用部署,但我正在尝试切换到 ClusterIP 服务类型和 ingress-nginx,但是当我尝试通过主持人 url。描述入口也显示 error: endpoints "django-service" not founderror: endpoints "default-http-backend" not found。我做错了什么?

这是我的服务和入口 yaml:

---
apiVersion: v1
kind: Service
metadata:
  name: django-service
spec:
  type: ClusterIP
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 8000
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: django-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/force-ssl-redirect: 'true'
    nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
  tls:
  - hosts:
    - django.example.com
  rules:
  - host: django.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: django-service
            port:
              number: 80
  ingressClassName: nginx

kubectl 获取全部

$ kubectl get all
NAME                                        READY   STATUS    RESTARTS   AGE
pod/django-app-5bdd8ffff9-79xzj             1/1     Running   0          7m44s
pod/postgres-58fffbb5cc-247x9               1/1     Running   0          7m44s

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/django-service   ClusterIP   10.233.29.58    <none>        80/TCP     7m44s
service/pg-service       ClusterIP   10.233.14.137   <none>        5432/TCP   7m44s

NAME                                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/django-app             1/1     1            1           7m44s
deployment.apps/postgres               1/1     1            1           7m44s

NAME                                              DESIRED   CURRENT   READY   AGE
replicaset.apps/django-app-5bdd8ffff9             1         1         1       7m44s
replicaset.apps/postgres-58fffbb5cc               1         1         1       7m44s

描述入口

$ kubectl describe ing django-ingress
Name:             django-ingress
Labels:           <none>
Namespace:        django
Address:          10.10.30.50
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
TLS:
  SNI routes django.example.com
Rules:
  Host                      Path  Backends
  ----                      ----  --------
  django.example.com
                            /   django-service:80 (<error: endpoints "django-service" not found>)
Annotations:                nginx.ingress.kubernetes.io/force-ssl-redirect: true
                            nginx.ingress.kubernetes.io/rewrite-target: /
                            nginx.ingress.kubernetes.io/ssl-redirect: true
Events:
  Type    Reason  Age                   From                      Message
  ----    ------  ----                  ----                      -------
  Normal  Sync    5m28s (x2 over 6m5s)  nginx-ingress-controller  Scheduled for sync
  Normal  Sync    5m28s (x2 over 6m5s)  nginx-ingress-controller  Scheduled for sync

我想你忘了在服务中部署 link。

apiVersion: v1
kind: Service
metadata:
  name: django-service
spec:
  type: ClusterIP
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 8000
  selector:
    app: your-deployment-name

您的标签也必须在您的部署中设置:

spec:
  selector:
    matchLabels:
      app: your-deployment-name
  template:
    metadata:
      labels:
        app: your-deployment-name