一、service资源
service帮助pod暴露端口
创建一个service
创建service
[root@k8s-master svc]# mkdir svc
[root@k8s-master svc]# cat k8s-syc.yml
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
[root@k8s-master svc]# kubectl create -f k8s-syc.yml
非交互配置副本
[root@k8s-master svc]# kubectl scale rc nginx2 --replicas=2
修改podnginxindex文件
[root@k8s-master svc]# kubectl exec -it nginx2-gk92v /bin/bash
##指定容器名字[root@k8s-master svc]# kubectl exec -it --container=name nginx2-gk92v /bin/bash
负载均衡
[root@k8s-master svc]# curl http://10.0.0.12:30000/
nginx-02
[root@k8s-master svc]# curl http://10.0.0.12:30000/
nginx-01
端口只能是30000-372* 几几忘记了。
修改呗
vim /etc/kubernetes/apiserverKUBE_API_ARGS="--service-node-port-range=3000-50000"[root@k8s-master svc]# systemctl restart kube-apiserver.service
[root@k8s-master svc]# kubectl expose rc nginx2 --port=80 --target-port=80 --type=NodePort
[root@k8s-master svc]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 7h
myweb 10.254.179.46 <nodes> 80:30000/TCP 23m
nginx2 10.254.242.83 <nodes> 80:5492/TCP 29s
[root@k8s-master svc]# curl 10.0.0.12:5492
nginx-02
[root@k8s-master svc]# curl 10.0.0.12:5492
nginx-01
service默认使用iptables来实现负载均衡, k8s 1.8新版本中推荐使用lvs(四层负载均衡 传输层tcp,udp)
有问题请加博主微信进行沟通!
全部评论