#service:负载均衡,服务的自动发现 service帮助pod暴露端口
#创建一个service
cd k8s_yaml/
mkdir svc
vim k8s_svc.yaml
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
kubectl create -f k8s_svc.yaml
kubectl edit svc myweb
selector:
app: myweb
改为myweb
kubectl edit rc nginx
spec:
replicas: 2
数量改为2
#检测
[root@k8s-master ~]# kubectl describe svc myweb
Name: myweb
Namespace: default
Labels: <none>
Selector: app=myweb
Type: NodePort
IP: 10.254.16.176
Port: <unset> 80/TCP
NodePort: <unset> 30000/TCP
Endpoints: 172.18.35.2:80,172.18.35.3:80
Session Affinity: None
No events.
kubectl exec -it nginx-ls85f /bin/bash
echo 'web01'>/usr/share/nginx/html/index.html
kubectl exec -it nginx-mvq25 /bin/bash
echo 'web02'>/usr/share/nginx/html/index.html
curl -I 10.0.0.13:30000
curl -I 10.0.0.12:30000
[root@k8s-master ~]# curl 10.0.0.12:30000
web02
[root@k8s-master ~]# curl 10.0.0.12:30000
web01
kubectl scale rc nginx --replicas=2 #调整rc的副本书
kubectl exec -it pod_name /bin/bash #进入pod容器
#修改nodePort范围
vim /etc/kubernetes/apiserver
KUBE_API_ARGS="--service-node-port-range=3000-60000"
#命令行创建service资源
kubectl expose rc nginx --type=NodePort --port=80
service默认使用iptables来实现负载均衡, k8s 1.8新版本中推荐使用lvs(四层负载均衡 传输层tcp,udp)