资源清单
1.Kubernetes的资源清单的介绍
官网参考:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#deployment-v1-apps
命令参考:
kubectl explain pod
kubectl get pod -o yaml |less
2. 自主式Pod资源清单
[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 指明api资源版本
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>
命令帮助参考:kubectl explain pod
kubectl api-versions 查看api资源属于那个群组或版本
[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>
2.2 交互式进入pod内部
[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
2.4 pod的镜像拉取策略
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 ------->镜像拉取时策略,需要时拉取
2.5 容器的端口映射
[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 ------>应用这个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>
验证:
iptables -t nat -nL | grep 80
2.6 定义pod使用主机网络模式
定义容器使用主机网络模式会直接使用宿主机的网络,但是只能有一个不能有副本
[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 -------->可通过主机网络IP访问pod
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
2.7 定义pod的重启策略
命令行模式
[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/