创建deployment,k8s 使用 kubectl create 来创建资源,-f 制定资源描述的文件
[root@op demo1]# kubectl create -f nginx-dep.yaml
deployment.apps/nginx created
[root@op demo1]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-76c9787465-8bqj8 0/1 ContainerCreating 0 7s
这时会去加载镜像,等一会可以看到:
[root@op demo1]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-76c9787465-8bqj8 1/1 Running 0 83s
查看所有信息:
kubectl describe pod/nginx-76c9787465-8bqj8
修改 yaml 文件后可从新部署:
kubectl apply -f nginx-dep.yaml
查看此时集群里的资源:
[root@op demo1]# kubectl get pods,deployments,rs,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-7c7bc94684-td5k8 1/1 Running 0 5m16s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 1/1 1 1 19m
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-76c9787465 0 0 0 19m
replicaset.extensions/nginx-7c7bc94684 1 1 1 5m16s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 5h21m
rs 有两个,因为 apply 过一次修改后的配置文件
删除deployment。k8s 使用 kubectl delete 来删除资源, 使用方式可以用 kubectl delete --help 来查看. kubectl delete 可以直接删除资源,如 kubectl delete pods xxxx ;
也可以用 -f 指定 json 或 yaml 文件来删除。
[root@op demo1]# kubectl delete -f nginx-dep.yaml
deployment.apps "nginx" deleted
[root@op demo1]# kubectl get deployment,pods,services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 19h
[root@op demo1]# kubectl get deployment,pods,rs
No resources found.
[root@op demo1]#
创建资源
kubectl create -f nginx-dep-cm.yaml
查看
[root@op demo1]# kubectl get pods,configmaps,deployments
NAME READY STATUS RESTARTS AGE
pod/nginx-76cc9959f6-z2lb6 1/1 Running 0 2m28s
NAME DATA AGE
configmap/nginx-config 1 2m29s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 1/1 1 1 2m29s
ConfigMap 是 Projected Volume 的一种。Projected Volume 有4种类型,secret、downwardAPI、configMap、serviceAccountToken。
关于更多 k8s 存储的的知识见:https://kubernetes.io/docs/concepts/storage/volumes/
访问:
[root@op demo1]# curl http://127.0.0.1:30080
404 Not Found
404 Not Found
nginx/1.14.2
[root@op demo1]# curl http://127.0.0.1:30080/bd
302 Found
Found
The document has moved here.
启动
kubectl create -f nginx-dep-pv.yaml
主要是创建 pv 对应物理的资源,pv一般由运维创建。pvc 指向 pv,pvc由开发者创建。pvc类似接口,pv类似接口的实现。dep 定义 pod时,定义 volumes,然后在容器内挂载 volumes。
查看定义的资源:
[root@op ~]# kubectl get pods,pv,pvc,deployments
NAME READY STATUS RESTARTS AGE
pod/nginx-77954756cf-8qnzn 1/1 Running 0 3h59m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/nginx-data-pv 20Gi RWO Retain Bound default/nginx-data-pvc 3h59m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/nginx-data-pvc Bound nginx-data-pv 20Gi RWO 3h59m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 1/1 1 1 3h59m
定义 pv 时,指定是 local path, 地址在 /opt/nginx,这个目录会自己创建。这时往这个目录里放个文件,如:
[root@op ~]# cd /opt/nginx/
[root@op nginx]# ll
总用量 4
-rw-r--r-- 1 root root 5 1月 22 18:28 index.html
[root@op nginx]# curl http://127.0.0.1:30080/
hehe
[root@op nginx]#
可以用 curl 来访问 html 了。