截止至2015年9月1日,CentOS 已经把 kubernetes 加入官方源。
目前各相关组件版本如下
kubernetes环境角色如下
Tips:
yum update -y
k8s的管理接口
k8s调度器,容器的启动、迁移、扩容缩减时候,选择哪个node,就看它了。
k8s对node的控制行为,比如怎么去调用node启动一个容器。
负责node的管理,基本所有操作都靠它。
每个node里的Container都在一个私有网络中,kube-proxy的作用就是做一个反向代理,让访问者访问这个node的时候,可以转发到内部对应的container。
Tips: 当然,这是我狭隘的理解,仅供参考。
yum install etcd -y
yum install kubernetes-master -y
yum install kubernetes-node flannel docker -y
[root@localhost ~]# egrep -v “^#” /etc/etcd/etcd.conf
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.248:2379"
启动服务
systemctl enable etcd
systemctl start etcd
[root@localhost ~]# egrep -v ‘^#’ /etc/kubernetes/apiserver | grep -v ‘^$’
KUBE_API_ADDRESS="--address=0.0.0.0"
KUBE_ETCD_SERVERS="--etcd_servers=http://192.168.1.248:2379"
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
KUBE_API_ARGS=""
[root@localhost kubernetes]# egrep -v ‘^#’ /etc/kubernetes/controller-manager |grep -v ‘^$’
KUBE_CONTROLLER_MANAGER_ARGS="--node-monitor-grace-period=10s --pod-eviction-timeout=10s"
[root@localhost ~]# egrep -v ‘^#’ /etc/kubernetes/config | egrep -v ‘^$’
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow_privileged=false"
KUBE_MASTER="--master=http://192.168.1.247:8080"
启动服务
systemctl enable kube-apiserver kube-scheduler kube-controller-manager
systemctl start kube-apiserver kube-scheduler kube-controller-manager
[root@localhost ~]# egrep -v ‘^#’ /etc/kubernetes/config | grep -v ‘^$’
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow_privileged=false"
KUBE_MASTER="--master=http://192.168.1.247:8080"
[root@localhost ~]# egrep -v ‘^#’ /etc/kubernetes/kubelet | grep -v ‘^$’
KUBELET_ADDRESS="--address=127.0.0.1"
KUBELET_HOSTNAME="--hostname_override=192.168.1.246"
KUBELET_API_SERVER="--api_servers=http://192.168.1.247:8080"
KUBELET_ARGS="--pod-infra-container-image=kubernetes/pause"
启动服务
systemctl enable kubelet kube-proxy
systemctl start kubelet kube-proxy
初始化flannel的etcd配置
etcdctl -C 192.168.1.248:2379 set /coreos.com/network/config '{ "Network": "10.1.0.0/16" }'
[root@bogon ~]# egrep -v ‘^#’ /etc/sysconfig/flanneld | grep -v ‘^$’
FLANNEL_ETCD="http://192.168.1.248:2379"
FLANNEL_ETCD_KEY="/coreos.com/network"
启动服务
systemctl enable flanenld
systemctl restart flanneld, docker
若网络还不通,就重启下node吧。
三、创建 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