一、service:提供服务的自动发现和负载均衡,帮助暴露pod端口
二、k8s中的三种ip地址:
node ip:node节点
clusterIP:VIP
pod ip:容器ip
三、创建一个service
apiVersion: v1
kind: Service #简称svc
metadata:
name: myweb
spec:
type: NodePort #默认ClusterIP
ports:
- port: 80 #clusterIP
nodePort: 30000 #node port
targetPort: 80 #pod port
selector:
app: myweb2
四、启动service服务
[root@k8s-master svc]# kubectl create -f k8s_svc.yaml
service "myweb" created
五、查看service的vip地址
[root@k8s-master svc]# kubectl get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 4h
myweb 10.254.135.104 <nodes> 80:30000/TCP 11s
六、查看资源标签
[root@k8s-master svc]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
nginx-1fcrw 1/1 Running 0 49s 172.18.57.4 10.0.0.12 app=myweb
nginx-1qc9k 1/1 Running 0 49s 172.18.78.3 10.0.0.13 app=myweb
nginx-2z8cn 1/1 Running 0 49s 172.18.57.2 10.0.0.12 app=myweb
nginx-8s8z6 1/1 Running 0 49s 172.18.57.3 10.0.0.12 app=myweb
nginx-nb7ps 1/1 Running 0 49s 172.18.78.4 10.0.0.13 app=myweb
七、把rc资源改成2个,测试负载均衡
[root@k8s-master svc]# kubectl scale rc nginx --replicas=2
replicationcontroller "nginx" scaled
[root@k8s-master svc]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
nginx-1qc9k 1/1 Running 0 1m 172.18.78.3 10.0.0.13 app=myweb
nginx-nb7ps 1/1 Running 0 1m 172.18.78.4 10.0.0.13 app=myweb
八、进入容器中编辑nginx的配置文件
[root@k8s-master svc]# kubectl exec -it nginx-1qc9k /bin/bash
root@nginx-1qc9k:/# cd /usr/share/nginx/html/
root@nginx-1qc9k:/usr/share/nginx/html# echo "web01" >index.html
[root@k8s-master svc]# kubectl exec -it nginx-nb7ps /bin/bash
root@nginx-nb7ps:/# cd /usr/share/nginx/html/
root@nginx-nb7ps:/usr/share/nginx/html# echo "web02" >index.html
九、curl检查效果
[root@k8s-master svc]# curl 10.0.0.12:30000
web01
[root@k8s-master svc]# curl 10.0.0.12:30000
web01
[root@k8s-master svc]# curl 10.0.0.12:30000
web02
十、查看详情
[root@k8s-master svc]# kubectl describe svc myweb
Name: myweb
Namespace: default
Labels: <none>
Selector: app=myweb
Type: NodePort
IP: 10.254.135.104
Port: <unset> 80/TCP
NodePort: <unset> 30000/TCP
Endpoints: 172.18.78.3:80,172.18.78.4:80 #pod ip 地址
Session Affinity: None
No events.
十一、不是同一个标签不会把myweb2加入负载均衡
[root@k8s-master svc]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
nginx-1qc9k 1/1 Running 0 13m 172.18.78.3 10.0.0.13 app=myweb
nginx-nb7ps 1/1 Running 0 13m 172.18.78.4 10.0.0.13 app=myweb
nginx2-1dd3q 1/1 Running 0 42s 172.18.57.3 10.0.0.12 app=myweb2
nginx2-3rkf6 1/1 Running 0 42s 172.18.78.5 10.0.0.13 app=myweb2
nginx2-gbdhk 1/1 Running 0 42s 172.18.57.2 10.0.0.12 app=myweb2
nginx2-jhn0m 1/1 Running 0 42s 172.18.57.4 10.0.0.12 app=myweb2
nginx2-xjzwb 1/1 Running 0 42s 172.18.78.6 10.0.0.13 app=myweb2
[root@k8s-master svc]# kubectl describe svc myweb
Name: myweb
Namespace: default
Labels: <none>
Selector: app=myweb
Type: NodePort
IP: 10.254.135.104
Port: <unset> 80/TCP
NodePort: <unset> 30000/TCP
Endpoints: 172.18.78.3:80,172.18.78.4:80
Session Affinity: None
No events.
十二、修改nodePort端口范围
[root@k8s-master svc]# vim /etc/kubernetes/apiserver
# Add your own!
KUBE_API_ARGS="--service-node-port-range=3000-50000"
0 Comments