kubectl expose 发布服务
1,命令格式
Usage:
kubectl expose (-f FILENAME | TYPE NAME) [–port=port]
[–protocol=TCP|UDP|SCTP] [–target-port=number-or-name] [–name=name]
[–external-ip=external-ip-of-service] [–type=type] [options]
2,新增service(先新建2个web服务—新建svc,执行myweb后端)
[root@centos7_1 ~]# kubectl create deployment myweb --image=nginx -r 2
[root@centos7_1 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
myweb 2/2 2 2 16m
#使用默认发布出来的服务是clusterIP,只能K8S内部访问
[root@centos7_1 ~]# kubectl expose deployment myweb --port=80 --target-port=80 --name=myweb-svc
service/myweb-svc exposed
#deployment myweb :需要指向的对象
#–port=80 :新建svc服务对外的端口号,一般和后端应用端口号一样,也可以不一样
#–target-port=80 :svc服务后端服务器应用的端口号
#–name=myweb-svc :给服务定义一个名字
3, 查看service(svc)
[root@centos7_1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.68.0.1 443/TCP 3d22h
myweb-svc ClusterIP 10.68.23.84 80/TCP 7s
[root@centos7_1 ~]# kubectl describe svc myweb-svc
Name: myweb-svc
Namespace: default
Labels: app=myweb
Annotations:
Selector: app=myweb
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.68.23.84
IPs: 10.68.23.84
Port: 80/TCP
TargetPort: 80/TCP
Endpoints: 172.20.1.8:80,172.20.2.7:80
Session Affinity: None
Events:
#访问测试
[root@centos7_1 ~]# curl 10.68.23.84
4, 修改svc(将type=ClusterIP修改成NodePort),此修改后,则可以使用nodeIP:port进行访问
[root@centos7_1 ~]# kubectl edit svc myweb-svc
service/myweb-svc edited
…
type: NodePort
…
[root@centos7_1 ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.68.0.1 443/TCP 3d23h
myweb-svc NodePort 10.68.23.84 80:31629/TCP 11m app=myweb
[root@centos7_1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.137.100 Ready master 3d23h v1.21.0
192.168.137.101 Ready node 3d22h v1.21.0
192.168.137.102 Ready node 3d22h v1.21.0
#直接访问node:nodeport就可以访问对应发布出来的应用
http://192.168.137.101:31629/
5, 删除svc
[root@centos7_1 ~]# kubectl delete svc myweb-svc
service “myweb-svc” deleted