微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用nginx对k8s集群中的service做负载均衡

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>

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐