***声明***
本文译自Kurbernetes官方文档
<https://kubernetes.io/docs/getting-started-guides/centos/centos_manual_config/#support-level>;
1. 准备工作
先选择master和s/nodes的IP地址,PS:(个人建议写在纸上,配置的时候可以直接看,文档看多了会眼花看错)
Host: | IP Address: |
centos-master | 192.168.56.200 |
centos-node-1 | 192.168.56.201 |
centos-node-2 | 192.168.56.202 |
centos-node-3 | 192.168.56.203 |
为了使编辑过程更加轻松愉快,个人推荐大家安装vim,相比vi和nano更加强大(本文档将全程使用vim来进行配置)
2. 配置所有主机
1. 创建虚拟容器库
$ vim /etc/yum.repos.d/virt7-docker-common-release.repo
然后把下方内容复制到文档中
[virt7-docker-common-release] name=virt7-docker-common-release baseurl=http://cbs.centos.org/repos/virt7-docker-common-release/x86_64/os/ gpgcheck=0 |
2. 所有主机安装必要软件
$ yum -y install --enablerepo=virt7-docker-common-release kubernetes etcd flannel
3. 使用下方指令把master和所有的地址加到 /etc/hosts里面。
echo "192.168.56.200 centos-master 192.168.56.201 centos-node-1 192.168.56.202 centos-node-2 192.168.56.103 centos-node-3" >> /etc/hosts |
4. 在所有主机上编辑 /etc/kubernetes/config (修改KUBE_MASTER)
$ vim /etc/kubernetes/config
# logging to stderr means we get it in the systemd journal KUBE_LOGTOSTDERR="--logtostderr=true" # journal message level, 0 is debug KUBE_LOG_LEVEL="--v=0" # Should this cluster be allowed to run privileged docker containers KUBE_ALLOW_PRIV="--allow-privileged=false" # How the replication controller and scheduler find the kube-apiserver KUBE_MASTER="--master=http://centos-master:8080"; |
5. 把所有主机的防火墙关闭(如果关闭不了,先关闭SELinux),关闭后需要reboot所有主机。
$ systemctl stop firewalld.service #停止防火墙
$ systemctl disable firewalld.service #设置成开机不启动防火墙
$ firewall-cmd --state #查看防火墙是否关闭
$ reboot #重启~
3. 配置centos_master
1. 配置kubernetes services(直接按照下方更改)
$ vim /etc/etcd/etcd.conf
# [member] ETCD_NAME=default ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"; #[cluster] ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"; |
2. 配置 /etc/kubernetes/apiserver(直接按照下方更改)
$ vim /etc/kubernetes/apiserver
# The address on the local server to listen to. KUBE_API_ADDRESS="--address=0.0.0.0" # The port on the local server to listen on. KUBE_API_PORT="--port=8080" # Port kubelets listen on KUBELET_PORT="--kubelet-port=20250" # Comma separated list of nodes in the etcd cluster KUBE_ETCD_SERVERS="--etcd-servers=http://centos-master:2379"; # Address range to use for services KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" # Add your own! KUBE_API_ARGS="" |
3. 启动ETCD并且配置ETCD,覆盖master的网络配置
(这个网段一定要是没被使用的,我选择的172.30.0.0/16)
$ systemctl start etcd #启动ETCD
$ etcdctl mkdir /kube-centos/network #创建文件夹,保存网络配置
$ etcdctl mk /kube-centos/network/config "{ \"Network\": \"172.30.0.0/16\", \"SubnetLen\": 24, \"Backend\": { \"Type\": \"vxlan\" } }"
4. 配置flannel区覆盖docker网络配置
$ vim /etc/sysconfig/flanneld
# Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS="http://centos-master:2379"; # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="/kube-centos/network" # Any additional options that you want to pass #FLANNEL_OPTIONS="" |
5. 在master上启动相应服务(前面都正确的话,所有服务都会显示绿色的Active)
for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler flanneld; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done |
4. 配置所有node
(kubelet,然后启动kubelete和proxy)
1. 配置kubelet
$ vim /etc/kubernetes/kubelet #改成下表的样子
#其中的centos-n要改成你自己设置的node名字。
# The address for the info server to serve on KUBELET_ADDRESS="--address=0.0.0.0" # The port for the info server to serve on KUBELET_PORT="--port=20250" # You may leave this blank to use the actual hostname # Check the node number! KUBELET_HOSTNAME="--hostname-override=centos-node-n" # Location of the api-server KUBELET_API_SERVER="--api-servers=http://centos-master:8080"; # Add your own! KUBELET_ARGS="" |
2. 配置flannel,覆盖docker网络。
$ vim /etc/sysconfig/flanneld
# Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS="http://centos-master:2379"; # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="/kube-centos/network" # Any additional options that you want to pass #FLANNEL_OPTIONS="" |
3. 运行node上配置好的services (kube-proxy, kubelet, flannel, docker)
for SERVICES in kube-proxy kubelet flanneld docker; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done |
4. 配置kubectl(直接复制黏贴即可)
kubectl config set-cluster default-cluster --server=http://centos-master:8080 kubectl config set-context default-context --cluster=default-cluster --user=default-admin kubectl config use-context default-context |
5. 检查配置结果
集群(cluster)是否可以看得见所有node(s)
$ kubectl get nodes #成功的话应该显示下表内容
NAME STATUS AGE VERSION centos--1 Ready 3d v1.6.0+fff5156 centos--2 Ready 3d v1.6.0+fff5156 centos--3 Ready 3d v1.6.0+fff5156 |
Kubernetes常用命令: