kubectl 使用紀錄

Posted by Kubeguts on 2021-05-22

使用minikube來練習kubectl指令,如何操作kubernetes

事前安裝

啟動環境

若minikube安裝成功,啟動minikube環境

1
minikube start

透過kubectl查看會得到已經起好一組kubernetes cluster

1
kubectl get po -A

pic02

kubectl 指令

kubectl version : 查看版本

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
$ kubectl version -o json
{
"clientVersion": {
"major": "1",
"minor": "21",
"gitVersion": "v1.21.0",
"gitCommit": "cb303e613a121a29364f75cc67d3d580833a7479",
"gitTreeState": "clean",
"buildDate": "2021-04-08T16:31:21Z",
"goVersion": "go1.16.1",
"compiler": "gc",
"platform": "linux/amd64"
},
"serverVersion": {
"major": "1",
"minor": "20",
"gitVersion": "v1.20.2",
"gitCommit": "faecb196815e248d3ecfb03c680a4507229c2a56",
"gitTreeState": "clean",
"buildDate": "2021-01-13T13:20:00Z",
"goVersion": "go1.15.5",
"compiler": "gc",
"platform": "linux/amd64"
}
}

kubectl cluster-info: 檢查cluster資訊

1
2
3
4
5
$ kubectl cluster-info
Kubernetes control plane is running at https://192.168.49.2:8443
KubeDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

kubectl get all: 取得Kubernetes Pods, Deployments, Services等相關資訊

1
2
3
kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 59m

取得pod資訊,可以多加 --watch,持續監測pod狀態

1
2
3
$ k get pods --watch
NAME READY STATUS RESTARTS AGE
postgres-lastest 0/1 CrashLoopBackOff 6 9m49s

kubectl run [contanier-name] --image=[image-name] 創建kubernetes服務內容

可指定docker image,創建一個包含該docker container之pod的deployment

1
2
$ kubectl run postgres-lastest --image postgres
pod/postgres-lastest created
1
2
3
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/postgres-lastest 0/1 ContainerCreating 0 9s

要注意本範例創建postgres會出現問題,因為沒有給postgres帳號密碼,會出現錯誤

kubectl create -f [ymal檔案] 以yaml檔案,將服務創建出來

透過yaml檔案可以將pods, deployment, services, configMaps一起創建出來

例如這組yaml檔案,可以創建出postgres服務

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Pod
metadata:
name: postgres-pod
labels:
name: postgres-pod
app: demo-voting-app
spec:
containers:
- name: postgres
image: postgres:9.4
env:
- name: POSTGRES_USER
value: admin
- name: POSTGRES_PASSWORD
value: admin
ports:
- containerPort: 5432
1
kubectl create

輸出結果

1
2
k create -f postgres.yaml
pod/postgres-pod created

kubectl delete [pod, service, deployment..] 砍掉服務

若要砍掉一個pod名稱 postgres-1

1
2
$ k delete pods postgres-1
pod "postgres-1" deleted

kubectl logs [pods] 查看服務狀態

如果有個服務要檢查其訊息,可透過kubectl logs檢查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ k logs postgres-pod
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Etc/UTC

kubectl port-forward [pod] [ports] 將ports從pod內部對外介接,使外界可以與pod服務溝通

由於pod內部是interal network,是自己私有的網域,故若我們想要連線到內部的服務,必須要用port-forward方式,將內部port映射到minikube所在外部的port號,才能夠連接

1
2
3
$ k port-forward postgres-pod 5432
Forwarding from 127.0.0.1:5432 -> 5432
Forwarding from [::1]:5432 -> 5432

kubectl expose ... 將服務的port映射出來到不同的pods

若要使不同的pods可以做溝通,
必須要透過kubectl expose對services做設置

kubectl apply [resource] 套用新設定

可以透過yaml檔案,讓運行中的服務,套用新設定,並作更動

除錯紀錄

創建pod時遇到 CrashLoopBackOff

創建postgres服務的pod出了這問題

1
2
3
$ k get pods
NAME READY STATUS RESTARTS AGE
postgres-lastest 0/1 CrashLoopBackOff 8 18m

原因是因為postgres這個container一值無法成功啟動,導致kubernetes不斷嘗試要啟動,造成CrashLoopbackOff

透過 kubectl describe pod查看發生什麼問題

1
kubectl describe pod postgres-lastest

輸出結果

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
49
50
51
52
53
54
55
56
57
Name:         postgres-lastest
Namespace: default
Priority: 0
Node: minikube/192.168.49.2
Start Time: Tue, 04 May 2021 08:37:36 +0000
Labels: run=postgres-lastest
Annotations: <none>
Status: Running
IP: 172.17.0.3
IPs:
IP: 172.17.0.3
Containers:
postgres-lastest:
Container ID: docker://90a3219c26dfeb8f9c1bd3adc89ebc8a544bf9738148ba79a417c231cabc688b
Image: postgres
Image ID: docker-pullable://postgres@sha256:61d5d8ef6cb4e2035f053f26b6b455c201a809354084cc8426b6904b8dd35602
Port: <none>
Host Port: <none>
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 04 May 2021 09:04:31 +0000
Finished: Tue, 04 May 2021 09:04:31 +0000
Ready: False
Restart Count: 10
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-26dwl (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-26dwl:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-26dwl
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 27m default-scheduler Successfully assigned default/postgres-lastest to minikube
Normal Pulled 27m kubelet Successfully pulled image "postgres" in 13.528059916s
Normal Pulled 27m kubelet Successfully pulled image "postgres" in 2.274773124s
Normal Pulled 26m kubelet Successfully pulled image "postgres" in 2.301793879s
Normal Pulled 26m kubelet Successfully pulled image "postgres" in 2.301692707s
Normal Created 26m (x4 over 27m) kubelet Created container postgres-lastest
Normal Started 26m (x4 over 27m) kubelet Started container postgres-lastest
Normal Pulling 25m (x5 over 27m) kubelet Pulling image "postgres"
Warning BackOff 2m19s (x116 over 27m) kubelet Back-off restarting failed container

可透過 kubectl logs查看pod發生什麼問題

1
2
3
4
5
6
7
8
9
10
$ k logs postgres-lastest
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.

See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html

仔細一看可瞭解,原來是帳號密碼忘記設置了。

撇步

kubectl簡化成k

Mac/Linux

若要操作kubectl,可用alias k="kubectl 將kubectl指令簡化成k

參考

除錯紀錄