k8s笔记 | Ingress

时间:2024-05-04 11:14:22

安装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.****.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: /