k8s入门-Pod的基本管理
Pod是Kubernetes最小的管理单元,一个Pod可以代表一个运行在集群中的进程。
一、创建一个nginx的Pod
使用YAML格式来描述一个Pod
[[email protected]-01 ~]# cat nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
YAML文件中定义了版本,类型,名称和,镜像和端口。
创建Pod
[[email protected] ~]# kubectl create -f nginx-pod.yaml
pod/nginx-pod created
查看Pod
[[email protected] ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 58s
查看Pod更多信息
[[email protected] ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod 1/1 Running 0 10m 172.30.64.7 k8s-01 <none> <none>
不仅可以看到pod的启动状态,还能看到pod的IP地址(172.30.64.7)和所被分配的节点(k8s-01)。
查看Pod详情(排错用)
[[email protected] ~]# kubectl describe pod nginx-pod
Name: nginx-pod
Namespace: default
Priority: 0
Node: k8s-01/192.168.0.71
Start Time: Mon, 11 May 2020 15:00:37 +0800
Labels: app=nginx
Annotations: <none>
Status: Running
IP: 172.30.64.7
IPs:
IP: 172.30.64.7
Containers:
nginx:
Container ID: docker://db49e7059de1bfbd1b0b1af09625de8e728b12eec7790060a45dfc0cd4db4585
Image: nginx
Image ID: docker-pullable://[email protected]:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 11 May 2020 15:01:33 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4cxn2 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-4cxn2:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-4cxn2
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 360s
node.kubernetes.io/unreachable:NoExecute for 360s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned default/nginx-pod to k8s-01
Normal Pulling 12m kubelet, k8s-01 Pulling image "nginx"
Normal Pulled 11m kubelet, k8s-01 Successfully pulled image "nginx"
Normal Created 11m kubelet, k8s-01 Created container nginx
Normal Started 11m kubelet, k8s-01 Started container nginx
查看Pod日志
[[email protected] ~]# kubectl logs pod/nginx-pod
删除Pod
[[email protected] ~]# kubectl delete nginx-pod
Pod中的镜像拉取策略 当kubelet尝试拉取指定的镜像时,[imagePullPolicy]和镜像的标签会生效。
- imagePullPolicy: IfNotPresent:仅当镜像在本地不存在时镜像才被拉取。
- imagePullPolicy: Always:每次启动 pod 的时候都会拉取镜像。
省略imagePullPolicy,镜像标签为:latest或被省略,Always被应用。 imagePullPolicy被省略,并且镜像的标签被指定且不是:latest,IfNotPresent被应用。 imagePullPolicy: Never:镜像被假设存在于本地。 没有尝试拉取镜像。
二、进入到Pod
怎么样进入到Pod中呢?
如果Pod中只有一个容器,可以直接使用下面这个命令:
[[email protected] ~]# kubectl exec -it nginx-pod bash
[email protected]:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
如果Pod中有多个容器,则需要使用``命令如下:
$ kubectl --namespace=kube-system exec -it nginx-pod -c nginx-app bash
其中ube-system为namespace名称,nginx-pod为pod名称,nginx-app为pod中的一个容器的名称。
三、访问Pod
从上面的信息中可以看到Pod的ip地址为172.30.64.7
,直接用curl访问下:
[[email protected] ~]# curl 172.30.64.7
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
在集群中是可以访问,但是这个nginx部署之后,是给外部提供访问的,这个时候就要用到kubectl的port-forward
了。
可以通过下面这个命令将pod里面的端口映射到主机上来:
[[email protected] ~]# kubectl port-forward nginx-pod 18080:80
Forwarding from 127.0.0.1:18080 -> 80
可以看到,已经将pod中的80端口映射到了主机的18080端口,现在直接来访问下:
[[email protected] ~]# curl 127.0.0.1:18080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
这里只是监听了127.0.0.1的地址,还可以通过增加--address
参数来修改监听地址,具体可以通过kubectl port-forward --help
命令进行查看。