当前位置: 首页 > news >正文

CentOS7配置Kubernetes(K8S)集群

1.物理环境 

两台CentOS7机器,地址为192.168.0.51和192.168.0.61

1.1 机器信息

  1. 192.168.0.51 Master/etcd
  2. 192.168.0.61 Node

2. 部署主机

2.1 部署etcd

yum -y install etcd

yum安装的etcd默认配置文件在/etc/etcd/etcd.conf。编辑配置文件,更改为:

# [member]
ETCD_NAME=master
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR="" #ETCD_SNAPSHOT_COUNT="10000" #ETCD_HEARTBEAT_INTERVAL="100" #ETCD_ELECTION_TIMEOUT="1000" #ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5" #ETCD_MAX_WALS="5" #ETCD_CORS="" # #[cluster] #ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380" # if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..." #ETCD_INITIAL_CLUSTER="default=http://localhost:2380" #ETCD_INITIAL_CLUSTER_STATE="new" #ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.0.51:2379,http://192.168.0.51:4001"
#ETCD_DISCOVERY="" #ETCD_DISCOVERY_SRV="" #ETCD_DISCOVERY_FALLBACK="proxy" #ETCD_DISCOVERY_PROXY="" 

启动并验证状态

[root@localhost ~]# vim /etc/etcd/etcd.conf
[root@localhost ~]# systemctl start etcd
[root@localhost ~]# etcdctl set testdir/testkey0 0 0
[root@localhost ~]# etcdctl get testdir/testkey0 0
[root@localhost ~]# etcdctl set testdir/testkey0 1 1
[root@localhost ~]# etcdctl get testdir/testkey0 1
[root@localhost ~]# etcdctl -C http://192.168.0.51:4001 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://192.168.0.51:2379
cluster is healthy
[root@localhost ~]# etcdctl -C http://192.168.0.51:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://192.168.0.51:2379
cluster is healthy

2.2 部署master

安装kubernetes:

yum -y install kubernetes

安装kubernetes会安装docker,所以不单独再安装docker(如果安装docker版本过高会冲突,建议卸载原有docker) 启动Docker并开机启动

systemctl enable docker && systemctl start docker

在kubernetes master上需要运行以下组件:

    Kubernets API Server

    Kubernets Controller Manager

    Kubernets Scheduler

修改配置:

2.2.1 /etc/kubernetes/apiserver

### # kubernetes system config # # The following values are used to configure the kube-apiserver # # The address on the local server to listen to. KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" # The port on the local server to listen on. KUBE_API_PORT="--port=8080" # Port minions listen on # KUBELET_PORT="--kubelet-port=10250" # Comma separated list of nodes in the etcd cluster KUBE_ETCD_SERVERS="--etcd-servers=http://192.168.0.51:2379" # Address range to use for services KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" # default admission control policies #KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota" KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota" # Add your own! KUBE_API_ARGS="" 

这里去除ServiceAccount是为了跳过验证。

2.2.2 /etc/kubernetes/config

### # kubernetes system config # # The following values are used to configure various aspects of all # kubernetes services, including # # kube-apiserver.service # kube-controller-manager.service # kube-scheduler.service # kubelet.service # kube-proxy.service # 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 controller-manager, scheduler, and proxy find the apiserver KUBE_MASTER="--master=http://192.168.0.51:8080" 

设置开机启动并启动服务

[root@localhost kubernetes]# systemctl enable kube-apiserver.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-apiserver.service to /usr/lib/systemd/system/kube-apiserver.service.
[root@localhost kubernetes]# systemctl start kube-apiserver.service
[root@localhost kubernetes]# systemctl enable kube-controller-manager.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-controller-manager.service to /usr/lib/systemd/system/kube-controller-manager.service.
[root@localhost kubernetes]# systemctl start kube-controller-manager.service
[root@localhost kubernetes]# systemctl enable kube-scheduler.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-scheduler.service to /usr/lib/systemd/system/kube-scheduler.service.
[root@localhost kubernetes]# systemctl start kube-scheduler.service 

3. 部署节点

安装kubernetes

yum -y install kubernetes
systemctl enable docker && systemctl start docker

3.1 配置服务

在kubernetes node上需要运行以下组件:

    Kubelet

    Kubernets Proxy

修改配置:

3.1.1 /etc/kubernetes/config

### # kubernetes system config # # The following values are used to configure various aspects of all # kubernetes services, including # # kube-apiserver.service # kube-controller-manager.service # kube-scheduler.service # kubelet.service # kube-proxy.service # 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 controller-manager, scheduler, and proxy find the apiserver KUBE_MASTER="--master=http://192.168.0.51:8080" 

3.1.2 /etc/kubernetes/kubelet

### # kubernetes kubelet (minion) config # The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces) KUBELET_ADDRESS="--address=0.0.0.0" # The port for the info server to serve on # KUBELET_PORT="--port=10250" # You may leave this blank to use the actual hostname KUBELET_HOSTNAME="--hostname-override=192.168.0.61" # location of the api-server KUBELET_API_SERVER="--api-servers=http://192.168.0.51:8080" # pod infrastructure container KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest" # Add your own! KUBELET_ARGS="" 

设置开机启动并启动服务

[root@localhost kubernetes]# systemctl enable kubelet.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@localhost kubernetes]# systemctl start kubelet.service
A dependency job for kubelet.service failed. See 'journalctl -xe' for details.
[root@localhost kubernetes]# systemctl enable kube-proxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
[root@localhost kubernetes]# systemctl start kube-proxy.service 

这里发现kubelet未启动成功 查看日志

[root@localhost kubernetes]# journalctl -xe
-- -- Unit docker-storage-setup.service has begun starting up.
Jul 26 14:40:00 localhost.localdomain kernel: SELinux: initialized (dev overlay, type overlay), uses xattr
Jul 26 14:40:00 localhost.localdomain systemd[1]: docker-storage-setup.service: main process exited, code=exited, status=1/FAILURE
Jul 26 14:40:00 localhost.localdomain systemd[1]: Failed to start Docker Storage Setup.
-- Subject: Unit docker-storage-setup.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit docker-storage-setup.service has failed. -- -- The result is failed.
Jul 26 14:40:00 localhost.localdomain systemd[1]: Unit docker-storage-setup.service entered failed state.
Jul 26 14:40:00 localhost.localdomain systemd[1]: docker-storage-setup.service failed.
Jul 26 14:40:00 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
-- Subject: Unit docker.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit docker.service has begun starting up.
Jul 26 14:40:00 localhost.localdomain container-storage-setup[22194]: ERROR: XFS filesystem at /var has ftype=0, cannot use overlay backend; consider different driver or separate vol
Jul 26 14:40:01 localhost.localdomain dockerd-current[22281]: time="2018-07-26T14:40:00.993907399+08:00" level=info msg="libcontainerd: new containerd process, pid: 22293"
Jul 26 14:40:01 localhost.localdomain systemd[1]: Started Session 170 of user root.
-- Subject: Unit session-170.scope has finished start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit session-170.scope has finished starting up. -- -- The start-up result is done.
Jul 26 14:40:01 localhost.localdomain CROND[22302]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jul 26 14:40:02 localhost.localdomain systemd[1]: Starting Session 170 of user root.
-- Subject: Unit session-170.scope has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit session-170.scope has begun starting up.
Jul 26 14:40:02 localhost.localdomain dockerd-current[22281]: time="2018-07-26T14:40:02.044621941+08:00" level=warning msg="overlay2: the backing xfs filesystem is formatted without
Jul 26 14:40:02 localhost.localdomain dockerd-current[22281]: Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer
Jul 26 14:40:02 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Jul 26 14:40:02 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.

注意到Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer,根据报错信息找到是启动docker出错,原因则是当前kernel不支持overlay2,解决方案为关闭selinux或者使用新的kernel。这里我直接关闭selinux。

[root@localhost kubernetes]# vim /etc/sysconfig/docker # /etc/sysconfig/docker # Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false' if [ -z "${DOCKER_CERT_PATH}" ]; then
 DOCKER_CERT_PATH=/etc/docker
fi 

然后重新启动kubelet

systemctl start kubelet.service

查看结果:

[root@localhost kubernetes]# kubectl -s http://192.168.0.51:8080/ get node
NAME STATUS AGE
192.168.0.61 Ready 12m

在master上可以直接查看

[root@localhost kubernetes]# kubectl get node NAME STATUS AGE
192.168.0.61 Ready 1m 

注意到这里的AGE不同,目前还不清楚原因,做个记录。

4. 部署Flannel

在所有节点(包括master和node)上安装flannel

yum -y install flannel

4.1 修改配置

所有节点修改/etc/sysconfig/flanneld

[root@localhost kubernetes]# vim /etc/sysconfig/flanneld # Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS="http://192.168.0.51:2379" # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="/atomic.io/network" # Any additional options that you want to pass #FLANNEL_OPTIONS="" 

4.2 修改etc配置

Flannel使用Etcd进行配置,来保证多个Flannel实例之间的配置一致性,所以需要在etcd上进行如下配置:(‘/atomic.io/network/config’这个key与上文/etc/sysconfig/flannel中的配置项FLANNEL_ETCD_PREFIX是相对应的,错误的话启动就会出错)

etcdctl mk /atomic.io/network/config '{ "Network": "10.0.0.0/16" }'

4.3 启动

启动Flannel之后,需要依次重启docker、kubernete。 master:

systemctl enable flanneld.service systemctl start flanneld.service systemctl restart docker systemctl restart kube-apiserver.service systemctl restart kube-controller-manager.service systemctl restart kube-scheduler.service 

node:

systemctl enable flanneld.service systemctl start flanneld.service systemctl restart docker systemctl restart kubelet.service systemctl restart kube-proxy.service

本文转移开源中国- CentOS7配置Kubernetes(K8S)集群

相关文章:

  • Docker在Linux/Windows上运行NetCore文章系列
  • 使用Python将MongoDB数据导到MySQL
  • Rem之自适应js
  • ubuntu16.04下安装postgresql 10.3
  • Groovy与Java集成常见的坑
  • SQLserver视图修改sql
  • Visual Studio Code编写C/C++代码常见问题
  • iOS8 自定义UITabBar (使用popToViewController导致的UITabBarButton重叠的问题)
  • 排序算法(Merge Sort)中的 merge 步
  • BAT集体升级云事业部,这背后都藏着哪些“小心思”?
  • 【福利】BAT架构师分享最全Java架构师学习技能图谱:包含Java编程+网络+设计模式+数据库+分布式等...
  • 电磁波、无线电、802、WLAN及WiFi的区别与联系
  • 为什么Fiddler可以但是Charles不行
  • js基础--数组
  • Java 8 中的 Streams API 详解
  • [PHP内核探索]PHP中的哈希表
  • JavaScript 如何正确处理 Unicode 编码问题!
  • Angular 响应式表单之下拉框
  • docker-consul
  • input的行数自动增减
  • java8-模拟hadoop
  • java多线程
  • python3 使用 asyncio 代替线程
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 浮现式设计
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 构建工具 - 收藏集 - 掘金
  • 好的网址,关于.net 4.0 ,vs 2010
  • 区块链共识机制优缺点对比都是什么
  • 再次简单明了总结flex布局,一看就懂...
  • Java数据解析之JSON
  • 函数计算新功能-----支持C#函数
  • ​2020 年大前端技术趋势解读
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • (3)选择元素——(17)练习(Exercises)
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C++17) std算法之执行策略 execution
  • (Oracle)SQL优化技巧(一):分页查询
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (五)c52学习之旅-静态数码管
  • (转)linux 命令大全
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .net和php怎么连接,php和apache之间如何连接
  • /etc/skel 目录作用
  • @transaction 提交事务_【读源码】剖析TCCTransaction事务提交实现细节
  • @value 静态变量_Python彻底搞懂:变量、对象、赋值、引用、拷贝
  • [20190113]四校联考
  • [BT]BUUCTF刷题第8天(3.26)
  • [BT]BUUCTF刷题第9天(3.27)
  • [BZOJ2850]巧克力王国
  • [cocos creator]EditBox,editing-return事件,清空输入框
  • [C语言]——分支和循环(4)
  • [go] 策略模式
  • [IE技巧] IE 中打开Office文件的设置