全文目录
Kubernetes Cloud Native 实践 ( 一 ) 安装
Kubernetes Cloud Native 实践 ( 二 ) 简单使用
Kubernetes Cloud Native 实践 ( 三 ) NFS/PV/PVC
Kubernetes Cloud Native 实践 ( 四 ) 中间件上云
Kubernetes Cloud Native 实践 ( 五 ) 应用上云
Kubernetes Cloud Native 实践 ( 六 ) 集成ELK日志平台
Kubernetes Cloud Native 实践 ( 七 ) 应用监控
Kubernetes Cloud Native 实践 ( 八 ) CICD集成
Kubernetes Cloud Native 实践 ( 九 ) 运维管理
Kubernetes Cloud Native 实践 ( 十 ) 相关问题
Kubernetes Cloud Native 实践 ( 十一 ) 运行截图
简单使用
- 安装ingress: 将外部流量路由到集群内部服务
wget [https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.1/deploy/static/provider/cloud/deploy.yaml](https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.1/deploy/static/provider/cloud/deploy.yaml)
sed -i 's/registry.k8s.io\/ingress-nginx\/controller:v1.3.1@sha256:54f7fe2c6c5a9db9a0ebf1131797109bb7a4d91f56b9b362bde2abd237dd1974/registry.cn-hangzhou.aliyuncs.com\/google_containers\/nginx-ingress-controller:v1.3.1/g' ./deploy.yaml
sed -i 's/registry.k8s.io\/ingress-nginx\/kube-webhook-certgen:v1.3.0@sha256:549e71a6ca248c5abd51cdb73dbc3083df62cf92ed5e6147c780e30f7e007a47/registry.cn-hangzhou.aliyuncs.com\/google_containers\/kube-webhook-certgen:v1.3.0/g' ./deploy.yaml
kubectl apply -f deploy.yaml
- 安装metallb: 负载均衡
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.7/config/manifests/metallb-native.yaml
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
- 为metallb创建ip池对象. 使用其他的ip (非node使用的ip). 使用layer2模式. https://kubernetes.github.io/ingress-nginx/deploy/baremetal/#a-pure-software-solution-metallb
- 附一下官方文档: 在第 2 层模式下,服务 IP 的所有流量都进入一个节点。从那里, kube-proxy将流量传播到所有服务的 pod。从这个意义上说,第 2 层没有实现负载均衡器。相反,它实现了故障转移机制,以便在当前领导节点由于某种原因发生故障时,不同的节点可以接管。
- 生产自建集群不推荐使用L2模式: https://www.lixueduan.com/posts/cloudnative/01-metallb/#%E5%B1%80%E9%99%90%E6%80%A7
kubectl apply -f xxxx.yaml
kubectl get service ingress-nginx-controller --namespace=ingress-nginx
验证ip是否已分配
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: ip-pool
namespace: metallb-system
spec:
addresses:
- 192.168.222.134-192.168.222.135
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: l2-advertisement
namespace: metallb-system
spec:
ipAddressPools:
- ip-pool
MetalLB生效后, service分配到了一个ip: 192.168.222.134
root@localhost:~# kubectl get service -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller LoadBalancer 10.10.158.184 192.168.222.134 80:31079/TCP,443:30881/TCP 17h
ingress-nginx-controller-admission ClusterIP 10.10.29.235 <none> 443/TCP 17h
可以通过MTR/traceroute工具看到流量是从192.168.222.132经过再到192.168.222.134的, 因为Leader所在节点就是192.168.222.132
- 创建测试服务
kubectl create deployment deployment-demo --image=httpd --port=80
kubectl expose deployment deployment-demo
kubectl create ingress ingress-demo --class=nginx --rule="test.test.com/*=deployment-demo:80"
- 规则限制了只能填域名
- 查看
ingress-nginx-controller
service所在的节点, 更改hosts文件, 将test.test.com映射到该节点ip, 然后访问test.test.com
- 浏览器显示:
It works!
注意: 本文归作者所有, 未经作者允许, 不得转载. 若有谬误, 欢迎指出