k8s笔记 | Ingress

安装Ingress

添加helm创库

Installation Guide - Ingress-Nginx Controller

Ingress | Kubernetes

下载包

将 文件helm 放到 /usr/local/bin/

并给到执行权限

# 添加可执行权限
chmod u+x helm
# 测试是否能运行
helm version

# 结果
version.BuildInfo{Version:"v3.14.4", GitCommit:"81c902a123462fd4052bc5e9aa9c513c4c8fc142", GitTreeState:"clean", GoVersion:"go1.21.9"}

# 查看仓库列表
helm repo list


# 添加仓库列表
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx

# 搜索 ingress-nginx
helm search repo ingress-nginx

# 拉取 ingress-nginx
helm pull ingress-nginx/ingress-nginx

配置参数

创建namesapce

安装ingress

# 将下载好的安装包解压
tar -xf ingress-nginx-4.10.1.tgz

# 解压后进入解压目录
cd ingress-nginx

# 修改values.yaml 
# 修改镜像为国内镜像
# registry: registry.cn-hangzhou.aliyuncs.com 这个我试了,镜像没有拉下来
registry: registry.aliyuncs.com
image: google_container/kube-webhook-certgen
image: google_containers/ingress-nginx-controller
hostNetwork: true
dnsPolicy:ClusterFirstWithHostNet
kind: DaemonSet
nodeSelector:
    ingress:"true"

# 将docker.io/jettech/kube-webhook-certgen 改为国内镜像
# 针对 htts 和 http [共有两处]

type: ClusterIp # 最上面一个设置就行


admissionWebhooks 下的 enable:false

# 专门为 ingress创建一个 namespace
kubectl create ns ingress-nginx

# 为需要部署ingress的节点 加上标签
kubectl label node k8s-node2 ingress=true

# 如需要删除ns下面的资源,可以使用 
kubectl delete ns ingress-nginx

# 安装ingress-nginx
helm install ingress-nginx -n ingress-nginx .

# 结果  

NAME: ingress-nginx
LAST DEPLOYED: Sat Apr 20 02:06:23 2024
NAMESPACE: ingress-nginx
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The ingress-nginx controller has been installed.
Get the application URL by running these commands:
  export POD_NAME="$(kubectl get pods --namespace ingress-nginx --selector app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/instance=ingress-nginx,app.kubernetes.io/component=controller --output jsonpath="{.items[0].metadata.name}")"
  kubectl port-forward --namespace ingress-nginx "${POD_NAME}" 8080:80
  echo "Visit http://127.0.0.1:8080 to access your application."

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

# 测试新增label是否 ingress是否会自动新增
kubectl label node k8s-node2 ingress=true
kubectl get po -n ingress-nginx
# 结果
NAME                             READY   STATUS    RESTARTS   AGE
ingress-nginx-controller-ldbpq   1/1     Running   0          71s
ingress-nginx-controller-tzkgs   1/1     Running   0          9m19s


kubectl label nodes k8s-node3  ingress

# 结果
NAME                             READY   STATUS    RESTARTS   AGE
ingress-nginx-controller-tzkgs   1/1     Running   0          13m

基本使用

#查看 
kubectl get po -n ingress-nginx

# 结果
NAME                             READY   STATUS    RESTARTS   AGE
ingress-nginx-controller-tzkgs   1/1     Running   0          21s

创建一个ingress

这里需要配合之前创建的 deploy+service

k8s笔记 | Service 服务https://blog.csdn.net/weixin_41104307/article/details/138240683wolfcode-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nginx-example
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    - host: k8s.wolfcode.cn
      http:
        paths:
          - pathType: Prefix
            backend:
              service:
                name: nginx-svc
                port:
                  number: 80
            path: /api

# 创建ingress
kubectl create -f wolfcode-ingress.yaml

# 查看ingress
kubectl get ingress

# 结果
NAME                    CLASS    HOSTS             ADDRESS   PORTS   AGE
ingress-nginx-example   <none>   k8s.wolfcode.cn             80      19s

host文件修改注意事项

192.168.10.102  k8s.wolfcode.cn

# 这里需要安装了ingress节点的node才能访问

多域名配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nginx-example
  annotations:
    kubernetes.io/ingress.class: "nginx"
    # nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: k8s.wolfcode.cn
      http:
        paths:
          - pathType: Prefix
            backend:
              service:
                name: nginx-svc
                port:
                  number: 80
            path: /api
          - pathType: Exact
            backend:
              service:
                name: nginx-svc
                port:
                  number: 80
            path: /
相关推荐
垂杨有暮鸦⊙_⊙3 分钟前
阅读2020-2023年《国外军用无人机装备技术发展综述》笔记_技术趋势
笔记·学习·无人机
Xiao Fei Xiangζั͡ޓއއ1 小时前
一觉睡醒,全世界计算机水平下降100倍,而我却精通C语言——scanf函数
c语言·开发语言·笔记·程序人生·面试·蓝桥杯·学习方法
baijin_cha1 小时前
机器学习基础04_朴素贝叶斯分类&决策树分类
笔记·决策树·机器学习·分类
波克比QWQ2 小时前
rust逆向初探
笔记·rust逆向
LuckyLay2 小时前
Spring学习笔记_36——@RequestMapping
java·spring boot·笔记·spring·mapping
坚硬果壳_2 小时前
《硬件架构的艺术》笔记(一):亚稳态
笔记·学习
狂奔solar2 小时前
分享个好玩的,在k8s上部署web版macos
前端·macos·kubernetes
qiyi.sky2 小时前
JavaWeb——Web入门(8/9)- Tomcat:基本使用(下载与安装、目录结构介绍、启动与关闭、可能出现的问题及解决方案、总结)
java·前端·笔记·学习·tomcat
dal118网工任子仪3 小时前
web安全漏洞之ssrf入门
笔记·学习·计算机网络·网络安全
mit6.8243 小时前
[Docker#9] 存储卷 | Volume、Bind、Tmpfs | -v/mount | MySQL 灾难恢复 | 问题
linux·运维·docker·容器·架构