官网参考:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#deployment-v1-apps
命令参考:
kubectl explain pod
kubectl get pod -o yaml |less
[kubeadm@server1 ~]$ mkdir mainfest
[kubeadm@server1 ~]$ cd mainfest/
[kubeadm@server1 mainfest]$ ls
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myapp:v1
[kubeadm@server1 mainfest]$ kubectl create -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 9s
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 4m16s 10.244.1.13 server2 <none> <none>
[kubeadm@server1 mainfest]$ curl 10.244.1.13
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml ##自主式pod清单的修改需要先删除在创建
pod "myapp" deleted
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myapp:v2
[kubeadm@server1 mainfest]$ kubectl create -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 3s 10.244.2.14 server3 <none> <none>
[kubeadm@server1 mainfest]$ curl 10.244.2.14
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml
pod "myapp" deleted
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
- name: demo
image: busyboxplus
tty: true
stdin: true
stdinOnce: true
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 2/2 Running 0 5s
[kubeadm@server1 mainfest]$ kubectl describe pod myapp
Name: myapp
Namespace: default
Priority: 0
Node: server2/172.25.1.2
Start Time: Mon, 22 Jun 2020 20:43:54 +0800
Labels: <none>
Annotations: Status: Running
IP: 10.244.1.22
IPs:
IP: 10.244.1.22
Containers:
myappv1:
Container ID: docker://c3a8b858d190b68214e4bed175c27b96c202b983006ffbb0e5f1b8c3dfdc2517
Image: myapp:v1
Image ID: docker-pullable://myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 22 Jun 2020 20:43:55 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-5qqxc (ro)
demo:
Container ID: docker://7596aa40bd7d6623791038c06bacecd8169ccdff65d76609f92831f2697ce434
Image: busyboxplus
Image ID: docker-pullable://busyboxplus@sha256:ef538eae80f40015736f1ee308d74b4f38f74e978c65522ce64abdf8c8c5e0d6
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 22 Jun 2020 20:43:55 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-5qqxc (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-5qqxc:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-5qqxc
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 10s default-scheduler Successfully assigned default/myapp to server2
Normal Pulled 10s kubelet, server2 Container image "myapp:v1" already present on machine
Normal Created 10s kubelet, server2 Created container myappv1
Normal Started 10s kubelet, server2 Started container myappv1
Normal Pulling 10s kubelet, server2 Pulling image "busyboxplus"
Normal Pulled 10s kubelet, server2 Successfully pulled image "busyboxplus"
Normal Created 10s kubelet, server2 Created container demo
Normal Started 10s kubelet, server2 Started container demo
kubectl explain pod.spec.containers
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
- name: demo
image: busyboxplus
tty: true
stdin: true
stdinOnce: true
imagePullPolicy: IfNotPresent
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
ports:
- name: http
containerPort: 80
hostPort: 80
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 10s 10.244.1.23 server2 <none> <none>
[kubeadm@server1 mainfest]$ curl 10.244.1.23
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
定义容器使用主机网络模式会直接使用宿主机的网络,但是只能有一个不能有副本
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
ports:
- name: http
containerPort: 80
hostPort: 80
hostNetwork: true
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 4s 172.25.1.2 server2 <none> <none>
[kubeadm@server1 mainfest]$ curl 172.25.1.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[kubeadm@server1 mainfest]$ kubectl run demo --image=busyboxplus -it --restart=Never
If you don't see a command prompt, try pressing enter.
[ root@demo:/ ]$ ls
bin/ etc/ lib/ linuxrc mnt/ proc/ run sys/ usr/
dev/ home/ lib64 media/ opt/ root/ sbin/ tmp/ var/
[ root@demo:/ ]$ [kubeadm@server1 mainfest]$ logout
[root@server1 ~]# su - kubeadm
Last login: Mon Jun 22 18:36:13 CST 2020 on pts/0
[kubeadm@server1 ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 0/1 Completed 0 18s
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
restartPolicy: Never
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl exec -it myapp -- sh
/ # nginx -s stop
2020/06/22 14:00:56 [notice] 12#12: signal process started
/ # command terminated with exit code 137
[kubeadm@server1 mainfest]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 0/1 Completed 0 36s
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
resources:
requests:
memory: 100Mi
cpu: 0.1
limits:
memory: 200Mi
cpu: 0.2
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl describe pod myapp
Name: myapp
Namespace: default
Priority: 0
Node: server2/172.25.1.2
Start Time: Mon, 22 Jun 2020 22:32:49 +0800
Labels: <none>
Annotations: Status: Running
IP: 10.244.1.25
IPs:
IP: 10.244.1.25
Containers:
myappv1:
Container ID: docker://7e81871169467479581a5294c0f5094de5f01495d7e9746ad141ff30726d4776
Image: myapp:v1
Image ID: docker-pullable://myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 22 Jun 2020 22:32:50 +0800
Ready: True
Restart Count: 0
Limits:
cpu: 200m
memory: 200Mi
Requests:
cpu: 100m
memory: 100Mi
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-5qqxc (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-5qqxc:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-5qqxc
Optional: false
QoS Class: Burstable
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 21s default-scheduler Successfully assigned default/myapp to server2
Normal Pulled 21s kubelet, server2 Container image "myapp:v1" already present on machine
Normal Created 21s kubelet, server2 Created container myappv1
Normal Started 21s kubelet, server2 Started container myappv1
[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml
pod "myapp" deleted
[kubeadm@server1 mainfest]$ kubectl get node --show-labels ##查看节点的标签
NAME STATUS ROLES AGE VERSION LABELS
server1 Ready master 3d18h v1.18.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server1,kubernetes.io/os=linux,node-role.kubernetes.io/master=
server2 Ready <none> 3d18h v1.18.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server2,kubernetes.io/os=linux
server3 Ready <none> 3d3h v1.18.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server3,kubernetes.io/os=linux
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
resources:
requests:
memory: 100Mi
cpu: 0.1
limits:
memory: 200Mi
cpu: 0.2
nodeSelector:
kubernetes.io/hostname: server3
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 9s 10.244.2.28 server3 <none> <none>
$ kubectl get pod --show-labels
//查看标签
$ kubectl get pod -l app
//过滤包含app的标签
$ kubectl label pod demo version=v1
//打标签
$ kubectl label pod demo app=nginx --overwrite
//更改标签
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
labels:
app: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
resources:
requests:
memory: 100Mi
cpu: 0.1
limits:
memory: 200Mi
cpu: 0.2
nodeSelector:
kubernetes.io/hostname: server3
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myapp 1/1 Running 0 6s app=myapp
[kubeadm@server1 mainfest]$ kubectl label pod myapp app=nginx --overwrite
pod/myapp labeled
[kubeadm@server1 mainfest]$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myapp 1/1 Running 0 74s app=nginx
[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml
pod "myapp" deleted
[kubeadm@server1 mainfest]$ vim pod2.yml
[kubeadm@server1 mainfest]$ cat pod2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: deployment-example
spec:
# 3 Pods should exist at all times.
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx
spec:
containers:
- name: nginx
# Run this image
image: myapp:v1
[kubeadm@server1 mainfest]$ kubectl create -f pod2.yml
deployment.apps/deployment-example created
[kubeadm@server1 mainfest]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-7d5c95894c-4snlm 1/1 Running 0 29s
deployment-example-7d5c95894c-88t4h 1/1 Running 0 29s
deployment-example-7d5c95894c-tlxfs 1/1 Running 0 29s
[kubeadm@server1 mainfest]$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
deployment-example-7d5c95894c-4snlm 1/1 Running 0 60s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-88t4h 1/1 Running 0 60s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-tlxfs 1/1 Running 0 60s app=nginx,pod-template-hash=7d5c95894c
[kubeadm@server1 mainfest]$ vim pod2.yml