创建service

默认创建

  1. kubectl expose deployment my-dep --port=8000 --target-port=80
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: my-dep
  6. name: my-dep
  7. spec:
  8. selector:
  9. app: my-dep
  10. ports:
  11. - port: 8000
  12. protocol: TCP
  13. targetPort: 80

ClusterIP方式(默认方式)

  1. kubectl expose deployment my-dep --port=8000 --target-port=80 --type=ClusterIP
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: my-dep
  6. name: my-dep
  7. spec:
  8. ports:
  9. - port: 8000
  10. protocol: TCP
  11. targetPort: 80
  12. selector:
  13. app: my-dep
  14. type: ClusterIP

NodePort方式

  1. kubectl expose deployment my-dep --port=8000 --target-port=80 --type=NodePort
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: my-dep
  6. name: my-dep
  7. spec:
  8. ports:
  9. - port: 8000
  10. protocol: TCP
  11. targetPort: 80
  12. selector:
  13. app: my-dep
  14. type: NodePort

image.png

查看service

  1. kubectl get service
  2. kubectl get svc
  3. #查看所有
  4. kubectl get svc -A

访问service

  1. #宿主机、pod ip访问
  2. kubectl get service
  3. curl 10.110.134.55:8000
  4. #pod内域名访问
  5. curl my-dep.default.svc:8000
  6. #浏览器访问,NodePort方式
  7. 106.12.145.172:32237

删除service

  1. kubectl delete svc my-dep