1. 程式人生 > >『中級篇』k8s的NodePort類型Service以及Label的簡單實用(68)

『中級篇』k8s的NodePort類型Service以及Label的簡單實用(68)

原創文章 ports gin 名稱 aac 實用 pod 應用名稱 create

>原創文章,歡迎轉載。轉載請註明:轉載自IT人故事會,謝謝!
>原文鏈接地址:『中級篇』k8s的NodePort類型Service以及Label的簡單實用(68)

上次主要說了service的一種類型,clusterIp,這次說下NodePort。源碼:https://github.com/limingios/docker/tree/master/No.10

通過pod創建service

  • 進入labs目錄下的service
    cd deployk8s-master
    cd labs
    cd services

技術分享圖片

  • 查看nginx-pod
    apiVersion: v1
    kind: Pod
    metadata:
    name: nginx-pod
    labels:
    app: nginx
    spec:
    containers:
    - name: nginx-container
    image: nginx
    ports:
    - name: nginx-port
      containerPort: 8
kubectl create -f pod_nginx.yml 
kubectl get pods

技術分享圖片

技術分享圖片

  • 創建service類型是nodePort

    默認的type clusterIP的形式

    kubectl expose pods nginx-pod -h
    expose pods nginx-pod --type=NodePort
    kubectl describe node

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

明白了啥了沒?其實nodePort就是直接暴露出來一個端口,直接就可以訪問了,爽是爽但是不安全。

通過pod 根據yml文件的形式創建service

  • 刪除service

    pod還在,service已經成功刪除了,app必須對應。

kubectl delete service nginx-pod
kubectl get pods
kubectl get svc
get pods --show-labels
more service_nginx.yml 

技術分享圖片

技術分享圖片

  • 創建service
    
    kubectl create -f service_nginx.yml
    vim service_nginx.yml 

![](https://upload-images.jianshu.io/upload_images/11223715-4933d96baa53bf91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/11223715-edd5dd38a90fd4ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/11223715-75c9976c9103bdcc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/11223715-8574801ad70e13e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

* label的理解
>Label機制是K8S中一個重要設計,通過Label進行對象弱關聯,靈活地分類和選擇不同服務或業務,讓用戶根據自己特定的組織結構以松耦合方式進行服務部署。
Label是一對KV,對用戶而言非常有意義的,但對K8S本身而言沒有直接意義的。Label可以在創建對象時指定,也可以在後期修改,每個對象可以擁有多個標簽,但key值必須是唯一的。
Label可隨意定義,但建議可讀性,比如設置Pod的應用名稱和版本號等。另外Lable是不具有唯一性的,為了更準確標識資源對象,應為資源對象設置多維度的label。

> nodePort是所有的pod都可以使用,如果使用nodePort的話,占用了很多端口,是不是很占用資源呢!label就把看成別名就可以了,方便操作指定的pod。

![](http://upload-images.jianshu.io/upload_images/11223715-3407e1c7ac8d7935?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

『中級篇』k8s的NodePort類型Service以及Label的簡單實用(68)