本文主要是介绍82、k8s的service-NodePort端口开放和生命周期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0、单节点服务,以及k8s命令
[root@master01 ~]# kubectl create deployment nginx1 --image=nginx:1.22 --replicas=3[root@master01 ~]# kubectl create deployment nginx1 --image=nginx:1.22 ##创建资源
deployment.apps/nginx1 created[root@master01 opt]# kubectl delete deployments nginx1 ##删除资源
deployment.apps "nginx1" deleted[root@master01 ~]# kubectl get pod ##拉取镜像
[root@master01 ~]# kubectl describe pod nginx1-654cb56c4-rlldr ##查看镜像拉取具体情况
[root@master01 opt]# kubectl delete pod nginx1-654cb56c4-rlldr ##删除容器
pod "nginx2-65747db8cd-zfsdt" deleted[root@master01 ~]# kubectl get pod -o wide ##查看pod容器的ip地址可以看到
[root@master01 ~]# kubectl create deployment test1 --image=nginx:1.18 --replicas=3 ##拉取三个镜像副本##删除pod
[root@master01 docker]# kubectl delete pod nginx1-654cb56c4-rlldr
pod "nginx1-654cb56c4-rlldr" deleted
[root@master01 docker]# kubectl delete pod nginx2-65747db8cd-ws5r6
pod "nginx2-65747db8cd-ws5r6" deleted
[root@master01 docker]# kubectl delete pod test1-866b78d79f-qffhg
pod "test1-866b78d79f-qffhg" deleted
[root@master01 docker]# kubectl delete pod test4-6b57d78d9-82nsh
pod "test4-6b57d78d9-82nsh" deleted##重新拉取
[root@master01 docker]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-7nfqr 1/1 Running 0 48s
nginx2-65747db8cd-zfsdt 1/1 Running 0 33s
test1-866b78d79f-lq57j 1/1 Running 0 19s
test4-6b57d78d9-k8j76 1/1 Running 0 4s##查看容器的ip地址
[root@master01 docker]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx1-654cb56c4-7nfqr 1/1 Running 0 5m26s 10.244.1.5 node01 <none> <none>
nginx2-65747db8cd-zfsdt 1/1 Running 0 5m11s 10.244.2.10 node02 <none> <none>
test1-866b78d79f-lq57j 1/1 Running 0 4m57s 10.244.2.11 node02 <none> <none>
test4-6b57d78d9-k8j76 1/1 Running 0 4m42s 10.244.1.6 node01 <none> <none>##查看路由网关
[root@master01 docker]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.168.2 0.0.0.0 UG 100 0 0 ens33
10.244.0.0 0.0.0.0 255.255.255.0 U 0 0 0 cni0
10.244.1.0 10.244.1.0 255.255.255.0 UG 0 0 0 flannel.1
10.244.2.0 10.244.2.0 255.255.255.0 UG 0 0 0 flannel.1
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.168.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33##测试
[root@master01 docker]# curl 10.244.1.5
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>##查看资源对象
[root@master01 docker]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx1 1/1 1 1 139m
nginx2 1/1 1 1 133m
test1 1/1 1 1 129m
test4 1/1 1 1 114m##service的30000端口与容器的80端口做映射
[root@master01 docker]# kubectl expose deployment nginx1 --port=30000 --target-port=80
service/nginx1 exposed##查看service
[root@master01 docker]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 34h
nginx1 ClusterIP 10.96.163.136 <none> 30000/TCP 71s##修改
[root@master01 docker]# kubectl edit svc nginx1 28 type: NodePort##与宿主机的端口做映射
[root@master01 docker]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 34h
nginx1 NodePort 10.96.163.136 <none> 30000:32357/TCP 4m42s##测试外部访问宿主机,通过映射到容器内
[root@master01 docker]# curl 192.168.168.81:32357
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>[root@master01 docker]# kubectl exec -it nginx1-654cb56c4-7nfqr bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-7nfqr:/# cd /usr/share/nginx/html
root@nginx1-654cb56c4-7nfqr:/usr/share/nginx/html# ls
50x.html index.html
root@nginx1-654cb56c4-7nfqr:/usr/share/nginx/html# echo 123 > index.html
[root@master01 docker]# curl 192.168.168.81:32357
123
1、k8s部署nginx实现负载均衡
[root@master01 ~]# kubectl create deployment nginx1 --image=nginx:1.22 --replicas=3
[root@master01 opt]# kubectl get pod
[root@master01 opt]# kubectl describe pod nginx1-654cb56c4-dtj7v
[root@master01 opt]# kubectl get pod -o wide
[root@master01 opt]# route -n
[root@master01 opt]# curl 10.244.2.14
[root@master01 opt]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx1 3/3 3 3 4m16s##service的30000端口与容器的80端口做映射
[root@master01 docker]# kubectl expose deployment nginx1 --port=30000 --target-port=80
service/nginx1 exposed##查看service
[root@master01 docker]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 34h
nginx1 ClusterIP 10.96.163.136 <none> 30000/TCP 71s##修改
[root@master01 docker]# kubectl edit svc nginx1 28 type: NodePort##与宿主机的端口做映射
[root@master01 docker]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 34h
nginx1 NodePort 10.96.163.136 <none> 30000:32357/TCP 4m42s##测试外部访问宿主机,通过映射到容器内
[root@master01 docker]# curl 192.168.168.81:32357
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>[root@master01 opt]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-dtj7v 1/1 Running 0 9m35s
nginx1-654cb56c4-r5q8r 1/1 Running 0 9m35s
nginx1-654cb56c4-swhz5 1/1 Running 0 9m35s[root@master01 opt]# kubectl exec -it nginx1-654cb56c4-dtj7v bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-dtj7v:/# cd /usr/share/nginx/html
root@nginx1-654cb56c4-dtj7v:/usr/share/nginx/html#
root@nginx1-654cb56c4-dtj7v:/usr/share/nginx/html# ls
50x.html index.html
root@nginx1-654cb56c4-dtj7v:/usr/share/nginx/html# echo 123 > index.html
root@nginx1-654cb56c4-dtj7v:/usr/share/nginx/html# exit
exit[root@master01 docker]# curl 192.168.168.81:32357
123[root@master01 opt]# kubectl exec -it nginx1-654cb56c4-r5q8r bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-r5q8r:/# cd /usr/share/nginx/html/
root@nginx1-654cb56c4-r5q8r:/usr/share/nginx/html# echo 456 > index.html
root@nginx1-654cb56c4-r5q8r:/usr/share/nginx/html# exit
exit[root@master01 opt]# kubectl exec -it nginx1-654cb56c4-swhz5 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-swhz5:/# echo 789 > /usr/share/nginx/html/index.html
root@nginx1-654cb56c4-swhz5:/# exit
exit[root@master01 opt]# curl 192.168.168.81:32357
456
[root@master01 opt]# curl 192.168.168.81:32357
789
[root@master01 opt]# curl 192.168.168.81:32357
123
[root@master01 opt]# curl 192.168.168.81:32357
456
[root@master01 opt]# curl 192.168.168.81:32357
789
[root@master01 opt]# curl 192.168.168.81:32357
123
一、kubectl
kubectl陈述式资源管理方式(命令行)管理(增删改查)
创建资源对象:pod 控制器 service 声明式( yaml文件写的)
#陈述式命令的开头 kubectl 命令行工具
1.1、kubectl 命令
1、查看集群的版本
[root@master01 ~]# kubectl version
2、查看集群资源对象的版本和他的简写
[root@master01 ~]# kubectl api-resources
3、查看集群的信息
[root@master01 ~]# kubectl cluster-info
4、查看集群的日志
[root@master01 ~]# tail -f /var/log/messages
[root@master01 ~]# journalctl -u kubelet -f
5、查看集群组件的健康状态
[root@master01 ~]# kubectl get cs
6、查看节点的状态
[root@master01 ~]# kubectl get node
7、查询默认命名空间里面的当前运行的pod default
[root@master01 ~]# kubectl get podname:pod的名称
ready:1/1 正常状态
status:running 运行 表名pod属于正常状态
restart:表示pod的重启次数。自愈状态,pod非正常状态下,会自动重启,状态正常后不会再进行重启。
age:当前pod的运行时间。
8、查看命名空间kube-system的pod
[root@master01 ~]# kubectl get pod -n kube-system
9、创建、删除命名空间
[root@master01 ~]# kubectl create namespace xy102
namespace/xy102 created
[root@master01 ~]# kubectl delete ns xy102
namespace "xy102" deleted
10、查看当前命名空间的所有资源
[root@master01 ~]# kubectl get all
11、查看pod的详细信息,pod被部署在哪个节点上。
[root@master01 ~]# kubectl get pod -o wide
1.2、deployment部署方式:
-
资源对象的部署的方式叫做deployment
-
无状态部署方式,pod的名称是随机生成的。
-
创建时可以指定副本数(pod)
-
滚动更新,先更新一个,更新好了之后再更新余下的pod
-
自我修复,默认的策略就是重启容器,删除pod相当于重启pod。
-
支持回滚,如果更新有问题,可以恢复到上一个版本
-
pod的扩容和缩容(手动)
1、在xy102命名空间里面创建资源镜像
##创建命名空间
[root@master01 ~]# kubectl create namespace xy102##在xy102命名空间里面创建资源镜像
[root@master01 ~]# kubectl create deployment nginx1 --image=nginx:1.22 --replicas=3 -n xy102
deployment.apps/nginx1 created##拉取镜像
[root@master01 ~]# kubectl get pod -n xy102
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-kh46c 1/1 Running 0 27s
nginx1-654cb56c4-kw6d5 1/1 Running 0 27s
nginx1-654cb56c4-xcksm 1/1 Running 0 27s
2、此处delete不再是删除,而是重启
[root@master01 ~]# kubectl get pod -n xy102
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-kh46c 1/1 Running 0 27s
nginx1-654cb56c4-kw6d5 1/1 Running 0 27s
nginx1-654cb56c4-xcksm 1/1 Running 0 27s
[root@master01 ~]# kubectl delete pod nginx1-654cb56c4-kh46c -n xy102 ##删除pod-nginx1-654cb56c4-kh46c
pod "nginx1-654cb56c4-kh46c" deleted
[root@master01 ~]# kubectl get pod -n xy102 ##相当于重启pod
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-7plg2 1/1 Running 0 10s
nginx1-654cb56c4-kw6d5 1/1 Running 0 5m38s
nginx1-654cb56c4-xcksm 1/1 Running 0 5m38s##集群控制器(资源对象)deployment创建的pod,delete pod相当于重启pod,不能删除pod。
3、跳过资源对象直接创建pod–nginx2
[root@master01 ~]# kubectl delete pod nginx1-654cb56c4-kh46c -n xy102 ##删除
pod "nginx1-654cb56c4-kh46c" deleted
[root@master01 ~]# kubectl get pod -n xy102 ##重启
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-7plg2 1/1 Running 0 10s
nginx1-654cb56c4-kw6d5 1/1 Running 0 5m38s
nginx1-654cb56c4-xcksm 1/1 Running 0 5m38s##无资源对象直接创建pod--nginx2
[root@master01 ~]# kubectl run nginx2 --image=nginx:1.22 -n xy102
pod/nginx2 created
[root@master01 ~]# kubectl get pod -n xy102
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-7plg2 1/1 Running 0 4m48s
nginx1-654cb56c4-kw6d5 1/1 Running 0 10m
nginx1-654cb56c4-xcksm 1/1 Running 0 10m
nginx2 1/1 Running 0 11s
4、没用资源对象创建nignx2,删除pod就直接删除
[root@master01 ~]# kubectl delete pod nginx2 -n xy102
pod "nginx2" deleted
[root@master01 ~]# kubectl get pod -n xy102
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-7plg2 1/1 Running 0 6m25s
nginx1-654cb56c4-kw6d5 1/1 Running 0 11m
nginx1-654cb56c4-xcksm 1/1 Running 0 11m
5、查看pod的详细信息
[root@master01 ~]# kubectl describe pod -n xy102 nginx1-654cb56c4-7plg2
[root@master01 ~]# kubectl run nginx2 --image=nginx:1.22 -n xy102
pod/nginx2 created
[root@master01 ~]# kubectl describe pod -n xy102 nginx2
6、查看详情
[root@master01 ~]# kubectl describe deployments.apps -n xy102 nginx1
7、查看日志
[root@master01 ~]# kubectl logs nginx1-654cb56c4-7plg2 -n xy102
[root@master01 ~]# kubectl logs -f nginx1-654cb56c4-7plg2 -n xy102 ##动态查看
8、进入自定义命名空间的容器
[root@master01 ~]# kubectl exec -it nginx1-654cb56c4-7plg2 bash
##没加自定义命名空间,进入不了容器
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Error from server (NotFound): pods "nginx1-654cb56c4-7plg2" not found
-------------------以上纯属看错误--------------------
[root@master01 ~]# kubectl exec -it -n xy102 nginx1-654cb56c4-7plg2 bash ##自定义命令空间,需要添加命令空间,进入pod的容器
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-7plg2:/#
9、pod的数量扩容或者缩容
[root@master01 ~]# kubectl get pod -n xy102
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-7plg2 1/1 Running 0 34m
nginx1-654cb56c4-kw6d5 1/1 Running 0 40m
nginx1-654cb56c4-xcksm 1/1 Running 0 40m
nginx2 1/1 Running 0 24m
[root@master01 ~]# kubectl get pod -n xy102 -o wide[root@node02 ~]# docker ps | grep nginx
10、容器副本缩容------命令行
[root@master01 ~]# kubectl scale deployment nginx1 -n xy102 --replicas=1
11、容器副本扩容------命令行
[root@master01 ~]# kubectl scale deployment nginx1 -n xy102 --replicas=3
12、容器副本缩容-------修改文件
[root@master01 ~]# kubectl edit deployment nginx1 -n xy102 replicas: 1deployment.apps/nginx1 edited
[root@master01 ~]# kubectl get pod -n xy102 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx1-654cb56c4-5fsnh 0/1 Terminating 0 85s <none> node01 <none> <none>
nginx1-654cb56c4-kw6d5 1/1 Running 0 44m 10.244.2.19 node02 <none> <none>
nginx1-654cb56c4-n8wn7 0/1 Terminating 0 85s 10.244.1.16 node01 <none> <none>
nginx2 1/1 Running 0 29m 10.244.2.21 node02 <none> <none>
[root@master01 ~]#
13、容器副本扩容-------修改文件
[root@master01 ~]# kubectl edit deployment nginx1 -n xy102 replicas: 3deployment.apps/nginx1 edited
[root@master01 ~]# kubectl get pod -n xy102 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx1-654cb56c4-6q4g9 1/1 Running 0 17s 10.244.1.17 node01 <none> <none>
nginx1-654cb56c4-k84cb 1/1 Running 0 17s 10.244.1.18 node01 <none> <none>
nginx1-654cb56c4-kw6d5 1/1 Running 0 46m 10.244.2.19 node02 <none> <none>
nginx2 1/1 Running 0 31m 10.244.2.21 node02 <none> <none>
二、service类型以及工作原理
#pod的ip地址随着pod的生命周期有可能会发生改变,内部访问我们通过pod的ip可以直接访问,外部访问是否会收到影响呢?
service类型以及工作原理
service如何与pod进行关联,这种关联不受pod的ip地址变化的影响。
整个service的端口80和容器的80端口做映射
[root@master01 ~]# kubectl expose deployment nginx1 --port=80 --target-port=80 --name=nginx -n xy102
##前面一个port是集群的service的端口,和容器内的80端口映射。
2.1、service的类型:
1、默认的类型,ClusterIP提供集群内部的一个虚拟ip地址,让其pod来访问的,pod可以通过serviceip直接访问到内部的容器。
内部组件通信使用
2、NodePort:在每个节点(集群的所有节点)都会开放一个端口,外部就可以通过本机的ip+端口(nodeport)访问的容器服务。
每个节点modeport的端口都是一致的,端口范围:30000-32767。
3、LoadBalancer:云平台的运行商(阿里云、腾讯云)提供LoadBalanceder 地址。需要付费的。
提供之后,也是通过访问负载均衡的地址,可以实现pod的流量转发。
4、ExternalName:把service的名字映射到DNS的域名上,让pod去访问集群外部的资源,而且设置为此类型,service将不能提供四层负载均衡的服务。
5、ingress:可以实现域名访问,既可以做七层转发也可以做四层转发
2.2、开启端口NodePort------为了让service和所有节点映射端口,这里端口是随机映射的。
[root@master01 ~]# kubectl edit svc -n xy102 nginxtype: NodePort ##开启节点宿主机端口映射
service/nginx edited##查看services网络端口映射情况
[root@master01 ~]# kubectl get svc -n xy102 nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.96.117.184 <none> 80:30834/TCP 71m
[root@master01 ~]# netstat -antp | grep 30834##nginx1---1页面设置
[root@master01 ~]# kubectl exec -it -n xy102 nginx1-654cb56c4-6q4g9 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-6q4g9:/# echo 123 > /usr/share/nginx/html/index.html
root@nginx1-654cb56c4-6q4g9:/# exit
exit##nginx1---2页面设置
[root@master01 ~]# kubectl exec -it -n xy102 nginx1-654cb56c4-k84cb bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-k84cb:/# echo 456 > /usr/share/nginx/html/index.html
root@nginx1-654cb56c4-k84cb:/# exit
exit##nginx1---3页面设置
[root@master01 ~]# kubectl exec -it -n xy102 nginx1-654cb56c4-kw6d5 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-kw6d5:/# echo 789 > /usr/share/nginx/html/index.html
root@nginx1-654cb56c4-kw6d5:/# exit
exitcurl测试是否映射完成-----30834(宿主机)--->80(service)------>80(容器)
[root@master01 ~]# curl 192.168.168.81:30834
789
[root@master01 ~]# curl 192.168.168.81:30834
123
[root@master01 ~]# curl 192.168.168.81:30834
456
[root@master01 ~]# curl 192.168.168.81:30834
789
2、查看命名空间xy102的nginx1集群详细信息
[root@master01 ~]# kubectl describe deployments.apps nginx1 -n xy102
Name: nginx1
Namespace: xy102
CreationTimestamp: Wed, 28 Aug 2024 10:30:40 +0800
Labels: app=nginx1
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx1
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:Labels: app=nginx1Containers:nginx:Image: nginx:1.22Port: <none>Host Port: <none>Environment: <none>Mounts: <none>Volumes: <none>
Conditions:Type Status Reason---- ------ ------Progressing True NewReplicaSetAvailableAvailable True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx1-654cb56c4 (3/3 replicas created)
Events: <none>
3、修改同命名空间的无资源对象的nginx2加入label:app:nginx1,从而实现通过标签加入负载均衡
[root@master01 ~]# kubectl edit pod nginx2 -n xy1029 labels:10 app: nginx1pod/nginx2 edited修改nginx2默认访问页面
[root@master01 ~]# kubectl exec -it nginx2 -n xy102 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx2:/# echo 222 > /usr/share/nginx/html/index.html
root@nginx2:/# exit
exit##master主机测试
[root@master01 ~]# curl 192.168.168.81:30834
222
[root@master01 ~]# curl 192.168.168.81:30834
789
[root@master01 ~]# curl 192.168.168.81:30834
456
[root@master01 ~]# curl 192.168.168.81:30834
123##node01节点测试
[root@node01 ~]# curl 192.168.168.82:30834
789
[root@node01 ~]# curl 192.168.168.82:30834
456
[root@node01 ~]# curl 192.168.168.82:30834
123
[root@node01 ~]# curl 192.168.168.82:30834
222##node02节点测试
[root@node02 ~]# curl 192.168.168.83:30834
222
[root@node02 ~]# curl 192.168.168.83:30834
789
[root@node02 ~]# curl 192.168.168.83:30834
456
[root@node02 ~]# curl 192.168.168.83:30834
123##查看nginx2的容器的详细信息
[root@master01 ~]# kubectl describe pod nginx2 -n xy102
Name: nginx2
Namespace: xy102
Priority: 0
Node: node02/192.168.168.83
Start Time: Wed, 28 Aug 2024 10:46:07 +0800
Labels: app=nginx1 ##标签已改
4、重启查看标签
[root@master01 ~]# kubectl delete pod -n xy102 nginx1-654cb56c4-kw6d5 [root@master01 ~]# kubectl get pod -o wide -n xy102[root@master01 ~]# kubectl describe pod nginx1-654cb56c4-dgvjd -n xy102
5、总结:
NodePort:service根据标签来匹配对应的pod,只要标签匹配,都能转发到指定的pod内的容器。
总结:
service的类型:
1、默认类型,ClusterIP 提供集群内部的一个虚拟ip地址,让其他的pod来访问的,pod可以通过这个servicip直接访问到内部的容器。
内部组件通信使用。对内
2、NodePort: 在每个节点(集群的所有节点)都会开放一个端口,外部就可以通过本机的ip+端口(nodeport)访问pod内的容器服务。
每个节点nodeport的端口都是一致的。端口是有范围的:30000-32767。
NodePort: service根据标签来匹配对应的pod。只要标签匹配,都能转发到指定的pod内的容器。
3、LoadBalancer: 云平台的运营商(阿里云,腾讯云)提供loadbalancer的地址。需要付费的。
提供之后,也是通过访问负载均衡的地址,可以实现pod的流量转发。
4、ExternalName: 把service的名字映射到DNS的域名上,让pod去访问集群外部的资源,而且设置为此类型,service不能提供四层
负载均衡的服务。
[root@master01 ~]# kubectl get endpoints -n xy102
NAME ENDPOINTS AGE
nginx 10.244.1.17:80,10.244.1.18:80,10.244.2.21:80 + 1 more... 140m
三、k8s项目的生命周期
1、在k8s项目的生命周期:
发布—>修改---->更新----->回滚----->销毁
[root@master01 ~]# curl -I 192.168.168.81:30834
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Wed, 28 Aug 2024 05:46:58 GMT
Content-Type: text/html
Content-Length: 4
Last-Modified: Wed, 28 Aug 2024 04:39:59 GMT
Connection: keep-alive
ETag: "66ceaa1f-4"
Accept-Ranges: bytes
2、更新版本------滚动点建立
[root@master01 ~]# kubectl set image deployment/nginx1 nginx=nginx:1.18 -n xy102
deployment.apps/nginx1 image updated
[root@master01 ~]# kubectl get pod -o wide -n xy102
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx1-86f8f84f7f-dw4q4 1/1 Running 0 33s 10.244.1.19 node01 <none> <none>
nginx1-86f8f84f7f-hhj62 1/1 Running 0 30s 10.244.1.20 node01 <none> <none>
nginx1-86f8f84f7f-kwdxz 1/1 Running 0 31s 10.244.2.23 node02 <none> <none>
nginx2 1/1 Running 0 3h2m 10.244.2.21 node02 <none> <none>##更新回滚点
[root@master01 ~]# kubectl set image deployment/nginx1 nginx=nginx:1.18 --record -n xy102[root@master01 ~]# kubectl rollout history deployment/nginx1 -n xy10212 kubectl set image deployment/nginx1 nginx=nginx:1.18 --record=true --namespace=xy102
[root@master01 ~]# curl -I 192.168.168.81:30834
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 28 Aug 2024 05:49:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Apr 2020 14:09:01 GMT
Connection: keep-alive
ETag: "5e9efe7d-264"
Accept-Ranges: bytes
3、查看回滚点
[root@master01 ~]# kubectl rollout history deployment/nginx1 -n xy102
deployment.apps/nginx1
REVISION CHANGE-CAUSE
1 <none>
2 <none>
#数字大小决定了距离上次更新操作的远近,数字越大,就是最近的一次操作。
4、回滚到指定的点
[root@master01 ~]# kubectl rollout undo deployment/nginx1 --to-revision=1 -n xy102
deployment.apps/nginx1 rolled back
[root@master01 ~]# curl -I 192.168.168.81:30834
查看版本[root@master01 ~]# vim /etc/docker/daemon.json {"registry-mirrors": ["https://hub-mirror.c.163.com","https://docker.m.daocloud.io","https://ghcr.io","https://mirror.baidubce.com","https://docker.nju.edu.cn"],"exec-opts": ["native.cgroupdriver=systemd"],"log-driver": "json-file","log-opts": {"max-size": "100m"}
}
[root@master01 ~]# kubectl rollout undo deployment/nginx1 --to-revision=7 -n xy102[root@master01 ~]# kubectl set image deployment/nginx1 nginx=nginx:1.20 --record -n xy102
7 kubectl set image deployment/nginx1 nginx=nginx:1.12 --record=true --namespace=xy102[root@master01 ~]# kubectl get pod -n xy102[root@master01 ~]# curl -I 192.168.168.81:30834
HTTP/1.1 200 OK
Server: nginx/1.12.2[root@master01 ~]# kubectl rollout undo deployment/nginx1 --to-revision=13 -n xy102##13 kubectl set image deployment/nginx1 nginx=nginx:1.20 --record=true --namespace=xy102[root@master01 ~]# curl -I 192.168.168.81:30834
HTTP/1.1 200 OK
Server: nginx/1.20.2
5、查看deployment资源镜像
[root@master01 ~]# kubectl get deployments.apps nginx1 -n xy102
NAME READY UP-TO-DATE AVAILABLE AGE
nginx1 3/3 3 3 4h13m
6、删除deployment资源镜像
[root@master01 ~]# kubectl delete deployments.apps nginx1 -n xy102
deployment.apps "nginx1" deleted
7、删除无资源镜像的pod
[root@master01 ~]# kubectl delete pod nginx2 -n xy102
pod "nginx2" deleted
[root@master01 ~]# kubectl get pod -n xy102 ##查看命名空间的pod
No resources found in xy102 namespace.
8、查看命名空间里面的service
[root@master01 ~]# kubectl get svc -n xy102
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.96.117.184 <none> 80:30834/TCP 3h26m
9、删除service
[root@master01 ~]# kubectl delete svc -n xy102 nginx
service "nginx" deleted
10、查看命名空间
[root@master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 2d
kube-node-lease Active 2d
kube-public Active 2d
kube-system Active 2d
xy102 Active 4h32m
11、创建命名空间
[root@master01 ~]# kubectl create ns xy102
namespace/xy102 created
[root@master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 2d
kube-node-lease Active 2d
kube-public Active 2d
kube-system Active 2d
xy102 Active 4s
12、删除指定命名空间
[root@master01 ~]# kubectl delete ns xy102
namespace "xy102" deleted
USTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.96.117.184 80:30834/TCP 3h26m
## 9、删除service
[root@master01 ~]# kubectl delete svc -n xy102 nginx
service “nginx” deleted
## 10、查看命名空间
[root@master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 2d
kube-node-lease Active 2d
kube-public Active 2d
kube-system Active 2d
xy102 Active 4h32m
## 11、创建命名空间
[root@master01 ~]# kubectl create ns xy102
namespace/xy102 created
[root@master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 2d
kube-node-lease Active 2d
kube-public Active 2d
kube-system Active 2d
xy102 Active 4s
## 12、删除指定命名空间
[root@master01 ~]# kubectl delete ns xy102
namespace “xy102” deleted
这篇关于82、k8s的service-NodePort端口开放和生命周期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!