标签:kubernetes中的Pod简述与实 pod kubernetes deployment
kubernetes中的Pod简述与实践#说明
Docker是kubernetes中最常用的容器运行时,但是Pod也支持其他容器运行时。
#容器进行时,看参考如下文章或博客:
(1)解密容器运行时
(2)Kubernetes(k8s)容器运行时(CRI)简介
(3)容器运行时接口、容器网络接口、容器存储接口解析
#实战
##创建一个nginx容器的可以定义为:
cat pod1-deployment
apiVersion: v1
kind: Pod
metadata:
name: nginx-test
namespace: test
labels:
app: web
spec:
containers:
- name: front-end
image: nginx:1.7.9
ports:
- containerPort: 80
#创建pod
kubectl create -f pod1-deployment --namespace=test
pod "nginx-test" created
#查看pod
kubectl get po --namespace=test
NAME READY STATUS RESTARTS AGE
nginx-test 1/1 Running 0 1m
#查看pod详细情况
kubectl describe po nginx-test --namespace=test
Name: nginx-test
Namespace: test
Node: swarm1/10.0.0.38
Start Time: Thu, 17 May 2018 17:33:50 +0800
Labels: app=web
Annotations: <none>
Status: Running
IP: 10.244.2.131
Containers:
front-end:
Container ID: docker://5af319b84acfa076ac8500f39c81ff05e6664e6562d9593f5c7ba2fde0118372
Image: nginx:1.7.9
Image ID: docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 17 May 2018 17:34:07 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-z4ffx (ro)
...
...
#进入到pod(容器)内部
kubectl exec -it nginx-test /bin/bash --namespace=test
root@nginx-test:/# nginx -v
nginx version: nginx/1.7.9
##说明
kubectl exec
这条命令是对docker exec命令的包装,可以让你执行容器内部的命令,如果pod中只有一个容器在运行则此命令可以作用在pod上如:
kubectl exec -it [pod] /bin/bash。
我们可以使用如下命令来进入一个运行中的pod:
kubectl exec -it [pod name] --/bin/bash,
-i:用来启动标准输入流STDIN
-t:将输入流定向到TTY(伪终端)中
-c [container-name]:一个Pod中启动了多个容器,使用该参数来进入特定的容器中。
#实战
##在一个pod里放置两个容器:nginx与redis
cat test-deployment
apiVersion: v1
kind: Pod
metadata:
name: rss-site
namespace: test
labels:
app: web
spec:
containers:
- name: front-end
image: nginx:1.7.9
ports:
- containerPort: 80
- name: rss-reader
image: dockerhub.com/redis:3.2.8
ports:
- containerPort: 88
##说明:文档中“ image: dockerhub.com/redis:3.2.8”并不是真实的,如有需要请自行去下载!
#创建pod
kubectl create -f test-deployment --namespace=test
pods "rss-site"created
#查看pod
kubectl get po --namespace=test
NAME READY STATUS RESTARTS AGE
rss-site 2/2 Running 0 2h
#查看pod详细情况
kubectl describe po rss-site --namespace=test
Name: rss-site
Namespace: test
Node: swarm1/10.0.0.38
Start Time: Thu, 17 May 2018 16:31:57 +0800
Labels: app=web
Annotations: <none>
Status: Running
IP: 10.244.2.130
Containers:
front-end:
Container ID: docker://adb8115e7a559b0680a0a8bc79c4f535f1c6b8227acdb25f406c41bc7a29ac3c
Image: nginx:1.7.9
Image ID: docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 17 May 2018 16:32:20 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-z4ffx (ro)
rss-reader:
Container ID: docker://19e642c9bfa2fe2c5161ea015ea02c0470eb360744df452a28f975f81acce30a
Image: dockerhub.datagrand.com/global/redis:3.2.8
Image ID: docker-pullable://dockerhub.datagrand.com/global/redis@sha256:de7a70c04bb2de36bb32c44fe26ce92b9715922f80e07cb3f25aaf00a1f49bb5
Port: 88/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 17 May 2018 16:32:24 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-z4ffx (ro)
#分别进入pod(容器)内部
##进入front-end
kubectl exec -it rss-site -c front-end /bin/bash --namespace=test
root@rss-site:/# nginx -v
nginx version: nginx/1.7.9
#进入rss-reade
kubectl exec -it rss-site -c rss-reader /bin/bash --namespace=test
root@rss-site:/data#
##说明
在一个Pod中同时运行多个容器是一种比较高级的用法。只有当你的容器需要紧密配合协作的时候才考虑用这种模式。例如,你有一个容器作为web服务器运行,需要用到共享的volume,有另一个“sidecar”容器来从远端获取资源更新这些文件,如下图所示:
标签:kubernetes中的Pod简述与实 pod kubernetes deployment
原文地址:http://blog.51cto.com/wutengfei/2117648