k8s使用心得
查看当前所有namespaces
[root@master ~]# kubectl get namespaces -A
NAME STATUS AGE
default Active 63d
hkd Active 31d
kube-node-lease Active 57d
kube-public Active 63d
kube-system Active 63d
nginx-ingress Active 30d
test Active 30d
查看指定namespaces 下的ingress
[root@master ~]# kubectl get ingress --namespace=test
NAME CLASS HOSTS ADDRESS PORTS AGE
cloud-eureka <none> cloud-eureka.test.demo.kuboard.cn 80 26d
查看ingress详情
[root@master ~]# kubectl describe ingress --namespace=test
Name: cloud-eureka
Namespace: test
Address:
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
cloud-eureka.test.demo.kuboard.cn
/ cloud-eureka:sntdnc (10.100.171.66:8761)
Annotations: k8s.kuboard.cn/displayName: eureka注册中心
k8s.kuboard.cn/workload: cloud-eureka
Events: <none>
从ingress详情中可以看到,ingress下面的service名称是cloud-eureka,目前该service下只有一个pod:10.100.171.66:8761
查看service
[root@master ~]# kubectl get service --namespace=test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cloud-config NodePort 10.96.249.98 <none> 8888:32155/TCP 21d
cloud-eureka NodePort 10.96.181.80 <none> 8761:31739/TCP 27d
service详情
[root@master ~]# kubectl describe service cloud-eureka --namespace=test
Name: cloud-eureka
Namespace: test
Labels: k8s.kuboard.cn/layer=cloud
k8s.kuboard.cn/name=cloud-eureka
Annotations: k8s.kuboard.cn/displayName: eureka注册中心
k8s.kuboard.cn/workload: cloud-eureka
Selector: k8s.kuboard.cn/layer=cloud,k8s.kuboard.cn/name=cloud-eureka
Type: NodePort
IP: 10.96.181.80
Port: sntdnc 8761/TCP
TargetPort: 8761/TCP
NodePort: sntdnc 31739/TCP
Endpoints: 10.100.171.66:8761
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
查看pod
[root@master ~]# kubectl get pods --namespace=test
NAME READY STATUS RESTARTS AGE
cloud-config-0 1/1 Running 5 21d
cloud-eureka-0 1/1 Running 3 21d
pod详情
[root@master ~]# kubectl describe pods cloud-eureka-0 --namespace=test
Name: cloud-eureka-0
Namespace: test
Priority: 0
Node: worker/192.168.75.11
Start Time: Thu, 09 Jul 2020 09:12:57 +0800
Labels: controller-revision-hash=cloud-eureka-7f95b587f
k8s.kuboard.cn/layer=cloud
k8s.kuboard.cn/name=cloud-eureka
statefulset.kubernetes.io/pod-name=cloud-eureka-0
Annotations: cni.projectcalico.org/podIP: 10.100.171.66/32
cni.projectcalico.org/podIPs: 10.100.171.66/32
Status: Running
IP: 10.100.171.66
IPs:
IP: 10.100.171.66
Controlled By: StatefulSet/cloud-eureka
Containers:
hkd-eureka:
Container ID: docker://5df85e2f1769341b9903ef060405780cb3fe5a0037e23177f64f54e616598bcc
Image: idocker.io/hkd-eureka:0.5
Image ID: docker-pullable://idocker.io/hkd-eureka@sha256:0057434b09b824a76fdd7006c107c3d295f26c300080b305a9cad455750450c8
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 30 Jul 2020 09:55:41 +0800
Last State: Terminated
Reason: Error
Exit Code: 143
Started: Thu, 30 Jul 2020 09:55:25 +0800
Finished: Thu, 30 Jul 2020 09:55:26 +0800
Ready: True
Restart Count: 3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-7tr62 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-7tr62:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-7tr62
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: <none>
从pod详情中可以看到该pod所在node主机,里面的容器等信息
进入pod里的容器中
[root@master ~]# kubectl exec -it cloud-eureka-0 --namespace=test -- /bin/sh
/ # date
Thu Jul 30 12:11:20 CST 2020
/ # ps -ef
PID USER TIME COMMAND
1 root 2:35 java -Djava.security.egd=file:/dev/./urandom -jar /hkd-eureka-1.0.jar
101 root 0:00 /bin/sh
118 root 0:00 /bin/sh
125 root 0:00 ps -ef
/ #
临时执行pod容器中的命令
[root@master ~]# kubectl exec cloud-eureka-0 --namespace=test -- date
Thu Jul 30 13:48:41 CST 2020
常用命令总结
kubectl get 资源类型
获取类型为Deployment的资源列表
kubectl get deployments
获取类型为Pod的资源列表
kubectl get pods
获取类型为Node的资源列表
kubectl get nodes
# 查看所有名称空间的 Deployment
kubectl get deployments -A
kubectl get deployments --all-namespaces
# 查看 kube-system 名称空间的 Deployment
kubectl get deployments -n kube-system
并非所有对象都在名称空间里
kubectl describe 资源类型 资源名称
查看名称为nginx-XXXXXX的Pod的信息
kubectl describe pod nginx-XXXXXX
查看名称为nginx的Deployment的信息
kubectl describe deployment nginx
kubectl logs - 查看pod中的容器的打印日志(和命令docker logs 类似)
查看名称为nginx-pod-XXXXXXX的Pod内的容器打印的日志
kubectl logs -f nginx-pod-XXXXXXX
kubectl exec - 在pod中的容器环境内执行命令(和命令docker exec 类似)
在名称为nginx-pod-xxxxxx的Pod中运行bash
kubectl exec -it nginx-pod-xxxxxx -- /bin/bash