這篇文章為大家分享kubernetes中kubectl的管理方法。文章涵蓋kubectl 陳述式管理方法、kubectl 聲明式管理方法以及kubectl的核心資源介紹,希望大家通過(guò)這篇文章能有所收獲。
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、成都網(wǎng)站制作、屯留網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、屯留網(wǎng)絡(luò)營(yíng)銷、屯留企業(yè)策劃、屯留品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供屯留建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
kubectl小潔:
kubectl是官方的CLI命令行工具,用于apiserver進(jìn)行通信,將用戶在命令行輸入的命令,組織并轉(zhuǎn)化為apiserver能識(shí)別的信息,進(jìn)而實(shí)現(xiàn)管理k8s各種資源的一種有效途徑。
1、查看當(dāng)前集群所有命名空間
[root@test-nodes1 ~]# kubectl get namespace
NAME STATUS AGE
default Active 42h
kube-node-lease Active 42h
kube-public Active 42h
kube-system Active 42h
------------------------------------------------------------------------------------------
2、查看default命名空間下的所有資源
[root@test-nodes1 ~]# kubectl get all -n default
NAME READY STATUS RESTARTS AGE
pod/nginx-ds-76fr8 0/1 ImagePullBackOff 0 39h
pod/nginx-ds-zz7jn 0/1 ErrImagePull 0 39h
pod/nginx-ds1-qg45q 1/1 Running 0 39h
pod/nginx-ds1-whnmv 1/1 Running 0 39h
#pod資源
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.0.1 443/TCP 42h
#service資源
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/nginx-ds 2 2 0 2 0 39h
daemonset.apps/nginx-ds1 2 2 2 2 2 39h
#pod控制器
------------------------------------------------------------------------------------------
3、創(chuàng)建與刪除命名空間
[root@test-nodes1 ~]# kubectl create namespace test
namespace/test created
[root@test-nodes1 ~]# kubectl get namespace
NAME STATUS AGE
default Active 42h
kube-node-lease Active 42h
kube-public Active 42h
kube-system Active 42h
test Active 6s
[root@test-nodes1 ~]# kubectl delete namespace test
namespace "test" deleted
------------------------------------------------------------------------------------------
4、創(chuàng)建deployment(pods控制器)資源
[root@test-nodes1 ~]# kubectl create deployment nignx-dp --image=test-harbor.cedarhd.com/public/nginx:v1.7.9 -n kube-public
deployment.apps/nignx-dp created
------------------------------------------------------------------------------------------
5、查看kube-public命名空間下的deployment資源控制器
[root@test-nodes1 ~]# kubectl get deployment -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nignx-test 0/1 1 0 70s
------------------------------------------------------------------------------------------
6、查看kube-public命名空間下pods的運(yùn)行情況
[root@test-nodes1 ~]# kubectl get deployment -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nignx-test 1/1 1 1 21s
------------------------------------------------------------------------------------------
7、查看kube-public空間下的pods資源
[root@test-nodes1 ~]# kubectl get pods -n kube-public -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nignx-dp-7f6d4979bd-nnc57 0/1 ImagePullBackOff 0 13m 172.7.21.4 test-nodes1.cedarhd.com
nignx-test-655d6fbcb5-r9t57 1/1 Running 0 4m1s 172.7.22.4 test-nodes2.cedarhd.com
------------------------------------------------------------------------------------------
8、在test-nodes2.cedarhd.com節(jié)點(diǎn)上curl 172.7.22.4 nginx
[root@test-nodes2 ~]# curl 172.7.22.4
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
------------------------------------------------------------------------------------------
9、查看deployment下具體pods(nginx-test)詳細(xì)信息
[root@test-nodes1 ~]# kubectl describe deployment nignx-test -n kube-public
Name: nignx-test
Namespace: kube-public
CreationTimestamp: Mon, 03 Feb 2020 02:02:59 -0500
Labels: app=nignx-test
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nignx-test
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nignx-test
Containers:
nginx:
Image: test-harbor.cedarhd.com/public/nginx:v1.7.9
Port:
Host Port:
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: nignx-test-655d6fbcb5 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 22m deployment-controller Scaled up replica set nignx-test-655d6fbcb5 to 1
------------------------------------------------------------------------------------------
10、進(jìn)入nginx-test pod資源
[root@test-nodes1 ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nignx-test-655d6fbcb5-r9t57 1/1 Running 0 24m
[root@test-nodes1 ~]# kubectl exec -ti nignx-test-655d6fbcb5-r9t57 /bin/bash -n kube-public
#與docker 操作方法一致
root@nignx-test-655d6fbcb5-r9t57:/# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
12: eth0@if13: mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:07:16:04 brd ff:ff:ff:ff:ff:ff
inet 172.7.22.4/24 brd 172.7.22.255 scope global eth0
valid_lft forever preferred_lft forever
------------------------------------------------------------------------------------------
11、刪除pod資源(即重啟)
[root@test-nodes1 ~]# kubectl delete pod nignx-test-655d6fbcb5-r9t57 -n kube-public
pod "nignx-test-655d6fbcb5-r9t57" deleted
[root@test-nodes1 ~]# kubectl get pods -n kube-public -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nignx-dp-7f6d4979bd-nnc57 0/1 ImagePullBackOff 0 40m 172.7.21.4 test-nodes1.cedarhd.com
nignx-test-655d6fbcb5-c42z8 1/1 Running 0 18s 172.7.21.5 test-nodes1.cedarhd.com
------------------------------------------------------------------------------------------
12、刪除deployment
[root@test-nodes1 ~]# kubectl get deploy -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nignx-dp 0/1 1 0 53m
nignx-test 1/1 1 1 36m
[root@test-nodes1 ~]# kubectl delete deploy nignx-dp -n kube-public
deployment.extensions "nignx-dp" deleted
------------------------------------------------------------------------------------------
13、為nginx-dp pod資源創(chuàng)建service資源,保障pod的高可用,通過(guò)集群IP訪問(wèn)
[root@test-nodes1 ~]# kubectl get all -n kube-public
NAME READY STATUS RESTARTS AGE
pod/nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 17s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-dp 1/1 1 1 17s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dp-5b9b697bcc 1 1 1 17s
[root@test-nodes1 ~]# kubectl get pods -n kube-public -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 3m19s 172.7.21.4 test-nodes1.cedarhd.com
[root@test-nodes1 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
service/nginx-dp exposed
[root@test-nodes1 ~]# kubectl get svc -n kube-public #查看service資源
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.224.56 80/TCP 12m
[root@test-nodes1 ~]# kubectl get all -n kube-public
NAME READY STATUS RESTARTS AGE
pod/nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 5m21s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-dp ClusterIP 192.168.224.56 80/TCP 79s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-dp 1/1 1 1 5m21s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dp-5b9b697bcc 1 1 1 5m21s
[root@test-nodes1 ~]# curl 192.168.224.56
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@test-nodes1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.3.153.221:6443 Masq 1 0 0
-> 10.3.153.222:6443 Masq 1 0 0
TCP 192.168.224.56:80 nq
-> 172.7.21.4:80 Masq 1 0 0
小結(jié):
聲明式資源管理方法依賴于—資源配置清單(yaml\json),偏于修改POD配置。
1、查看當(dāng)前pod的資源配置清單
[root@test-nodes1 ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 5h31m
-nodes1.cedarhd.com
[root@test-nodes1 ~]# kubectl get pods nginx-dp-5b9b697bcc-jtrlp -n kube-public -o yaml
apiVersion: v1
kind: Pod #類型為pod
metadata:
creationTimestamp: "2020-02-03T07:50:11Z"
generateName: nginx-dp-5b9b697bcc-
labels:
app: nginx-dp
pod-template-hash: 5b9b697bcc
name: nginx-dp-5b9b697bcc-jtrlp
namespace: kube-public
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: nginx-dp-5b9b697bcc
uid: 30bbaf90-c97e-4167-9419-45a632e9b3ce
resourceVersion: "222257"
selfLink: /api/v1/namespaces/kube-public/pods/nginx-dp-5b9b697bcc-jtrlp
uid: 59d63844-8ee7-4d7e-8536-d4e5de1ba903
spec:
containers:
- image: test-harbor.cedarhd.com/public/nginx:v1.7.9
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-nt4w9
readOnly: true
DNSPolicy: ClusterFirst
enableServiceLinks: true
nodeName: test-nodes1.cedarhd.com
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-nt4w9
secret:
defaultMode: 420
secretName: default-token-nt4w9
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2020-02-03T07:50:11Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2020-02-03T07:50:14Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2020-02-03T07:50:14Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2020-02-03T07:50:11Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://745d5ad3412e5bccf2fb27dacce57e76987e8f6881cdb3aec79912888ba37ad6
image: test-harbor.cedarhd.com/public/nginx:v1.7.9
imageID: docker-pullable://test-harbor.cedarhd.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2
lastState: {}
name: nginx
ready: true
restartCount: 0
state:
running:
startedAt: "2020-02-03T07:50:13Z"
hostIP: 10.3.153.221
phase: Running
podIP: 172.7.21.4
qosClass: BestEffort
startTime: "2020-02-03T07:50:11Z"
------------------------------------------------------------------------------------------
2、獲取service資源的配置清單
[root@test-nodes1 ~]# kubectl get service -n kube-public
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.224.56 80/TCP 5h31m
[root@test-nodes1 ~]# kubectl get service nginx-dp -o yaml -n kube-public
apiVersion: v1
kind: Service #類型為service
metadata:
creationTimestamp: "2020-02-03T07:54:13Z"
labels:
app: nginx-dp
name: nginx-dp
namespace: kube-public
resourceVersion: "222606"
selfLink: /api/v1/namespaces/kube-public/services/nginx-dp
uid: 1b2310b5-6016-4692-b632-5c43d6dc4de5
spec:
clusterIP: 192.168.224.56
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-dp
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
------------------------------------------------------------------------------------------
3、explain查看字段幫助說(shuō)明
kubectl explain service.metadata
------------------------------------------------------------------------------------------
4、新建一個(gè)service的資源配置清單
[root@test-nodes1 ~]# vi nginx-ds-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-ds
name: nginx-ds
namespace: kube-public
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-ds
sessionAffinity: None
type: ClusterIP
------------------------------------------------------------------------------------------
5、通過(guò)聲明式資源配置清單創(chuàng)建一個(gè)service資源
[root@test-nodes1 ~]# kubectl create -f nginx-ds-svc.yaml
service/nginx-ds created
[root@test-nodes1 ~]# kubectl get service -n kube-public
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.224.56 80/TCP 5h39m
nginx-ds ClusterIP 192.168.66.3 80/TCP 16s
------------------------------------------------------------------------------------------
6、在線修改nginx-ds的 service 資源端口為81
[root@test-nodes1 ~]# kubectl get svc -n kube-public
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.224.56 80/TCP 5h49m
nginx-ds ClusterIP 192.168.66.3 80/TCP 10m
[root@test-nodes1 ~]# kubectl edit svc nginx-ds -n kube-public
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2020-02-03T13:23:41Z"
labels:
app: nginx-ds
name: nginx-ds
namespace: kube-public
resourceVersion: "250724"
selfLink: /api/v1/namespaces/kube-public/services/nginx-ds
uid: 5840630d-e00d-4e98-91a1-2b65a1eb22f4
spec:
clusterIP: 192.168.66.3
ports:
- port: 81 #修改對(duì)外端口81
protocol: TCP
targetPort: 80
selector:
app: nginx-ds
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
"/tmp/kubectl-edit-wp634.yaml" 27L, 684C written
service/nginx-ds edited
[root@test-nodes1 ~]# kubectl get svc -n kube-public
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.224.56 80/TCP 5h50m
nginx-ds ClusterIP 192.168.66.3 81/TCP 10m
------------------------------------------------------------------------------------------
7、刪除一個(gè)service資源
[root@test-nodes1 ~]# kubectl delete svc nginx-ds -n kube-public
service "nginx-ds" deleted
[root@test-nodes1 ~]# kubectl get all -n kube-public
NAME READY STATUS RESTARTS AGE
pod/nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 6h2m
#pod資源,承載容器應(yīng)用所在
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-dp ClusterIP 192.168.224.56 80/TCP 5h67m
#service資源,Service是Kubernetes里最核心的資源對(duì)象之一,Service定義了一個(gè)服務(wù)的訪問(wèn)入口地址,前端的應(yīng)用(Pod)通過(guò)這個(gè)入口地址訪問(wèn)其背后的一組由Pod副本組成的集群實(shí)力
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-dp 1/1 1 1 6h2m
#deployment資源,Deployment同樣也是Kubernetes系統(tǒng)的一個(gè)核心概念,
主要職責(zé)和RC一樣的都是保證Pod的數(shù)量和健康,可理解為pod控制器,
當(dāng)我們刪除pod時(shí),會(huì)再啟動(dòng),不是真正刪除,如需刪除該pod,必須刪除
該控制器
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dp-5b9b697bcc 1 1 1 6h2m
1、創(chuàng)建一個(gè)nginx pod
[root@test-nodes1 ~]# vi nginx-pod.yaml
apiVersion: v1 kind: Pod metadata: name: nginx labels: app: web spec: containers: - name: nginx image: test-harbor.cedarhd.com/public/nginx:v1.7.9 ports: - containerPort: 80 [root@test-nodes1 ~]# kubectl create -f nginx-pod.yaml #創(chuàng)建一個(gè)POD pod/nginx created [root@test-nodes1 ~]# kubectl get all NAME READY STATUS RESTARTS AGE pod/nginx 1/1 Running 0 10m #剛剛創(chuàng)建的POD pod/nginx-ds1-qg45q 1/1 Running 0 47h pod/nginx-ds1-whnmv 1/1 Running 0 47h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 192.168.0.1
443/TCP 2d2h NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/nginx-ds1 2 2 2 2 2 47h ------------------------------------------------------------------------------------------ 2、為該pod創(chuàng)建一個(gè)service [root@test-nodes1 ~]# kubectl expose pod nginx --port=80 -n default service/nginx exposed [root@test-nodes2 ~]# kubectl get all NAME READY STATUS RESTARTS AGE pod/nginx 1/1 Running 0 14m pod/nginx-ds1-qg45q 1/1 Running 0 47h pod/nginx-ds1-whnmv 1/1 Running 0 47h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 192.168.0.1 443/TCP 2d2h service/nginx ClusterIP 192.168.123.163 80/TCP 107s NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/nginx-ds1 2 2 2 2 2 47h [root@test-nodes2 ~]# curl 192.168.123.163 獲取nginx頁(yè)面 ------------------------------------------------------------------------------------------ 3、創(chuàng)建一個(gè)deployment [root@test-nodes2 ~]# kubectl create deployment nginx-test --image=test-harbor.cedarhd.com/public/nginx:v1.7.9 deployment.apps/nginx-test created [root@test-nodes2 ~]# kubectl get all NAME READY STATUS RESTARTS AGE pod/nginx 1/1 Running 0 27m pod/nginx-ds1-qg45q 1/1 Running 0 47h pod/nginx-ds1-whnmv 1/1 Running 0 47h pod/nginx-test-5674474869-5nr7j 1/1 Running 0 4s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 192.168.0.1 443/TCP 2d2h service/nginx ClusterIP 192.168.123.163 80/TCP 15m NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/nginx-ds1 2 2 2 2 2 47h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx-test 1/1 1 1 4s NAME DESIRED CURRENT READY AGE replicaset.apps/nginx-test-5674474869 1 1 1 4s
以上就是kubernetes中kubectl管理方法的介紹,內(nèi)容較為全面,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ骺赡軙?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。