码迷,mamicode.com
首页 > 其他好文 > 详细

二进制部署K8s集群第20节K8s服务暴露之NodePort型Service

时间:2020-10-06 20:22:27      阅读:28      评论:0      收藏:0      [点我收藏+]

标签:nat   res   ide   replica   temp   app   label   start   pos   

集群外访问:用户->域名->负载均衡器->NodeIP:Port(Service IP)->Pod IP:端口
技术图片

1、创建NodePort型Service资源清单

[root@hdss7-21 ~]# kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:v1.7.9 -n kube-public --replicas=2 
[root@hdss7-21 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
service/nginx-dp exposed # 默认创建clusterIP,只允许内网访问集群IP
[root@hdss7-21 ~]# kubectl get all -n kube-public                            NAME                            READY   STATUS    RESTARTS   AGE
pod/nginx-dp-7f74c75ff9-8vzx8   1/1     Running   0          <invalid>
pod/nginx-dp-7f74c75ff9-9cwnt   1/1     Running   0          <invalid>

NAME               TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
service/nginx-dp   ClusterIP   192.168.128.131   <none>        80/TCP    41s

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-dp   2/2     2            2           68s

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dp-7f74c75ff9   2         2         2       <invalid>
[root@hdss7-21 ~]# cat > svc.yml  <<‘eof‘ # 创建NodePort型service
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: nginx-dp-svc
  name: nginx-dp-svc
  namespace: kube-public
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 3010
      protocol: TCP
  selector:
    app: nginx-dp
  sessionAffinity: None
eof
[root@hdss7-21 ~]# kubectl create -f svc.yml                
service/nginx-dp created
[root@hdss7-21 ~]# kubectl get all -n kube-public --show-labels -o wide  
NAME                            READY   STATUS    RESTARTS   AGE         IP           NODE                NOMINATED NODE   READINESS GATES   LABELS
pod/nginx-dp-7f74c75ff9-9gx2k   1/1     Running   0          <invalid>   172.7.22.3   hdss7-22.host.com   <none>           <none>            app=nginx-dp,pod-template-hash=7f74c75ff9
pod/nginx-dp-7f74c75ff9-r9kgx   1/1     Running   0          <invalid>   172.7.21.4   hdss7-21.host.com   <none>           <none>            app=nginx-dp,pod-template-hash=7f74c75ff9

NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)       AGE    SELECTOR       LABELS
service/nginx-dp       ClusterIP   192.168.96.174   <none>        80/TCP        6m5s   app=nginx-dp   app=nginx-dp
service/nginx-dp-svc   NodePort    192.168.20.171   <none>        80:3010/TCP   85s    app=nginx-dp   k8s-app=nginx-dp-svc

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES                              SELECTOR       LABELS
deployment.apps/nginx-dp   2/2     2            2           6m12s   nginx        harbor.od.com/public/nginx:v1.7.9   app=nginx-dp   app=nginx-dp

NAME                                  DESIRED   CURRENT   READY   AGE         CONTAINERS   IMAGES                              SELECTOR                                    LABELS
replicaset.apps/nginx-dp-7f74c75ff9   2         2         2       <invalid>   nginx        harbor.od.com/public/nginx:v1.7.9   app=nginx-dp,pod-template-hash=7f74c75ff9   app=nginx-dp,pod-template-hash=7f74c75ff9

2、通过edit将ClusterIP改变成NodePort

kubectl edit svc nginx-dp -n kube-public
spec:
  clusterIP: 192.168.96.174
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
      selector:
    app: nginx-dp
      sessionAffinity: None
      type: NodePort  #  清加nodeport
[root@hdss7-21 ~]# kubectl get all -n kube-public 
NAME                            READY   STATUS    RESTARTS   AGE
pod/nginx-dp-7f74c75ff9-9gx2k   1/1     Running   0          <invalid>
pod/nginx-dp-7f74c75ff9-r9kgx   1/1     Running   0          <invalid>

NAME                   TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/nginx-dp       NodePort   192.168.96.174   <none>        80:27051/TCP   15m
service/nginx-dp-svc   NodePort   192.168.20.171   <none>        80:3010/TCP    11m

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-dp   2/2     2            2           15m

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dp-7f74c75ff9   2         2         2       <invalid>

二进制部署K8s集群第20节K8s服务暴露之NodePort型Service

标签:nat   res   ide   replica   temp   app   label   start   pos   

原文地址:https://blog.51cto.com/yht1990/2540036

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!