②. Service服务暴露NodePort方式
①. NodePort命令行的方式
kubectl expose deployment my-dep --port=8000 --target-port=80 --type=NodePort
NodePort范围在 30000-32767之间
[root@k8smaster ~]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE my-dep-01 3/3 3 3 21m [root@k8smaster ~]# kubectl expose deploy my-dep-01 --port=8000 --target-port=80 --type=NodePort service/my-dep-01 exposed [root@k8smaster ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d my-dep-01 NodePort 10.96.235.249 <none> 8000:30267/TCP 5s [root@k8smaster ~]# curl 10.96.235.249:8000 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@k8smaster ~]#
②. yaml文件的方式
apiVersion: v1 kind: Service metadata: labels: app: my-dep-01 name: my-dep-01 spec: ports: - port: 8000 protocol: TCP targetPort: 80 selector: app: my-dep-01 type: NodePort