externalIPs

场景:AB 集群业务互相访问

1.将运行svc的主机设置为不可调度
2.构建svc时候用externalIPs指定运行svc的主机内网IP
3.A B两个集群做同样的操作
4.A B集群构建svc时候用endpoints执行需要访问的另外集群的svc主机内网IP

 

demo

启动一个nginx服务 并设置externalips

cat nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web01
  template:
    metadata:
      name: nginx
      labels:
        app: web01
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  externalIPs:
    - 192.168.1.62
  ports:
  - port: 8000
    targetPort: 80
    protocol: TCP
  selector:
    app: web01

 

说明:对外的IP:192.168.1.62,把宿主机上8000端口映射到容器80端口上,走的协议为TCP,选择器选择所有属于app=web1分组的容器

  

访问:

新建一个endpoints svc 制定刚刚 nginx的IP 和端口

cat protext.yaml

apiVersion: v1
kind: Endpoints
metadata:
  name: prometheus
subsets:
  - addresses:
    - ip: 192.168.1.62
    ports:
      - port: 8000
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus
spec:
  ports:
    - port: 9090
      protocol: TCP

 

 

 

 

 kubectl get endpoints

 

 

 

模拟访问(由于没有两套k8s环境 只能通过svc cluster-ip 访问)

curl 10.103.188.149:9090

[root@master externalIPs]# curl 10.103.188.149:9090
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

可以看到访问10.103.188.149:9000端口就相当于访问了192.168.1.62:8000端口

 

posted @ 2021-02-22 17:39  执酒  阅读(1089)  评论(0编辑  收藏  举报