使用helm安裝ingress,實現用域名的方式訪問k8s內部應用


k8s集群版本

k8s集群版本是1.22

提前部署好nginx服務和創建好svc

deployment方式部署的nginx服務,1個副本

 

創建的服務

 

通過服務可以代理到nginx服務

curl 10.105.183.50:801

 

安裝helm管理工具

https://helm.sh/docs/intro/install/

tar -zxvf helm-v3.6.3-linux-amd64.tar.gz

mv linux-amd64/helm /usr/local/bin/helm

helm version

 

添加ingress的helm倉庫

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx

helm repo list

 

 

另外,如果要移除,helm repo remove ingress-nginx

 

搜索ingress-nginx

helm search repo ingress-nginx

 

下載

helm pull ingress-nginx/ingress-nginx --version 4.0.6

 

解壓、配置

tar xf ingress-nginx-4.0.1.tgz

cd ingress-nginx

vim values.yaml

配置文件已經放到網盤

部署ingress-nginx-controller

創建命名空間:kubectl create ns ingress-nginx

安裝到node1,給node1打標簽:kubectl label node k8s-node01 ingress=true

安裝:helm install ingress-nginx -n ingress-nginx .

(卸載:helm uninstall ingress-nginx -n ingress-nginx)

 

NAME: ingress-nginx
LAST DEPLOYED: Sun Dec 12 20:01:46 2021
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 --namespace ingress-nginx get pods -o jsonpath="{.items[0].metadata.name}" -l "app=ingress-nginx,component=controller,release=ingress-nginx")
  kubectl --namespace ingress-nginx port-forward $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:
    annotations:
      kubernetes.io/ingress.class: nginx
    name: example
    namespace: foo
  spec:
    ingressClassName: example-class
    rules:
      - host: www.example.com
        http:
          paths:
            - path: /
              pathType: Prefix
              backend:
                service:
                  name: exampleService
                  port: 80
    # 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

 

查看:kubectl get po -n ingress-nginx

服務

ps -ef |grep ingress

netstat -lntp | grep 80

 

創建ingress

使用v1版本

vim ingress-v1.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: www.baidu2.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-svc
            port:
              number: 801

 

kubectl create -f ingress-v1.yaml

kubectl get ingress

 

通過ingress暴露的域名訪問

由於沒有域名,這里只是簡單模擬,

修改host文件:C:\Windows\System32\drivers\etc\hosts

最后一行添加:192.168.117.145 www.baidu2.com

請求域名,先從本地hosts文件找

 

訪問首頁

 

查看容器

 

進入到容器,添加一個頁面

 

不加路徑,默認就是訪問的index.html

 

訪問添加的頁面

 

至此,可以通過ingress把請求代理到svc,svc代理到pod。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM