centos7上使用kubeadm创建Kubernetes1.10集群

Kubernetes,简称 k8s(k,8 个字符,s——明白了?)或者 “kube”,是一个开源的 Linux 容器自动化运维平台,它消除了容器化应用程序在部署、伸缩时涉及到的许多手动操作。换句话说,你可以将多台主机组合成集群来运行 Linux 容器,而 Kubernetes 可以帮助你简单高效地管理那些集群。构成这些集群的主机还可以跨越公有云私有云以及混合云。目前已经是容器编排的标准,背后主要有google和红帽支持。

kubeadm是Kubernetes官方提供的快速搭建k8s集群的工具,比目前网上使用其他方法搭建要简单快速的多,而且碰都的问题也少。如果大家按照本文操作出现任何问题,请留言,我会尽量及时回复。本文主要是参考的Kubernetes官方文档Using kubeadm to Create a Cluster。按照本文搭建的集群不够安全,也做不到高可用,仅建议个人学习研究用途,不建议部署至生产环境。

前提条件

1. 2台以上安装了centos7 x64的服务器,可以是物理机,虚拟机或者vps。其中一台作为master节点,其他的作为node节点。

2. 每台机器至少2g内存,作为master的服务器至少要2个核。

3. 所有服务器之间的网络是互通的,hostname不能相同,并且不含有下划线。

4. 服务器是在墙外的,因为搭建的过程中要下载的一些文件,墙内可能会很慢或者根本下载不了,这意味着使用国内的阿里云,腾讯云之类的vps搭建会很麻烦。如果还没有墙外的服务器,可以去看下vultr,我用的就是这个,性价比比较高,也很稳定,   推荐使用东京或美国西部的节点,连接比较快,有时ip会ping不通,应该是被墙了,在其他区域再创建一个再试就ok了。

5. 会用ssh连接服务器,并能执行简单的命令,以及编辑保存文件。下文的命令有些可能需要root权限,如果提示没有权限,在命令行前面加sudo再执行一次。

搭建步骤

第1-6步是每台服务器都需要的。

升级系统,在命令行运行

yum update -y

     2. 关闭防火墙,swap,因为k8s需要运行多个服务在不同的服务器上通讯,需要开放多个端口,简单起见,直接把防火墙关了,不推荐在生产环境这么做。关掉swap,k8s的组件kebelet才可以正常工作。

systemctl disable firewalld

systemctl stop firewalld

swapoff -a    

  3.安装docker

yum install -y docker

systemctl enable docker && systemctl start docker

    4.安装kubeadm,kubelet,kubectl

cat < /etc/yum.repos.d/kubernetes.repo

[kubernetes]

name=Kubernetes

baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch

enabled=1

gpgcheck=1

repo_gpgcheck=1

gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

EOF

setenforce 0

yum install -y kubelet kubeadm kubectl

systemctl enable kubelet && systemctl start kubelet

5.关闭selinux,因为kubelet目前支持selinux还有点问题

setenforce 0

打开/etc/sysconfig/selinux文件

vi /etc/sysconfig/selinux

找到SELINUX那行,改为

SELINUX=disabled

保存文件

    6.设置net.bridge.bridge-nf-call-iptables为1

cat <  /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

EOF

sysctl --system

    7.初始化master,在master的节点上运行

kubeadm init --pod-network-cidr=192.168.0.0/16

如果你看到类似下面的信息说明master初始化成功了

[init] Using Kubernetes version: v1.8.0

[init] Using Authorization modes: [Node RBAC]

[preflight] Running pre-flight checks

[kubeadm] WARNING: starting in 1.8, tokens expire after 24 hours by default (if you require a non-expiring token use --token-ttl 0)

[certificates] Generated ca certificate and key.

[certificates] Generated apiserver certificate and key.

[certificates] apiserver serving cert is signed for DNS names [kubeadm-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.138.0.4]

[certificates] Generated apiserver-kubelet-client certificate and key.

[certificates] Generated sa key and public key.

[certificates] Generated front-proxy-ca certificate and key.

[certificates] Generated front-proxy-client certificate and key.

[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"

[kubeconfig] Wrote KubeConfig file to disk: "admin.conf"

[kubeconfig] Wrote KubeConfig file to disk: "kubelet.conf"

[kubeconfig] Wrote KubeConfig file to disk: "controller-manager.conf"

[kubeconfig] Wrote KubeConfig file to disk: "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 often takes around a minute; or longer if the control plane images have to be pulled.

[apiclient] All control plane components are healthy after 39.511972 seconds

[uploadconfig] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace

[markmaster] Will mark node master as master by adding a label and a taint

[markmaster] Master master tainted and labelled with key/value: node-role.kubernetes.io/master=""

[bootstraptoken] Using token:

[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] Creating the "cluster-info" ConfigMap in the "kube-public" namespace

[addons] Applied essential addon: kube-dns

[addons] Applied essential addon: kube-proxy

Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run (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:

  http://kubernetes.io/docs/admin/addons/

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

as root:

  kubeadm join --token : --discovery-token-ca-cert-hash sha256:

把上面输出的最后一行 kubeadm join复制保存下来,后面在node节点加入到集群中需要用到

运行下面的命令初始化kebectl配置文件

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

    8.安装网络插件,以使pod能相互通讯,这里我们安装的是Calico.在master节点运行

kubectl apply -f https://docs.projectcalico.org/v3.0/getting-started/kubernetes/installation/hosted/kubeadm/1.7/calico.yaml

运行以下命令检查kube-dns pod 已经运行,一般需要几十秒

kubectl get pods --all-namespaces

如果输出中有名字以kube-dns的pod状态是Running,说明网络插件已经正常工作,然后就可以把node节点加入到集群

[root@kube-master ~]# kubectl get pods --all-namespaces

NAMESPACE    NAME                                      READY    STATUS    RESTARTS  AGE

kube-system  calico-etcd-dfpnn                        1/1      Running  0          13h

kube-system  calico-kube-controllers-5449fdfcd-z8n45  1/1      Running  0          13h

kube-system  calico-node-8jmzt                        2/2      Running  0          13h

kube-system  calico-node-b4x99                        2/2      Running  0          13h

kube-system  etcd-kube-master                          1/1      Running  0          13h

kube-system  kube-apiserver-kube-master                1/1      Running  0          13h

kube-system  kube-controller-manager-kube-master      1/1      Running  0          13h

kube-system  kube-dns-86f4d74b45-v6qr5                3/3      Running  0          14h

kube-system  kube-proxy-8nl2w                          1/1      Running  0          13h

kube-system  kube-proxy-klnjb                          1/1      Running  0          14h

kube-system  kube-scheduler-kube-master                1/1      Running  0          13h

    9.使pod能运行在master上,在master运行如下命令。否则k8s不会调度非系统pod到master节点上

kubectl taint nodes --all node-role.kubernetes.io/master-

 10.kube-dns运行后加入node节点,在node节点运行第7步保存的kubeadm join,类似下面的语句。

kubeadm join --token : --discovery-token-ca-cert-hash sha256:

如果成功,输出类似下面

[preflight] Running pre-flight checks

[discovery] Trying to connect to API Server "10.138.0.4:6443"

[discovery] Created cluster-info discovery client, requesting info from "https://10.138.0.4:6443"

[discovery] Requesting info from "https://10.138.0.4:6443" again to validate TLS against the pinned public key

[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "10.138.0.4:6443"

[discovery] Successfully established connection with API Server "10.138.0.4:6443"

[bootstrap] Detected server version: v1.8.0

[bootstrap] The server supports the Certificates API (certificates.k8s.io/v1beta1)

[csr] Created API client to obtain unique certificate for this node, generating keys and certificate signing request

[csr] Received signed certificate from the API server, generating KubeConfig...

Node join complete:

* Certificate signing request sent to master and response

  received.

* Kubelet informed of new secure connection details.

Run 'kubectl get nodes' on the master to see this machine join.

   11.验证node成功加入集群,在master命令行运行

kubectl get node

如果集群正常运行,输出类似

NAME          STATUS    ROLES    AGE      VERSION

kube-master  Ready    master    1h        v1.10.0

kube-node    Ready        2m        v1.10.0

恭喜你,已经拥有了一个可用的k8s集群!

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

推荐阅读更多精彩内容