k8s部署ingress-nginx

官网安装指南可以查看这里:https://kubernetes.github.io/ingress-nginx/deploy/

首先我们下载ingress deploy文件

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

注意ingress和k8s需要版本匹配,因为我k8s是1.19版本,所以ingress选择的是v1.1.3,下载到上述deploy.yaml文件以后,由于网络问题,需要将镜像修改下

k8s.gcr.io/ingress-nginx/controller:v1.1.3@sha256:0bc88eb15f9e7f84e8e56c14fa5735aaa488b840983f87bd79b1054190e660de
#修改为:
willdockerhub/ingress-nginx-controller:v1.1.3
#另一个镜像
k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660
#修改为:
dyrnq/kube-webhook-certgen:v1.1.1
#然后注释
externalTrafficPolicy: Local
#修改
type: LoadBalancer
#为
type: NodePort

改完以后直接部署

kubectl apply -f deploy.yaml

然后查看POD和SVC

kubectl get pod -n ingress-nginx -o wide
kubectl get svc -n ingress-nginx -o wide

微信截图_20230531195023.png
接下来我们准备svc、pod和ingress

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-pod
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx-pod
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: default
spec:
  ports:
    - port: 8333
      name: nginx-pod
      protocol: TCP
      targetPort: 80
  selector:
    app: nginx-pod
  #type: ClusterIP
  sessionAffinity: None
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-http
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: "test.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: nginx-service
            port:
              number: 8333

通过cluster IP访问

微信截图_20230531195327.png

通过添加的ingress规则中的test.com域名访问,需要带上ingress-nginx-controller映射的端口39676

微信截图_20230531195255.png

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/918.html