【K8S运维知识汇总】第3天1:kubectl详解——陈述式资源管理方法(deployment资源、service资源)

网友投稿 736 2022-12-01

【K8S运维知识汇总】第3天1:kubectl详解——陈述式资源管理方法(deployment资源、service资源)

【K8S运维知识汇总】第3天1:kubectl详解——陈述式资源管理方法(deployment资源、service资源)

查看名称空间:

[root@hdss7-21 ~]# kubectl get namespace # 等同于kubectl get nsNAME STATUS AGEdefault Active 3d22hkube-node-lease Active 3d22hkube-public Active 3d22hkube-system Active 3d22h

查看defalut名称空间下的所有资源:

[root@hdss7-21 ~]# kubectl get all -n default # 等同于kubectl get allNAME READY STATUS RESTARTS AGEpod/nginx-ds-pqszs 1/1 Running 0 2d12hpod/nginx-ds-vnlx9 1/1 Running 0 2d12hNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 192.168.0.1 443/TCP 3d23hNAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGEdaemonset.apps/nginx-ds 0 0 0 0 0

创建名称空间:

[root@hdss7-21 ~]# kubectl create namespace appnamespace/app created[root@hdss7-21 ~]# kubectl get nsNAME STATUS AGEapp Active 45sdefault Active 3d23hkube-node-lease Active 3d23hkube-public Active 3d23hkube-system Active 3d23h

删除名称空间:

[root@hdss7-21 ~]# kubectl delete namespace appnamespace "app" deleted[root@hdss7-21 ~]# kubectl get nsNAME STATUS AGEdefault Active 3d23hkube-node-lease Active 3d23hkube-public Active 3d23hkube-system Active 3d23h

创建deployment资源:

# 在名称为kube-public的名称空间创建名称为nginx-db资源进行pod控制[root@hdss7-21 ~]# kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:v1.7.9 -n kube-publicdeployment.apps/nginx-db created[root@hdss7-21 ~]# kubectl get deploy -n kube-publicNAME READY UP-TO-DATE AVAILABLE AGEnginx-db 0/1 1 0 2m28s[root@hdss7-21 ~]# kubectl get pods -n kube-publicNAME READY STATUS RESTARTS AGEnginx-db-7b4dd9978d-8wzdl 0/1 Pending 0

查看 kube-public名称空间下的Pod

[root@hdss7-21 ~]# kubectl get pods -n kube-public -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-db-7b4dd9978d-8wzdl 0/1 Pending 0 8m15s # 根据pod名称可以获得对应的容器,一般包含2个容器,分别是基础容器和业务容器

查看详细信息:

[root@hdss7-21 ~]# kubectl describe deployment nginx-db -n kube-publicName: nginx-dbNamespace: kube-publicCreationTimestamp: Wed, 01 Jul 2020 10:58:43 +0800Labels: app=nginx-dbAnnotations: deployment.kubernetes.io/revision: 1Selector: app=nginx-dbReplicas: 1 desired | 1 updated | 1 total | 0 available | 1 unavailableStrategyType: RollingUpdate # 发布策略:滚动升级MinReadySeconds: 0RollingUpdateStrategy: 25% max unavailable, 25% max surgePod Template: Labels: app=nginx-db Containers: nginx: Image: harbor.od.com/public/nginx:v1.7.9 Port: Host Port: Environment: Mounts: Volumes: Conditions: Type Status Reason ---- ------ ------ Available False MinimumReplicasUnavailable Progressing False ProgressDeadlineExceededOldReplicaSets: NewReplicaSet: nginx-db-7b4dd9978d (1/1 replicas created)Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set nginx-db-7b4dd9978d to 1# kubectl发出的指令先找apiserver通信确认,apiserver再找scheduler确认,scheduler在最合适的节点通过kubelet来启动容器

进入pod资源

[root@hdss7-21 ~]# kubectl get pods -n kube-publicNAME READY STATUS RESTARTS AGEpod/nginx-dp-5dfc689474-fc6z9 1/1 Running 0 4s# kubectl exec可以跨主机进入不同的pod[root@hdss7-21 ~]# kubectl exec -it nginx-dp-5dfc689474-fc6z9 /bin/bash root@nginx-ds-pqszs:/# ip addr1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever8: eth0@if9: mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:07:15:02 brd ff:ff:ff:ff:ff:ff inet 172.7.21.3/24 brd 172.7.21.255 scope global

删除pod资源:删除后,pod控制器会立即重启一个新的pod资源,是重启Pod的重要方法

[root@hdss7-21 ~]# kubectl delete pod nginx-db-7b4dd9978d-8wzdl -n kube-public pod "nginx-db-7b4dd9978d-8wzdl" deleted# 强制删除[root@hdss7-21 ~]# kubectl delete pod nginx-db-7b4dd9978d-8wzdl -n kube-public --force --grace-period=0

删除deployment资源

[root@hdss7-21 ~]# kubectl delete deployment nginx-db -n kube-publicdeployment.extensions "nginx-db" deleted[root@hdss7-21 ~]# kubectl get deployment -n kube-publicNo resources found.[root@hdss7-21 ~]# kubectl get pods -n kube-publicNo resources found.[root@hdss7-21 ~]#

创建service资源

# 暴露80端口[root@hdss7-21 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-publicservice/nginx-dp exposed[root@hdss7-21 ~]# kubectl get all -n kube-publicNAME READY STATUS RESTARTS AGEpod/nginx-dp-5dfc689474-4dfsq 1/1 Running 0 44m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/nginx-dp ClusterIP 192.168.103.156 80/TCP 29mNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-dp 2/2 2 2 44mNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-dp-5dfc689474 2 2 2

查看端口是否可访问

# 当无法curl时,检查宿主机的内核中是否开启了ipv4转发功能[root@hdss7-21 ~]# cat /proc/sys/net/ipv4/ip_forward0[root@hdss7-21 ~]# echo "1" >!$echo "1" >/proc/sys/net/ipv4/ip_forward[root@hdss7-21 ~]# cat /proc/sys/net/ipv4/ip_forward1[root@hdss7-21 ~]# curl 192.168.103.156 Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer to

# 访问成功# 查看lvs服务信息[root@hdss7-21 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConnTCP 192.168.0.1:443 nq -> 10.4.7.21:6443 Masq 1 0 0 -> 10.4.7.22:6443 Masq 1 0 0 TCP 192.168.103.156:80 nq -> 172.7.22.2:80 Masq 1 0 0

对deployment资源扩容:默认创建时只有一个pod资源

[root@hdss7-21 ~]# kubectl scale deployment nginx-dp --replicas=2 -n kube-publicdeployment.extensions/nginx-dp scaled# 查看扩容后的信息[root@hdss7-21 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConnTCP 192.168.0.1:443 nq -> 10.4.7.21:6443 Masq 1 0 0 -> 10.4.7.22:6443 Masq 1 0 0 TCP 192.168.103.156:80 nq -> 172.7.22.2:80 Masq 1 0 0 -> 172.7.22.3:80 Masq 1 0 0

查看svc详细信息

[root@hdss7-21 ~]# kubectl describe svc nginx-dp -n kube-publicName: nginx-dp # service资源名称Namespace: kube-publicLabels: app=nginx-dpAnnotations: Selector: app=nginx-dp # pod标签名称,将service与pod关联起来Type: ClusterIPIP: 192.168.103.156 # 该IP网段对集群内部使用Port: 80/TCPTargetPort: 80/TCPEndpoints: 172.7.21.2:80,172.7.22.2:80Session Affinity: NoneEvents:

总结

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【Linux云计算架构:第四阶段-Linux虚拟化-私有云-docker】第14章—— GitLab与Jenkins持续集成平台使用方法
下一篇:SpringBoot开发教程之AOP日志处理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~