本文是《Docker自建虚拟机实验环境及基础入门》一文的 续篇,从上文实验环境下继续操作。
Kubernetes节点分为Master和Minion两种类型,Master节点只需一个,Minion节点可以有多个,组成集群。而集群通过ETCD服务保存集群配置信息,通过Flanneld服务协调集群内容器的IP地址段。下图是简单示意:
下面描述如何在一台虚拟机中安装一个伪Kubernetes集群,以实现Kubernetes集群的全部功能,达到学习Kubernetes的目的。
本文所用的文件请去 https://pan.baidu.com/s/1eSei6KM 里提取,下面是文件列表。
下面是安装步骤:
一、 安装ETCD
rpm -ivh etcd-3.0.15-1.el7.x86_64.rpm
etcd配置:
安装完成后,用下面命令查看etcd服务,找到etcd的启动脚本,然后进行修改:
service etcd status
Redirecting to /bin/systemctl status etcd.service
● etcd.service - Etcd Server
Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
cat /usr/lib/systemd/system/etcd.service
查看 EnvironmentFile 是哪个文件,例如:
EnvironmentFile=-/etc/etcd/etcd.conf
就是要修改/etc/etcd/etcd.conf文件。删除该文件多余部分,只留下如下内容:
vi /etc/etcd/etcd.conf
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_NAME="etcd202"
ETCD_LISTEN_PEER_URLS="http://192.168.165.202:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.165.202:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.165.202:2380"
ETCD_INITIAL_CLUSTER="etcd202=http://192.168.165.202:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.165.202:2379"
ETCD_INITIAL_CLUSTER_STATE=new
这里假设虚拟机的IP地址是 192.168.165.202,etcd202是自定义的一个代号,本文件主要定义etcd服务对外的服务端口,用于kubernetes存放配置数据。
修改/usr/lib/systemd/system/etcd.service
ExecStart= 后面写成下面内容:
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\" --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" --initial-advertise-peer-urls=\"${ETCD_INITIAL_ADVERTISE_PEER_URLS}\" --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\""
就是使用上面文件中的定义的内容启动etcd服务。修改了这个文件要reload,以后再修改/etc/etcd/etcd.conf时就不用reload。
systemctl daemon-reload
启动etcd
service etcd start
验证:查看服务状态,
service etcd status
Redirecting to /bin/systemctl status etcd.service
● etcd.service - Etcd Server
Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2017-02-28 02:24:43 EST; 1min 31s ago
Main PID: 41239 (etcd)
Memory: 4.8M
CGroup: /system.slice/etcd.service
└─41239 /usr/bin/etcd --name=etcd202 --data-dir=/var/lib/etcd/default.etcd --listen-client-urls=http://192.168.165.202:2379 --listen-peer-urls=http://19...
Feb 28 02:24:43 hdp02 etcd[41239]: 1113039976060d7a received vote from 1113039976060d7a at term 2
Feb 28 02:24:43 hdp02 etcd[41239]: 1113039976060d7a became leader at term 2
Feb 28 02:24:43 hdp02 etcd[41239]: raft.node: 1113039976060d7a elected leader 1113039976060d7a at term 2
Feb 28 02:24:43 hdp02 etcd[41239]: setting up the initial cluster version to 3.0
Feb 28 02:24:43 hdp02 etcd[41239]: set the initial cluster version to 3.0
Feb 28 02:24:43 hdp02 etcd[41239]: enabled capabilities for version 3.0
Feb 28 02:24:43 hdp02 etcd[41239]: published {Name:etcd202 ClientURLs:[http://192.168.165.202:2379]} to cluster 2da0d08dc2b23eb5
Feb 28 02:24:43 hdp02 etcd[41239]: ready to serve client requests
Feb 28 02:24:43 hdp02 etcd[41239]: serving insecure client requests on 192.168.165.202:2379, this is strongly discouraged!
Feb 28 02:24:43 hdp02 systemd[1]: Started Etcd Server.
再验证键值的读写:
etcdctl --endpoints http://192.168.165.202:2379 set /cluster "example-k8s"
etcdctl --endpoints http://192.168.165.202:2379 get /cluster
example-k8s
二、 安装 flannel
rpm -ivh flannel-0.5.5-1.el7.x86_64.rpm
vi /etc/sysconfig/flanneld
# Flanneld configuration options
# etcd url location. Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://192.168.165.202:2379"
# etcd config key. This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_KEY="/k8s/network"
FLANNEL_ETCD_PREFIX="/k8s/network"
# Any additional options that you want to pass
#FLANNEL_OPTIONS=""
上面将prefix定为 /k8s/network
现在在etcd中增加入口:
etcdctl --endpoints http://192.168.165.202:2379 mkdir /k8s/network
测试增加一个config属性:
etcdctl --endpoints http://192.168.165.202:2379 set /k8s/network/config '{"Network":"192.192.0.0/16", "Backend": {"Type": "vxlan"}}'
测试读取刚增加的属性:
etcdctl --endpoint http://192.168.171.55:2379 get /k8s/network/config
{"Network": "192.192.0.0/16", "Backend": {"Type": "vxlan"}}
这里定义子网的IP是192.192. 开头的,后面docker的container启动时,获取的IP就按此规则。
启动 flanneld服务:
service flanneld start
验证:
# ifconfig flannel0
flannel0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1472
inet 192.192.32.0 netmask 255.255.0.0 destination 192.192.32.0
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 500 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
三、 修改docker配置
上次已安装好docker服务,这次需要将docker的IP地址获取切换给flanneld管理:
查看docker的服务脚本位置
看service docker status的输出,修改 /usr/lib/systemd/system/docker.service 文件:
vi /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS
……
修改完成后刷新服务配置。
systemctl daemon-reload
启动docker:
service docker start
使docker服务开机启动
chkconfig docker on
验证:
ifconfig 查看到的docker0 flannel.0在本机分配的ip网段地址是一致的。
上传pause镜像并push,后面要用。
docker load -i pause.tar
5f70bf18a086: Loading layer [===================================>] 1.024 kB/1.024 kB
41ff149e94f2: Loading layer [===================================>] 748.5 kB/748.5 kB
Loaded image: 192.168.171.51:5000/pause-amd64:3.0
docker tag 192.168.171.51:5000/pause-amd64:3.0 pause-amd64:3.0
这里pause.tar中包含的镜像是带有私有镜像服务器信息的,用tag命令去掉私有镜像服务器IP和端口部分的信息。
上面准备工作完成。下面开始
四、 安装 kubernetes
建立一个kubernetes目录,例如 /usr/kubernetes/bin,将这个目录设到缺省路径下,将下面文件放入该目录中并改成可执行权限:
kubelet
kubectl
kube-scheduler
kube-proxy
kube-controller-manager
kube-apiserver
export PATH=$PATH:/usr/kubernetes/bin
chmod +x kube*
使用 start_k8s_master.sh启动 master。
cat start_k8s_master.sh
nohup kube-apiserver --insecure-bind-address=0.0.0.0 --insecure-port=8080 --cors_allowed_origins=.* --etcd_servers=http://192.168.165.202:2379 --v=1 --logtostderr=false --log_dir=/var/log/k8s/apiserver --service-cluster-ip-range=192.192.0.0/16 2>&1 1>/dev/null &
nohup kube-controller-manager --master=192.168.165.202:8080 --enable-hostpath-provisioner=false --v=1 --logtostderr=false --log_dir=/var/log/k8s/controller-manager 2>&1 1>/dev/null &
nohup kube-scheduler --master=192.168.165.202:8080 --v=1 --logtostderr=false --log_dir=/var/log/k8s/scheduler 2>&1 1>/dev/null &
使用 start_k8s_minion.sh启动 slave 。
cat start_k8s_minion.sh
nohup kubelet --address=0.0.0.0 --port=10250 --v=1 --hostname_override=192.168.65.202 --api_servers=http://192.168.165.202:8080 --pod-infra-container-image=pause-amd64:3.0 --logtostderr=false --docker-endpoint="http://192.168.165.202:4243" 2>&1 1>/dev/null &
nohup kube-proxy --master=192.168.165.202:8080 --log_dir=/var/log/k8s/proxy --v=1 --logtostderr=false 2>&1 1>/dev/null &
这里的pause-amd64:3.0 就是我们刚load的kubernetes缺省要用到的镜像。
验证:
kubectl get nodes
会看到有一个kubernetes节点了(因为是伪集群,只有一个节点)。
真实集群可以将flanneld和start_k8s_minion.sh这两部分在多台机器上安装以扩展集群。
五、 测试 kubernetes
因为没有私有镜像注册服务器,我们自己load镜像进行测试:
例如上面百度盘中有 image_a_1_0.tar.gz是一个镜像离线包,用命令
docker load -i image_a_1_0.tar.gz
可将镜像上载到虚拟机中
kubectl run image_a --image=registry.server.com:5000/image_a:v1.0
service "image_a" created
deployment "image_a" created
可以看到可以部署镜像到节点中到,这里image_a是自定义的名称,registry.server.com:5000/image_a:v1.0是load 文件 image_a_1_0.tar.gz的镜像名称,有私有镜像服务器信息,本地运行镜像时可以照抄,也可以用docker tag registry.server.com:5000/image_a:v1.0 image_a:v1.0 重标签一下去掉这部分信息。