之前一直用kubectl这个命令操作,这些都是基于命令来操作K8S
kubectl get pods
kubectl get nodes
kubectl get svc
kubectl create deployment...
kubectl expose deployment...
kubectl 文档
命令行工具 (kubectl) | Kubernetes
命令参考
Kubectl Reference Docs
yaml模板
但以后我们更多的是使用yaml文件来操作k8s,所以我们要习惯阅读yaml文件
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --help
#不是真的运行
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --dry-run -o yaml
#将yaml文件的内容放到tomcat6.yaml这个文件中
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --dry-run -o yaml >tomcat6.yaml
#执行这个文件(老师删除了一些空数据和时间的)
kubectl apply -f tomcat6.yaml
#等同于1的创建
kubectl get pods
部署产生yaml
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: tomcat6name: tomcat6
spec:replicas: 1selector:matchLabels:app: tomcat6strategy: {}template:metadata:creationTimestamp: nulllabels:app: tomcat6spec:containers:- image: tomcat:6.0.53-jre8name: tomcatresources: {}
status: {}
以yaml的形式创建一个部署
tomcat6.yaml的内容
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: tomcat6name: tomcat6
spec:replicas: 3selector:matchLabels:app: tomcat6template:metadata:creationTimestamp: nulllabels:app: tomcat6spec:containers:- image: tomcat:6.0.53-jre8name: tomcat
kubectl apply -f tomcat6.yaml
kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml
暴露端口产生的yaml
apiVersion: v1
kind: Service
metadata:creationTimestamp: nulllabels:app: tomcat6name: tomcat6
spec:ports:- port: 80protocol: TCPtargetPort: 8080selector:app: tomcat6type: NodePort
status:loadBalancer: {}
kubectl get pods
kubectl get pod tomcat6-5f7ccf4cb9-4ww68
kubectl get pod tomcat6-5f7ccf4cb9-4ww68 -o yaml
查看pod的yaml
apiVersion: v1
kind: Pod
metadata:creationTimestamp: "2025-08-05T20:15:48Z"generateName: tomcat6-5f7ccf4cb9-labels:app: tomcat6pod-template-hash: 5f7ccf4cb9name: tomcat6-5f7ccf4cb9-4ww68namespace: defaultownerReferences:- apiVersion: apps/v1blockOwnerDeletion: truecontroller: truekind: ReplicaSetname: tomcat6-5f7ccf4cb9uid: f7c17866-4114-4944-a585-81ff89b22ccfresourceVersion: "38686"selfLink: /api/v1/namespaces/default/pods/tomcat6-5f7ccf4cb9-4ww68uid: 6d833dea-e91a-46cf-8a6f-dbd68ba2409a
spec:containers:- image: tomcat:6.0.53-jre8imagePullPolicy: IfNotPresentname: tomcatresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: default-token-n7xsgreadOnly: truednsPolicy: ClusterFirstenableServiceLinks: truenodeName: k8s-node2priority: 0restartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}serviceAccount: defaultserviceAccountName: defaultterminationGracePeriodSeconds: 30tolerations:- effect: NoExecutekey: node.kubernetes.io/not-readyoperator: ExiststolerationSeconds: 300- effect: NoExecutekey: node.kubernetes.io/unreachableoperator: ExiststolerationSeconds: 300volumes:- name: default-token-n7xsgsecret:defaultMode: 420secretName: default-token-n7xsg
status:conditions:- lastProbeTime: nulllastTransitionTime: "2025-08-05T20:15:48Z"status: "True"type: Initialized- lastProbeTime: nulllastTransitionTime: "2025-08-05T20:15:50Z"status: "True"type: Ready- lastProbeTime: nulllastTransitionTime: "2025-08-05T20:15:50Z"status: "True"type: ContainersReady- lastProbeTime: nulllastTransitionTime: "2025-08-05T20:15:48Z"status: "True"type: PodScheduledcontainerStatuses:- containerID: docker://34c40001948acbb1ae62ac58f5a29ee69e99729c444ca7942d5e0f3424150f3dimage: tomcat:6.0.53-jre8imageID: docker-pullable://tomcat@sha256:8c643303012290f89c6f6852fa133b7c36ea6fbb8eb8b8c9588a432beb24dc5dlastState: {}name: tomcatready: truerestartCount: 0started: truestate:running:startedAt: "2025-08-05T20:15:50Z"hostIP: 10.0.2.15phase: RunningpodIP: 10.244.1.7podIPs:- ip: 10.244.1.7qosClass: BestEffortstartTime: "2025-08-05T20:15:48Z"
以yaml的形式创建 pod
kubectl apply -f mypod.yaml
kubectl get pods
mypod.yaml的内容如下
apiVersion: v1
kind: Pod
metadata:labels:app: tomcat6-newname: tomcat6-newnamespace: default
spec:containers:- image: tomcat:6.0.53-jre8imagePullPolicy: IfNotPresentname: tomcat6-new- image: nginximagePullPolicy: IfNotPresentname: nginx