使用helm安装ingress,实现用域名的方式访问k8s内部应用
实现目标
通过域名访问接口,也就是说,可以通过ingress把请求代理到svc,svc代理到pod。
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。
__EOF__
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!