service暴露pod----nginx
1.编写nginx-deployment.yaml文件
[root@k8s-master deployment]# vim nginx-deployment.yaml
[root@k8s-master deployment]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
namespace: default
spec:
selector:
matchLabels:
app: web
replicas: 2
template:
metadata:
name: test-nginx
labels:
app: web
spec:
containers:
- name: test-nginx
image: daocloud.io/library/nginx
ports:
- containerPort: 80
2.应用并查看
[root@k8s-master deployment]# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@k8s-master deployment]# kubectl get pod |grep nginx-deployment
nginx-deployment-c68c96bb7-g9xqn 1/1 Running 0 90s
nginx-deployment-c68c96bb7-mthsb 1/1 Running 0 90s
3.编写nginx-service.yaml文件
[root@k8s-master deployment]# vim nginx-service.yaml
[root@k8s-master deployment]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort #类型,以NodePort的方式暴露端口给外网
ports:
- nodePort: 30005 #node节点暴露的端⼝
port: 8080 #ClusterIP的端⼝
targetPort: 80 #pod节点的端⼝
selector: #标签选择器
app: web
4.应用并测试
[root@k8s-master deployment]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
[root@k8s-master deployment]# kubectl apply -f nginx-service.yaml
service/my-service created
[root@k8s-master deployment]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
my-service NodePort 10.111.86.105 <none> 8080:30005/TCP 5s
[root@k8s-master deployment]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.22.139:6443 9d
my-service 10.244.1.33:80,10.244.2.29:80 41s
5.打开浏览器输入node节点IP:端口测试
Service暴露pod----tomcat
1.编写yaml文件----deployment与service一同创建
[root@k8s-master deployment]# vim tomcat-deploy.yaml
[root@k8s-master deployment]# cat tomcat-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
namespace: default
spec:
selector:
matchLabels:
app: tomcat
replicas: 2
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: daocloud.io/library/tomcat:7.0.76-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: my-tomcat
spec:
type: NodePort
ports:
- nodePort: 31000 #是有一个范围,在30000~32457之间
port: 8081 #ClusterIP的端⼝,8080端口上述nginx已使用
targetPort: 8080
selector:
app: tomcat
2.应用并查看
[root@k8s-master deployment]# kubectl apply -f tomcat-deploy.yaml
deployment.apps/tomcat created
service/my-tomcat created[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
my-service NodePort 10.111.86.105 <none> 8080:30005/TCP 39m
my-tomcat NodePort 10.105.150.1 <none> 8081:31000/TCP 7s
[root@k8s-master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.22.139:6443 9d
my-service 10.244.1.33:80,10.244.2.29:80 40m
my-tomcat 10.244.2.25:8080,10.244.2.31:8080 61s
3.打开浏览器输入node节点IP:端口测试
端口详解
1. nodePort
2.port
3.targetPort
targetPort很好理解,targetPort是pod上的端⼝,从port和nodePort上到来的数据最终经过kube-proxy流⼊到后端pod的targetPort上进⼊容器;