1、clusterip
kind: Service
apiVersion: v1
metadata:
name: my-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
[root@k8s-master rc-test]# kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 9d my-service 10.254.233.44 <none> 80/TCP 16s mysql None <none> 3306/TCP 8d
[root@k8s-master rc-test]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.137.110:6443 9d
my-service 172.168.69.2:80,172.168.95.2:80 9m
mysql 172.168.69.3:3306 8d
此种方式可以在k8s内部使用,比如建立一个busybox,在busybox上面,可以访问10.254.233.44
在node上面,也可以使用endpoint的ip访问,比如在master上面访问172.168.69.2
2、NodePort
kind: Service
apiVersion: v1
metadata:
name: my-service
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30062
[root@k8s-master rc-test]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 10d
my-service 10.254.209.157 <nodes> 80:30062/TCP 5m
mysql None <none> 3306/TCP 8d
[root@k8s-master rc-test]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.137.110:6443 10d
my-service 172.168.69.2:80,172.168.95.2:80 6m
mysql 172.168.69.3:3306 8d
相比clusterIP,多了一个node上面的接口,cluster的两种方式还是可以访问。
另外node IP方式也可以访问了。