Kubernates的安装

使用kubeadm安装Kubernetes 1.12

1. 系统配置

  • 准备三台机器
cat /etc/hosts

master 172.16.0.5
node1 172.16.0.6
node1 172.16.0.4

在三台机器上同样操作

  • 在各个节点禁用防火墙
systemctl stop firewalld
systemctl disable firewalld
  • 操作iptables

vi /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
  • 禁用SELINUX:
setenforce 0
vi /etc/selinux/config
SELINUX=disabled
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
  • 禁用 Swap
Swapoff -a

2. 在master上安装docker,kubectl

  • 使用阿里云的镜像

vim /etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
enabled=1
  • 导入key
wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
rpm --import rpm-package-key.gpg
  • 安装 docker kubelet kubeadm kubectl
yum install docker-ce-18.03.1.ce-1.el7.centos kubelet kubeadm kubectl
  • 给dockder设置阿里云的加速器, 这里也可以选用其他的加速器,感觉阿里云非常快
  • 设置docker代理和非代理

vim /usr/lib/systemd/system/docker.service,在[Service]下添加

[Service]
Environment="NO_PROXY=127.0.0.0/8,106.12.206.63/12"
  • 设置开机自启动
systemctl start docker
systemctl start docker kubectl

3. 提前下载k8s需要的docker镜像

vim pull_kube_images.sh

#!/bin/bash
images=(k8s-gcr-io-kube-apiserver:12.2.0 kube-controller-manager:12.2.0 kube-scheduler:12.2.0 kube-proxy:1.12.0 etcd:3.2.24 coredns:1.2.2 pause:3.1)

for imageName in ${images[@]};do
    docker pull mrright/$imageName
done

# kubeadm init --kubernetes-version=v1.12.2 --pod-network-cidr=10.244.0.0/16 service- cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
# [使用kubeadm安装Kubernetes 1.12](https://www.kubernetes.org.cn/4619.html)

docker pull jmgao1983/flannel:v0.10.0-amd64

docker tag jmgao1983/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
docker tag mrright/k8s-gcr-io-kube-apiserver:12.2.0 k8s.gcr.io/k8s-gcr-io-kube-apiserver:v1.12.0
docker tag mrright/kube-controller-manager:12.2.0 k8s.gcr.io/kube-controller-manager:v1.12.0
docker tag mrright/kube-scheduler:12.2.0 k8s.gcr.io/kube-scheduler:v1.12.0
docker tag mrright/kube-proxy:1.12.0 k8s.gcr.io/kube-proxy:v1.12.0
docker tag mrright/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2
docker tag mrright/pause:3.1 k8s.gcr.io/pause:3.1
docker tag mrright/etcd:3.2.24 k8s.gcr.io/etcd:3.2.24

执行 pull_kube_images.sh。 执行

sh pull_kube_images.sh

4.初始化master节点

kubeadm init   --kubernetes-version=v1.12.0   --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap

一定要记录下安装成功的日志

[root@instance-ayp8yd7t ~]# kubeadm init   --kubernetes-version=v1.12.0   --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12
[init] using Kubernetes version: v1.12.0
[preflight] running pre-flight checks
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.03.1-ce. Latest validated version: 18.06
[preflight/images] Pulling images required for setting up a Kubernetes cluster
[preflight/images] This might take a minute or two, depending on the speed of your internet connection
[preflight/images] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[preflight] Activating the kubelet service
[certificates] Generated ca certificate and key.
[certificates] Generated apiserver certificate and key.
[certificates] apiserver serving cert is signed for DNS names [instance-ayp8yd7t kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.16.0.5]
[certificates] Generated apiserver-kubelet-client certificate and key.
[certificates] Generated front-proxy-ca certificate and key.
[certificates] Generated front-proxy-client certificate and key.
[certificates] Generated etcd/ca certificate and key.
[certificates] Generated etcd/healthcheck-client certificate and key.
[certificates] Generated etcd/server certificate and key.
[certificates] etcd/server serving cert is signed for DNS names [instance-ayp8yd7t localhost] and IPs [127.0.0.1 ::1]
[certificates] Generated etcd/peer certificate and key.
[certificates] etcd/peer serving cert is signed for DNS names [instance-ayp8yd7t localhost] and IPs [172.16.0.5 127.0.0.1 ::1]
[certificates] Generated apiserver-etcd-client certificate and key.
[certificates] valid certificates and keys now exist in "/etc/kubernetes/pki"
[certificates] Generated sa key and public key.
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
[controlplane] wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml"
[controlplane] wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager.yaml"
[controlplane] wrote Static Pod manifest for component kube-scheduler to "/etc/kubernetes/manifests/kube-scheduler.yaml"
[etcd] Wrote Static Pod manifest for a local etcd instance to "/etc/kubernetes/manifests/etcd.yaml"
[init] waiting for the kubelet to boot up the control plane as Static Pods from directory "/etc/kubernetes/manifests"
[init] this might take a minute or longer if the control plane images have to be pulled
[apiclient] All control plane components are healthy after 23.002799 seconds
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.12" in namespace kube-system with the configuration for the kubelets in the cluster
[markmaster] Marking the node instance-ayp8yd7t as master by adding the label "node-role.kubernetes.io/master=''"
[markmaster] Marking the node instance-ayp8yd7t as master by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "instance-ayp8yd7t" as an annotation
[bootstraptoken] using token: t1gns8.718fr2w4v63qirkx
[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes master 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/

You can now join any number of machines by running the following on each node
as root:

  kubeadm join 172.16.0.5:6443 --token t1gns8.718fr2w4v63qirkx --discovery-token-ca-cert-hash sha256:485ad8654b8b6933b845df60d4d1315ecc060dffb0e1e2d61c9bef11c843ca0f
  • 按照提示继续完成以下
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

到这里mster节点就部署完成了, 后面将node加入集群

  • 在master上依次执行
scp /usr/lib/systemd/system/docker.service node1:/usr/lib/systemd/system/docker.service
scp /etc/sysconfig/kubelet node1:/etc/sysconfig/
scp /etc/docker/daemon.json node1:/etc/docker/daemon.json

scp pull_kube_images.sh node1:/root/
scp rpm-package-key.gpg node1:/root/


scp /usr/lib/systemd/system/docker.service node2:/usr/lib/systemd/system/docker.service
scp /etc/sysconfig/kubelet node2:/etc/sysconfig/
scp /etc/docker/daemon.json node2:/etc/docker/daemon.json

scp pull_kube_images.sh node2:/root/
scp rpm-package-key.gpg node2:/root/

在node1,node2上

  • 导入key
rpm --import rpm-package-key.gpg
  • 安装 docker kubelet kubeadm kubectl
yum install docker-ce-18.03.1.ce-1.el7.centos kubelet kubeadm kubectl
  • 给dockder设置阿里云的加速器
systemctl start docker
systemctl enable docker kubelet
  • 下载镜像
sh pull_kube_images.sh
  • 加入集群

就是在maste初始化好时记录的最后的日志,复制出来执行一下

kubeadm join 172.16.0.5:6443 --token t1gns8.718fr2w4v63qirkx --discovery-token-ca-cert-hash sha256:485ad8654b8b6933b845df60d4d1315ecc060dffb0e1e2d61c9bef11c843ca0f

ok,集群搭建成功 ,使用kubectl get nodes 查看集群节点状况

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,194评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,058评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,780评论 0 346
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,388评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,430评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,764评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,907评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,679评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,122评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,459评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,605评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,270评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,867评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,734评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,961评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,297评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,472评论 2 348

推荐阅读更多精彩内容