k8s使用ingress-nginx負載均衡
ingress-nginx負載均衡呼叫順序:使用者--->ingress-nginx(pod)--->ingress-nginx(控制器)--->ingress--->service--->pod
1.下載安裝ingress-nginx(七層排程器)
--詳細文件
--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
[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-d2h65 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
[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
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-d2h65 1/1 Running 0 18d
[root@k8s1 ~]# kubectl exec -n ingress-nginx -it nginx-ingress-controller-68db76b4db-d2h65 -- /bin/sh
$
5.訪問web站點
[root@k8s1 ~]# curl /hostname.html --返回的結果就是三個pod的主機名
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-n288f
[root@k8s1 ~]#
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25854343/viewspace-2641755/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- gRPC負載均衡(自定義負載均衡策略)RPC負載
- gRPC負載均衡(客戶端負載均衡)RPC負載客戶端
- 負載均衡負載
- 使用Nginx配置TCP負載均衡NginxTCP負載
- 使用nginx進行負載均衡Nginx負載
- 使用YARP來實現負載均衡負載
- IP負載均衡負載
- WebSocket負載均衡Web負載
- nginx負載均衡Nginx負載
- NGINX 負載均衡Nginx負載
- 【Nginx】負載均衡Nginx負載
- 負載均衡---ribbon負載
- LoadBalancer負載均衡負載
- LVS 負載均衡負載
- 負載均衡技術(一)———負載均衡技術介紹負載
- 解密負載均衡技術和負載均衡演算法解密負載演算法
- 負載均衡技術(二)———常用負載均衡服務介紹負載
- 【知識分享】四層負載均衡和七層負載均衡負載
- 微服務之負載均衡使用場景微服務負載
- 在K8S中,負載均衡器有何作用?K8S負載
- Nginx負載均衡模式Nginx負載模式
- 漫談負載均衡負載
- 負載均衡簡介負載
- golang grpc 負載均衡GolangRPC負載
- gRPC的負載均衡RPC負載
- 負載均衡詳解負載
- 負載均衡知多少?負載
- Linux LVS 負載均衡Linux負載
- 淺談負載均衡負載
- 負載均衡叢集負載
- 負載均衡補充負載
- 負載均衡4層負載
- 負載均衡之keepalived負載
- LVS負載均衡群集負載
- 負載均衡和動態負載均衡分別是什麼?-VeCloud負載Cloud
- LVS負載均衡群集概念、NAT模式LVS負載均衡實戰部署負載模式
- 使用Rancher建立負載均衡的容器應用負載
- 做了反向代理和負載均衡的nginx配置檔案簡單示例(nginx.conf) HTTP負載均衡/TCP負載均衡負載NginxHTTPTCP