更多kubernetes文章: k8s专栏目录
port targetPort nodePort关系见文章:
http://blog.csdn.net/liyingke112/article/details/76022267
1.准备镜像 编辑配置文件,这里使用的是httpd做测试,默认开80端口
[root@tensorflow1 serviceTest]# cat httpd.yaml
# ------------------- Deployment ------------------- #
kind: Deployment
apiVersion: apps/v1beta2
metadata:
labels:
k8s-app: httpd
name: httpd
spec:
replicas: 1
selector:
matchLabels:
k8s-app: httpd
template:
metadata:
labels:
k8s-app: httpd
spec:
containers:
- name: httpd
image: httpd
---
# ------------------- Service ------------------- #
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: httpd
name: httpd
spec:
type: NodePort
ports:
- port: 8081
targetPort: 80
nodePort: 30001
selector:
k8s-app: httpd
2.启动
[root@tensorflow1 serviceTest]# kubectl create -f httpd.yaml
3.查看启动情况
[root@tensorflow1 serviceTest]# kubectl get all -o wide --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE
default po/httpd-68f9d7648d-tv9xv 1/1 Running 0 26m 10.244.1.10 tensorflow2
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
default svc/httpd NodePort 10.100.205.181 8081:30001/TCP 35m k8s-app=httpd
[root@tensorflow1 serviceTest]# kubectl describe svc/httpd
Name: httpd
Namespace: default
Labels: k8s-app=httpd
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"k8s-app":"httpd"},"name":"httpd","namespace":"default"},"spec":{"ports":[{"...
Selector: k8s-app=httpd
Type: NodePort
IP: 10.100.205.181
Port: 8081/TCP
TargetPort: 80/TCP
NodePort: 30001/TCP
Endpoints: 10.244.1.10:80
Session Affinity: None
External Traffic Policy: Cluster
Events:
4.访问
直接访问pod ip:TargetPort (即Endpoints) 10.244.1.10:80 ,生命周期短,一般不用
[root@tensorflow1 serviceTest]# curl "10.244.1.10:80"
It works!
访问service的clusterIP:port,集群内访问,集群外无法不能访问
[root@tensorflow1 serviceTest]# curl "10.100.205.181:8081"
It works!
访问service的MasterNode:nodePort,这里masterNode指的是任意一台k8s集群内物理机,在所有物理机上开端口,主要是为了给集群外机器访问的,如果不需要则不用配置。
[root@tensorflow1 serviceTest]# curl "192.168.1.139:30001"
It works!