创建一个来源一个文件或标准输入的资源
[root@master ~]# kubectl create deployment b1 --image busybox
deployment.apps/b1 created
创建好后查看,发现b1状态是ContainerCreating
,表示正在拉镜像,如果你的镜像早早拉下来了,那它就直接启动了
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
b1-68578fbb6c-gzhn2 0/1 ContainerCreating 0 10s
过了一会再查看b1,发现是CrashLoopBackOff
,表示已经退掉了,退掉了并不代表没运行,它是运行了,运行之后退出了;因为busybox默认是打开/bin程序,而/bin一执行就退出了,跟容器里面一样的,一启动就退出了。
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
b1-68578fbb6c-gzhn2 0/1 CrashLoopBackOff 3 102s
再创建一个b2,在命令后面加上一个-- date
,过一会查看状态,发现也挂掉了,因为-- date命令也是一样的一执行就没了
[root@master ~]# kubectl create deployment b2 --image busybox -- date
deployment.apps/b2 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
b1-68578fbb6c-gzhn2 0/1 CrashLoopBackOff 7 14m
b2-75d5678b7f-76zpc 0/1 CrashLoopBackOff 7 12m
再创建一个b3,还是在创建b1的命令后,加上-- sleep 6000
,过了一会发现,运行起来了
[root@master ~]# kubectl create deployment b3 --image busybox -- sleep 6000
deployment.apps/b3 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
b1-68578fbb6c-gzhn2 0/1 CrashLoopBackOff 7 14m
b2-75d5678b7f-76zpc 0/1 CrashLoopBackOff 7 13m
b3-84d7f7d4bf-lbcnh 1/1 Running 0 47s
由此发现b3里面有任务,就可以运行的,而前面两个是没有任务的,或者说任务一执行就没了,就会挂掉
一下启动三个pods,用kubectl create
,这里我们创建的是deployment无状态)类型的,replicas是用来设置要启动多少pods
[root@master ~]# kubectl create deployment myapp --image nginx --replicas 3
deployment.apps/myapp created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-chght 1/1 Running 0 4m1s
myapp-6d8d776547-dc7jb 1/1 Running 0 4m1s
myapp-6d8d776547-qb7pt 1/1 Running 0 4m1s
nginx-6799fc88d8-kwd2b 1/1 Running 0 24h
暴露端口号,用-- port <端口号>
[root@master ~]# kubectl create deployment myapp1 --image nginx --port 80
deployment.apps/myapp1 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-chght 1/1 Running 0 8m18s
myapp-6d8d776547-dc7jb 1/1 Running 0 8m18s
myapp-6d8d776547-qb7pt 1/1 Running 0 8m18s
myapp1-677f4bf9bf-cf8ln 1/1 Running 0 102s
获取node节点、pod、service信息
列出所有的pod,在ps终端中打印出来
[root@master ~]# kubectl get pods
显示pods的详细信息
[root@master ~]# kubectl get pods -o wide
列出单replication的控制器的指定名称
[root@master ~]# kubectl get deployment myapp
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 3/3 3 3 15m
一起列出replication的控制器和服务显示
[root@master ~]# kubectl get svc
[root@master ~]# kubectl get service
暴露端口号,--target-port
表示暴露目标端口号
创建一个服务,这个服务在它的80端口号连接它的时候用容器的8000,用外面的80访问容器里的8000
## 把80映射到8000,因为它的类型是ClusterIP,表示这个service只能在集群中能访问到;NodePort则表示是在真机上可以访问的
[root@master ~]# kubectl expose deployment myapp --port 80 --target-port 8000
service/myapp exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp ClusterIP 10.110.171.169 80/TCP 3s
nginx NodePort 10.111.4.86 80:30859/TCP 41h
删除pod和service相同的名字,删除的时候指定你要删除的类型,你用哪个类型创建的就用哪个类型来删除
## 因为b1属于deployment类型的控制器,我们是通过控制器来管理的,而不是通过pod自身,而pod有两种类型,一种是自助式pod,一种是控制器管理的pod;我们现在用的是控制器管理的pod,所有要用控制器来管理它
[root@master ~]# kubectl delete deployment b1
deployment.apps "b1" deleted
[root@master ~]# kubectl get pods
No resources found in default namespace.
[root@master ~]# kubectl delete svc myapp
service "myapp" deleted
[root@master ~]# kubectl delete pods nginx
pod "nginx" deleted
[root@master ~]# kubectl get pods
No resources found in def
ault namespace.
使用默认编辑器编辑服务器上定义的资源
[root@master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: node1.example.com/192.168.235.172
Start Time: Mon, 20 Dec 2021 22:14:38 +0800
Labels: app=nginx
································
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 87s
...
labels:
app: test //将原本的nginx改为test
name: nginx
[root@master ~]# kubectl describe pod nginx
...
Labels: app=test
扩容或缩容 Deployment、ReplicaSet、Replication Controller或 Job 中Pod数量
将名为nginx中的pod副本数量设置为3
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 8m30s
[root@master ~]# kubectl scale --replicas 3 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/3 3 1 8m56s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-5tsjt 1/1 Running 0 16s
nginx-6799fc88d8-dwrsh 1/1 Running 0 9m5s
nginx-6799fc88d8-sn82p 1/1 Running 0 15s
如果当前副本数为3,则将其扩展至5
[root@master ~]# kubectl scale --current-replicas 3 --replicas 5 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-5tsjt 1/1 Running 0 62s
nginx-6799fc88d8-dwrsh 1/1 Running 0 9m51s
nginx-6799fc88d8-jkmln 0/1 ContainerCreating 0 2s
nginx-6799fc88d8-qm5ld 0/1 ContainerCreating 0 2s
nginx-6799fc88d8-sn82p 1/1 Running 0 61s
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 4/5 5 4 9m58s
[root@master ~]#
自动扩展,给定一个范围,自动根据业务的访问量增加或减少
设定nginx这个deployment的副本数最少为1,最多为5
[root@master ~]# kubectl autoscale --min 1 --max 5 deployment/nginx
horizontalpodautoscaler.autoscaling/nginx autoscaled
[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx /80% 1 5 0 8s
显示标签为 kubernetes.io/cluster-service=true 的控制平面和服务的地址。要进一步调试和诊断集群问题,请使用“kubectl cluster-info dump”
[root@master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.235.179:6443
KubeDNS is running at https://192.168.235.179:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
查看特定资源或资源组的详细信息
查看名为nginx 的pod的详细信息
[root@master ~]# kubectl describe pod nginx
Name: nginx-6799fc88d8-5tsjt
Namespace: default
Priority: 0
Node: node1.example.com/192.168.235.172
Start Time: Mon, 20 Dec 2021 22:23:28 +0800
Labels: app=nginx
pod-template-hash=6799fc88d8
Annotations:
Status: Running
IP: 10.244.1.5
IPs:
IP: 10.244.1.5
Controlled By: ReplicaSet/nginx-6799fc88d8
Containers:
nginx:
Container ID: docker://5a331ad8c751b41bfa7fd98f4f73e1c97cbc9f8aa76aada48f0be3fe22c10097
Image: nginx
Image ID: docker-pullable://nginx@sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Port:
Host Port:
State: Running
Started: Mon, 20 Dec 2021 22:23:37 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-n67dr (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-n67dr:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-n67dr
Optional: false
QoS Class: BestEffort
Node-Selectors:
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 8m9s default-scheduler Successfully assigned default/nginx-6799fc88d8-5tsjt to node1.example.com
Normal Pulling 8m8s kubelet Pulling image "nginx"
Normal Pulled 8m kubelet Successfully pulled image "nginx" in 7.583042375s
Normal Created 8m kubelet Created container nginx
Normal Started 8m kubelet Started container nginx
输出pod或指定资源中容器的日志。如果pod中只有一个容器,则容器名是可选的
// 查看nginx的日志
[root@master ~]# kubectl logs deployment/nginx
Found 5 pods, using pod/nginx-6799fc88d8-dwrsh
/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 14:14:43 [notice] 1#1: using the "epoll" event method
2021/12/20 14:14:43 [notice] 1#1: nginx/1.21.4
2021/12/20 14:14:43 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/12/20 14:14:43 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/20 14:14:43 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 14:14:43 [notice] 1#1: start worker processes
2021/12/20 14:14:43 [notice] 1#1: start worker process 32
2021/12/20 14:14:43 [notice] 1#1: start worker process 33
连接到一个正在运行的容器
//获取正在运行中的pod nginx的输出,默认连接到pod中的第一个容器
[root@master ~]# kubectl attach nginx
Defaulting container name to nginx.
Use 'kubectl describe pod/nginx -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
在容器内执行命令
//默认在pod/nginx的第一个容器中运行date并打印输出
[root@master ~]# kubectl exec deployment/nginx date
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Mon Dec 20 14:38:25 UTC 2021
将一个或多个本地端口转发到pod
//将容器中的80端口随即映射到本机的端口
[root@master ~]# kubectl port-forward nginx-6799fc88d8-5tsjt :80
Forwarding from 127.0.0.1:46459 -> 80
Forwarding from [::1]:46459 -> 80
[root@master ~]# curl 127.0.0.1:46459
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@master ~]#
将文件和目录复制到容器或者从容器中拷贝
//将本地的anaconda-ks.cfg文件传输到pod/nginx的/tmp目录下
[root@master ~]# kubectl cp anaconda-ks.cfg nginx-6799fc88d8-5tsjt:/tmp
[root@master ~]# kubectl exec pod/nginx-6799fc88d8-5tsjt -- ls -l /tmp
total 4
-rw------- 1 root root 1252 Dec 20 14:48 anaconda-ks.cfg
更新(增加、修改或删除)资源上的 label(标签)。
//更改标签
[root@master ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Mon, 20 Dec 2021 22:14:38 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx
Port:
Host Port:
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets:
NewReplicaSet: nginx-6799fc88d8 (5/5 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 37m deployment-controller Scaled up replica set nginx-6799fc88d8 to 1
Normal ScalingReplicaSet 29m deployment-controller Scaled up replica set nginx-6799fc88d8 to 3
Normal ScalingReplicaSet 28m deployment-controller Scaled up replica set nginx-6799fc88d8 to 5
//追加标签
[root@master ~]# kubectl label deployment/nginx user=yaya
deployment.apps/nginx labeled
[root@master ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Mon, 20 Dec 2021 22:14:38 +0800
Labels: app=nginx
user=yaya
在服务器上打印支持的 API 资源
//查看所有资源
[root@master ~]# kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
在服务器上以’组/版本’的形式打印支持的api版本
[root@master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
// 用Dockerfile编写两个镜像
//制作镜像1
[root@master ~]# mkdir httpd
[root@master ~]# cd httpd
[root@master httpd]# vim Dockerfile
[root@master httpd]# cat Dockerfile
FROM busybox
RUN mkdir /data && \
echo "test page on v1" > /data/index.html
ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
[root@master httpd]# docker build -t weixiaoya/httpd:v0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
latest: Pulling from library/busybox
3cb635b06aa2: Pull complete
Digest: sha256:b5cfd4befc119a590ca1a81d6bb0fa1fb19f1fbebd0397f25fae164abe1e8a6a
Status: Downloaded newer image for busybox:latest
---> ffe9d497c324
Step 2/3 : RUN mkdir /data && echo "test page on v1" > /data/index.html
---> Running in bf174265c61d
Removing intermediate container bf174265c61d
---> a074d85c6622
Step 3/3 : ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
---> Running in e362ffafa0e2
Removing intermediate container e362ffafa0e2
---> 104d28f2d58c
Successfully built 104d28f2d58c
Successfully tagged weixiaoya/httpd:v0.1
//制作镜像2
[root@master httpd]# vim Dockerfile
[root@master httpd]# cat Dockerfile
FROM busybox
RUN mkdir /data && \
echo "test page on v2" > /data/index.html
ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
[root@master httpd]# docker build -t weixiaoya/httpd:v2 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
---> ffe9d497c324
Step 2/3 : RUN mkdir /data && echo "test page on v2" > /data/index.html
---> Running in aa475f8038dd
Removing intermediate container aa475f8038dd
---> 867882b9f918
Step 3/3 : ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
---> Running in 4cbc3af592c9
Removing intermediate container 4cbc3af592c9
---> e423298d601e
Successfully built e423298d601e
Successfully tagged weixiaoya/httpd:v2
[root@master httpd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
weixiaoya/httpd v2 e423298d601e About a minute ago 1.24MB
weixiaoya/httpd v0.1 104d28f2d58c 3 minutes ago 1.24MB
busybox latest ffe9d497c324 13 days ago 1.24MB
[root@master ~]# docker push weixiaoya/httpd:v0.1
The push refers to repository [docker.io/weixiaoya/httpd]
0d4853dfdf52: Pushed
64cac9eaf0da: Mounted from library/busybox
v0.1: digest: sha256:fb79b8b64543613f2677aeb489451b329ed7b4ccbade1820d9d5205495107f4f size: 734
用k8s基于httpd:v0.1镜像运行镜像3个pod
[root@master ~]# kubectl create deploy httpd --image weixiaoya/httpd:v0.1 --replicas 3
deployment.apps/httpd created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-7649d9b878-5lvf7 1/1 Running 0 8m4s
httpd-7649d9b878-ck6cq 1/1 Running 0 8m4s
httpd-7649d9b878-pkqkk 1/1 Running 0 8m4s
//暴露端口
[root@master ~]# kubectl expose deploy httpd --port 80 --type NodePort
service/httpd exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd NodePort 10.111.22.218 80:31547/TCP 33s
kubernetes ClusterIP 10.96.0.1 443/TCP 14h
[root@master ~]# curl 10.111.22.218
test page on v1
[root@master ~]# curl 192.168.235.179:31547
test page on v1
[root@master ~]# kubectl set image deploy/httpd httpd=weixiaoya/httpd:v2
deployment.apps/httpd image updated
//创建一个新pod,删除一个旧pod ,直到更新完成
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-7649d9b878-5lvf7 1/1 Terminating 0 11m
httpd-7649d9b878-ck6cq 1/1 Running 0 11m
httpd-7649d9b878-pkqkk 1/1 Terminating 0 11m
httpd-cb9c79f99-gfk9z 0/1 ContainerCreating 0 10s
httpd-cb9c79f99-w722f 1/1 Running 0 11s
httpd-cb9c79f99-zcsw5 1/1 Running 0 35s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-cb9c79f99-gfk9z 1/1 Running 0 101s
httpd-cb9c79f99-w722f 1/1 Running 0 102s
httpd-cb9c79f99-zcsw5 1/1 Running 0 2m6s
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd NodePort 10.111.22.218 80:31547/TCP 4m54s
//访问
[root@master ~]# curl 10.111.22.218
test page on v2
[root@master ~]# curl 192.168.235.179:31547
test page on v2
[root@master ~]# kubectl rollout undo deploy/httpd
deployment.apps/httpd rolled back
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-7649d9b878-96cnm 1/1 Running 0 8s
httpd-7649d9b878-mq6mh 1/1 Running 0 6s
httpd-7649d9b878-rtmjt 1/1 Running 0 10s
httpd-cb9c79f99-gfk9z 1/1 Terminating 0 3m21s
httpd-cb9c79f99-w722f 1/1 Terminating 0 3m22s
httpd-cb9c79f99-zcsw5 1/1 Terminating 0 3m46s
[root@master ~]# curl 10.111.22.218
test page on v1
[root@master ~]# curl 192.168.235.179:31547
test page on v1
//创建版本3
[root@master httpd]# vim Dockerfile
[root@master httpd]# cat Dockerfile
FROM busybox
RUN mkdir /data && \
echo "test page on v3" > /data/index.html
ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
[root@master httpd]# docker build -t weixioaya/httpd:v3 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
---> ffe9d497c324
Step 2/3 : RUN mkdir /data && echo "test page on v3" > /data/index.html
---> Running in 3ba8c85f7663
Removing intermediate container 3ba8c85f7663
---> 7259f488af3b
Step 3/3 : ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
---> Running in 4b0eeaad3b71
Removing intermediate container 4b0eeaad3b71
---> 7dabf8af51eb
Successfully built 7dabf8af51eb
Successfully tagged weixioaya/httpd:v3
[root@master httpd]# kubectl set image deploy/httpd httpd=weixiaoya/httpd:v3
deployment.apps/httpd image updated
//更新至版本v3
[root@master httpd]# kubectl set image deploy/httpd httpd=weixiaoya/httpd:v3
[root@master httpd]# curl 10.111.22.218
test page on v3
//回滚
[root@master httpd]# kubectl rollout undo deploy/httpd
deployment.apps/httpd rolled back
//回到v1
[root@master httpd]# curl 10.111.22.218
test page on v1
//再次回滚
[root@master ~]# kubectl rollout undo deploy/httpd
deployment.apps/httpd rolled back
[root@master httpd]# curl 10.111.22.218
test page on v3
//发现回到了v3,没有回到v2,说明回滚到之前的版本也算一次版本记录,所以最多只能回滚一次