要在 k3s 中安装 nginx 版本的 Ingress,请按照以下步骤进行操作:
- 安装 Helm
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
- 添加 stable 的 Helm 仓库
helm repo add stable https://kubernetes-charts.storage.googleapis.com/
helm repo update
- 安装 nginx-ingress 控制器
helm install nginx-ingress stable/nginx-ingress --set controller.publishService.enabled=true
- 等待一段时间,直到 ingress-controller 和 ingress-default-backend 这两个 pod 的状态变为 Running
kubectl get pods -n default -l app.kubernetes.io/name=ingress-nginx
- 部署一个示例应用程序,并使用 Ingress 将其暴露出来
例如,部署一个 hello-world 应用程序,并使用 Ingress 将其暴露在域名 example.com 下:
创建一个 Deployment
kubectl create deployment hello-world --image=gcr.io/google-samples/hello-app:1.0
创建一个 Service,并将它公开为 ClusterIP 类型
kubectl expose deployment hello-world --port=8080 --type=ClusterIP
创建一个 Ingress,将它绑定到 example.com 域名上,并指定 hello-world Service 作为后端服务
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: hello-world
spec:
rules:
- host: example.com
http:
paths:
- path: /
backend:
serviceName: hello-world
servicePort: 8080
EOF
在本地修改 hosts 文件,添加一条记录将 example.com 解析为 k3s 集群的 IP 地址
在浏览器中访问 http://example.com,应该能够看到 hello-world 应用程序的输出
注意:在生产环境中,请根据实际情况进行配置,并采取必要的安全措施。