二进制部署K8S集群(二十)K8s服务暴露之NodePort型Service


集群外访问:用户-->域名-->负载均衡器-->NodeIP:Port(ServiceIP)-->Pod IP:端口
二进制部署K8S集群(二十)K8s服务暴露之NodePort型Service

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集群(二十)K8s服务暴露之NodePort型Service

上一篇:7、AOP编程【重点】


下一篇:模型权重记录与恢复