使用 kubectl 管理 Kubernetes 容器平台
- [root@master ~]# kubectl get pods -n kube-system -o wide //命名空间kube-system
- Namespace / ns
- Nodes / node
- Deployment / deploy
- Pods / pod / po
实验拓扑图
- 当前集群构成:一主,两个 minion 构成的 Kubernetes 集群
节点角色 | IP地址 |
master | 192.168.70.106 |
etcd | 192.168.70.106 |
note1(minion1) | 192.168.70.100 |
note2(minion2) | 192.168.70.105 |
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 3d17h v1.14.1
note1 Ready <none> 3d15h v1.14.1
note2 Ready <none> 3d16h v1.14.1
[root@master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-jrrzg 1/1 Running 2 3d17h
coredns-fb8b8dccf-stx5b 1/1 Running 2 3d17h
etcd-master 1/1 Running 2 3d17h
kube-apiserver-master 1/1 Running 2 3d17h
kube-controller-manager-master 1/1 Running 2 3d17h
kube-flannel-ds-amd64-2m2c6 1/1 Running 4 3d16h
kube-flannel-ds-amd64-jgjb7 1/1 Running 2 3d16h
kube-flannel-ds-amd64-r6ghq 1/1 Running 4 3d15h
kube-proxy-2p7cg 1/1 Running 4 3d16h
kube-proxy-c8dhh 1/1 Running 2 3d17h
kube-proxy-tb292 1/1 Running 4 3d15h
kube-scheduler-master 1/1 Running 2 3d17h
[root@master ~]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-fb8b8dccf-jrrzg 1/1 Running 2 3d17h 10.244.0.7 master <none> <none>
coredns-fb8b8dccf-stx5b 1/1 Running 2 3d17h 10.244.0.6 master <none> <none>
etcd-master 1/1 Running 2 3d17h 192.168.70.106 master <none> <none>
kube-apiserver-master 1/1 Running 2 3d17h 192.168.70.106 master <none> <none>
kube-controller-manager-master 1/1 Running 2 3d17h 192.168.70.106 master <none> <none>
kube-flannel-ds-amd64-2m2c6 1/1 Running 4 3d16h 192.168.70.105 note2 <none> <none>
kube-flannel-ds-amd64-jgjb7 1/1 Running 2 3d16h 192.168.70.106 master <none> <none>
kube-flannel-ds-amd64-r6ghq 1/1 Running 4 3d15h 192.168.70.100 note1 <none> <none>
kube-proxy-2p7cg 1/1 Running 4 3d16h 192.168.70.105 note2 <none> <none>
kube-proxy-c8dhh 1/1 Running 2 3d17h 192.168.70.106 master <none> <none>
kube-proxy-tb292 1/1 Running 4 3d15h 192.168.70.100 note1 <none> <none>
kube-scheduler-master 1/1 Running 2 3d17h 192.168.70.106 master <none> <none>
- kubectl 创建和删除一个 pod 相关操作
命令 | 说明 |
run | 在集群中运行一个特定的镜像,即在群集中运行一个pod |
create | 通过文件名或标准输入创建资源 |
delete | 通过文件名、 标准输入、 资源名称或标签选择器来删除资源。 |
上传导入镜像
//node1节点
[root@note1 ~]# ls
anaconda-ks.cfg etcd.tar k8s-dns-sidecar-amd64.tar kube-apiserver.tar kube-dnsmasq-amd64.tar kube-scheduler.tar pause.tar
coredns.tar flannel.tar k8s-package kube-controller-manager.tar kube-proxy.tar nginx.tar
[root@note1 ~]# scp nginx.tar 192.168.70.105:~
[root@note1 ~]# docker load -i nginx.tar
//node2节点
[root@note2 ~]# ls
anaconda-ks.cfg etcd.tar k8s-dns-sidecar-amd64.tar kube-apiserver.tar kube-dnsmasq-amd64.tar kube-scheduler.tar pause.tar
coredns.tar flannel.tar k8s-package kube-controller-manager.tar kube-proxy.tar nginx.tar
[root@note2 ~]# docker load -i nginx.tar
//主节点
[root@master ~]# kubectl run nginx-app --image=nginx:latest
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-app created
[root@master ~]# kubectl get ns //查看命名空间
NAME STATUS AGE
default Active 3d18h //未指定命名空间,针对default命名空间操作
kube-node-lease Active 3d18h
kube-public Active 3d18h
kube-system Active 3d18h //组件命名空间
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-krw9f 1/1 Running 0 29m
//deployment--rs--pod
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 132m
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app-dc476ff-krw9f 1/1 Running 0 141m 10.244.1.2 note2 <none> <none>
//note2 节点
[root@note2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c6be242ff1d nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours
//主节点
[root@master ~]# kubectl delete pod/nginx-app-dc476ff-krw9f
pod "nginx-app-dc476ff-krw9f" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-5rvsr 1/1 Running 0 18m
You have new mail in /var/spool/mail/root
[root@master ~]# kubectl delete deployment/nginx-app
deployment.extensions "nginx-app" deleted
[root@master ~]# kubectl get pod
No resources found.
[root@master ~]# kubectl get deploy
No resources found.
[root@master ~]# kubectl run nginx-app --image=nginx:latest
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-app created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 0/1 1 0 18s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 24s
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 26s
[root@master ~]# kubectl run nginx2 --image=nginx:latest --replicas=2
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx2 created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 5m14s
nginx2 1/2 2 1 46s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 5m21s
nginx2-84fc8c74c7-nxslb 1/1 Running 0 53s
nginx2-84fc8c74c7-sp4hb 1/1 Running 0 53s
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 5m24s
nginx2 2/2 2 2 56s
[root@master ~]# kubectl run nginx-app2 --image=nginx:latest --replicas=2 --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-app2 created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 7m6s
nginx-app2 1/2 2 1 23s
nginx2 2/2 2 2 2m38s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 7m8s
nginx-app2-55969f45bb-bbbc9 1/1 Running 0 25s
nginx-app2-55969f45bb-r89hr 1/1 Running 0 25s
nginx2-84fc8c74c7-nxslb 1/1 Running 0 2m40s
nginx2-84fc8c74c7-sp4hb 1/1 Running 0 2m40s
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 7m10s
nginx-app2 2/2 2 2 27s
nginx2 2/2 2 2 2m42s
[root@master ~]# kubectl run nginx-app3 --image=nginx:latest --replicas=2 --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-app3 created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 8m42s
nginx-app2 2/2 2 2 119s
nginx-app3 2/2 2 2 24s
nginx2 2/2 2 2 4m14s
[root@master ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-app 1/1 1 1 8m55s nginx-app nginx:latest run=nginx-app
nginx-app2 2/2 2 2 2m12s nginx-app2 nginx:latest run=nginx-app2
nginx-app3 2/2 2 2 37s nginx-app3 nginx:latest run=nginx-app3
nginx2 2/2 2 2 4m27s nginx2 nginx:latest run=nginx2
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app-dc476ff-fbph2 1/1 Running 0 10m 10.244.1.4 note2 <none> <none>
nginx-app2-55969f45bb-bbbc9 1/1 Running 0 3m20s 10.244.1.6 note2 <none> <none>
nginx-app2-55969f45bb-r89hr 1/1 Running 0 3m20s 10.244.2.7 note1 <none> <none>
nginx-app3-8549f5d7b-khkpb 1/1 Running 0 105s 10.244.1.7 note2 <none> <none>
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 105s 10.244.2.8 note1 <none> <none>
nginx2-84fc8c74c7-nxslb 1/1 Running 0 5m35s 10.244.1.5 note2 <none> <none>
nginx2-84fc8c74c7-sp4hb 1/1 Running 0 5m35s 10.244.2.6 note1 <none> <none>
[root@master ~]# kubectl describe pod nginx-app3-8549f5d7b-khkpb
//ImagePullBackOff #从后端把镜像拉取到本地(失败)
[root@master ~]# kubectl run nginx-app10 --image=nginx:ladsdfsdafafst --replicas=2 --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-app10 created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 19m
nginx-app10-54494bcbc5-7vz4g 0/1 ContainerCreating 0 38s
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 38s
nginx-app2-55969f45bb-bbbc9 1/1 Running 0 12m
nginx-app2-55969f45bb-r89hr 1/1 Running 0 12m
nginx-app3-8549f5d7b-khkpb 1/1 Running 0 10m
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 10m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 14m
nginx2-84fc8c74c7-sp4hb 1/1 Running 0 14m
[root@master ~]# kubectl describe pod nginx-app10-54494bcbc5-pmbjn
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal BackOff 24h (x2 over 24h) kubelet, note2 Back-off pulling image "nginx:ladsdfsdafafst"
Warning Failed 24h (x2 over 24h) kubelet, note2 Error: ImagePullBackOff
Normal Pulling 24h (x3 over 24h) kubelet, note2 Pulling image "nginx:ladsdfsdafafst"
Warning Failed 24h (x3 over 24h) kubelet, note2 Failed to pull image "nginx:ladsdfsdafafst": rpc error: code = Unknown desc = Error response from daemon: manifest for nginx:ladsdfsdafafst not found
Warning Failed 24h (x3 over 24h) kubelet, note2 Error: ErrImagePull
Normal Scheduled 111s default-scheduler Successfully assigned default/nginx-app10-54494bcbc5-pmbjn to note2
//terminating 终止状态
[root@master ~]# kubectl delete pod nginx2-84fc8c74c7-sp4hb
pod "nginx2-84fc8c74c7-sp4hb" deleted
^C
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 25m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 6m55s
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 6m55s
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 27s
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 67s
nginx-app3-8549f5d7b-khkpb 1/1 Running 0 17m
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 17m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 21m
nginx2-84fc8c74c7-sp4hb 0/1 Terminating 0 21m
nginx2-84fc8c74c7-tps9k 0/1 ContainerCreating 0 2s
- -image-pull-policy
- Always
- Never
- IfNotPresent
- kubectl run 和 docker run 一样,kubectl run 能将一个 pod 运行起来。创建一个deployment 或job 来管理容器。 语法: kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] NAME指定deployment和service的名称; --replicas缩写-r,指定副本数,默认为1; --expose如果为true,会创建有ClusterIP的service,默认为false; --port表示容器暴露的端口,如果expose为true,该端口也是service的端口; --image指定容器用的镜像; --env设置环境变量 replica [ˈ ˈreplɪ kə ] 副本
- 1、ContainerCreating #容器创建 2、ImagePullBackOff #从后端把镜像拉取到本地(失败) 3、terminating [ˈtɜːrmɪneɪtɪŋ] #终止。当删除 pod 时的状态
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 27m
nginx-app10 0/2 2 0 8m42s
nginx-app2 2/2 2 2 20m
nginx-app3 2/2 2 2 19m
nginx2 2/2 2 2 22m
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 27m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 9m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 9m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 2m32s
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 3m12s
nginx-app3-8549f5d7b-khkpb 1/1 Running 0 19m
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 19m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 23m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 2m7s
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app-dc476ff-fbph2 1/1 Running 0 32m 10.244.1.4 note2 <none> <none>
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 13m 10.244.2.9 note1 <none> <none>
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 13m 10.244.1.8 note2 <none> <none>
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 7m1s 10.244.2.10 note1 <none> <none>
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 7m41s 10.244.1.9 note2 <none> <none>
nginx-app3-8549f5d7b-khkpb 1/1 Running 0 23m 10.244.1.7 note2 <none> <none>
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 23m 10.244.2.8 note1 <none> <none>
nginx2-84fc8c74c7-nxslb 1/1 Running 0 27m 10.244.1.5 note2 <none> <none>
nginx2-84fc8c74c7-tps9k 1/1 Running 0 6m36s 10.244.2.11 note1 <none> <none>
[root@master ~]# curl 10.244.2.10
<!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>
//pod的生命周期是短暂的(很少用IP地址访问)
端口映射,向外部暴露服务
- Service/svc
- ClusterIP,默认的方式,通过集群IP来对外提供服务,这种方式只能在集群内部访问。
- NodePort,利用NAT技术在Node的指定端口上提供对外服务。外部应用通过”:端口”的方式访问。
- LoadBalancer,利用外部的负载均衡设施进行服务的访问。(用于公有云环境)
- ExternalName,这是1.7版本之后 kube-dns 提供的功能。(集群内的服务可以访问集群外的服务)
// ClusterIP,默认的方式,通过集群IP来对外提供服务,这种方式只能在集群内部访问。
[root@master ~]# kubectl expose deploy/nginx-app2 --port=80
service/nginx-app2 exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d21h
nginx-app2 ClusterIP 10.104.130.94 <none> 80/TCP 17s
[root@master ~]# curl 10.104.130.94
<!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>
// NodePort,利用NAT技术在Node的指定端口上提供对外服务。外部应用通过”:端口”的方式访问。
[root@master ~]# kubectl expose deploy/nginx-app3 --port=80 --type=NodePort
service/nginx-app3 exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d22h
nginx-app2 ClusterIP 10.104.130.94 <none> 80/TCP 32m
nginx-app3 NodePort 10.102.58.60 <none> 80:32598/TCP 3s
[root@master ~]# mount /dev/sr0 /mnt/
[root@master ~]# yum -y install net-tools
[root@master ~]# netstat -anplt | grep 32598
tcp6 0 0 :::32598 :::* LISTEN 2652/kube-proxy
//集群外浏览器访问,可以是集群内任意一个节点的地址,需要注意端口号。
[root@note1 ~]# netstat -anplt | grep 32598
tcp6 0 0 :::32598 :::* LISTEN 1833/kube-proxy
[root@note2 ~]# netstat -anplt | grep 32598
tcp6 0 0 :::32598 :::* LISTEN 1845/kube-proxy
- 查询RS控制器
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 96m
nginx-app10 0/2 2 0 77m
nginx-app2 2/2 2 2 89m
nginx-app3 2/2 2 2 87m
nginx2 2/2 2 2 91m
You have new mail in /var/spool/mail/root
[root@master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-app-dc476ff 1 1 1 96m
nginx-app10-54494bcbc5 2 2 0 77m
nginx-app2-55969f45bb 2 2 2 89m
nginx-app3-8549f5d7b 2 2 2 88m
nginx2-84fc8c74c7 2 2 2 91m
- 删除pod
副本机制,自我修复功能。
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 98m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 79m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 79m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 73m
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 73m
nginx-app3-8549f5d7b-khkpb 1/1 Running 0 89m
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 89m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 93m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 72m
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 98m
nginx-app10 0/2 2 0 79m
nginx-app2 2/2 2 2 91m
nginx-app3 2/2 2 2 90m
nginx2 2/2 2 2 94m
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 101m
nginx-app10 0/2 2 0 82m
nginx-app2 2/2 2 2 94m
nginx-app3 2/2 2 2 92m
nginx2 2/2 2 2 96m
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 101m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 82m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 82m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 76m
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 76m
nginx-app3-8549f5d7b-v64sm 1/1 Running 0 111s
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 92m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 96m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 75m
[root@master ~]# kubectl delete pod/nginx-app3-8549f5d7b-v64sm
pod "nginx-app3-8549f5d7b-v64sm" deleted
^C
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 101m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 83m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 83m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 76m
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 77m
nginx-app3-8549f5d7b-knh67 0/1 ContainerCreating 0 3s
nginx-app3-8549f5d7b-v64sm 0/1 Terminating 0 2m31s
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 93m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 97m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 76m
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 102m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 83m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 83m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 77m
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 78m
nginx-app3-8549f5d7b-knh67 1/1 Running 0 37s
nginx-app3-8549f5d7b-vqhmg 1/1 Running 0 94m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 98m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 76m
- 删除 deployment
[root@master ~]# kubectl delete deploy nginx-app3
deployment.extensions "nginx-app3" deleted
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 1/1 1 1 106m
nginx-app10 0/2 2 0 87m
nginx-app2 2/2 2 2 99m
nginx2 2/2 2 2 101m
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-dc476ff-fbph2 1/1 Running 0 106m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 87m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 87m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 81m
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 82m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 102m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 80m
[root@master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-app-dc476ff 1 1 1 107m
nginx-app10-54494bcbc5 2 2 0 88m
nginx-app2-55969f45bb 2 2 2 100m
nginx2-84fc8c74c7 2 2 2 102m
- 删除service
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d22h
nginx-app2 ClusterIP 10.104.130.94 <none> 80/TCP 69m
nginx-app3 NodePort 10.102.58.60 <none> 80:32598/TCP 37m
[root@master ~]# kubectl delete svc/nginx-app3
service "nginx-app3" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d22h
nginx-app2 ClusterIP 10.104.130.94 <none> 80/TCP 71m
[root@master ~]# kubectl delete svc/nginx-app2
service "nginx-app2" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d23h
[root@master ~]# kubectl expose --help
kubectl管理应用生命周期(pod)
[root@master ~]# kubectl create deploy nginx --image=nginx
deployment.apps/nginx created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 21s
nginx-app 1/1 1 1 118m
nginx-app10 0/2 2 0 99m
nginx-app2 2/2 2 2 111m
nginx2 2/2 2 2 113m
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-65f88748fd-9rhgv 1/1 Running 0 45s
nginx-app-dc476ff-fbph2 1/1 Running 0 118m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 100m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 100m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 93m
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 94m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 114m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 93m
[root@master ~]# kubectl describe pod nginx-65f88748fd-9rhgv
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-65f88748fd-9rhgv 1/1 Running 0 4m3s
nginx-app-dc476ff-fbph2 1/1 Running 0 122m
nginx-app10-54494bcbc5-7vz4g 0/1 ImagePullBackOff 0 103m
nginx-app10-54494bcbc5-pmbjn 0/1 ImagePullBackOff 0 103m
nginx-app2-55969f45bb-pdxqj 1/1 Running 0 96m
nginx-app2-55969f45bb-rpcmx 1/1 Running 0 97m
nginx2-84fc8c74c7-nxslb 1/1 Running 0 117m
nginx2-84fc8c74c7-tps9k 1/1 Running 0 96m
yaml 语法规则
- yml
- yaml
//创建yaml文件
[root@master ~]# vi abc.yml
[root@master ~]# cat abc.yml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: nginx
spec:
replicas: 1
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
protocol: TCP
[root@master ~]# kubectl create -f abc.yml
Error from server (AlreadyExists): error when creating "abc.yml": deployments.extensions "nginx" already exists
[root@master ~]# kubectl delete deploy nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl create -f abc.yml
deployment.extensions/nginx created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 88s
nginx-app 1/1 1 1 132m
nginx-app10 0/2 2 0 113m
nginx-app2 2/2 2 2 125m
nginx2 2/2 2 2 127m
yaml 配置文件常见单词
kind: 同类,类型 ; apiVersion API 版本 ; metadata 元数据 ; spec: 规格,说明书(定义具体参数); replicas [ˈ ˈreplɪ ɪkə əs s]: 副本 selector [s ɪˈlektə ə(r)] 选择器; template [ˈ ˈtempleɪ ɪt]: 模板
- 字典和字典可以相互嵌套,字典和列表也可以相互嵌套。
- 查看yaml顶级字段
[root@master ~]# kubectl explain deployment
KIND: Deployment
VERSION: extensions/v1beta1
DESCRIPTION:
DEPRECATED - This group version of Deployment is deprecated by
apps/v1beta2/Deployment. See the release notes for more information.
Deployment enables declarative updates for Pods and ReplicaSets.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
metadata <Object>
Standard object metadata.
spec <Object>
Specification of the desired behavior of the Deployment.
status <Object>
Most recently observed status of the Deployment.
- 查看二级字段
[root@master ~]# kubectl explain deployment.metadata
[root@master ~]# kubectl explain deployment.spec
[root@master ~]# kubectl explain deployment.spec.template //下一级字段
[root@master ~]# kubectl explain deployment.spec.template.metadata
[root@master ~]# kubectl explain deployment.spec.template.spec