CentOS7配置Kubernetes(K8S)集群

简介: 1.物理环境 两台CentOS7机器,地址为192.168.0.51和192.168.0.61 1.1 机器信息 192.168.0.51 Master/etcd 192.168.0.61 Node 2. 部署主机 2.1 部署etcd yum -y install etcd yum安装的etcd默认配置文件在/etc/etcd/etcd.conf。

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

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
6天前
|
缓存 容灾 网络协议
ACK One多集群网关:实现高效容灾方案
ACK One多集群网关可以帮助您快速构建同城跨AZ多活容灾系统、混合云同城跨AZ多活容灾系统,以及异地容灾系统。
|
17天前
|
Kubernetes Ubuntu 网络安全
ubuntu使用kubeadm搭建k8s集群
通过以上步骤,您可以在 Ubuntu 系统上使用 kubeadm 成功搭建一个 Kubernetes 集群。本文详细介绍了从环境准备、安装 Kubernetes 组件、初始化集群到管理和使用集群的完整过程,希望对您有所帮助。在实际应用中,您可以根据具体需求调整配置,进一步优化集群性能和安全性。
68 12
|
19天前
|
Prometheus Kubernetes 监控
OpenAI故障复盘 - 阿里云容器服务与可观测产品如何保障大规模K8s集群稳定性
聚焦近日OpenAI的大规模K8s集群故障,介绍阿里云容器服务与可观测团队在大规模K8s场景下我们的建设与沉淀。以及分享对类似故障问题的应对方案:包括在K8s和Prometheus的高可用架构设计方面、事前事后的稳定性保障体系方面。
|
21天前
|
Kubernetes 网络协议 应用服务中间件
Kubernetes Ingress:灵活的集群外部网络访问的利器
《Kubernetes Ingress:集群外部访问的利器-打造灵活的集群网络》介绍了如何通过Ingress实现Kubernetes集群的外部访问。前提条件是已拥有Kubernetes集群并安装了kubectl工具。文章详细讲解了Ingress的基本组成(Ingress Controller和资源对象),选择合适的版本,以及具体的安装步骤,如下载配置文件、部署Nginx Ingress Controller等。此外,还提供了常见问题的解决方案,例如镜像下载失败的应对措施。最后,通过部署示例应用展示了Ingress的实际使用方法。
45 2
|
1月前
|
存储 Kubernetes 关系型数据库
阿里云ACK备份中心,K8s集群业务应用数据的一站式灾备方案
本文源自2024云栖大会苏雅诗的演讲,探讨了K8s集群业务为何需要灾备及其重要性。文中强调了集群与业务高可用配置对稳定性的重要性,并指出人为误操作等风险,建议实施周期性和特定情况下的灾备措施。针对容器化业务,提出了灾备的新特性与需求,包括工作负载为核心、云资源信息的备份,以及有状态应用的数据保护。介绍了ACK推出的备份中心解决方案,支持命名空间、标签、资源类型等维度的备份,并具备存储卷数据保护功能,能够满足GitOps流程企业的特定需求。此外,还详细描述了备份中心的使用流程、控制台展示、灾备难点及解决方案等内容,展示了备份中心如何有效应对K8s集群资源和存储卷数据的灾备挑战。
|
8月前
|
Kubernetes Linux 网络安全
CentOS7上kubeadm方式部署Kubernetes(v1.24.3版本)
CentOS7上kubeadm方式部署Kubernetes(v1.24.3版本)
432 0
|
8月前
|
Kubernetes 应用服务中间件 网络安全
CentOS7上二进制部署Kubernetes高可用集群(v1.18版本)
CentOS7上二进制部署Kubernetes高可用集群(v1.18版本)
438 0
|
Kubernetes 网络协议 Ubuntu
K8S(一):在centos7上 利用kubeadmin 部署Kubernetes
在centos7上 利用kubeadmin 部署Kubernetes
950 0
K8S(一):在centos7上 利用kubeadmin 部署Kubernetes
|
Kubernetes Cloud Native Ubuntu
Minikube v1.25.2 在 Centos 7.9 部署 Kubernetes v1.23.8(2)
Minikube v1.25.2 在 Centos 7.9 部署 Kubernetes v1.23.8(2)
Minikube v1.25.2 在 Centos 7.9 部署 Kubernetes v1.23.8(2)
|
Kubernetes Linux 网络安全
CentOS7环境下快速部署Kubernetes(k8s)集群
CentOS7环境下快速部署Kubernetes(k8s)集群
1349 0
CentOS7环境下快速部署Kubernetes(k8s)集群