背景:过去应用大多是大型单体应用,他的开发周期长,运行在几台机器上就可以了,运维人员部署也不频繁,监控起来难度也没有那么大,但是还是面临着各种环境上的问题,有的时候在测试环境没有问题,上了生产就各种奇葩问题出现,这不免让开发运维人员都很头疼。最头疼的是随着微服务理念的兴起,大的单体服务被拆解成了一个个的小而独立的微服务组件,这使得开发迭代变得更加快速,服务数量越来越多,对于组件的配置,部署,监控,故障处理等一系列的问题都会从原来变成数倍,这简直就是运维的灾难。为了解决这个问题Kubernetes应运而生。
环境搭建
环境准备
CPU数:每个节点cpu核数必须 >=2核,否则无法启动。
DNS网络:设置位本地连通的DNS否则无法下载镜像。
- linux内核:要求linux内核必须是4版本以上。
准备三台虚拟机或者云服务器(但是与服务器价钱太高划不来,可以使用VMware虚拟机进行搭建如果不知道如何使用VMware进行搭建集群事前准备的可以看我往期VMware集群搭建内容)
环境依赖
#1、为了方便,给每一台机器设置主机名
hostnamectl set-hostname k8s-master01
hostnamectl set-hostname k8s-node01
hostnamectl set-hostname k8s-node02
#查看主机名称
hostname
#给每一台机器配置ip host映射
vi /etc/hosts
192.168.70.128 k8s-master01
192.168.70.129 k8s-node01
192.168.70.130 k8s-node02
#2、安装依赖环境,注意:每一台机器都需要安装此依赖环境
yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git iproute lrzsz bash-completion tree bridge-utils unzip bind-utils gcc
#3、安装iptables,启动iptables,设置开机自启,清空iptables规则,保存当前规则到默认规则
#关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
# 置空iptables
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
#4、关闭selinux
#闭swap分区【虚拟内存】并且永久关闭虚拟内存
swapoff -a && sed -i '/ swap / s/^(.*)$/#1/g' /etc/fstab
#关闭selinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#5、升级Linux内核为4以上
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
#安装内核
yum --enablerepo=elrepo-kernel install -y kernel-lt
#设置开机从新内核启动
grub2-set-default 'CentOS Linux (5.4.111-1.el7.elrepo.x86_64) 7 (Core)'
#注意:设置完内核后,需要重启服务器才会生效!!
reboot
#查询内核
uname -r
#6、调整内核参数,对于k8s
cat > kubernetes.conf <
错误提示:
#如果在执行手动刷新的时候报
sysctl: cannot stat /proc/sys/net/netfilter/nf_conntrack_max: 没有那个文件或目录
#可能是 conntrack没有加载
#执行下边命令
lsmod |grep conntrack
modprobe ip_conntrack
#再次执行手动刷新发现好了
#7、关闭系统不需要的服务
systemctl stop postfix && systemctl disable postfix
#8、设置日志保存方式
#1).创建保存日志的目录
mkdir /var/log/journal
#2).创建配置文件存放目录
mkdir /etc/systemd/journald.conf.d
#3).创建配置文件
cat > /etc/systemd/journald.conf.d/99-prophet.conf < /etc/sysconfig/modules/ipvs.modules <
部署docker
#1、安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2
#紧接着配置一个稳定(stable)的仓库、仓库配置会保存到/etc/yum.repos.d/docker-ce.repo文件中
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#更新Yum安装的相关Docke软件包&安装Docker CE
yum update -y && yum install docker-ce -y
#2、设置docker daemon文件
#创建/etc/docker目录
mkdir /etc/docker -p
#更新daemon.json文件
cat > /etc/docker/daemon.json <
kubeadm[一键安装k8s]
#1、安装kubernetes的时候,需要安装kubelet, kubeadm等包,但k8s官网给的yum源是packages.cloud.google.com,国内访问不了,此时我们可以使用阿里云的yum仓库镜像。
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#2、安装kubeadm、kubelet、kubectl (版本这个东西并不影响我们学习,1.15.1在学习上来说够用了)
yum install -y kubeadm-1.15.1 kubelet-1.15.1 kubectl-1.15.1
# 启动 kubelet
systemctl enable kubelet && systemctl start kubelet
# 验证安装成功,执行下边命令可以看到相关提示证明安装成功
kubectl --help
构建集群
依赖镜像导入
# 查看我们安装过程中需要的镜像
kubeadm config images list
# 输出结果如果下
k8s.gcr.io/kube-apiserver:v1.15.1
k8s.gcr.io/kube-controller-manager:v1.15.1
k8s.gcr.io/kube-scheduler:v1.15.1
k8s.gcr.io/kube-proxy:v1.15.1
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
# 大家可以使用导出的tar包,对于这个版本寻找我是废了很大周章,所以大家就使用我的就可以,如果向要其他版本的可以私信我。
大家可用他通过下载我的 提取码: zjdk 进行下载对应的镜像,下在完成上传到服务器/opt目录下
书写脚本进行导入;[你可以进入到opt/k8s一个一个的导入]
#!/bin/bash
ls /opt/kubeadm-basic.images > /tmp/images-list.txt
cd /opt/kubeadm-basic.images
for i in $(cat /tmp/images-list.txt)
do
docker load -i $i
done
rm -rf /tmp/images-list.txt
#修改脚本执行权限
chmod 755 image-load.sh
#执行脚本
./image-load.sh
# 传输文件及镜像到其他node节点
scp -r image-load.sh kubeadm-basic.images root@k8s-node01:/opt/
scp -r image-load.sh kubeadm-basic.images root@k8s-node02:/opt/
k8s部署
#初始化主节点 只在主节点执行。
#1.拉去镜像资源配置文件
kubeadm config print init-defaults > kubeadm-config.yaml
#2、修改yaml资源文件 下边需要修改的地方已经标记出来
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.70.128 # 此处使用你自己的ip
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: k8s-master01
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.19.0 #此处使用你自己的版本
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16 # 添加flannel模型通信 此处固定。
serviceSubnet: 10.96.0.0/12
scheduler: {} # 在下边在插入指定ipvs网络通信
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: kubeProxyConfiguration
featureGates:
SupportIPVSProxyMode: true
mode: ipvs
#3.初始化主节点,开始部署
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs | tee kubeadm-init.log
下边是常见错
##如果你的cpu是一个就会报下边错误,这就为甚最开始让大家使用2cpu的虚拟机
[ERROR NumCPU]: the number of available CPUs 1 is less than the required 2
##如果你docker服务忘记启动也会报错
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR CRI]: container runtime is not running: output: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
, error: exit status 1
[ERROR Service-Docker]: docker service is not active, please run 'systemctl start docker.service'
[ERROR IsDockerSystemdCheck]: cannot execute 'docker info': exit status 1
[ERROR SystemVerification]: failed to get docker info: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
构建成功会显示成下边的鸭子
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully! # 表示构建成功
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube # 下边三条是要求我们手动创建的
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.70.128:6443 --token abcdef.0123456789abcdef
--discovery-token-ca-cert-hash sha256:a33be40c0ef657e7565b399e7b44ad27bdf6477fd54047c70fdd8e4ee24c2850
进行手动初始化
#创建目录,保存连接配置缓存,认证文件
mkdir -p $HOME/.kube
#拷贝集群管理配置文件
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
#授权给配置文件
chown $(id -u):$(id -g) $HOME/.kube/config
执行命令前查询node
kubectl get po
# 显示下边结果 但是status为notReady,是因为我们还么有安装flannel网络插
NAME STATUS ROLES AGE VERSION
k8s-master01 NotReady master 63m v1.15.1
安装flannel插件
# kube-flannel.yml文件大家可以直接下载
https://pan.baidu.com/s/1w4IUIM7x2k85Lew-qTGY8g
提取码【zjdk】
# 开始部署flannel
kubectl apply -f kube-flannel.yml
# 查看系统po是否正常启动
kubectl get po -n kube-system
# 会显示下边的鸭子
NAME READY STATUS RESTARTS AGE
coredns-5c98db65d4-p8x8v 0/1 Pending 0 49m
coredns-5c98db65d4-ts4qm 0/1 Pending 0 49m
etcd-k8s-master01 1/1 Running 1 48m
kube-apiserver-k8s-master01 1/1 Running 1 49m
kube-controller-manager-k8s-master01 1/1 Running 1 49m
kube-flannel-ds-amd64-mwbcq 0/1 Init:ImagePullBackOff 0 15m
kube-proxy-hhvqh 1/1 Running 1 49m
kube-scheduler-k8s-master01 1/1 Running 1 49m
# 发现有的po没有成功拉取进项显示ImagePullBackOff。这是因为我们从仓库拉取镜像的时候失败了。
拉取镜像失败解决方案
# 因为我已经将镜像源换成了7牛云,还是拉不下来,我们可以更直接的将镜像下载到本地。
docker pull registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-amd64
docker pull registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-arm64
docker pull registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-arm
docker pull registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-ppc64le
docker pull registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-s390x
# 重新打tag
docker tag registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-amd64 quay-mirror.qiniu.com/coreos/flannel:v0.12.0-amd64
docker tag registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-arm64 quay-mirror.qiniu.com/coreos/flannel:v0.12.0-arm64
docker tag registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-arm quay-mirror.qiniu.com/coreos/flannel:v0.12.0-arm
docker tag registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-ppc64le quay-mirror.qiniu.com/coreos/flannel:v0.12.0-ppc64le
docker tag registry.cn-shanghai.aliyuncs.com/leozhanggg/flannel:v0.12.0-s390x quay-mirror.qiniu.com/coreos/flannel:v0.12.0-s390x
# 然后再执行
kubectl apply -f kube-flannel.yml
kubectl get po -n kube-system
## 发现启动正常了。启动结果如下
[root@k8s-master01 ~]# kubectl get po -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-5c98db65d4-p8x8v 1/1 Running 0 62m
coredns-5c98db65d4-ts4qm 1/1 Running 0 62m
etcd-k8s-master01 1/1 Running 1 61m
kube-apiserver-k8s-master01 1/1 Running 1 61m
kube-controller-manager-k8s-master01 1/1 Running 1 61m
kube-flannel-ds-amd64-mwbcq 1/1 Running 0 27m
kube-proxy-hhvqh 1/1 Running 1 62m
kube-scheduler-k8s-master01 1/1 Running 1 61m
# 我们再次查看node的时候发现已经是ready状态了
[root@k8s-master01 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master01 Ready master 63m v1.15.1
主节点构建成功后会有一个日志文件kubeadm-init.log
cat kubeadm-init.log
#在文件的最后会发现下边这句话。我们就复制这个句话在各个节点进行执行
kubeadm join 192.168.70.128:6443 --token abcdef.0123456789abcdef
--discovery-token-ca-cert-hash sha256:a33be40c0ef657e7565b399e7b44ad27bdf6477fd54047c70fdd8e4ee24c2850
## 执行成功后回到主节点我们在查看节点,虽然还是not ready 我们只需要等一会就好了。
[root@k8s-master01 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master01 Ready master 70m v1.15.1
k8s-node01 NotReady 20s v1.15.1
k8s-node02 NotReady 17s v1.15.1
说明,如果长时间一直处于not ready状态可以用一下方法进行查看
# 1.查看发现另外两个node节点下载不下来kube-flannel-ds-amd64,使用我们上边的方法进行下载镜像重新tag
kubectl get pod -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-5c98db65d4-p8x8v 1/1 Running 1 22h 10.244.0.4 k8s-master01
coredns-5c98db65d4-ts4qm 1/1 Running 1 22h 10.244.0.5 k8s-master01
etcd-k8s-master01 1/1 Running 2 22h 192.168.70.128 k8s-master01
kube-apiserver-k8s-master01 1/1 Running 2 22h 192.168.70.128 k8s-master01
kube-controller-manager-k8s-master01 1/1 Running 2 22h 192.168.70.128 k8s-master01
kube-flannel-ds-amd64-2zls4 0/1 Init:ImagePullBackOff 0 6m39s 192.168.70.129 k8s-node01
kube-flannel-ds-amd64-75clq 1/1 Running 0 6m39s 192.168.70.128 k8s-master01
kube-flannel-ds-amd64-c6mhw 0/1 Init:ImagePullBackOff 0 6m39s 192.168.70.130 k8s-node02
kube-proxy-2m5rv 1/1 Running 1 21h 192.168.70.129 k8s-node01
kube-proxy-fwprm 1/1 Running 1 21h 192.168.70.130 k8s-node02
kube-proxy-hhvqh 1/1 Running 2 22h 192.168.70.128 k8s-master01
kube-scheduler-k8s-master01 1/1 Running 2 22h 192.168.70.128 k8s-master01
#2.还可以可以进入到对应的ndoe服务上 查看节点错误,也会发现镜像下不来,把镜像拉取下来就好了。
journalctl -f -u kubelet
4月 12 20:17:24 k8s-node01 kubelet[807]: E0412 20:17:24.020709 807 kuberuntime_manager.go:749] init container start failed: ImagePullBackOff: Back-off pulling image "quay-mirror.qiniu.com/coreos/flannel:v0.12.0-amd64"
4月 12 20:17:24 k8s-node01 kubelet[807]: E0412 20:17:24.020784 807 pod_workers.go:190] Error syncing pod 69a5f6b6-ed51-45ce-9658-2706f63beaba ("kube-flannel-ds-amd64-8mt8d_kube-system(69a5f6b6-ed51-45ce-9658-2706f63beaba)"), skipping: failed to "StartContainer" for "install-cni" with ImagePullBackOff: "Back-off pulling image \"quay-mirror.qiniu.com/coreos/flannel:v0.12.0-amd64\""
这样我们就完成了一个k8s集群搭建。接下来我么看是学习他的核心组件。撒花