對于使用 minikube 的第一個 helm kubernetes 部署,我正在嘗試設定一個 Ingress 控制器。我做的步驟。
Helm create ingress
從 templates 檔案夾中洗掉所有檔案并清除 values.yaml 的內容并從一個新的 chart.yaml 開始
apiVersion: v2
name: ingress
description: A Helm chart for Ingress Controller
type: application
version: 1.1.0
appVersion: 1.17.0
keywords:
- ingress
- nginx
- api-gateway
home: https://github.com/harry
maintainers:
- name: Harry
url: https://github.com/harry
dependencies:
- name: nginx-ingress
version: 0.15.1
repository: https://helm.nginx.com/stable
接下來是創建 tgz 檔案的命令
helm dependency update
然后是 /templates 中的 ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Values.ingress.name }}
annotations:
kubernetes.io/ingress.class: {{ .Values.ingress.annotations.class }}
spec:
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
http:
paths:
{{- range .paths }}
- path: {{ .path }}
pathType: Prefix
backend:
service:
name: {{ .backend.serviceName }}
port:
number: {{ .backend.servicePort }}
{{- end }}
{{- end }}
和一個默認的 values.yaml
ingress:
name: ingress-service
replicaCount: 1
annotations:
class: nginx
hosts:
- host: chart-example.local
paths:
- path: /
backend:
serviceName: serviceName
servicePort: 8080
最后我申請
helm install -f ingress.yaml ingress ./ingress
以下 ingress.yaml
ingress:
hosts:
- host: nutrizone.k8s.com
paths:
- path: /api/
backend:
serviceName: trala-backend
servicePort: 8080
- path: /
backend:
serviceName: trala-ui
servicePort: 8080
結果
kubectl exec pod/ingress-nginx-ingress-controller-55b985bcfd-7p7hk -n default -- cat /etc/nginx/nginx.conf
/api 無處可尋
location / {
return 404;
}
重新安裝幾次并更新到較新版本的 nginx 后(可以在 values.yaml 中看到 - 但是應該正確轉換舊語法)我有點迷茫
在第一次建議更新 values.yaml 之后
我將 values.yaml 和父 ingress.yaml 中的塊更改為 nginx-ingress 并更新了 /template/ingress.yaml(我需要索引才能使名稱“nginx-ingress”起作用)
helm delete ingress
helm install -f ingress.yaml ingress ./ingress
遺憾的是沒有區別。
更新 2 添加 github 專案 https://github.com/Chris2301/helm
uj5u.com熱心網友回復:
dependencies:
- name: nginx-ingress
version: 0.15.1
repository: https://helm.nginx.com/stable
您已宣告該依賴項稱為“nginx-ingress”。為了讓 Helm 將圖表值傳遞給子圖表,您需要將它們放在一個與圖表同名的塊中,因此,在您的情況下,您必須將您的值放在一個名為nginx-ingress
在 OP 遇到相同問題的地方查看此問題。
查看代碼后更新
你所做的是正確的。組態檔不會更新,因為入口控制器使用您的ingress資源來確定如何路由您的請求。
例如:
$ kubectl get ingress ingress-service -o yaml | grep "spec:" -A 18
spec:
rules:
- host: project.k8s.com
http:
paths:
- backend:
service:
name: project-backend
port:
number: 8080
path: /api/
pathType: Prefix
- backend:
service:
name: nginx
port:
number: 80
path: /
pathType: Prefix
我修改了鏈接到“/”的服務以測驗功能。
我安裝了bitnami nginx chart來提供測驗后端服務:
helm install nginx bitnami/nginx
這創建了一個nginx在默認命名空間中呼叫的服務(也是我安裝圖表的地方)
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-ingress LoadBalancer 10.109.222.226 10.109.222.226 80:30892/TCP,443:30210/TCP 27m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 38m
nginx LoadBalancer 10.97.31.201 10.97.31.201 80:32484/TCP 25m
所以現在我可以將外部 IP 用于您的圖表 ( ingress-nginx-ingress) 創建的負載均衡器,并使用curl. 我們必須在Host這里使用,以便入口控制器知道要匹配哪個規則。
$ curl 10.109.222.226 -H "Host: project.k8s.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>
可以看到,控制器正確回傳了nginx的內容。您也可以通過直接點擊 nginx 服務來驗證這一點(這次不需要使用Host標頭)
$ curl 10.97.31.201
<!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>
現在,如果我們嘗試使用/api/端點,它將失敗,因為后端服務 ( project-backend) 不存在:
$ curl 10.109.222.226/api/ -H "Host: project.k8s.com"
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.23.2</center>
</body>
</html>
因此,要回答您最初的問題,/api/它不在 nginx 配置中,因為控制器使用入口資源來確定路由。
您的圖表按預期執行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537953.html
