您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。