0
点赞
收藏
分享

微信扫一扫

Ingress Nginx Controller 使用

zidea 2023-06-26 阅读 93

1.Ingress Nginx Controller 安装

helm安装

[root@k8s-master01 26]# wget https://get.helm.sh/helm-v3.6.3-linux-amd64.tar.gz
[root@k8s-master01 26]# tar -zxvf helm-v3.0.0-linux-amd64.tar.gz
[root@k8s-master01 26]# mv linux-amd64/helm /usr/local/bin/helm
[root@k8s-master01 26]# helm version
version.BuildInfo{Version:"v3.8.2", GitCommit:"6e3701edea09e5d55a8ca2aae03a68917630e91b", GitTreeState:"clean", GoVersion:"go1.17.5"}

下载ingress-controller控制包

[root@k8s-master01 26]# helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
[root@k8s-master01 26]# helm repo update
[root@k8s-master01 26]# helm pull ingress-nginx/ingress-nginx --version 4.0.1

更改对应的配置

[root@k8s-master01 26]# tar xf ingress-nginx-4.0.1.tgz
[root@k8s-master01 26]# cd ingress-nginx
vim values.yaml
#########
需要修改的位置
a) Controller 和 admissionWebhook 的镜像地址,需要将公网镜像同步至公司内网镜
像仓库
b) 镜像的 digest 值注释
c) hostNetwork 设置为 true
d) dnsPolicy 设置为 ClusterFirstWithHostNet
e) NodeSelector 添加 ingress: "true"部署至指定节点
f) 类型更改为 kind: DaemonSet
g) 讲 ingress nginx 设置为默认的 ingressClass

部署 ingress,给需要部署 ingress 的节点上打标签
[root@k8s-master01 26]# kubectl label node k8s-node01 ingress=true
[root@k8s-master01 26]# kubectl create ns ingress-nginx
[root@k8s-master01 26]# helm install ingress-nginx -n ingress-nginx .


ingress-nginx的进阶

  1. ingress-nginx的简单使用

[root@k8s-master01 26]# kubectl create deployment web --image=nginx:1.23
deployment.apps/web created
[root@k8s-master01 26]# kubectl expose deployment web --port 80
service/web exposed
[root@k8s-master01 26]# kubectl get po -A
NAMESPACE       NAME                                     READY   STATUS    RESTARTS      AGE
default         nginx1-d6667489c-fcdkg                   1/1     Running   0             93m
default         web-75dbdc8458-vnvpk                     1/1     Running   0             30s
ingress-nginx   ingress-nginx-controller-548nq           1/1     Running   3 (61m ago)   119m
kube-system     calico-kube-controllers-d4bfdcb9-srq4h   1/1     Running   0             152m
kube-system     calico-node-l58nj                        1/1     Running   0             152m
kube-system     calico-node-rtzj5                        1/1     Running   0             152m
kube-system     coredns-6d8c4cb4d-5d9sw                  1/1     Running   0             3h50m
kube-system     coredns-6d8c4cb4d-r68nm                  1/1     Running   0             3h50m
kube-system     etcd-k8s-master01                        1/1     Running   0             3h51m
kube-system     kube-apiserver-k8s-master01              1/1     Running   1 (63m ago)   3h51m
kube-system     kube-controller-manager-k8s-master01     1/1     Running   9 (64m ago)   3h51m
kube-system     kube-proxy-bzkbv                         1/1     Running   0             3h50m
kube-system     kube-proxy-jlhps                         1/1     Running   0             152m
kube-system     kube-scheduler-k8s-master01              1/1     Running   9 (64m ago)   3h51m
study-ingress   nginx-5b78fb99bc-vtbhl                   1/1     Running   0             110m

#创建ingress规则
[root@k8s-master01 26]# cat web-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
  - host: web.example.com
    http:
      paths:
      - backend:
          service:
            name: web
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific

kubectl apply -f web-ingress.yaml
备注:ingress的控制器部署在node01节点,访问的时候需要在node01的/etc/hosts文件
里面配置相关内容
[root@k8s-node01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.215.15 k8s-node01 myapp.example.com nginx.test-788.com web.example.com

[root@k8s-node01 ~]# curl -I web.example.com
HTTP/1.1 200 OK
Date: Mon, 26 Jun 2023 14:34:33 GMT
Content-Type: text/html
Content-Length: 615
Connection: keep-alive
Last-Modified: Tue, 28 Mar 2023 15:01:54 GMT
ETag: "64230162-267"
Accept-Ranges: bytes


  1. Ingress Nginx 域名重定向 Redirect

以上面的web服务举例,修改ingress的访问规

[root@k8s-master01 26]# cat  web-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/permanent-redirect: "https://www.baidu.com"
spec:
  rules:
  - host: web.example.com
    http:
      paths:
      - backend:
          service:
            name: web
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific

执行kubectl apply -f web-ingress.yaml ,在windowns电脑的/etc/hosts配置相关的数据如图

Ingress Nginx Controller 使用_ico

浏览器打开就直接跳到百度了

举报

相关推荐

0 条评论