#创建3个叫web类型的 deployment 的pod
[root@master ~]# kubectl create deployment web --image nginx --replicas 3
deployment.apps/web created
# 以 ps 输出格式列出所有 pod
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 Completed 0 24h
nginx 1/1 Running 1 24h
web-96d5df5c8-kwsd4 1/1 Running 0 13s
web-96d5df5c8-rt9x5 0/1 ContainerCreating 0 13s
web-96d5df5c8-x9wmr 0/1 ContainerCreating 0 13s
#以 ps 输出格式列出所有 pod 并提供更多信息(例如节点名称)
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 0/1 Completed 0 24h 10.244.1.9 node1 <none> <none>
nginx 1/1 Running 1 24h 10.244.2.5 node2 <none> <none>
web-96d5df5c8-kwsd4 1/1 Running 0 33s 10.244.1.14 node1 <none> <none>
web-96d5df5c8-rt9x5 1/1 Running 0 33s 10.244.1.15 node1 <none> <none>
web-96d5df5c8-x9wmr 1/1 Running 0 33s 10.244.1.13 node1 <none> <none>
#以 ps 输出格式列出具有指定 NAME 的单个复制控制器
[root@master ~]# kubectl get deployment web
NAME READY UP-TO-DATE AVAILABLE AGE
web 3/3 3 3 49s
[root@master ~]#
# 以 JSON 输出格式列出单个 Pod
[root@master ~]# kubectl get -o json pod web-96d5df5c8-kwsd4
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"creationTimestamp": "2021-12-20T14:43:51Z",
"generateName": "web-96d5df5c8-",
"labels": {
"app": "web",
"pod-template-hash": "96d5df5c8"
},
"managedFields": [
{
"apiVersion": "v1",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:metadata": {
"f:generateName": {},
"f:labels": {
".": {},
"f:app": {},
"f:pod-template-hash": {}
},
"f:ownerReferences": {
".": {},
"k:{\"uid\":\"e75e20b4-0084-4de3-9831-e50f16ef4fcc\"}": {
......
2 create
[root@master ~]# kubectl create deployment test --image nginx
deployment.apps/test created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-5f6778868d-dw678 0/1 ContainerCreating 0 16s
web-96d5df5c8-kwsd4 1/1 Running 0 9m18s
web-96d5df5c8-rt9x5 1/1 Running 0 9m18s
web-96d5df5c8-x9wmr 1/1 Running 0 9m18s
[root@master ~]#
# 创建一个deployment类型叫v1的pod并执行sleep 600
[root@master ~]# kubectl create deployment v1 --image busybox -- sleep 600
deployment.apps/v1 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-5f6778868d-dw678 1/1 Running 0 2m31s
v1-8449569d5c-jt6tv 0/1 ContainerCreating 0 7s
web-96d5df5c8-kwsd4 1/1 Running 0 11m
web-96d5df5c8-rt9x5 1/1 Running 0 11m
web-96d5df5c8-x9wmr 1/1 Running 0 11m
3 run
#运行一个nginx的pod(默认创建pod类型)
[root@master ~]# kubectl run nginx --image nginx
pod/nginx created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Running 0 8s
#运行一个标签为"app=nginx"的pod
[root@master ~]# kubectl run nginx --image nginx --labels "app=nginx" --replicas 3
Flag --replicas has been deprecated, has no effect and will be removed in the future.
Error from server (AlreadyExists): pods "nginx" already exists
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3m43s
[root@master ~]#
[root@master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: node2/192.168.200.153
Start Time: Sun, 19 Dec 2021 22:13:20 +0800
Labels: run=nginx
Annotations: <none>
Status: Running
IP: 10.244.2.4
# 试运行;打印相应的 API 对象而不创建它们
[root@master ~]# kubectl run nginx --image nginx --dry-run=client
pod/nginx created (dry run)
[root@master ~]#
[root@master ~]# kubectl get pods
No resources found in default namespace.
#启动一个busybox pod并保持在前台,如果退出不要重启
[root@master ~]# kubectl run -it busybox --image busybox --restart=Never
If you don't see a command prompt, try pressing enter.
/ # exit
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 Completed 0 42s
4 expose
#myapp的80映射到本机的8080
[root@master ~]# kubectl expose deployment myapp --port 8080 --target-port 80
service/myapp exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
myapp ClusterIP 10.108.162.12 <none> 8080/TCP 11s
nginx NodePort 10.109.48.237 <none> 80:31253/TCP 21h
[root@master ~]#
[root@master ~]# curl 10.108.162.12:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
5 delete
# 删除所有deployment 类型的pods
[root@master ~]# kubectl delete deployment --all
deployment.apps "test" deleted
deployment.apps "v1" deleted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 Completed 0 24h
nginx 1/1 Running 1 24h
test-5f6778868d-dw678 0/1 Terminating 0 10m
v1-8449569d5c-jt6tv 1/1 Terminating 0 8m15s
#删除所有pod
[root@master ~]# kubectl delete pod --all
pod "busybox" deleted
pod "nginx" deleted
[root@master ~]# kubectl get pods
No resources found in default namespace.
[root@master ~]#
7 scale
#将名为“nginx”的pod集扩展到 3
[root@master ~]# kubectl create deployment nginx --image nginx
deployment.apps/nginx created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-62jw6 0/1 ContainerCreating 0 7s
[root@master ~]# kubectl scale --replicas 3 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-62jw6 1/1 Running 0 42s
nginx-6799fc88d8-c29qh 1/1 Running 0 11s
nginx-6799fc88d8-qhj2p 1/1 Running 0 11s
[root@master ~]#
# 如果名为nginx当前大小的部署为 3,则将nginx扩展到 4
[root@master ~]# kubectl scale --current-replicas 3 --replicas 4 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-62jw6 1/1 Running 0 2m7s
nginx-6799fc88d8-c29qh 1/1 Running 0 96s
nginx-6799fc88d8-htlpq 0/1 ContainerCreating 0 7s
nginx-6799fc88d8-qhj2p 1/1 Running 0 96s
[root@master ~]#
8 autoscale
#自动扩展部署“nginx”,pod 数量在 2 到 10 之间,未指定目标 CPU 利用率,因此将使用默认自动扩展策略
[root@master ~]# kubectl autoscale deployment nginx --min 2 --max 10
horizontalpodautoscaler.autoscaling/nginx autoscaled
[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx <unknown>/80% 2 10 0 8s
[root@master ~]# kubectl get podsNAME
error: the server doesn't have a resource type "podsNAME"
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-62jw6 1/1 Running 0 4m59s
nginx-6799fc88d8-c29qh 1/1 Running 0 4m28s
[root@master ~]#
#自动扩展复制控制器“nginx”,pod 数量在 1 到 5 之间,目标 CPU 利用率为 80%
[root@master ~]# kubectl create deployment web --image nginx
deployment.apps/web created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-96d5df5c8-jv5vj 0/1 ContainerCreating 0 4s
[root@master ~]#
[root@master ~]# kubectl autoscale deployment web --min 2 --max 10
horizontalpodautoscaler.autoscaling/web autoscaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-96d5df5c8-jv5vj 1/1 Running 0 37s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-96d5df5c8-jv5vj 1/1 Running 0 54s
web-96d5df5c8-mtkqv 0/1 ContainerCreating 0 12s
[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx <unknown>/80% 2 10 2 13m
web Deployment/web <unknown>/80% 2 10 2 45s
[root@master ~]# kubectl delete hpa web
horizontalpodautoscaler.autoscaling "web" deleted
[root@master ~]# kubectl autoscale deployment web --max=5 --cpu-percent=80
horizontalpodautoscaler.autoscaling/web autoscaled
[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx <unknown>/80% 2 10 2 14m
web Deployment/web <unknown>/80% 1 5 2 15s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-96d5df5c8-jv5vj 1/1 Running 0 2m8s
web-96d5df5c8-mtkqv 1/1 Running 0 86s
[root@master ~]#
9 logs
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-96d5df5c8-jv5vj 1/1 Running 0 3m8s
web-96d5df5c8-mtkqv 1/1 Running 0 2m26s
[root@master ~]# kubectl logs deployment/web
Found 2 pods, using pod/web-96d5df5c8-jv5vj
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/20 15:29:38 [notice] 1#1: using the "epoll" event method
2021/12/20 15:29:38 [notice] 1#1: nginx/1.21.4
2021/12/20 15:29:38 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/12/20 15:29:38 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/20 15:29:38 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 15:29:38 [notice] 1#1: start worker processes
2021/12/20 15:29:38 [notice] 1#1: start worker process 32
2021/12/20 15:29:38 [notice] 1#1: start worker process 33
2021/12/20 15:29:38 [notice] 1#1: start worker process 34
2021/12/20 15:29:38 [notice] 1#1: start worker process 35
[root@master ~]#
10 attach
#从运行 pod web中获取输出;使用 'kubectl.kubernetes.io/default-container' 注释 # 选择要附加的容器,否则将选择 pod 中的第一个容器
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-qmw4h 1/1 Running 0 54s
nginx-6799fc88d8-xn6mk 1/1 Running 0 61s
test-5f6778868d-2dmrq 1/1 Running 0 43s
v1 0/1 ContainerCreating 0 6s
web-96d5df5c8-jv5vj 1/1 Running 0 6m13s
web-96d5df5c8-mtkqv 1/1 Running 0 5m31s
[root@master ~]#
[root@master ~]# kubectl attach v1
Defaulting container name to v1.
Use 'kubectl describe pod/v1 -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
error: unable to upgrade connection: container v1 not found in pod v1_default
[root@master ~]#
11 describe
#描述一个pod
[root@master ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Mon, 20 Dec 2021 23:34:33 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-6799fc88d8 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m16s deployment-controller Scaled up replica set nginx-6799fc88d8 to 1
Normal ScalingReplicaSet 2m9s deployment-controller Scaled up replica set nginx-6799fc88d8 to 2
[root@master ~]#
12 cluster-info
#打印控制平面和集群服务的地址
[root@master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.200.145:6443
KubeDNS is running at https://192.168.200.145:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@master ~]#
13 exec
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-qmw4h 1/1 Running 0 5m
nginx-6799fc88d8-xn6mk 1/1 Running 0 5m7s
test-5f6778868d-2dmrq 1/1 Running 0 4m49s
v1 1/1 Running 0 4m12s
web-96d5df5c8-jv5vj 1/1 Running 0 10m
web-96d5df5c8-mtkqv 1/1 Running 0 9m37s
[root@master ~]# kubectl exec -it v1 -- /bin/bash
root@v1:/# exit
exit
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 47h
nginx NodePort 10.109.48.237 <none> 80:31253/TCP 47h
[root@master ~]#
14 edit
#编辑pod(把test的标签改为app=apache)
[root@master ~]#
kubectl describe pod test
Name: test
Namespace: default
Priority: 0
Node: node1/192.168.200.144
Start Time: Sun, 19 Dec 2021 21:32:01 -0500
Labels: app=nginx
[root@master ~]#
kubectl edit pod/test
pod/test edited
[root@master ~]#
kubectl describe pod test
Name: test
Namespace: default
Priority: 0
Node: node1/192.168.200.144
Start Time: Sun, 19 Dec 2021 21:32:01 -0500
Labels: app=apache
......
15 port-forward
[root@master ~]# kubectl port-forward pod/v1 80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:32933 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10248 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10249 0.0.0.0:*
LISTEN 0 128 192.168.72.142:2379 0.0.0.0:*
LISTEN 0 128 127.0.0.1:2379 0.0.0.0:*
LISTEN 0 128 192.168.72.142:2380 0.0.0.0:*
LISTEN 0 128 127.0.0.1:2381 0.0.0.0:*
LISTEN 0 128 127.0.0.1:80 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10257 0.0.0.0:*
LISTEN 0 128 0.0.0.0:32722 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10259 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:10250 *:*
LISTEN 0 128 *:6443 *:*
LISTEN 0 128 [::1]:80 [::]:*
LISTEN 0 128 *:10256 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]# curl 127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 监听所有地址的80端口,转发到 pod 中的80
[root@master ~]# kubectl port-forward --address 0.0.0.0 pod/web 80
Forwarding from 0.0.0.0:80 -> 80
Handling connection for 80
Handling connection for 80
[root@k8s-master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:32933 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10248 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10249 0.0.0.0:*
LISTEN 0 128 192.168.72.142:2379 0.0.0.0:*
LISTEN 0 128 127.0.0.1:2379 0.0.0.0:*
LISTEN 0 128 192.168.72.142:2380 0.0.0.0:*
LISTEN 0 128 127.0.0.1:2381 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10257 0.0.0.0:*
LISTEN 0 128 0.0.0.0:32722 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10259 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:10250 *:*
LISTEN 0 128 *:6443 *:*
LISTEN 0 128 *:10256 *:*
LISTEN 0 128 [::]:22 [::]:*
16 cp
#把本地文件cp到pod中
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-qmw4h 1/1 Running 0 11m
nginx-6799fc88d8-xn6mk 1/1 Running 0 11m
test-5f6778868d-2dmrq 1/1 Running 0 11m
v1 1/1 Running 0 10m
web-96d5df5c8-jv5vj 1/1 Running 0 16m
web-96d5df5c8-mtkqv 1/1 Running 0 15m
[root@master ~]# echo "xixihaha" > test
[root@master ~]# ls
anaconda-ks.cfg kube-flannel.yml test
[root@master ~]# kubectl cp test v1:/tmp/
[root@master ~]# kubectl exec v1 -- cat /tmp/test
xixihaha
[root@master ~]#
17 label
给资源设置标签
[root@master ~]# kubectl label --overwrite=true deployment test app=test
deployment.apps/test labeled
[root@master ~]# kubectl describe pod test
Name: test-5f6778868d-2dmrq
Namespace: default
Priority: 0
Node: node1/192.168.200.144
Start Time: Mon, 20 Dec 2021 23:34:51 +0800
Labels: app=test
pod-template-hash=5f6778868d
Annotations: <none>
Status: Running
IP: 10.244.1.37
IPs:
IP: 10.244.1.37
Controlled By: ReplicaSet/test-5f6778868d
# 修改标签
[root@master ~]# kubectl label --overwrite=true pod test-5f6778868d-2dmrq app=set
pod/test-5f6778868d-2dmrq labeled
[root@master ~]# kubectl pod test
[root@master ~]# kubectl pod test-5f6778868d-2dmrq
Error: unknown command "pod" for "kubectl"
Did you mean this?
top
Run 'kubectl --help' for usage.
[root@master ~]# kubectl describe pod test
Name: test-5f6778868d-2dmrq
Namespace: default
Priority: 0
Node: node1/192.168.200.144
Start Time: Mon, 20 Dec 2021 23:34:51 +0800
Labels: app=set
pod-template-hash=5f6778868d
Annotations: <none>
Status: Running
IP: 10.244.1.37
IPs:
IP: 10.244.1.37
Containers:
# 追加标签
[root@master ~]# kubectl label test-5f6778868d-2dmrq xixi=haha
error: the server doesn't have a resource type "test-5f6778868d-2dmrq"
[root@master ~]# kubectl label pods test-5f6778868d-2dmrq xixi=haha
pod/test-5f6778868d-2dmrq labeled
[root@master ~]# kubectl describe pod test-5f6778868d-2dmrq
Name: test-5f6778868d-2dmrq
Namespace: default
Priority: 0
Node: node1/192.168.200.144
Start Time: Mon, 20 Dec 2021 23:34:51 +0800
Labels: app=set
pod-template-hash=5f6778868d
xixi=haha
Annotations: <none>
Status: Running
IP: 10.244.1.37
IPs:
IP: 10.244.1.37
Containers: