三、创建 Pods (Containers)
为了创建一个pod,我们需要在kubernetes master上面定义一个yaml 或者 json配置文件。然后使用kubectl命令创建pod
# mkdir -p /k8s/pods
# cd /k8s/pods/
# vim nginx.yaml
在nginx.yaml里面增加如下内容:
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
创建pod:
# kubectl create -f nginx.yaml
此时有如下报错:
Error from server: error when creating "nginx.yaml": Pod "nginx" is forbidden: no API token found for service account default/default, retry after the token is automatically created and added to the service account
解决办法是编辑/etc/kubernetes/apiserver 去除 KUBE_ADMISSION_CONTROL中的SecurityContextDeny,ServiceAccount,并重启kube-apiserver.service服务:
#vim /etc/kubernetes/apiserver
KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,ResourceQuota"
#systemctl restart kube-apiserver.service
之后重新创建pod:
# kubectl create -f nginx.yaml
pods/nginx
查看pod:
# kubectl get pod nginx
NAME READY STATUS RESTARTS AGE
nginx 0/1 Image: nginx is not ready on the node 0 34s
这里STATUS一直是这个,创建不成功,下面排错。通过查看pod的描述发现如下错误:
# kubectl describe pod nginx
Wed, 28 Oct 2015 10:25:30 +0800 Wed, 28 Oct 2015 10:25:30 +0800 1 {kubelet 192.168.1.16} implicitly required container POD pulled Successfully pulled Pod container image "gcr.io/google_containers/pause:0.8.0"
Wed, 28 Oct 2015 10:25:30 +0800 Wed, 28 Oct 2015 10:25:30 +0800 1 {kubelet 192.168.1.16} implicitly required container POD failed Failed to create docker container with error: no such image
Wed, 28 Oct 2015 10:25:30 +0800 Wed, 28 Oct 2015 10:25:30 +0800 1 {kubelet 192.168.1.16} failedSync Error syncing pod, skipping: no such image
Wed, 28 Oct 2015 10:27:30 +0800 Wed, 28 Oct 2015 10:29:30 +0800 2 {kubelet 192.168.1.16} implicitly required container POD failed Failed to pull image "gcr.io/google_containers/pause:0.8.0": image pull failed for gcr.io/google_containers/pause:0.8.0, this may be because there are no credentials on this request. details: (API error (500): invalid registry endpoint "http://gcr.io/v0/". HTTPS attempt: unable to ping registry endpoint https://gcr.io/v0/
v2 ping attempt failed with error: Get https://gcr.io/v2/: dial tcp 173.194.72.82:443: i/o timeout
这里可能会遇到pod状态一直处于Penning的问题,此时可以通过kubectl describe pods/pod-name来查看pod信息,如果没有出错信息,那么Minion一直处于下载镜像中,下载好之后pod即会成功启动。
从网上找到 pause:0.8.0 的镜像,然后再每个node上导入镜像:
请在境外docker服务器执行 docker pull 命令下载镜像
gcr.io/google_containers/pause:latestgcr.io/google_containers/pause:1.0gcr.io/google_containers/pause:0.8.0
再用导出镜像
docker save -o pause.tar gcr.io/google_containers/pausegzip pause.tar
最后把这个包放到 kubernetes 环境所有的 docker 服务器上
docker load -i pause.tar.gz
在执行以下命令即可成功创建pod
[root@master ~]#kubectl create -f nginx.yamlpods/nginx
查看pod
[root@master ~]# kubectl get pod nginxNAME READY STATUS RESTARTS AGEnginx 1/1 Running 0 2min
前往nodes节点上查看docker images
[root@slave1 ~]# docker imagesREPOSITORY TAG
更多Docker相关教程见以下内容:
Ubuntu 14.04安装Docker
在Ubuntu Trusty 14.04 (LTS) (64-bit)安装Docker