kubernetes04(kubernetes集群搭建)

kubernetes04(kubernetes集群搭建)

一.引子

欢迎来到kubernetes集群搭建步骤,笔者此篇目的希望大家在集群搭建过程中不要走笔者走过的坑。

二.kubernertes集群搭建步骤

(一).环境准备

1.技术概述
在搭建kubernetes集群的过程中,我们使用kubeadm工具进行部署,由k8s官方所提供的专门部署集群的管理工具。每一个节点主机上包括master节点都要手动安装并运行docker,同时也都要手动安装并运行kubelet。如果将第一个节点初始化为master节点,在执行初始化这个步骤,其实就是通过kubeadm工具将API Server、etcd、controller-manager、scheduler各组件运行为Pod,也就是跑在docker上。而其他node节点,因已经运行了kubelet、docker组件,剩下的kube-proxy组件也是要运行在Pod上。其中kubelet是负责能运行Pod化容器的核心组件,docker是运行容器的引擎。

2.机器准备(四台机器)
10.10.40.11 master节点
10.10.40.12 node1节点
10.10.40.13 node2节点
10.10.40.14 路由器节点
三台机器都安装了centos7系统,安装过程选择基础安装。
设置主机名:

hostnamectl  set-hostname  k8s-master01

配置/etc/hosts文件,便于kubernetes集群调用和发现

vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
10.10.40.11 master
10.10.40.12 note01
10.10.40.13 note02
 scp /etc/hosts root@10.10.40.13:/etc

3.安装依赖包和工具

yum install -y conntrack ntpdate ntp ipvsadm ipset  iptables curl sysstat libseccomp wget  vim net-tools git

4.机器调试
(1).使用单网卡,并设置为仅主机模式,需要联网下载时开启第二张网卡,随后关闭

#第一张网卡配置
vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=10.10.40.11
NETMASK=255.255.255.0
#第二张网卡开启时配置
vim /etc/sysconfig/network-scripts/ifcfg-ens34
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
NAME=ens34
UUID=ef4d630d-e8cc-4b52-9f2d-cdc35f994a44
DEVICE=ens34
ONBOOT=yes(关闭时将yes改为no)
#swap分区关闭(一定要关闭,血泪教训)
(1)首先执行如下命令关闭 swap。
swapoff -a
(2)接着编辑 /etc/fstab 文件。
vi /etc/fstab
(3)将 /dev/mapper/centos-swap swap swap default 0 0 这一行前面加个 # 号将其注释掉。

(2).配置yum源/关闭iptables/关闭selinux/关闭NetworkManager等操作(详见linux基础,想要快速安装可以参考笔者以前发的Linux基础配置脚本)

#设置防火墙规则为iptables并设置空规则
systemctl  stop firewalld  &&  systemctl  disable firewalld
yum -y install iptables-services  &&  systemctl  start iptables  &&  systemctl  enable iptables  &&  iptables -F  &&  service iptables save
#关闭selinux
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#配置aliyun源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
#关闭系统不需要的服务
systemctl stop postfix && systemctl disable postfix

5.调整系统时区

# 设置系统时区为 中国/上海
timedatectl set-timezone Asia/Shanghai
# 将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog 
systemctl restart crond

6.设置 rsyslogd 和 systemd journald

mkdir /var/log/journal # 持久化保存日志的目录
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
# 最大占用空间 10G
SystemMaxUse=10G
# 单日志文件最大 200M
SystemMaxFileSize=200M
# 日志保存时间 2 周
MaxRetentionSec=2week
# 不将日志转发到 syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald

7.升级系统内核为 4.44
CentOS 7.x 系统自带的 3.10.x 内核存在一些 Bugs,导致运行的 Docker、Kubernetes 不稳定,例如: rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装一次!
yum --enablerepo=elrepo-kernel install -y kernel-lt
# 设置开机从新内核启动
grub2-set-default 'CentOS Linux (4.4.222-1.el7.elrepo.x86_64) 7 (Core)'

(二).master节点安装

1.安装 etce docker kubernetes

yum -y install etcd docker kubernetes

2.修改etcd及kubernetes的配置文件

#[root@localhost ~]# vim /etc/etcd/etcd.conf 
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"

#[root@localhost ~]# vim /etc/kubernetes/apiserver 
KUBE_API_ADDRESS="--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=10.254.0.0/16"
KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
KUBE_API_ARGS=""

#[root@localhost ~]# vim /etc/kubernetes/config 
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://10.10.40.10:8080"

3.对etcd,docker,kube-apiserver,kube-controller-manager,kube-scheduler服务进行开机自启并查看其运行状态

[root@localhost ~]#for SERVICES  in etcd docker kube-apiserver kube-controller-manager kube-scheduler;  do
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES -l
done

4.配置etcd的网络(在etcd中定义flannel网络 )

[root@localhost ~]# etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'

5.测试(node节点没有配置当然是没有资源的)

[root@localhost ~]# kubectl get nodes
No resources found.

(三).node节点安装

1.安装flannel docker kubernetes

yum -y install flannel docker kubernetes
#安装报错
错误:docker-ce-cli conflicts with 2:docker-1.13.1-103.git7f2769b.el7.centos.x86_64 错误:docker-ce confli
#解决方案
卸载了docker然后重装k8s就好了
yum -y remove docker-ce
yum -y remove docker-ce-client

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

[root@localhost ~]# vim /etc/sysconfig/flanneld 
# Flanneld configuration options  (master节点IP及端口)
FLANNEL_ETCD="http://10.10.40.11:2379"
# etcd url location.  Point this to the server where etcd runs(etcd url位置。指向运行etcd的服务器)
FLANNEL_ETCD_ENDPOINTS="http://10.10.40.11:2379"
# etcd config key.  This is the configuration key that flannel queries(etcd配置键。这是flannel查询的配置键)
# For address range assignment(地址范围分配)
FLANNEL_ETCD_PREFIX="/atomic.io/network"
# Any additional options that you want to pass(添加你想添加的其他内容)
#FLANNEL_OPTIONS=""

3.修改/etc/kubernetes/config文件(控制器管理器、调度程序和代理如何查找apiserver)

# 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(控制器管理器、调度程序和代理如何查找apiserver)
KUBE_MASTER="--master=http://10.10.40.11:8080"

4.vim /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=10.10.40.12"
# location of the api-server(api服务的区域,很明显是master节点)
KUBELET_API_SERVER="--api-servers=http://10.10.40.11:8080"
# pod infrastructure container(设置镜像下载地址)
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"

5.设置kube-proxy kubulet docker flanneld重启,开机自启并查看他们的状态

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

6.将第二个节点同时安装测试

[root@localhost ~]# kubectl get nodes
NAME          STATUS    AGE
10.10.40.12   Ready     7m

至此,基本搭建完成。大家一定要有耐心哦。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
第一章介绍docker的前世今生,了 解docker的实现原理,以Django项目为例,教大家如何编写最佳的Dockerfile实现构业务镜像的制作。通过本章的学习,大家会知道docker的概念及基本操作,并学会构建自己的业务镜像,并通过抓包的方式掌握Docker最常用的bridge网络模式的通信。第二章本章学习kubernetes的架构及工作流程,重点介绍如本章学习kubernetes的架构及工作流程,重点介绍如断的滚动更新,通过服务发现来实现集群内部的服务间访问,并通过ingress- -nginx实现外部使用域名访问集群内部的服务。同时介绍基于EFK如何搭建Kubernetes集群的日志收集系统。学完本章,我们的Django demo项目已经可以运行在k8s集群中,同时我们可以使用域名进行服务的访问。第三章本章基于k8s集群部署gitlab、sonarQube、 Jenkins等工具,并把上述工具集成到Jenkins中,以Django项目为例,通过多分支流水线及Jenkinsfle实现项目代码提交到不同的仓库分支,实现自动代码扫描、单元测试、docker容器构建、k8s服务的自动部署。第四章由于公司内部项目众多,大量的项目使用同一套流程做CICD,那么势必会存在大量的重复代码,因此本章主要通过使用groovy实现Jenkins的sharedL ibrary的开发,以提取项目在CICD实践过程中的公共逻辑,提供一系列的流程的接口供公司内各项目调用,开发完成后,还是以Django的demo项目为例,进行Jenkinsfle的改造,最后仅需通过简单的Jenkinsfle的配置,即可优雅的完成CICD流程的整个过程,此方式已在大型企业内部落地应用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值