官方案例,https://openelb.io/docs/getting-started/usage/use-openelb-in-layer-2-mode/
1. 官方案例
- 创建deployment、service
yaml
[root@kube-master cannary]# cat he.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: layer2-openelb
spec:
replicas: 2
selector:
matchLabels:
app: layer2-openelb
template:
metadata:
labels:
app: layer2-openelb
spec:
containers:
- image: luksa/kubia
name: kubia
ports:
- containerPort: 8080
---
kind: Service
apiVersion: v1
metadata:
name: layer2-svc
annotations:
# 声明service使用OpenELB
lb.kubesphere.io/v1alpha1: openelb
protocol.openelb.kubesphere.io/v1alpha1: layer2 # 声明OpenELB使用layer2模式
eip.openelb.kubesphere.io/v1alpha2: layer2-eip #指定创建地址池的名称
spec:
selector:
app: layer2-openelb
type: LoadBalancer # 类型必须是LoadBalancer
ports:
- name: http
port: 80
targetPort: 8080
externalTrafficPolicy: Cluster # 如果是 local,OpenELB在集群中随机选择一个包含Pod的节点来处理服务请求,请求只能到达这个node上
[root@kube-master cannary]# cat he.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: layer2-openelb
spec:
replicas: 2
selector:
matchLabels:
app: layer2-openelb
template:
metadata:
labels:
app: layer2-openelb
spec:
containers:
- image: luksa/kubia
name: kubia
ports:
- containerPort: 8080
---
kind: Service
apiVersion: v1
metadata:
name: layer2-svc
annotations:
# 声明service使用OpenELB
lb.kubesphere.io/v1alpha1: openelb
protocol.openelb.kubesphere.io/v1alpha1: layer2 # 声明OpenELB使用layer2模式
eip.openelb.kubesphere.io/v1alpha2: layer2-eip #指定创建地址池的名称
spec:
selector:
app: layer2-openelb
type: LoadBalancer # 类型必须是LoadBalancer
ports:
- name: http
port: 80
targetPort: 8080
externalTrafficPolicy: Cluster # 如果是 local,OpenELB在集群中随机选择一个包含Pod的节点来处理服务请求,请求只能到达这个node上
- 查看svc
bash
[root@kube-master cannary]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
layer2-svc LoadBalancer 192.168.64.66 10.0.0.71 80:31122/TCP 74s
[root@kube-master cannary]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
layer2-svc LoadBalancer 192.168.64.66 10.0.0.71 80:31122/TCP 74s
2. ingress案例
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: old-demo
spec:
replicas: 2
selector:
matchLabels:
run: old-demo
template:
metadata:
labels:
run: old-demo
spec:
containers:
- name: old-demo
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
protocol: TCP
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: old-demo
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
selector:
run: old-demo
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: old-demo
spec:
ingressClassName: nginx
rules:
- host: go.k8bernetes.cn
#- host: java.host.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: old-demo
port:
number: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: old-demo
spec:
replicas: 2
selector:
matchLabels:
run: old-demo
template:
metadata:
labels:
run: old-demo
spec:
containers:
- name: old-demo
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
protocol: TCP
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: old-demo
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
selector:
run: old-demo
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: old-demo
spec:
ingressClassName: nginx
rules:
- host: go.k8bernetes.cn
#- host: java.host.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: old-demo
port:
number: 80
- 查看ingress
bash
[root@kube-master cannary]# kubectl get ingress,svc
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress.networking.k8s.io/old-demo nginx go.k8bernetes.cn 10.103.236.70 80 10h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/old-demo ClusterIP 192.168.241.186 <none> 80/TCP 10h
[root@kube-master cannary]# kubectl get ingress,svc
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress.networking.k8s.io/old-demo nginx go.k8bernetes.cn 10.103.236.70 80 10h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/old-demo ClusterIP 192.168.241.186 <none> 80/TCP 10h
- 测试效果
在dns解析平台上面解析好A记录
❌ 注意
如果用LoadBalaner 的ip 直接去访问,则会提示404