当前位置:   article > 正文

K8S陈述式资源管理

K8S陈述式资源管理
陈述式

命令行:kubectl命令行工具

优点:90%以上的场景都可以满足,对增,删,查比较方便,对改不是很友好

缺点:命令比较冗长,复杂,难记

声明式

k8s当中的yaml文件来实现资源管理---声明式

Kubernetes kubectl 命令表 _ Kubernetes(K8S)中文文档_Kubernetes中文社区

所有的kubectl的命令行

 kubectl命令详解

可以查看,部署,查看pod的情况(详细的信息,日志,发布和回滚)

查看版本

  1. [root@master01 ~]# kubectl version
  2. Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.15", GitCommit:"8f1e5bf0b9729a899b8df86249b56e2c74aebc55", GitTreeState:"clean", BuildDate:"2022-01-19T17:27:39Z", GoVersion:"go1.15.15", Compiler:"gc", Platform:"linux/amd64"}
  3. Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.15", GitCommit:"8f1e5bf0b9729a899b8df86249b56e2c74aebc55", GitTreeState:"clean", BuildDate:"2022-01-19T17:23:01Z", GoVersion:"go1.15.15", Compiler:"gc", Platform:"linux/amd64"}

 查看k8s所有资源对象的简写

[root@master01 ~]# kubectl api-resources

 查看k8s的集群信息

  1. [root@master01 ~]# kubectl cluster-info
  2. Kubernetes control plane is running at https://192.168.233.81:6443
  3. KubeDNS is running at https://192.168.233.81:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  4. To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

 自动补齐命令

  1. [root@master01 ~]# source <(kubectl completion bash)
  2. 可以写在脚本里,也可以在命令行执行

查看master节点的状态

  1. [root@master01 ~]# kubectl get cs
  2. Warning: v1 ComponentStatus is deprecated in v1.19+
  3. NAME STATUS MESSAGE ERROR
  4. controller-manager Healthy ok
  5. scheduler Healthy ok
  6. etcd-0 Healthy {"health":"true"}
  7. 这个是简写
  8. [root@master01 ~]# kubectl get componentstatuses
  9. Warning: v1 ComponentStatus is deprecated in v1.19+
  10. NAME STATUS MESSAGE ERROR
  11. scheduler Healthy ok
  12. controller-manager Healthy ok
  13. etcd-0 Healthy {"health":"true"}

 查看默认命名空间pod的信息

  1. [root@master01 ~]# kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d6h
  4. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 12m
  5. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d6h
  6. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 12m
  7. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 12m
  8. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d6h
  9. pods也可以,一样的效果

 查看命名空间有哪些(所有)

  1. [root@master01 ~]# kubectl get ns
  2. NAME STATUS AGE
  3. default Active 3d7h
  4. kube-node-lease Active 3d7h
  5. kube-public Active 3d7h
  6. kube-system Active 3d7h
  7. 查看当前集群所有的命名空间
  8. default就是默认的命名空间

查看指定命名空间的pod

  1. [root@master01 ~]# kubectl get pod -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. coredns-7f89b7bc75-9ldmr 1/1 Terminating 2 3d7h
  4. coredns-7f89b7bc75-fm5fm 1/1 Running 0 15m
  5. coredns-7f89b7bc75-g4frq 1/1 Running 0 15m
  6. coredns-7f89b7bc75-vz54f 1/1 Terminating 2 3d7h
  7. etcd-master01 1/1 Running 4 3d7h
  8. kube-apiserver-master01 1/1 Running 4 3d7h
  9. kube-controller-manager-master01 1/1 Running 5 3d7h
  10. kube-flannel-ds-5mcgj 1/1 Running 5 3d7h
  11. kube-flannel-ds-lh8g5 1/1 Running 2 3d7h
  12. kube-flannel-ds-rfxh4 1/1 Running 2 3d7h
  13. kube-proxy-j9pgx 1/1 Running 2 3d7h
  14. kube-proxy-qwdd2 1/1 Running 4 3d7h
  15. kube-proxy-v259v 1/1 Running 2 3d7h
  16. kube-scheduler-master01 1/1 Running 4 3d7h
  17. 查看指定命名空间内的pod需要加 -n命名

查看pod的详细部署情况(查看默认命名空间内pod的详细信息)

  1. [root@master01 ~]# kubectl get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d6h 10.244.1.9 node01 <none> <none>
  4. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 17m <none> <none> <none> <none>
  5. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d6h 10.244.2.9 node02 <none> <none>
  6. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 17m <none> <none> <none> <none>
  7. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 17m <none> <none> <none> <none>
  8. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d6h 10.244.1.8 node01 <none> <none>

查看指定命名空间的详细信息

  1. [root@master01 ~]# kubectl get pod -n kube-system -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. coredns-7f89b7bc75-9ldmr 1/1 Terminating 2 3d7h 10.244.1.10 node01 <none> <none>
  4. coredns-7f89b7bc75-fm5fm 1/1 Running 0 22m 10.244.0.2 master01 <none> <none>
  5. coredns-7f89b7bc75-g4frq 1/1 Running 0 22m 10.244.0.3 master01 <none> <none>
  6. coredns-7f89b7bc75-vz54f 1/1 Terminating 2 3d7h 10.244.2.8 node02 <none> <none>
  7. etcd-master01 1/1 Running 4 3d7h 192.168.233.81 master01 <none> <none>
  8. kube-apiserver-master01 1/1 Running 4 3d7h 192.168.233.81 master01 <none> <none>
  9. kube-controller-manager-master01 1/1 Running 5 3d7h 192.168.233.81 master01 <none> <none>
  10. kube-flannel-ds-5mcgj 1/1 Running 5 3d7h 192.168.233.81 master01 <none> <none>
  11. kube-flannel-ds-lh8g5 1/1 Running 2 3d7h 192.168.233.83 node02 <none> <none>

查看节点信息和状态

  1. [root@master01 ~]# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. master01 Ready control-plane,master 3d8h v1.20.15
  4. node01 NotReady <none> 3d7h v1.20.15
  5. node02 NotReady <none> 3d7h v1.20.15
  6. [root@master01 ~]# kubectl get node -o wide
  7. NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
  8. master01 Ready control-plane,master 3d8h v1.20.15 192.168.233.81 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://24.0.7
  9. node01 NotReady <none> 3d7h v1.20.15 192.168.233.82 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://24.0.7
  10. node02 NotReady <none> 3d7h v1.20.15 192.168.233.83 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://24.0.7
  11. 查看node节点的详细信息

 查看pod的详细信息

  1. [root@master01 ~]# kubectl describe pod myapp-test-5d94dbb4f-9j2fm
  2. 加上name名字

 指定查看pod的详细信息

  1. [root@master01 ~]# kubectl get ns
  2. [root@master01 ~]# kubectl get pod -n kube-system
  3. [root@master01 ~]# kubectl describe pod -n kube-system etcd-master01
  4. 要-n指定命名空间,不指定就是查看默认的

 查看pod的输出日志

  1. [root@master01 ~]# kubectl logs myapp-test-5d94dbb4f-9j2fm
  2. [root@master01 ~]# kubectl logs -f myapp-test-5d94dbb4f-9j2fm
  3. -f表示动态查看日志
  4. 查看指定命名空间的日志输出信息
  5. [root@master01 ~]# kubectl get pod -n kube-system
  6. [root@master01 ~]# kubectl logs -f etcd-master01 -n kube-system
  7. 必须要-n指定申明命名空间,否则就是查看默认命名空间

 创建命名空间

  1. [root@master01 ~]# kubectl create ns chen
  2. namespace/chen created
  3. [root@master01 ~]# kubectl get ns
  4. NAME STATUS AGE
  5. chen Active 10s
  6. default Active 3d8h
  7. kube-node-lease Active 3d8h
  8. kube-public Active 3d8h
  9. kube-system Active 3d8h

删除命名空间

  1. [root@master01 ~]# kubectl delete ns chen
  2. namespace "chen" deleted
  3. [root@master01 ~]# kubectl get ns
  4. NAME STATUS AGE
  5. default Active 3d8h
  6. kube-node-lease Active 3d8h
  7. kube-public Active 3d8h
  8. kube-system Active 3d8h
  9. 要先声明动作delete删除指定的组件ns命名空间,chen是要删除命名空间的名称,基于副本控制器创建的pod,delete实际上是重新构建,也可以理解为重启,如果不是控制器创建的,如run那就是删除
  10. 删除pod
  11. [root@master01 ~]# kubectl get pod
  12. NAME READY STATUS RESTARTS AGE
  13. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
  14. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 57m
  15. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
  16. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 58m
  17. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 58m
  18. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
  19. [root@master01 ~]# kubectl delete pod myapp-test-5d94dbb4f-9j2fm
  20. 先声明动作:create , delete ,get , desvribe 指定对象:如 ns,pod,service 指定对象的名称 chen等的名称,如果不是默认命名空间需要 -n 指定命名空间

deployment的创建方式部署pod

特点以及两种方式部署

陈述式部署:命令行

声明式:yaml文件部署

滚动更新:不是一次性的吧所有pod全部部署,而是一个个来,pod的更新时使用,逐步的引入新的pod,逐步的减少旧的pod

自我修复:如果有pod节点发生故障,deployment会自动启动新的pod来进行代替

回滚:如果更新有问题,deployment会提供一个还原点,可以手动还原到未更新前的状态

扩容和缩容:deployment可以随时调整pod的数量,以适应流量的变化

上述的功能需要实现必须要基于deployment创建的服务才可以,绝大多数的pod都是使用deployment创建的

  1. [root@master01 ~]# kubectl get deployments
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. myapp-test 0/3 3 0 3d7h
  4. 查看默认命名空间使用deployment创建的pod数量
  5. [root@master01 ~]# kubectl get deployments -n kube-system
  6. NAME READY UP-TO-DATE AVAILABLE AGE
  7. coredns 2/2 2 2 3d8h
  8. 指定命名空间查看

daemonset创建方式

daemonset:不能通过命令行创建,只能通过yaml文件中点定义的方式创建

daemonset是后台运行创建,会在每个节点上都创建一个相同方式,相同版本的容器运行pod

daemonset一般都是依赖环境和重要组件。一般不会对这些资源进行操作。

  1. [root@master01 ~]# kubectl get daemonsets
  2. No resources found in default namespace.
  3. [root@master01 ~]# kubectl get daemonsets -n kube-system
  4. NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
  5. kube-flannel-ds 3 3 1 3 1 <none> 3d8h
  6. kube-proxy 3 3 1 3 1 kubernetes.io/os=linux 3d8h
  7. #daemonset不能通过命令行创建,只能通过yaml文件中点定义的方式创建

deployment部署pod

  1. [root@master01 ~]# kubectl create deployment nginx-chen --image=nginx
  2. deployment.apps/nginx-chen created
  3. [root@master01 ~]# kubectl get pod
  4. NAME READY STATUS RESTARTS AGE
  5. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
  6. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 73m
  7. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
  8. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 74m
  9. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 74m
  10. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
  11. nginx-chen-699bd94c4f-g667s 0/1 Pending 0 24s
  12. [root@master01 ~]# kubectl create deployment nginx-chen --image=nginx --replicas=3
  13. # --replicas=3表示指定运行容器的数量是3,不加默认是1
  14. 指定命名空间创建
  15. [root@master01 ~]# kubectl create ns chen
  16. namespace/chen created
  17. [root@master01 ~]# kubectl create deployment nginx-chen --image=nginx -n chen
  18. deployment.apps/nginx-chen created
  19. 在指定命名空间创建deployment
  20. [root@master01 ~]# kubectl get pod
  21. NAME READY STATUS RESTARTS AGE
  22. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
  23. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 77m
  24. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
  25. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 77m
  26. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 77m
  27. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
  28. nginx-chen-699bd94c4f-g667s 0/1 Pending 0 3m38s
  29. [root@master01 ~]# kubectl get pod -n chen
  30. NAME READY STATUS RESTARTS AGE
  31. nginx-chen-699bd94c4f-phbc6 0/1 Pending 0 15s
  32. [root@master01 ~]# kubectl delete pod nginx-chen-699bd94c4f-g667s
  33. pod "nginx-chen-699bd94c4f-g667s" deleted
  34. 删除掉
  35. [root@master01 ~]# kubectl delete pod nginx-chen-699bd94c4f-phbc6 -n chen
  36. pod "nginx-chen-699bd94c4f-phbc6" deleted
  37. 删除掉指定的命名空间里的pod
  38. 如果是基于deployment方式创建的pod,或者是daemonset方式创建的pod,是由控制器创建的pod,使用delete删除pod是删不掉的,相当于重启pod
  39. 基于deployment方式创建的pod,一旦删除deployment,基于这个deployment创建的pod都会被删除,慎用
  1. [root@master01 ~]# kubectl run nginx1 --image=nginx
  2. pod/nginx1 created
  3. [root@master01 ~]# kubectl get pod
  4. NAME READY STATUS RESTARTS AGE
  5. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
  6. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 86m
  7. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
  8. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 86m
  9. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 86m
  10. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
  11. nginx-chen-699bd94c4f-j86vn 0/1 Pending 0 6m27s
  12. nginx1 0/1 Pending 0 5s
  13. [root@master01 ~]# kubectl delete pod nginx1
  14. pod "nginx1" deleted
  15. [root@master01 ~]# kubectl get pod
  16. NAME READY STATUS RESTARTS AGE
  17. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
  18. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 87m
  19. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
  20. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 87m
  21. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 87m
  22. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
  23. nginx-chen-699bd94c4f-j86vn 0/1 Pending 0 7m10s
  24. 不是基于控制器创建,会被直接删除

远程进入节点容器

  1. [root@master01 ~]# kubectl exec -it nginx-chen-699bd94c4f-j86vn bash
  2. docker的exec只能在本机内部使用,不能跨主机,kubectl exec可以跨主机进入容器
  3. [root@master01 ~]# kubectl create deployment nginx --image=nginx -n chen
  4. [root@master01 ~]# kubectl get pod -n chen
  5. NAME READY STATUS RESTARTS AGE
  6. nginx-6799fc88d8-kkq5r 0/1 Pending 0 14s
  7. nginx-chen-699bd94c4f-bd6kk 0/1 Pending 0 13m
  8. [root@master01 ~]# kubectl exec -it nginx-chen-699bd94c4f-bd6kk bash -n chen

快速的让容器结束

  1. [root@master01 ~]# kubectl delete pod nginx-chen-699bd94c4f-j86vn --force --grace-period=0
  2. warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
  3. pod "nginx-chen-699bd94c4f-j86vn" force deleted
  4. #--grace-period:过度的存活期。默认是30秒。可以让pod优雅的结束容器内的进程,然后退出pod
  5. #=0:表示立即停止pod。必须要force实现
  6. #主要是用于结束卡在销毁状态的pod

对deployment创建的pod进行扩缩容

  1. [root@master01 ~]# kubectl scale deployment nginx-chen --replicas=3
  2. deployment.apps/nginx-chen scaled
  3. [root@master01 ~]# kubectl get pod
  4. NAME READY STATUS RESTARTS AGE
  5. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
  6. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 102m
  7. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
  8. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 102m
  9. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 102m
  10. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
  11. nginx-chen-699bd94c4f-g4f6k 0/1 Pending 0 11s
  12. nginx-chen-699bd94c4f-rn2l9 0/1 Pending 0 11s
  13. nginx-chen-699bd94c4f-wd266 0/1 Pending 0 2m1s
  14. 扩容
  15. [root@master01 ~]# kubectl scale deployment nginx-chen --replicas=1
  16. deployment.apps/nginx-chen scaled
  17. [root@master01 ~]# kubectl get pod
  18. NAME READY STATUS RESTARTS AGE
  19. myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
  20. myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 102m
  21. myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
  22. myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 102m
  23. myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 102m
  24. myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
  25. nginx-chen-699bd94c4f-wd266 0/1 Pending 0 2m22s
  26. 缩容
  27. 创建pod是并没有指定副本数,后续也可以对它的副本数进行修改,只能是deployment创建的方式才可以,daemonset创建的不行

如何把服务的service进行发布

  1. [root@master01 ~]# kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d9h
  4. myapp-test NodePort 10.96.208.213 <none> 80:30096/TCP 3d8h
  5. nginx NodePort 10.96.22.254 <none> 80:30221/TCP 3d8h
  6. [root@master01 ~]# kubectl get svc -n chen
  7. No resources found in chen namespace.
  8. #查看当前命名空间的service的类型
  9. #-n:指定命名空间
  10. [root@master01 ~]# kubectl delete svc nginx
  11. service "nginx" deleted
  12. [root@master01 ~]# kubectl get svc
  13. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  14. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d9h
  15. myapp-test NodePort 10.96.208.213 <none> 80:30096/TCP 3d8h
  16. #删除当前命名空间的service
  17. #-n:指定命名空间
ClusterIP类型
ClusterIP:创建service的默认类型,提供一个集群内部的虚拟IP地址,通过这个虚拟IP可以直接访问pod的资源。无法对外提供访问
NodePort类型

NodePort:会在每一个node节点上都开放一个相同对的端口。外部可以通过node的本机IP地址+端口,访问pod资源。集群外部访问service资源的一种方式。四层代理方式。

nodeip:nodeport

会随机指派,也可以指定。

端口会从30000-32767的固定范围随机指派一个端口,或者可以指定一个这个范围内的端口

基于deployment创建的pod可以使用的方式
  1. [root@master01 ~]# kubectl expose deployment nginx-chen --port=80 --target-port=80 --name=nginx-service --type=NodePort
  2. service/nginx-service exposed
  3. [root@master01 ~]# kubectl get svc
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d9h
  6. myapp-test NodePort 10.96.208.213 <none> 80:30096/TCP 3d8h
  7. nginx-service NodePort 10.96.214.169 <none> 80:30127/TCP 13s
  8. #--port=80:声明service集群的端口
  9. #--target-port=80:声明pod内部容器的端口
  10. nginx-service NodePort 10.96.94.66 <none> 80:30386/TCP 9s
  11. #10.96.94.66:集群内部的IP地址,外部不可以访问这个IP地址
  12. #80:对应的是内部的service的端口
  13. #30386:和内部的service的端口做映射
  14. pod内部的容器端口是固定的。
  15. --port是service和容器映射的端口可以自定义
  16. 但是--target-port容器内部的端口是固定的不可以自定义
  17. 80:30386这里的80是内部容器和service端口做的映射
  18. 这里的30386是外部主机和内部的service的端口做的映射
  19. 容器内部的端口 --port 先和service做映射。这个端口不能对外访问。如果类型是 NodePort 会在每个node节点上开通一个相同的端口,这个端口就是service和本机端口的映射。所以访问30386就可以访问容器内的服务。

修改service的默认端口
  1. [root@master01 ~]# kubectl edit svc nginx-service
  2. #修改service的默认端口
  3. # Please edit the object below. Lines beginning with a '#' will be ignored,
  4. # and an empty file will abort the edit. If an error occurs while saving this file will be
  5. # reopened with the relevant failures.
  6. #
  7. apiVersion: v1
  8. kind: Service
  9. metadata:
  10. creationTimestamp: "2023-12-31T23:49:21Z"
  11. labels:
  12. app: nginx-chen
  13. name: nginx-service
  14. namespace: default
  15. resourceVersion: "45708"
  16. uid: b91ce2b0-8ee8-451d-80df-b2572ba25539
  17. spec:
  18. clusterIP: 10.96.214.169
  19. clusterIPs:
  20. - 10.96.214.169
  21. externalTrafficPolicy: Cluster
  22. ports:
  23. - nodePort: 30127
  24. port: 80
  25. protocol: TCP
  26. targetPort: 80
  27. selector:
  28. app: nginx-chen
  29. sessionAffinity: None
  30. type: NodePort
  31. status:
  32. loadBalancer: {}
  33. pod内部的容器的端口是固定的,--port是service和容器映射的端口,可以是任意

LoadBalancer类型

LoadBalancer:如果service的类型设定为LoadBalancer。地址映射(云平台会提供一个LoadBalancer的地址)这种用法仅用于公有云服务供应商在云平台上设置好的service场景。再通过外部来访问,实现负载均衡访问。(地址需要额外付费)

创建好了service,指定类型为LoadBalancer。

ExternalName类型

ExternalName:DNS映射,给service分配一个域名,通过域名来访问后端pod资源。

使用ExternalName的service的类型,不能提供负载均衡,和 LoadBalancer 类型一样必须要设置一个LoadBalancer的地址才可以实现。

  1. kubectl edit svc nginx1-server
  2. #进入service内直接修改

四个类型

默认类型只对内访问

NodePort类型我配置过可以对外访问

LoadBalancer类型需要花钱

ExternalName类型需要配置域名

更新和回滚以及发布的方式

项目的生命周期:创建 > 发布 > 更新 > 回滚 > 删除
滚动更新
  1. kubectl set image deployment nginx1 nginx=nginx:1.10
  2. #滚动更新服务的版本

回滚

  1. kubectl rollout history deployment nginx1
  2. #查看回滚点信息
  3. #数字的大小决定了距离上次操作的远近。数字越大就是最近的一次操作
  4. kubectl set image deployment nginx1 nginx=nginx:1.15 --record
  5. #滚动更新服务的版本
  6. #--record:可以在回滚点上添加标识
  7. kubectl rollout undo deployment nginx1 --to-revision=1
  8. #指定回滚点还原
  9. kubectl rollout status deployment nginx1
  10. #查看回滚过程
  11. kubectl get pod -w
  12. #动态查看回滚状态
  13. kubectl get all
  14. #查看当前命名空间内部的所有信息。
  15. #-o wide:可以查看到详细信息
  16. #-n:指定命名空间查看

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/629173
推荐阅读
相关标签
  

闽ICP备14008679号