声明式资源管理方法
- 声明式资源管理方法依赖于资源配置清单(yaml/json)
- 查看资源配置清单方法
kubectl get service -n kube-public nginx-dp -o yaml
- 解释资源配置清单
kubectl explain service.spec
- 创建资源配置清单
nginx-ds-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-ds
name: nginx-ds
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-ds
sessionAffinity: None
type: ClusterIP
- 应用资源配置清单
kubectl apply -f nginx-ds-svc.yaml
-
修改资源配置清单
- 离线修改(修改yaml文件,然后apply)
- 在线修改( kubectl edit service nginx-ds )
-
删除资源配置清单
- 陈述式删除
- 声明式删除
kubectl delete -f nginx-ds-svc.yaml
资源
workload
- pod
- ReplicaSet
- Deployment
- StatefulSet
- DaemonSet
- Job
- Cronjob
服务发现及均衡
- Service
- Ingress
存储
- Volume
- CSI
配置
- ConfigMap
- Secret
- DownwardAPI
集群级资源
- Namespace
- Node
- Role
- ClusterRole
- RoleBinding
- ClusterRoleBinding
元数据型资源
- HPA
- PodTemplate
- LimitRange
查看资源yaml配置
[root@k8s-master1 ~]# kubectl get pod myapp-9b4987d5-5trrs -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2019-10-08T16:22:11Z"
generateName: myapp-9b4987d5-
labels:
pod-template-hash: 9b4987d5
run: myapp
name: myapp-9b4987d5-5trrs
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: myapp-9b4987d5
uid: cfe3135b-e9e5-11e9-91cb-fa163eb7acdb
resourceVersion: "2414919"
selfLink: /api/v1/namespaces/default/pods/myapp-9b4987d5-5trrs
uid: c82e1a9a-e9e7-11e9-91cb-fa163eb7acdb
spec:
containers:
- image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
name: myapp
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-47f49
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: 192.168.0.137
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-47f49
secret:
defaultMode: 420
secretName: default-token-47f49
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2019-10-08T16:22:11Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2019-10-08T16:22:13Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2019-10-08T16:22:13Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2019-10-08T16:22:11Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://cdba221a3332a4319e8dc2c3d5305cb728563762c549b3c4a38cd459e8044411
image: ikubernetes/myapp:v1
imageID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
lastState: {}
name: myapp
ready: true
restartCount: 0
state:
running:
startedAt: "2019-10-08T16:22:12Z"
hostIP: 192.168.0.137
phase: Running
podIP: 10.254.13.6
qosClass: BestEffort
startTime: "2019-10-08T16:22:11Z"
[root@k8s-master1 ~]# kubectl get deployments myapp -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
creationTimestamp: "2019-10-08T16:08:05Z"
generation: 4
labels:
run: myapp
name: myapp
namespace: default
resourceVersion: "2415009"
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/myapp
uid: cfe13e6a-e9e5-11e9-91cb-fa163eb7acdb
spec:
progressDeadlineSeconds: 600
replicas: 5
revisionHistoryLimit: 10
selector:
matchLabels:
run: myapp
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: myapp
spec:
containers:
- image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
name: myapp
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 5
conditions:
- lastTransitionTime: "2019-10-08T16:10:20Z"
lastUpdateTime: "2019-10-08T16:10:20Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2019-10-08T16:08:05Z"
lastUpdateTime: "2019-10-08T16:22:16Z"
message: ReplicaSet "myapp-9b4987d5" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 4
readyReplicas: 5
replicas: 5
updatedReplicas: 5
[root@k8s-master1 ~]# kubectl get service myapp -o yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2019-10-08T16:28:44Z"
labels:
run: myapp
name: myapp
namespace: default
resourceVersion: "2415774"
selfLink: /api/v1/namespaces/default/services/myapp
uid: b236c373-e9e8-11e9-91cb-fa163eb7acdb
spec:
clusterIP: 10.254.148.254
externalTrafficPolicy: Cluster
ports:
- nodePort: 41917
port: 80
protocol: TCP
targetPort: 80
selector:
run: myapp
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
创建资源方法
apiserver仅接收JSON格式的资源定义
yaml格式提供配置清单,apiserver可自动将其转为json格式,而后再提交执行
资源配置清单
- apiVersion : group/version
kubectl api-versions - kind : 资源类别
- metadata: 元数据
- name
- namespace
- labels
- annotations:
- spec: 期望的状态,disired state
- status: 当前状态,current state ,本字段由kubernetes集群维护;
每个资源的引用PATH /api/GROUP/VERSION/namespaces/NAMESPACE/TYPE/NAME
explain
kubectl explain pods
kubectl explain pods.metadata
kubectl explain pods.spec.containers
kubectl explain deployments.spec.template.spec.hostNetwork
kubectl explain daemonset.spec.template.spec.hostNetwork
kubectl explain daemonset.spec.template.spec.containers.ports.hostPort
kubectl explain deployment.spec.template.spec.containers.ports.hostPort
containers <[]Object> 列表 [ ] -
metadata 字典 { } json
pod
cat pod-daemon.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
- name: busybox
image: busybox:latest
command:
- "/bin/sh"
- "-c"
- "echo $(date) >> /usr/share/nginx/html/index.html; sleep 5"
创建资源
kubectl apply -f pod-daemon.yaml
#kubectl指定kubeconfig连接不同的k8s集群
kubectl --kubeconfig /path/to/kubeconfig apply -f pod-daemon.yaml
kubectl describe pods pod-demo
查看pod中容器日志
# kubectl logs pod名称 容器名称
kubectl logs pod-demo myapp
kubectl logs pod-demo busybox
进入pod中容器
# kubectl exec -it pod名称 -c 容器名称 -- /bin/sh
kubectl exec -it pod-demo -c myapp -- /bin/sh
删除资源
kubectl delete -f pod-daemon.yaml