kubectl get services -A NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 71m kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,9153/TCP 71m kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.99.160.10 <none> 8000/TCP 27m kubernetes-dashboard kubernetes-dashboard ClusterIP 10.108.78.40 <none> 80/TCP 27m ╭─localhost@MacBook-Pro ~
通过YAML创建命名空间
vim namespace.yaml
1 2 3 4 5
--- apiVersion: v1 kind: Namespace metadata: name: development
1 2 3 4 5 6 7 8 9 10 11
╰─$ kubectl apply -f namespace.yaml namespace/development created ╭─localhost@MacBook-Pro ~ ╰─$ kubectl get namespaces NAME STATUS AGE default Active 24h development Active 23s kube-node-lease Active 24h kube-public Active 24h kube-system Active 24h kubernetes-dashboard Active 23h
增加一个生产环境命名空间 vim namespace.yaml
1 2 3 4 5 6 7 8 9 10
--- apiVersion: v1 kind: Namespace metadata: name: development --- apiVersion: v1 kind: Namespace metadata: name: production
1 2 3 4 5 6 7 8 9 10 11 12 13 14
kubectl apply -f namespace.yaml namespace/development unchanged namespace/production created
╭─localhost@MacBook-Pro ~ ╰─$ kubectl get namespaces NAME STATUS AGE default Active 24h development Active 3m7s kube-node-lease Active 24h kube-public Active 24h kube-system Active 24h kubernetes-dashboard Active 23h production Active 6s
╭─localhost@MacBook-Pro ~ ╰─$ kubectl get namespaces NAME STATUS AGE default Active 24h kube-node-lease Active 24h kube-public Active 24h kube-system Active 24h kubernetes-dashboard Active 23h
╭─localhost@MacBook-Pro ~ ╰─$ kubectl apply -f deployment.yaml deployment.apps/pod-info-deployment created
查看创建后的状态
1 2 3 4 5 6 7 8 9 10 11 12 13
# 查看指定命名空间下的控制器信息 ╭─localhost@MacBook-Pro ~ ╰─$ kubectl get deployments -n development NAME READY UP-TO-DATE AVAILABLE AGE pod-info-deployment 3/3 3 3 3m43s
#查看development命名空间下的pod ╭─localhost@MacBook-Pro ~ ╰─$ kubectl get pods -n development NAME READY STATUS RESTARTS AGE pod-info-deployment-7b697c564d-w4mlt 1/1 Running 0 3m39s pod-info-deployment-7b697c564d-wrgsz 1/1 Running 0 3m39s pod-info-deployment-7b697c564d-z4rk6 1/1 Running 0 3m39s
#当前运行的三个node 时间也一样 ╭─localhost@MacBook-Pro ~ ╰─$ kubectl get pods -n development NAME READY STATUS RESTARTS AGE pod-info-deployment-7b697c564d-w4mlt 1/1 Running 0 24m pod-info-deployment-7b697c564d-wrgsz 1/1 Running 0 24m pod-info-deployment-7b697c564d-z4rk6 1/1 Running 0 24m #删除1个node ╭─localhost@MacBook-Pro ~ ╰─$ kubectl delete pod pod-info-deployment-7b697c564d-w4mlt -n development pod "pod-info-deployment-7b697c564d-w4mlt" deleted ^C% #再次查看其中1个node name变了 而且运行时间短 ╭─localhost@MacBook-Pro ~ ╰─$ kubectl get pods -n development NAME READY STATUS RESTARTS AGE pod-info-deployment-7b697c564d-6fg5x 1/1 Running 0 34s pod-info-deployment-7b697c564d-wrgsz 1/1 Running 0 26m pod-info-deployment-7b697c564d-z4rk6 1/1 Running 0 26m
通过事件日志查看pod运行情况 1、获取命名空间的pod服务信息
1 2 3 4 5
kubectl get pods -n development NAME READY STATUS RESTARTS AGE pod-info-deployment-7b697c564d-6fg5x 1/1 Running 0 5d21h pod-info-deployment-7b697c564d-wrgsz 1/1 Running 0 5d21h pod-info-deployment-7b697c564d-z4rk6 1/1 Running 0 5d21h
/ # wget 10.244.0.8:3000 Connecting to 10.244.0.8:3000 (10.244.0.8:3000) saving to 'index.html' index.html 100% |******************************************************************************| 103 0:00:00 ETA 'index.html' saved / #
7、退出busybox
1
/ # exit
查看应用程序日志
1、获取命名空间中pod状态
1 2 3 4 5
kubectl get pods -n development NAME READY STATUS RESTARTS AGE pod-info-deployment-7b697c564d-6fg5x 1/1 Running 0 6d21h pod-info-deployment-7b697c564d-wrgsz 1/1 Running 0 6d21h pod-info-deployment-7b697c564d-z4rk6 1/1 Running 0 6d21h
2、是用logs获取pod节点运行日志
1 2 3
kubectl logs pod-info-deployment-7b697c564d-6fg5x -n development undefined Example app listening on port 3000