k8s svc负载均衡
## k8s svc负载均衡
- run创建deploy
```bash
[root@zjucst-52668 ~]# kubectl run http --image=katacoda/docker-http-server:latest --replicas=1 -n cka
```
- expose创建svc,暴露ip端口,并且为其配置虚拟网卡
```bash
[root@zjucst-52668 ~]# 274 kubectl expose deployment http --external-ip="172.17.0.1" --port=8000 --target-port=80 -n cka
[root@zjucst-52668 ~]# ifconfig eth0:cka 172.17.0.1 up
```
- scale水平扩容pods
```bash
[root@zjucst-52668 ~]# kubectl scale deployment http --replicas=3 -ncka
[root@zjucst-52668 ~]# kubectl get pods -n cka
NAME READY STATUS RESTARTS AGE
http-989f8bcf9-gvjdf 1/1 Running 0 11s
http-989f8bcf9-jk556 1/1 Running 0 32m
http-989f8bcf9-p66hp 1/1 Running 0 11s
[root@zjucst-52668 ~]#
[root@zjucst-52668 ~]# kubectl describe svc http -ncka
Name: http
Namespace: cka
Labels: run=http
Annotations: <none>
Selector: run=http
Type: ClusterIP
IP: 10.247.204.130
External IPs: 172.17.0.1
Port: <unset> 8000/TCP
TargetPort: 80/TCP
Endpoints: 172.16.0.43:80,172.16.0.44:80,172.16.0.45:80
Session Affinity: None
Events: <none>
```
- 访问svc ip:port,将会随机得到不同pod的相应
```bash
[root@zjucst-52668 ~]# kubectl get svc -ncka
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
http ClusterIP 10.247.204.130 172.17.0.1 8000/TCP 22m
[root@zjucst-52668 ~]# kubectl describe svc http -ncka
Name: http
Namespace: cka
Labels: run=http
Annotations: <none>
Selector: run=http
Type: ClusterIP
IP: 10.247.204.130
External IPs: 172.17.0.1
Port: <unset> 8000/TCP
TargetPort: 80/TCP
Endpoints: 172.16.0.43:80,172.16.0.44:80,172.16.0.45:80
Session Affinity: None
Events: <none>
[root@zjucst-52668 ~]#
[root@zjucst-52668 ~]# curl 172.17.0.1:8000
<h1>This request was processed by host: http-989f8bcf9-gvjdf</h1>
[root@zjucst-52668 ~]# curl 172.17.0.1:8000
<h1>This request was processed by host: http-989f8bcf9-p66hp</h1>
[root@zjucst-52668 ~]# curl 172.17.0.1:8000
<h1>This request was processed by host: http-989f8bcf9-jk556</h1>
[root@zjucst-52668 ~]#
```
- 点赞
- 收藏
- 关注作者
评论(0)