《Kubernetes权威指南:从Docker到Kubernetes实践全接触》学习实验记录-创建pod,rc,service
2019-01-14 23:40 it长青 阅读(153) 评论(0) 收藏 举报**注意
要有一个 pod-infrastructure的镜像,从阿里云源上下载
[root@k8s_master ~]# docker pull registry.cn-hangzhou.aliyuncs.com/architect/pod-infrastructure
Using default tag: latest
Trying to pull repository registry.cn-hangzhou.aliyuncs.com/architect/pod-infrastructure ...
latest: Pulling from registry.cn-hangzhou.aliyuncs.com/architect/pod-infrastructure
239425a20f14: Pull complete
4be67d7b70f6: Pull complete
Digest: sha256:6398e16a3b3ac4b3aad02752a391f0fd16d728fc918e8c87cbcb853b667eba35
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/architect/pod-infrastructure:latest
[root@k8s_master ~]#
[root@k8s_master ~]#
[root@k8s_master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
172.16.111.200:5000/httpd v1 b92a0eb0b729 6 hours ago 364 MB
httpd v1 b92a0eb0b729 6 hours ago 364 MB
docker.io/registry latest 2e2f252f3c88 2 months ago 33.3 MB
registry.cn-shenzhen.aliyuncs.com/doio/registry latest 2e2f252f3c88 2 months ago 33.3 MB
172.16.111.200:5000/centos v 85b2308e24e6 6 months ago 199 MB
registry.cn-shenzhen.aliyuncs.com/itcp/centos latest 85b2308e24e6 6 months ago 199 MB
registry.cn-hangzhou.aliyuncs.com/architect/pod-infrastructure latest ee020ceeef01 2 years ago 215 MB
[root@k8s_master ~]#
[root@k8s_master ~]# docker tag ee020ceeef01 172.16.111.200:5000/pod-infrastructure:latest
[root@k8s_master ~]# docker push 172.16.111.200:5000/pod-infrastructure:latest
The push refers to a repository [172.16.111.200:5000/pod-infrastructure]
fdd73c81c68e: Pushed
afafa291bfcc: Pushed
latest: digest: sha256:0855fac828eb00d2f2656b30802dce79310218783fd513dc4911e00165d59d29 size: 740
[root@k8s_master ~]#
保存到自己的镜像仓库里
pods
Pod是Kubernetes的基本操作单元,把相关的一个或多个容器构成一个Pod,通常Pod里的容器运行相同的应用。Pod包含的容器运行在同一个Minion(Host)上,看作一个统一管理单元,共享相同的volumes和network namespace/IP和Port空间。
我们通过编写.yaml模板来定义pod
[root@k8s_master ~]# vim mypod.yaml
[root@k8s_master ~]#
[root@k8s_master ~]# cat mypod.yaml
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: pod1
spec:
containers:
- name: pod1
image: 172.16.111.200:5000/httpd:v1
ports:
- containerPort: 80
protocol: TCP
hostIP: 0.0.0.0
hostPort: 8080
[root@k8s_master ~]#
[root@k8s_master ~]# kubectl create -f mypod.yaml
pod "web" created
[root@k8s_master ~]#
[root@k8s_master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web 0/1 Pending 0 8m
webrc-5xgv2 1/1 Running 0 10m # Running 状态就是创建成功
webrc-nm716 1/1 Running 1 10m
[root@k8s_master ~]#
[root@k8s_master ~]#
到node节点上也能查看到
[root@k8s_node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
440d2d107323 172.16.111.200:5000/httpd:v1 "/usr/sbin/httpd -..." 3 minutes ago Up 3 minutes k8s_pod1.2d3905b1_webrc-5xgv2_default_fd8c5812-fba7-11e8-9a1d-000c29bd7985_dda39432
943ee23b48b2 172.16.111.200:5000/pod-infrastructure:latest "/pod" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp k8s_POD.2872049d_webrc-5xgv2_default_fd8c5812-fba7-11e8-9a1d-000c29bd7985_d1992ec9
[root@k8s_node1 ~]#
[root@k8s_node2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcc7e774ed5f 172.16.111.200:5000/httpd:v1 "/usr/sbin/httpd -..." 3 minutes ago Up 3 minutes k8s_pod1.2d3905b1_webrc-nm716_default_01b7e92b-fba8-11e8-9a1d-000c29bd7985_fd941516
129d8dcef6ed 172.16.111.200:5000/pod-infrastructure:latest "/pod" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp k8s_POD.2872049d_webrc-nm716_default_01b7e92b-fba8-11e8-9a1d-000c29bd7985_2eaaec16
[root@k8s_node2 ~]#
如何创建不成功,一般都是因为 pod-infrastructure 镜像在node节点中没拉取到
对pod的定义通过yaml或json格式的配置文件来完成
pod的生命周期是通过replication controller(RC)来管理的。pod的生命过程包括:通过模板进行定义,然后分配到一个node上运行,在pod所含容器运行结束后pod也结束。在整个过程中,pod处于以下4种状态之一:
1.pending:pod定义正确,提交到master,但其所包含的容器镜像还未完全创建。通过master对pod进行调度需要一些时间,之后node对镜像进行下载也需要一些时间。
2.running:pod已被分配到某个node上,且包含的所有容器镜像都已经创建完成,并成功运行起来
3.succeeded:pod中所有容器都成功结束,并且不会被重启,这是pod的一种最终状态
4.failed:pod中所有容器都结束了,但至少一个容器是以失败状态结束的,这也是pod的一种最终状态
RC
rc用于定义pod副本的数量。在master内,controller manager进程通过RC的定义来完成pod的创建、监控、启停等操作
只要定义了rc,pod就可以不用定义了,rc中会包括去创建pod
[root@k8s_master ~]# vim web_rc.yaml
[root@k8s_master ~]# cat web_rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: webrc # rc的名字
labels:
name: webrc # 标签的名字
spec:
replicas: 2 # 幅本数
selector:
name: pod1 # 标签名,指定pod的label名字,可通过
template:
metadata:
name: pod1
labels:
name: pod1 # 注意,这里要和上面spec.selector中的name一致
spec:
containers:
- name: pod1
image: 172.16.111.200:5000/httpd:v1
ports:
- containerPort: 80
protocol: TCP
hostIP: 0.0.0.0
hostPort: 8080
[root@k8s_master ~]#
[root@k8s_master ~]# kubectl create -f web_rc.yaml
replicationcontroller "webrc" created
[root@k8s_master ~]#
[root@k8s_master ~]# kubectl get rc
NAME DESIRED CURRENT READY AGE
webrc 2 2 1 11s # 要 READY 不为0才是创建成功
[root@k8s_master ~]#
[root@k8s_master ~]#
[root@k8s_master ~]#
RC通过label来调用pod
label以key/value键值对的形式附加到各种对象上,如pod,service,RC,node等
在为对象定义好label后,其他对象就可以使用label selector(选择器)来定义其作用的对象了。
#在输出中,可以查看到pod的label
kubectl get pods pod1 --output yaml -s http://master:8080
再次到 node 节点上查看
[root@k8s_node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8bdaf757967 172.16.111.200:5000/httpd:v1 "/usr/sbin/httpd -..." About a minute ago Up About a minute k8s_pod1.2d3905b1_webrc-wtvkx_default_07133f28-fbaa-11e8-9a1d-000c29bd7985_8b27f4d3
1b8d032925d0 172.16.111.200:5000/pod-infrastructure:latest "/pod" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp k8s_POD.2872049d_webrc-wtvkx_default_07133f28-fbaa-11e8-9a1d-000c29bd7985_754bd514
[root@k8s_node1 ~]#
[root@k8s_node2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
335d509707d6 172.16.111.200:5000/httpd:v1 "/usr/sbin/httpd -..." 2 minutes ago Up 2 minutes k8s_pod1.2d3905b1_web_default_3dc45f27-fba8-11e8-9a1d-000c29bd7985_a1121edf
0c30cd170582 172.16.111.200:5000/pod-infrastructure:latest "/pod" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp k8s_POD.2872049d_web_default_3dc45f27-fba8-11e8-9a1d-000c29bd7985_89e71535
[root@k8s_node2 ~]#
会发现虽然容器还是 httpd 和 pod-infrastructure 这两个镜像创建的,但是 容器id跟刚才用mypod.yaml创建的是不一样的
service
service只是提供了一个内部访问的IP
要实现负载均衡,还需要调度,由其中一个pod来完成
访问流程:
1.客户端通过主机IP:port访问服务
2.主机通过iptables将访问转发至service IP
3.通过iptables,再将访问service IP的流量转发至proxy,proxy产生一个随机端口
4.proxy在endpint中随机选择一个pod来进行回应
kube-proxy(每个node有一个,实现端口映射)--->endpoint(具有相同标签的pod的IP的集合)--->从endpoint里面选择一个pod来响应请求
在pod正常启动后,系统将会根据service的定义创建出与pod对应的endpoint对象,以建立起service与后端pod的对应关系
如果一个pod的副本数为2,这两个副本会分布在不同的node节点,不管访问哪个node节点主机的IP,都会实现负载均衡,所以在主机前面还可以放一个负载均衡器,比如:haproxy,实现客户端只需要访问一个IP,实现负载均衡
创建Service
[root@k8s_master ~]# vim apache_service.yaml
[root@k8s_master ~]# cat apache_service.yaml
apiVersion: v1
kind: Service
metadata:
name: web
labels:
name: weblab
spec:
ports:
- port: 8080
targetPort: 80
selector:
name: pod1 # 是 rc 的 template.metadata.labels.name 名
[root@k8s_master ~]# kubectl create -f apache_service.yaml -s http://master:8080
service "web" created
[root@k8s_master ~]#
[root@k8s_master ~]# kubectl get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 2d
web 10.254.114.97 <none> 8080/TCP 39s
[root@k8s_master ~]#
用这个地址在任何一个node节点上就可以访问到
[root@k8s_node1 ~]# curl 10.254.114.97:8080
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Apache HTTP Server Test Page powered by CentOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
.....
.....
</div>
</body></html>
[root@k8s_node1 ~]#
[root@k8s_node2 ~]# curl 10.254.114.97:8080
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Apache HTTP Server Test Page powered by CentOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
.....
.....
</div>
</body></html>
[root@k8s_node2 ~]#
查询后端服务
[root@k8s_master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 172.16.111.200:6443 2d
web 172.17.0.2:80 38m
[root@k8s_master ~]#
浙公网安备 33010602011771号