kubeadm安装Kubernetes集群

一、Kubernetes简介

Kubernetes(简称K8S)是开源的容器集群管理系统,可以实现容器集群的自动化部署、自动扩缩容、维护等功能。它既是一款容器编排工具,也是全新的基于容器技术的分布式架构领先方案。在Docker技术的基础上,为容器化的应用提供部署运行、资源调度、服务发现和动态伸缩等功能,提高了大规模容器集群管理的便捷性。

K8S集群中有管理节点与工作节点两种类型。管理节点主要负责K8S集群管理,集群中各节点间的信息交互、任务调度,还负责容器、Pod、NameSpaces、PV等生命周期的管理。工作节点主要为容器和Pod提供计算资源,Pod及容器全部运行在工作节点上,工作节点通过kubelet服务与管理节点通信以管理容器的生命周期,并与集群其他节点进行通信。

架构图

二、环境准备

Kubernetes支持在物理服务器或虚拟机中运行,本次使用阿里云云服务器ECS,硬件配置信息如表所示:

node.png

注:在所有节点上进行如下操作

1.关闭防火墙、selinux和swap。

systemctl stop firewalld
systemctl disable firewalld
setenforce 0 #实时动态关闭
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #禁止重启后自动开启
swapoff -a #实时动态关闭
sed -i '/ swap / s/^/#/' /etc/fstab #禁止重启后自动开启

2.配置内核参数,将桥接的IPv4流量传递到iptables的链

cat <<EOF >/etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF

modprobe br_netfilter  #执行该命令 如果不执行就会在应用k8s.conf时出现加载错误

sysctl -p /etc/sysctl.d/k8s.conf #应用配置文件

3.配置国内yum源

mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

4.配置国内Kubernetes源

cat <<EOF > /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

5.主机时间同步

yum install -y ntp
systemctl enable ntpd && systemctl start ntpd
cat <<EOF >/etc/ntp.conf 
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server ntp.aliyun.com iburst
#server  127.127.1.0  iburst     local  clock
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
EOF
systemctl restart ntpd

三、软件安装

注:在所有节点上进行如下操作

1.安装docker

# step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装Docker-CE
yum makecache fast
yum -y install docker-ce-18.03.1.ce-1.el7.centos
# Step 4: 开启Docker服务
systemctl start docker
# Step 5: 开机自启
systemctl enable docker

docker服务为容器运行提供计算资源,是所有容器运行的基本平台。

2.安装kubeadm、kubelet、kubectl

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable kubelet

Kubelet负责与其他节点集群通信,并进行本节点Pod和容器生命周期的管理。Kubeadm是Kubernetes的自动化部署工具,降低了部署难度,提高效率。Kubectl是Kubernetes集群管理工具。

四、部署master 节点

注:在master节点上进行如下操作

获取默认的初始化参数文件

kubeadm config print init-defaults > init.default.yaml

修改初始化参数文件

advertiseAddress: 172.22.207.108
imageRepository: registry.aliyuncs.com/google_containers
controlPlaneEndpoint: "172.22.207.108:6443"
podSubnet: 192.168.0.0/16

查看和拉取K8S集群需要的镜像

kubeadm config images list --config=init.default.yaml
kubeadm config images pull --config=init.default.yaml

运行kubeadm init命令安装master

kubeadm init --config=init.default.yaml
W0827 23:08:14.128814    2282 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.0
[preflight] Running pre-flight checks
        [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.03.1-ce. Latest validated version: 19.03
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [iz2zeb5993exopp7ser0cmz kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.22.207.108]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [iz2zeb5993exopp7ser0cmz localhost] and IPs [172.22.207.108 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [iz2zeb5993exopp7ser0cmz localhost] and IPs [172.22.207.108 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
W0827 23:08:17.513849    2282 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[control-plane] Creating static Pod manifest for "kube-scheduler"
W0827 23:08:17.515440    2282 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 20.501502 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node iz2zeb5993exopp7ser0cmz as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node iz2zeb5993exopp7ser0cmz as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: abcdef.0123456789abcdef
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[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 172.22.207.108:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:8ee560a10e7e6d5476f7a2962dabb387088523a56799461bb444a4b09dc4eaad

复制配置文件到home目录下

mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

查看节点

[root@iZ2zeb5993exopp7ser0cmZ ~]# kubectl get node
NAME                      STATUS     ROLES    AGE    VERSION
iz2zeb5993exopp7ser0cmz   NotReady   master   119s   v1.18.8
[root@iZ2zeb5993exopp7ser0cmZ ~]# kubectl get pod -n kube-system
NAME                                              READY   STATUS    RESTARTS   AGE
coredns-7ff77c879f-mbwcx                          0/1     Pending   0          2m17s
coredns-7ff77c879f-pxtj7                          0/1     Pending   0          2m17s
etcd-iz2zeb5993exopp7ser0cmz                      1/1     Running   0          2m27s
kube-apiserver-iz2zeb5993exopp7ser0cmz            1/1     Running   0          2m27s
kube-controller-manager-iz2zeb5993exopp7ser0cmz   1/1     Running   0          2m27s
kube-proxy-srjc5                                  1/1     Running   0          2m17s
kube-scheduler-iz2zeb5993exopp7ser0cmz            1/1     Running   0          2m27s

node为NotReady,因为还没有部署网络插件

安装calico网络插件

curl https://docs.projectcalico.org/manifests/calico-etcd.yaml -o calico.yaml

编辑calico.yaml

calico1.png

修改后

calico2.png
- name: CALICO_IPV4POOL_IPIP
  value: "Never"
- name: CALICO_IPV4POOL_CIDR
  value: "192.168.0.0/16"

创建calico插件,建议使用阿里云镜像加速器

kubectl create -f calico.yaml

node为Ready

[root@iZ2zeb5993exopp7ser0cmZ ~]# kubectl get node
NAME                      STATUS   ROLES    AGE   VERSION
iz2zeb5993exopp7ser0cmz   Ready    master   21m   v1.18.8
[root@iZ2zeb5993exopp7ser0cmZ ~]# kubectl get pod -n kube-system
NAME                                              READY   STATUS    RESTARTS   AGE
calico-kube-controllers-6f8bf57ff-tvmnx           1/1     Running   0          43s
calico-node-p29c8                                 1/1     Running   0          43s
coredns-7ff77c879f-mbwcx                          1/1     Running   0          21m
coredns-7ff77c879f-pxtj7                          1/1     Running   0          21m
etcd-iz2zeb5993exopp7ser0cmz                      1/1     Running   0          21m
kube-apiserver-iz2zeb5993exopp7ser0cmz            1/1     Running   0          21m
kube-controller-manager-iz2zeb5993exopp7ser0cmz   1/1     Running   0          21m
kube-proxy-srjc5                                  1/1     Running   0          21m
kube-scheduler-iz2zeb5993exopp7ser0cmz            1/1     Running   0          21m

打包证书

cd /etc/kubernetes && tar cvzf k8s-key.tgz pki/ca.* pki/sa.* pki/front-proxy-ca.* pki/etcd/ca.*

部署第二个master节点(第三个一样)

解压证书

tar -zxvf k8s-key.tgz -C /etc/kubernetes/

加入节点

kubeadm join 172.22.207.108:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:8ee560a10e7e6d5476f7a2962dabb387088523a56799461bb444a4b09dc4eaad --control-plane

加入node节点(无需复制证书)

kubeadm join 172.22.207.108:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:8ee560a10e7e6d5476f7a2962dabb387088523a56799461bb444a4b09dc4eaad

查看节点状态

[root@iZ2zeb5993exopp7ser0cmZ kubernetes]# kubectl get node
NAME                      STATUS   ROLES    AGE     VERSION
iz2zeb5993exopp7ser0cjz   Ready    master   3m42s   v1.18.8
iz2zeb5993exopp7ser0ckz   Ready    <none>   77s     v1.18.8
iz2zeb5993exopp7ser0clz   Ready    master   2m28s   v1.18.8
iz2zeb5993exopp7ser0cmz   Ready    master   55m     v1.18.8
[root@iZ2zeb5993exopp7ser0cmZ kubernetes]# kubectl get pod -n kube-system
NAME                                              READY   STATUS    RESTARTS   AGE
calico-kube-controllers-6f8bf57ff-tvmnx           1/1     Running   0          34m
calico-node-p29c8                                 1/1     Running   0          34m
calico-node-pwvpg                                 1/1     Running   0          2m29s
calico-node-xr67w                                 1/1     Running   0          78s
calico-node-zwdkf                                 1/1     Running   0          3m43s
coredns-7ff77c879f-mbwcx                          1/1     Running   0          55m
coredns-7ff77c879f-pxtj7                          1/1     Running   0          55m
etcd-iz2zeb5993exopp7ser0cjz                      1/1     Running   0          3m41s
etcd-iz2zeb5993exopp7ser0clz                      1/1     Running   0          2m27s
etcd-iz2zeb5993exopp7ser0cmz                      1/1     Running   0          55m
kube-apiserver-iz2zeb5993exopp7ser0cjz            1/1     Running   0          3m42s
kube-apiserver-iz2zeb5993exopp7ser0clz            1/1     Running   0          2m28s
kube-apiserver-iz2zeb5993exopp7ser0cmz            1/1     Running   0          55m
kube-controller-manager-iz2zeb5993exopp7ser0cjz   1/1     Running   0          3m41s
kube-controller-manager-iz2zeb5993exopp7ser0clz   1/1     Running   0          2m27s
kube-controller-manager-iz2zeb5993exopp7ser0cmz   1/1     Running   0          55m
kube-proxy-4dpqk                                  1/1     Running   0          78s
kube-proxy-srjc5                                  1/1     Running   0          55m
kube-proxy-tkdpt                                  1/1     Running   0          3m43s
kube-proxy-zzb6p                                  1/1     Running   0          2m29s
kube-scheduler-iz2zeb5993exopp7ser0cjz            1/1     Running   0          3m41s
kube-scheduler-iz2zeb5993exopp7ser0clz            1/1     Running   0          2m27s
kube-scheduler-iz2zeb5993exopp7ser0cmz            1/1     Running   0          55m
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342