1. <dd id="erndk"></dd>
                1. 用nginx對k8s集群中的service做負載均衡

                  互聯網 2021/12/28 7:10:03

                  用nginx對k8s集群中的service做負載均衡 [root@master test]# cat nginx.yml --- apiVersion: apps/v1 kind: Deployment metadata:name: webnamespace: default spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- …

                  用nginx對k8s集群中的service做負載均衡

                  [root@master test]# cat nginx.yml 
                  ---
                  apiVersion: apps/v1
                  kind: Deployment
                  metadata:
                    name: web
                    namespace: default
                  spec:
                    replicas: 3
                    selector:
                      matchLabels:
                        app: nginx
                    template:
                      metadata:
                        labels:
                          app: nginx
                      spec:
                        containers:
                        - name: nginx  
                          image: nginx
                          imagePullPolicy: IfNotPresent
                  
                  ---
                  apiVersion: v1
                  kind: Service
                  metadata:
                    name: web
                    namespace: default
                  spec:
                    ports:
                    - port: 8080          #service用來負載均衡的端口
                      protocol: TCP
                      nodePort: 30000     #nodePort的取值范圍在:30000-32767
                      targetPort: 80      #pod端口
                    selector:
                      app: nginx          #需要和上面deployment中的app: nginx一致
                    type: NodePort
                  
                  [root@master test]# kubectl apply -f nginx.yml 
                  deployment.apps/web unchanged
                  service/web configured
                  
                  [root@master test]# kubectl describe svc web
                  Name:                     web
                  Namespace:                default
                  Labels:                   <none>
                  Annotations:              <none>
                  Selector:                 app=nginx
                  Type:                     NodePort
                  IP Families:              <none>
                  IP:                       10.97.194.136
                  IPs:                      10.97.194.136
                  Port:                     <unset>  8080/TCP
                  TargetPort:               80/TCP
                  NodePort:                 <unset>  30000/TCP
                  Endpoints:                10.244.1.250:80,10.244.1.251:80,10.244.1.252:80
                  Session Affinity:         None
                  External Traffic Policy:  Cluster
                  Events:                   <none>
                  
                  
                  [root@master test]# kubectl get pod,svc
                  NAME                       READY   STATUS    RESTARTS   AGE
                  pod/busybox                1/1     Running   0          20m
                  pod/web-7cf7d6dbc8-4rmlv   1/1     Running   0          24m
                  pod/web-7cf7d6dbc8-fw7pn   1/1     Running   0          24m
                  pod/web-7cf7d6dbc8-mfxws   1/1     Running   0          24m
                  
                  NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
                  service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          8d
                  service/web          NodePort    10.97.194.136   <none>        8080:30000/TCP   24m
                  
                  [root@master test]# curl 10.97.194.136:8080
                  <!DOCTYPE html>
                  <html>
                  <head>
                  <title>Welcome to nginx!</title>
                  <style>
                  html { color-scheme: light dark; }
                  body { width: 35em; margin: 0 auto;
                  font-family: Tahoma, Verdana, Arial, sans-serif; }
                  </style>
                  </head>
                  <body>
                  <h1>Welcome to nginx!</h1>
                  <p>If you see this page, the nginx web server is successfully installed and
                  working. Further configuration is required.</p>
                  
                  <p>For online documentation and support please refer to
                  <a href="http://nginx.org/">nginx.org</a>.<br/>
                  Commercial support is available at
                  <a href="http://nginx.com/">nginx.com</a>.</p>
                  
                  <p><em>Thank you for using nginx.</em></p>
                  </body>
                  </html>
                  
                  bosybox是一個工具,非常小直接啟動它來驗證
                  [root@master test]# kubectl run busybox --rm -it --image=busybox /bin/sh
                  If you don't see a command prompt, try pressing enter.
                  / # wget web.default.svc.cluster.local:8080         #kubectl的任何pod里面都可以通過服務名.namespace名:服務端口的方式進行訪問
                  Connecting to web.default.svc.cluster.local:8080 (10.97.194.136:8080)
                  saving to 'index.html'
                  index.html           100% |******************************************|   615  0:00:00 ETA
                  'index.html' saved
                  / # ls
                  bin         etc         index.html  root        tmp         var
                  dev         home        proc        sys         usr
                  / # more index.html 
                  <!DOCTYPE html>
                  <html>
                  <head>
                  <title>Welcome to nginx!</title>
                  <style>
                  html { color-scheme: light dark; }
                  body { width: 35em; margin: 0 auto;
                  font-family: Tahoma, Verdana, Arial, sans-serif; }
                  </style>
                  </head>
                  <body>
                  <h1>Welcome to nginx!</h1>
                  <p>If you see this page, the nginx web server is successfully installed and
                  working. Further configuration is required.</p>
                  
                  <p>For online documentation and support please refer to
                  <a href="http://nginx.org/">nginx.org</a>.<br/>
                  Commercial support is available at
                  <a href="http://nginx.com/">nginx.com</a>.</p>
                  
                  <p><em>Thank you for using nginx.</em></p>
                  </body>
                  </html>
                  
                  

                   

                  隨時隨地學軟件編程-關注百度小程序和微信小程序
                  關于找一找教程網

                  本站文章僅代表作者觀點,不代表本站立場,所有文章非營利性免費分享。
                  本站提供了軟件編程、網站開發技術、服務器運維、人工智能等等IT技術文章,希望廣大程序員努力學習,讓我們用科技改變世界。
                  [用nginx對k8s集群中的service做負載均衡]http://www.yachtsalesaustralia.com/tech/detail-276727.html

                  贊(0)
                  關注微信小程序
                  程序員編程王-隨時隨地學編程

                  掃描二維碼或查找【程序員編程王】

                  可以隨時隨地學編程啦!

                  技術文章導航 更多>
                  掃一掃關注最新編程教程
                  国产在线拍揄自揄视频菠萝

                        1. <dd id="erndk"></dd>