[root@master ~]# kubectl get ns NAME STATUS AGE default Active 2d # 所有未指定Namespace的对象都会被分配到在default的命名空间 kube-node-lease Active 2d # 集群节点之间的心跳维护 v1.13开始引入 kube-public Active 2d # 此命名空间下的资源可以被所有人访问(包括未认证用户) kube-system Active 2d # 所有由Kubernetes系统创建的资源都处于这个命名空间
下面来看namespace资源的具体操作:
查看
查看具体的namespace
[root@master ~]# kubectl get ns default NAME STATUS AGE default Active 2d
[root@master ~]# kubectl run nginx --image=nginx:1.17.1 --port=80 --namespace dev deployment.apps/nginx created
查看Pod信息
获取Pod信息
[root@master ~]# kubectl get pods -n dev NAME READY STATUS RESTARTS AGE nginx-64777cd554-gjdxm 1/1 Running 0 12m
查看指定Pod详细信息
[root@master ~]# kubectl describe pod nginx-64777cd554-gjdxm -n dev Name: nginx-64777cd554-gjdxm Namespace: dev Priority: 0 Node: node2/192.168.52.133 Start Time: Fri, 24 Feb 2023 14:24:54 +0800 Labels: pod-template-hash=64777cd554 run=nginx Annotations: <none> Status: Running IP: 10.244.2.7 IPs: IP: 10.244.2.7 Controlled By: ReplicaSet/nginx-64777cd554 Containers: nginx: Container ID: docker://b345c9a0661fd8035f403593f1c6d2d03c754445232ca90efa4fda7650b96ebf Image: nginx:1.17.1 Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb Port: 80/TCP Host Port: 0/TCP State: Running Started: Fri, 24 Feb 2023 14:24:55 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-xp9r4 (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-xp9r4: Type: Secret (a volume populated by a Secret) SecretName: default-token-xp9r4 Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned dev/nginx-64777cd554-gjdxm to node2 Normal Pulled 6m48s kubelet, node2 Container image "nginx:1.17.1" already present on machine Normal Created 6m48s kubelet, node2 Created container nginx Normal Started 6m47s kubelet, node2 Started container nginx
访问指定Pod
获取Pod
[root@master ~]# kubectl get pods -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-64777cd554-gjdxm 1/1 Running 0 16m 10.244.2.7 node2
访问Pod
[root@master ~]# curl 10.244.2.7:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
删除指定Pod
删除指定Pod
[root@master ~]# kubectl delete pod nginx-64777cd554-gjdxm -n dev pod "nginx-64777cd554-gjdxm" deleted
再次查看Pod,发现又产生了一个
[root@master ~]# kubectl get pods -n dev NAME READY STATUS RESTARTS AGE nginx-64777cd554-gftqq 1/1 Running 0 41s
name in (master, slave):选择所有包含Label中key=”name”且value=”master”或”slave”的对象
name not in (frontend):选择所有包含Label中key=”name”且value不等于”frontend”的对象
标签的选择可以使用多个,此时将多个Label Selector进行组合,使用逗号分隔即可。例如
name=slave, env!=production name not in (frontend), env!=production
添加标签
查看pod资源的标签
[root@master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 21s <none>
为pod资源打标签
[root@master ~]# kubectl label pod nginx version=2.0 -n dev pod/nginx labeled
再次查看
[root@master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 2m23s version=2.0
更新标签
更新并再次查看:
[root@master ~]# kubectl label pod nginx -n dev version=3.0 --overwrite pod/nginx labeled [root@master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 5m21s version=3.0
筛选标签
列出所有标签
[root@master ~]# kubectl get pods -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 9m5s version=3.0 nginx01 1/1 Running 0 82s version=1.0 nginx02 1/1 Running 0 54s <none>
筛选标签,-l "version=3.0"
[root@master ~]# kubectl get pods -l "version=3.0" -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 9m58s version=3.0
[root@master ~]# kubectl get pods -l "version!=3.0" -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx01 1/1 Running 0 3m15s version=1.0 nginx02 1/1 Running 0 2m47s <none>
去除标签
使用labelName-删除标签:
[root@master ~]# kubectl get pods -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 12m tier=back,version=3.0 nginx01 1/1 Running 0 5m8s version=1.0 nginx02 1/1 Running 0 4m40s <none>
[root@master ~]# kubectl label po nginx -n dev tier- pod/nginx labeled
[root@master ~]# kubectl get pods -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 13m version=3.0 nginx01 1/1 Running 0 6m11s version=1.0 nginx02 1/1 Running 0 5m43s <none>
kubectl expose deploy nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev
查看Service
[root@master ~]# kubectl get svc svc-nginx1 -n dev -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR svc-nginx1 ClusterIP 10.108.19.184 <none> 80/TCP 2m36s run=nginx
访问Service
[root@master ~]# curl 10.108.19.184:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
创建集群外部能访问的Service
上面创建的Service的type类型为ClusterIP,这个IP地址只能在集群内部可访问
如果需要创建外部可以访问的Service,需要修改type为NodePort
kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev
查看Service
[root@master ~]# kubectl get svc -n dev -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR svc-nginx1 ClusterIP 10.108.19.184 <none> 80/TCP 7m50s run=nginx svc-nginx2 NodePort 10.111.68.23 <none> 80:31076/TCP 26s run=nginx
访问:
http://192.168.52.100:31076
删除Service
[root@master ~]# kubectl delete svc svc-nginx1 -n dev service "svc-nginx1" deleted