centos 7 搭建 k8s

Posted xmc2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos 7 搭建 k8s相关的知识,希望对你有一定的参考价值。

环境

Centos 7.2

master 192.168.121.101
node-1 192.168.121.134
node-2 192.168.121.135

Kubernetes集群组件:
– etcd 一个高可用的K/V键值对存储和服务发现系统
– flannel 实现夸主机的容器网络的通信
– kube-apiserver 提供kubernetes集群的API调用
– kube-controller-manager 确保集群服务
– kube-scheduler 调度容器,分配到Node
– kubelet 在Node节点上按照配置文件中定义的容器规格启动容器
– kube-proxy 提供网络代理服务,将service与pod打通。

Kubernetes工作模式server-client,Kubenetes Master提供集中化管理Minions。部署1台Kubernetes Master节点和2台Minion节点

技术图片

 

准备工作

关闭selinux

setenforce 0
vim /etc/sysconfig/selinux SELINUX=enforcing 改为 SELINUX=disabled

关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

1.配置阿里云yum源

cat << EOF |  tree /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

EOF

一、master 安装配置

1,使用yum安装etcd和kubernetes -master

yum install etcd kubernetes-master -y

2.编辑/etc/etcd/etcd.conf文件

mv /etc/etcd/etcd.conf /etc/etcd/etcd.conf.bak

cat << EOF > /etc/etcd/etcd.conf
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_NAME="default"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"

EOF

3.编辑/etc/kubernetes/apiserver文件

mv /etc/kubernetes/apiserver  /etc/kubernetes/apiserver.bak
cat << EOF > /etc/kubernetes/apiserver KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" KUBE_API_PORT="--port=8080" KUBELET_PORT="--kubelet-port=10250" KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379" KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=192.168.121.0/24" KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,ResourceQuota" KUBE_API_ARGS="" EOF

4.设置 etcd、kube-apiserver、kube-controller-manager、kube-scheduler 为开机自启

for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do systemctl start $SERVICES;systemctl enable $SERVICES;systemctl status $SERVICES; done

5.在etcd中定义flannel网络

etcdctl mk /atomic.io/network/config "Network":"172.40.0.0/16"

注意这里不建议设置172.17这个docker默认网段。删除这个网段用rm,可以etcdctl –help了解这个命令的用法。

二、node节点安装部署

以 node-1 192.168.121.134 为例

1.使用yum安装flannel和kubernetes-node

yum install flannel kubernetes-node -y

2.为flannel网络指定etcd服务,修改/etc/sysconfig/flanneld文件

mv /etc/sysconfig/flanneld   /etc/sysconfig/flanneld.bak


cat << EOF > /etc/sysconfig/flanneld FLANNEL_ETCD_ENDPOINTS="http://192.168.121.101:2379" FLANNEL_ETCD_PREFIX="/atomic.io/network" EOF

3.修改/etc/kubernetes/config文件

mv  /etc/kubernetes/config /etc/kubernetes/config.bak

cat << EOF > /etc/kubernetes/config
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow-privileged=false"
KUBE_MASTER="--master=http://192.168.121.101:8080"

EOF

4.修改node节点配置文件/etc/kubernetes/kubelet

mv  /etc/kubernetes/kubelet /etc/kubernetes/kubelet.bak

cat << EOF >  /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_PORT="--port=10250"
KUBELET_HOSTNAME="--hostname-override=192.168.121.134"
KUBELET_API_SERVER="--api-servers=http://192.168.121.101:8080"
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_ARGS=""

EOF

5,配置完node1,2后,在所有Node节点上启动flanneld,docker,kube-proxy,kubelet服务,并设置开机启动。这里强调一下,一定要注意etcd—->flannel—–>docker的先后启动顺序,否则会导致pod里边的ip会和其他node节点的pod不在一个网段,通信就有问题。

for SERVICES in flanneld docker kube-proxy kubelet;do systemctl start $SERVICES;systemctl enable $SERVICES;systemctl status $SERVICES; done

 

三、验证集群状态

master执行下面命令

kubectl get node

NAME STATUS AGE
192.168.121.134 Ready 15s
192.168.121.135 Ready 12s

 

参考 https://www.cnblogs.com/ajianboke/p/10916133.html

 

问题汇总

1.docker 启动失败报错 unable to configure the Docker daemon with file /etc...n: EOF\\n

解决方法

在 /etc/docker/daemon.json文件里加一个 就解决了

2. kubectl get pod  出现错误【The connection to the server localhost:8080 was refused - did you specif...

解决方法

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile

重启 kube-apiserver

systemctl  restart kube-apiserver

3.etcd启动不起来

解决方法

(1)删除 data-dir 文件

rm -rf  /var/lib/etcd/*

 

以上是关于centos 7 搭建 k8s的主要内容,如果未能解决你的问题,请参考以下文章

CentOS-7.5 搭建 MySQL 主从复制

CentOS 7.0服务器PHP环境搭建

CentOS 7 下nagios搭建记录

环境搭建-CentOS 7上通过Shell脚本自动化安装Harbor

centos 7.2 搭建 lnmp

Gitlab搭建-centOS 7