k8s in action pods

手动创建 pod、查看日志、配置端口转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
kubectl explain pods  # 解释pod的各个字段定义。
# 使用命令行创建pod yaml,然后手动编辑
hhzzer@admin-PC:~/k8s$ kubectl run kubia-manual --image=hhzzer/kubia --port=8080 --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: kubia-manual
name: kubia-manual
spec:
containers:
- image: hhzzer/kubia
name: kubia-manual
ports:
- containerPort: 8080
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
# 删除命令行创建的yaml部分字段,保留关键的。
hhzzer@admin-PC:~/k8s$ kubectl create -f kubia-manual.yaml --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
name: kubia-manual
namespace: default
spec:
containers:
- image: hhzzer/kubia
name: kubia
ports:
- containerPort: 8080
protocol: TCP
# 查看pod日志,一般应用程序将日志输出到标准输入输出,而不是文件,便于直接查看。
# 日志超过1天或>10M 会自动轮换。
hhzzer@admin-PC:~/k8s$ kubectl logs kubia-manual
kubia server starting ...
# 查看pod中指定容器的日志 ,pod后加容器名称即可。
hhzzer@admin-PC:~/k8s$ kubectl logs kubia-manual kubia
kubia server starting ...
# 为了便于请求pod的服务,可将本地端口转发到pod
hhzzer@admin-PC:~/k8s$ kubectl port-forward kubia-manual 8888:8080
Forwarding from 127.0.0.1:8888 -> 8080
Forwarding from [::1]:8888 -> 8080
# 检测
hhzzer@admin-PC:~/nodejs-app$ curl http://localhost:8888
You've hit kubia-manual

给 pod 打标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
hhzzer@admin-PC:~/k8s$ kubectl create -f kubia-manual-with-labels.yaml --dry-run=client -o
yaml
apiVersion: v1
kind: Pod
metadata:
labels:
creation_method: manual
env: prod
name: kubia-manual-v2
namespace: default
spec:
containers:
- image: hhzzer/kubia
name: kubia
ports:
- containerPort: 8080
protocol: TCP

# 查看pod标签
hhzzer@admin-PC:~/k8s$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
kubia-864b87d69f-dkp6m 1/1 Running 0 98m app=kubia,pod-template-hash=864b87d69f
kubia-864b87d69f-qsxwx 1/1 Running 0 121m app=kubia,pod-template-hash=864b87d69f
kubia-manual-v2 1/1 Running 0 28s creation_method=manual,env=prod
# 修改pod标签
hhzzer@admin-PC:~/k8s$ kubectl label pod kubia-864b87d69f-qsxwx creation_method=manual
# 加--overwrite 修改标签
hhzzer@admin-PC:~/k8s$ kubectl label pod kubia-manual-v2 env=debug --overwrite
# 通过标签筛选 只列标签名,筛选有标签的pod 可使用通配符 '!env' 等 ,标签值可以用in ,not in 组合。
hhzzer@admin-PC:~/k8s$ kubectl get pod -l env=debug
NAME READY STATUS RESTARTS AGE
kubia-manual-v2 1/1 Running 0 5m59s

配合标签调度 pod, spec 中的 nodeSelector

1
2
3
4
5
6
7
8
9
10
apiVersion: vl
kind: Pod
me七adata:
name: kubia-gpu
spec:
nodeSelector
gpu: true
containers:
- image: luksa/kubia
name: kubia

设置注解

1
2
3
4
5
6
7
8
9
10
hhzzer@admin-PC:~/k8s$ kubectl annotate pods kubia-manual-v2 mycompany.com/anotion="fooo bar"
hhzzer@admin-PC:~/k8s$ kubectl describe pods kubia-manual-v2
Name: kubia-manual-v2
Namespace: default
Priority: 0
Node: minikube/172.20.134.184
Start Time: Wed, 03 Mar 2021 17:42:26 +0800
Labels: creation_method=manual
env=debug
Annotations: mycompany.com/anotion: fooo bar

命名空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
hhzzer@admin-PC:~/k8s$ kubectl get ns
NAME STATUS AGE
default Active 7h14m
kube-node-lease Active 7h14m

hhzzer@admin-PC:~/k8s$ kubectl get po --namespace kube-system
NAME READY STATUS RESTARTS AGE
coredns-54d67798b7-z82zd 1/1 Running 0 7h14m
etcd-minikube 1/1 Running 0 7h15m
kube-apiserver-minikube 1/1 Running 0 7h15m
kube-controller-manager-minikube 1/1 Running 0 7h15m
kube-proxy-lhwr8 1/1 Running 0 7h15m
kube-scheduler-minikube 1/1 Running 0 7h15m
storage-provisioner 1/1 Running 0 7h15m
# 创建命名空间
hhzzer@admin-PC:~/k8s$ kubectl create namespace custom-ns --dry-run=client -o yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: custom-ns
spec: {}
status: {}

# 切换命名空间
hhzzer@admin-PC:~/k8s$ kubectl config set-context $(kubectl config current-context) --names
pace kube-system
Context "minikube" modified.
hhzzer@admin-PC:~/k8s$ kubectl get pods
NAME READY STATUS RESTARTS AGE
coredns-54d67798b7-z82zd 1/1 Running 0 7h21m
etcd-minikube 1/1 Running 0 7h21m
kube-apiserver-minikube 1/1 Running 0 7h21m
kube-controller-manager-minikube 1/1 Running 0 7h21m
kube-proxy-lhwr8 1/1 Running 0 7h21m
kube-scheduler-minikube 1/1 Running 0 7h21m
storage-provisioner 1/1 Running 0 7h21m