首页 > 系统相关 >ingress-nginx部署-helm方式

ingress-nginx部署-helm方式

时间:2024-07-01 19:09:37浏览次数:19  
标签:master1 ingress nginx controller helm k8s root

helm 安装ingress-nginx

Ingress-Nginx Controller 支持多种方式安装:

  • 使用heml安装chart
  • 使用kubectl apply,使用YAML文件;

详情可参考:https://kubernetes.github.io/ingress-nginx/deploy/

本文实践使用helm安装ingress-nginx

环境信息

# k8s版本
root@master1:~# kubectl get node
NAME      STATUS   ROLES           AGE    VERSION
master1   Ready    control-plane   169m   v1.28.2
node1     Ready    <none>          168m   v1.28.2
node2     Ready    <none>          168m   v1.28.2

# helm版本
root@master1:~# helm version
version.BuildInfo{Version:"v3.15.2", GitCommit:"1a500d5625419a524fdae4b33de351cc4f58ec35", GitTreeState:"clean", GoVersion:"go1.22.4"}

[root@k8s-master1 ~]# cat /etc/os-release

# 操作系统和内核版本
root@master1:~# cat /etc/issue
Ubuntu 24.04 LTS \n \l

root@master1:~# uname -r
6.8.0-31-generic

根据配套关系,我这里的k8s 1.28.2安装Ingress-nginx 1.10.1版本。

Ingress-NGINX version k8s supported version Alpine Version Nginx Version Helm Chart Version
v1.10.1 1.29, 1.28, 1.27, 1.26 3.19.1 1.25.3 4.10.1*
v1.10.0 1.29, 1.28, 1.27, 1.26 3.19.1 1.25.3 4.10.0*
v1.9.6 1.29, 1.28, 1.27, 1.26, 1.25 3.19.0 1.21.6 4.9.1*
v1.9.5 1.28, 1.27, 1.26, 1.25 3.18.4 1.21.6 4.9.0*
v1.9.4 1.28, 1.27, 1.26, 1.25 3.18.4 1.21.6 4.8.3
v1.9.3 1.28, 1.27, 1.26, 1.25 3.18.4 1.21.6 4.8.*
v1.9.1 1.28, 1.27, 1.26, 1.25 3.18.4 1.21.6 4.8.*
v1.9.0 1.28, 1.27, 1.26, 1.25 3.18.2 1.21.6 4.8.*
v1.8.4 1.27, 1.26, 1.25, 1.24 3.18.2 1.21.6 4.7.*
v1.7.1 1.27, 1.26, 1.25, 1.24 3.17.2 1.21.6 4.6.*
v1.6.4 1.26, 1.25, 1.24, 1.23 3.17.0 1.21.6 4.5.*
v1.5.1 1.25, 1.24, 1.23 3.16.2 1.21.6 4.4.*
v1.4.0 1.25, 1.24, 1.23, 1.22 3.16.2 1.19.10† 4.3.0
v1.3.1 1.24, 1.23, 1.22, 1.21, 1.20 3.16.2 1.19.10† 4.2.5

helm 安装 ingress-nginx

下载chart包

# 添加仓库
[root@master1 ~]# helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
"ingress-nginx" has been added to your repositories

# 更新
[root@master1 ~]# helm repo update

# 查询ingress-nginx的chart包,会查询到当前ingress-nginx最新的chart包
[root@master1 ~]# helm search repo ingress-nginx
NAME                            CHART VERSION   APP VERSION     DESCRIPTION                                       
ingress-nginx/ingress-nginx     4.10.1           1.10.1         Ingress controller for Kubernetes using NGINX a...

# 下载chart包
[root@master1 ~]# mkdir ingress-nginx
[root@master1 ~]# cd ingress-nginx/
[root@master1 ingress-nginx]# helm pull ingress-nginx/ingress-nginx
[root@master1 ingress-nginx]# ls
ingress-nginx-4.10.1.tgz

修改values.yaml文件

# 解压chart包
[root@k8s-master1 ingress-nginx]# tar xf ingress-nginx-4.10.1.tgz
# 查看chart目录结构
[root@k8s-master1 ingress-nginx]# ls ingress-nginx/
changelog  Chart.yaml  ci  OWNERS  README.md  README.md.gotmpl  templates  tests  values.yaml
# 修改values.yaml文件
[root@k8s-master1 ingress-nginx]# vi ingress-nginx/values.yaml

根据自己的部署的需求修改如下几个位置:

  1. 修改镜像仓库地址。以 ingress-nginx-contorller 的为例,默认是registry.k8s.io,如果无法访问,可以改为内部或国内的仓库地址。
controller:
  name: controller
  enableAnnotationValidations: false
  image:
    ## Keep false as default for now!
    chroot: false
    registry: registry.k8s.io
    image: ingress-nginx/controller
    ## for backwards compatibility consider setting the full image url via the repository value below
    ## use *either* current default registry/image or repository format or installing chart by providing the values.yaml will fail
    ## repository:
    tag: "v1.10.1"
    digest: sha256:e24f39d3eed6bcc239a56f20098878845f62baa34b9f2be2fd2c38ce9fb0f29e

备注:

本文环境所需的镜像是通过有代理的机器拉取后,离线导入到k8s环境中。

  1. 修改hostNetwork
  # -- Required for use with CNI based kubernetes installations (such as ones set up by kubeadm),
  # since CNI and hostport don't mix yet. Can be deprecated once https://github.com/kubernetes/kubernetes/issues/23920
  # is merged
  hostNetwork: true
  ## Use host ports 80 and 443
  ## Disabled by default
  1. 修改dnsPolicy
  # -- Optionally change this to ClusterFirstWithHostNet in case you have 'hostNetwork: true'.
  # By default, while using host network, name resolution uses the host's DNS. If you wish nginx-controller
  # to keep resolving names inside the k8s network, use ClusterFirstWithHostNet.
  dnsPolicy: ClusterFirstWithHostNet
  1. 添加label,用于部署调度。
  nodeSelector:
    kubernetes.io/os: linux
    ingress: "true"

如果只希望ingress-nginx部署到特定的几个节点,可以进行改设置。

  1. 修改部署类型为
  # -- Use a `DaemonSet` or `Deployment`
  kind: DaemonSet
  # -- Annotations to be added to the controller Deployment or DaemonSet
  ##

本文部署的环境只修改了hostNetwork、dnsPolicy和kind,其他保持默认。

部署chart

# 创建ns
[root@k8s-master1 ingress-nginx]# kubectl create ns ingress-nginx
namespace/ingress-nginx created

# 在chart目录下执行安装
[root@k8s-master1 ingress-nginx]# ls
changelog  Chart.yaml  ci  OWNERS  README.md  README.md.gotmpl  templates  tests  values.yaml
[root@k8s-master1 ingress-nginx]# helm install ingress-nginx -n ingress-nginx .

NAME: ingress-nginx
LAST DEPLOYED: Mon Jul  1 18:20:48 2024
NAMESPACE: ingress-nginx
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The ingress-nginx controller has been installed.
It may take a few minutes for the load balancer IP to be available.
You can watch the status by running 'kubectl get service --namespace ingress-nginx ingress-nginx-controller --output wide --watch'

An example Ingress that makes use of the controller:
  apiVersion: networking.k8s.io/v1
  kind: Ingress
  metadata:
    name: example
    namespace: foo
  spec:
    ingressClassName: nginx
    rules:
      - host: www.example.com
        http:
          paths:
            - pathType: Prefix
              backend:
                service:
                  name: exampleService
                  port:
                    number: 80
              path: /
    # This section is only required if TLS is to be enabled for the Ingress
    tls:
      - hosts:
        - www.example.com
        secretName: example-tls

If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:

  apiVersion: v1
  kind: Secret
  metadata:
    name: example-tls
    namespace: foo
  data:
    tls.crt: <base64 encoded cert>
    tls.key: <base64 encoded key>
  type: kubernetes.io/tls
  
# 查看创建的资源
root@master1:~/ingress-nginx# kubectl get all -n ingress-nginx
NAME                                 READY   STATUS    RESTARTS   AGE
pod/ingress-nginx-controller-dddq8   1/1     Running   0          2m49s
pod/ingress-nginx-controller-p7gd9   1/1     Running   0          2m49s

NAME          TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)              AGE
service/ingress-nginx-controller  LoadBalancer   10.102.230.242   <pending>     80:30974/TCP,443:30194/TCP   2m49s
service/ingress-nginx-controller-admission ClusterIP      10.110.51.58     <none>  443/TCP           2m49s

NAME              DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR     AGE
daemonset.apps/ingress-nginx-controller   2      2     2     2      2        kubernetes.io/os=linux   2m49s

# 本次部署不涉及外部LB,修改service/ingress-nginx-controller类型为NodePort
root@master1:~/ingress-nginx# kubectl edit -n ingress-nginx service/ingress-nginx-controller
...
  type: NodePort
...

创建实例测试 Ingress

my-nginx.yaml配置文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 1
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:latest
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    run: my-nginx
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  ingressClassName: nginx
  rules:
    - host: test.ingress.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx-service
                port:
                  number: 8080

创建 Deployment、service、ingress:

[root@master1 ingress-nginx]# kubectl create -f my-nginx.yaml
deployment.apps/my-nginx created
service/nginx-service created
ingress.networking.k8s.io/example-ingress created

nginx-ingress-controller所在的节点与域名做好hosts绑定,访问后端pod服务:

# 查看ingress-nginx部署的节点
root@master1:~/ingress-nginx# kubectl get pod -n ingress-nginx -o wide
NAME                             READY   STATUS    RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
ingress-nginx-controller-dddq8   1/1     Running   0       9m29s   192.168.0.62   node1   <none>        <none>
ingress-nginx-controller-p7gd9   1/1     Running   0       9m29s   192.168.0.63   node2   <none>       <none>


# 在master节点上添加域名解析,域名指向nginx-ingress-controller所在节点的ip
[rootmaster ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.62 test.ingress.com

# curl测试
[root@master1 ~]# curl test.ingress.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

问题记录:

访问ingress域名,提示404:

root@master1:~/ingress-nginx# curl http://test.ingress.com
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

首先测试直接访问pod、service返回都正常,后来网上查到类似案例,修改ingress增加 ingressClassName: nginx

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  ingressClassName: nginx		# 增加的部分
  rules:
    - host: test.ingress.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx-service
                port:
                  number: 8080

# 更新
kubectl apply -f my-nginx.yaml

标签:master1,ingress,nginx,controller,helm,k8s,root
From: https://www.cnblogs.com/lldhsds/p/18278650

相关文章

  • 生产环境部署Nginx服务器双机热备部署-keepalived(多种模式教程)
    前言:今天演示下生产环境keepalived的部署方式,安装模式有很多,比如说主备模型和双主模型,主备分:抢占模式和非抢占模式。这里我会一一展开说具体怎么配置一、双节点均部署Nginx:第一步:上传安装包到/usr/local/第二步:安装编译依赖(使用普通用户需要家sudo)yuminstallgccgcc-c......
  • nginx proxy_pass
    结果:http://abc.com/api/zwgrid/v1/export/laodongli结果:http://abc.com/api/zwgrid/v1/export/laodongli......
  • Windows上实现nginx的多负载,实现高可用,NLB 替代keepalived
    【转】https://blog.csdn.net/fcclzydouble/article/details/122841013NLB就是网络负载平衡,windowsServer2012中该功能允许你将传入的请求传播到最多达32台的服务器上,即可以使用最多32台服务器共同分担对外的网络请求服务。网络负载平衡,保证即使是在负载很重的情况下它们也能......
  • Ingress-Nginx 安装
    Ingress-Nginx安装配置一个Web服务器或者负载均衡器比想象的难。大多数Web服务的配置文件非常相似。有些应用需要一些奇怪的需求,但是在大多数的情况下,可以使用相同的逻辑以达到预期的效果。Ingress资源体现了这一思想,ingress控制器处理了上述奇怪的需求。Ingress控制......
  • Nginx的安装
    一、前言在上一篇博客中,讲述了在服务器上安装多个tomcat,现在这篇博客要讲是安装nginx二、配置Nginx的安装环境安装Nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yuminstallgcc-c++,安装完gcc后,才可以进行下一步的安装三、编译安装解压......
  • Nginx和Tomcat负载均衡,动静分离
    Nginx和Tomcat负载均衡,动静分离目录Nginx和Tomcat负载均衡,动静分离一、反向代理1、反向代理的概念2、反向代理的优势3、Nginx四层反向代理和七层反向代理二、Nginx动静分离实现原理1、动静分离的概念2、动静分离的原理3、Nginx静态处理优势三、Nginx负载均衡调度算法(6种)1、轮询(......
  • Nginx的概述和配置
    Nginx的概述和配置目录Nginx的概述和配置一、Nginx概述1、Nginx是什么2、Nginx的特点:3、Nginx和Apache的区别3.1nginx相对于apache的优点∶3.2apache相对于nginx的优点∶4、nginx的两种进程分别有什么作用二、编译安装nginx1、关闭防火墙2、将所需的安装包放到/data下,并解压3、......
  • Nginx实现负载均衡的4种常用方式及路径匹配规则
    一、Nginx实现负载均衡的4种常用方式为:轮询模式、IP哈希模式、权重模式、最少连接实现负载均衡需要在http模块中配置使用upstream模块定义后台的webserver的池子,名为proxy-web,在池子中我们可以添加多台后台webserver,其中状态检查、调度算法都是在池子中配置;然后在se......
  • nginx+keepealived 部署高可用
    nginx用docker部署1、安装docker2、拉取nginx镜像:dockerpullnginx3、启动nginx:dockerrun-d-p80:80--namenginxnginx:latest4、新建相关目录和文件夹(用来挂载容器内的文件):·mkdir-p/opt/nginx/{logs,conf,conf.d}·touch/opt/nginx/logs/{access.log,err......
  • nginx使用
    Nginx概述Nginx功能介绍静态的web资源服务器html,图片,js,css,txt等静态资源http/https协议的反向代理,7层url结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求tcp/udp协议的请求转发(反向代理)4层基础特性模块化设计,较好的扩展性高可靠性支持热部署:不停......