『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

>原创文章,欢迎转载。转载请注明:转载自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

『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

  • 查看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

『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

  • 创建service类型是nodePort

    默认的type clusterIP的形式

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

image.png

『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

明白了啥了没?其实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 

『中级篇』k8s的NodePort类型Service以及Label的简单实用(68)

image.png

  • 创建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)

猜你喜欢

转载自blog.51cto.com/12040702/2294204