本篇文章给大家分享的是有关k8s使用ingress-nginx负载均衡的示例分析,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
ingress-nginx负载均衡调用顺序:用户--->ingress-nginx(pod)--->ingress-nginx(控制器)--->ingress--->service--->pod
1.下载安装ingress-nginx(七层调度器)
https://kubernetes.github.io/ingress-nginx/deploy/ --详细文档
https://github.com/kubernetes/ingress-nginx/tree/master/deploy --ingress-nginx的yaml文件
[root@k8s1 ~]# kubectl explain ingress --帮助信息
[root@k8s1 ~]# vim /etc/sysconfig/kubelet --启用ipvs功能(service负载均衡有三种userspace,iptables,ipvs)
KUBE_PROXY_MODE=ipvs --添加参数
[root@k8s1 ~]# kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
[root@k8s1 ~]# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-68db76b4db-pqcl7 0/1 ContainerCreating 0 35s
[root@k8s1 ~]# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-68db76b4db-pqcl7 1/1 Running 0 9m20s
[root@k8s1 ~]#
2.创建service控制器和后端三个pod
[root@k8s1 ~]# vim service-pod.yaml
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
spec:
selector:
app: ingress-nginx --与下面的pod保持一至
release: canary
ports:
- name: http
targetPort: 80
port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy-ng
namespace: ingress-nginx
spec:
replicas: 3
selector:
matchLabels:
app: ingress-nginx --与上面的service保持一至
release: canary
template:
metadata:
labels:
app: ingress-nginx --与上面的service保持一至
release: canary
spec:
containers:
- name: ingress-nginx --与上面的service保持一至
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
[root@k8s1 ~]# kubectl apply -f service-pod.yaml
service/ingress-nginx created
deployment.apps/myapp-deploy-ng created
[root@k8s1 ~]# kubectl get pods -o wide -n ingress-nginx
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-ng-65d64df569-5f9l2 1/1 Running 0 9m6s 10.244.2.9 k8s3 <none> <none>
myapp-deploy-ng-65d64df569-n288f 1/1 Running 0 9m6s 10.244.1.76 k8s2 <none> <none>
myapp-deploy-ng-65d64df569-vnrj5 1/1 Running 0 9m6s 10.244.1.77 k8s2 <none> <none>
nginx-ingress-controller-68db76b4db-d2h75 1/1 Running 0 18d 10.244.2.240 k8s3 <none> <none>
[root@k8s1 ~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx ClusterIP 10.99.27.16 <none> 80/TCP 20s
3.创建service-nodeport
[root@k8s1 ~]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml
[root@k8s1 ~]# vim service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
nodePort: 30880
- name: https
port: 443
targetPort: 443
protocol: TCP
nodePort: 30443
selector:
app: ingress-nginx
[root@k8s1 ~]# kubectl apply -f service-nodeport.yaml
service/ingress-nginx configured
[root@k8s1 ~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.99.27.16 <none> 80:30880/TCP,443:30443/TCP 11h
[root@k8s1 ~]# kubectl describe svc ingress-nginx -n ingress-nginx
Name: ingress-nginx
Namespace: ingress-nginx
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"ingress-nginx","namespace":"ingress-nginx"},"spec":{"ports":[{"na...
Selector: app=ingress-nginx
Type: NodePort
IP: 10.99.27.16
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30880/TCP
Endpoints: 10.244.1.76:80,10.244.1.77:80,10.244.2.9:80 --后端代理的三个pod地址
Port: https 443/TCP
TargetPort: 443/TCP
NodePort: https 30443/TCP
Endpoints: 10.244.1.76:443,10.244.1.77:443,10.244.2.9:443
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s1 ~]# curl http://172.16.8.12:30880
4.创建ingress控制器
[root@k8s1 ~]# cat ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-myapp
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: myapp.magedu.com --域名
http:
paths:
- path:
backend:
serviceName: ingress-nginx --service名字
servicePort: 80
[root@k8s1 ~]# kubectl apply -f ingress.yaml
ingress.extensions/ingress-myapp created
[root@k8s1 ~]# kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
ingress-myapp myapp.magedu.com 80 4s
[root@k8s1 ~]# kubectl describe ingress ingress-myapp --查看ingress-myapp详细信息
[root@k8s1 ~]# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-68db76b4db-d2h75 1/1 Running 0 18d
[root@k8s1 ~]# kubectl exec -n ingress-nginx -it nginx-ingress-controller-68db76b4db-d2h75 -- /bin/sh
$
5.访问web站点
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html --返回的结果就是三个pod的主机名
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-n288f
[root@k8s1 ~]#
以上就是k8s使用ingress-nginx负载均衡的示例分析,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。