使用kubeadm快速搭建K8S 1.16.0集群

前提:

1、操作系统Centos 7.4
2、通过阿里云相关镜像源安装

操作步骤:

1、修改主机名

hostnamectl set-hostname hanyu1

vi /etc/resolv.conf

nameserver 10.150.0.251

2、安装docker

安装必要的工具

yum install -y yum-utils device-mapper-persistent-data lvm2

配置官方docker源或者aliyun docker源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

或者

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

查询docker版本(否则yum install默认安装最新的版本)

yum list docker-ce --showduplicates | sort -r

安装指定版本docker

yum -y install docker-ce-18.09.3-3.el7

启动docker

systemctl enable docker && systemctl start docker

查询docker服务状态

systemctl status docker

查看docker版本

docker version

安装kubelet、kubeadm 和 kubectl

配置kubernetes.repo的源,由于官方源国内无法访问,这里使用阿里云yum源

[root@hanyu1 ~]# 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

在所有节点上安装指定版本 kubelet、kubeadm 和 kubectl(这里我安装1.16.0,目前最新版本)

yum list kubectl --showduplicates | sort -r
yum list kubeadm --showduplicates | sort -r
yum list kubelet --showduplicates | sort -r
yum install -y kubectl-1.16.0-0 kubeadm-1.16.0-0 kubelet-1.16.0-0

启动kubelet服务

systemctl enable kubelet && systemctl start kubelet

关闭swap分区

swapoff -a

初始化master节点

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.0 --pod-network-cidr=10.244.0.0/16 --token-ttl 0

[root@hanyu1 ~]# kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.0 --pod-network-cidr=10.244.0.0/16 --token-ttl 0
[init] Using Kubernetes version: v1.16.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 Hostname]: hostname "hanyu1" could not be reached
[WARNING Hostname]: hostname "hanyu1": lookup hanyu1 on 10.150.0.251:53: server misbehaving
[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] Activating the kubelet service
[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 [hanyu1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.20.0.210]
[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 [hanyu1 localhost] and IPs [10.20.0.210 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [hanyu1 localhost] and IPs [10.20.0.210 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"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[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 36.003104 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.16" 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 hanyu1 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node hanyu1 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 4fz55x.x25v0mpq7370stvb
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[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
[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 10.20.0.210:6443 --token 4fz55x.x25v0mpq7370stvb --discovery-token-ca-cert-hash sha256:4342810abb3e5854bc936ef87bd975632754177a726c781ee471a92ddcb856d2

按照kubeadm init成功后打印提示,继续操作:

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

kubectl get nodes查询到节点处于NotReady状态,是因为网络插件还未就位,也就是这里要求运行的

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/

如果要暂时忽略让节点Ready,将如下文件的--network-plugin=cni字段去掉, (该文件是在kubeadm init或者kubeadm join过程中生成的), 修改完后重启kubelet即可(systemctl restart kubelet)

[root@hanyu2 ~]# cat /var/lib/kubelet/kubeadm-flags.env
KUBELET_KUBEADM_ARGS="--cgroup-driver=cgroupfs --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.1"

join node

[root@hanyu2 ~]# kubeadm join 10.20.0.210:6443 --token 4fz55x.x25v0mpq7370stvb --discovery-token-ca-cert-hash sha256:4342810abb3e5854bc936ef87bd975632754177a726c781ee471a92ddcb856d2
[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 Hostname]: hostname "hanyu2" could not be reached
[WARNING Hostname]: hostname "hanyu2": lookup hanyu2 on 10.150.0.251:53: server misbehaving
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.16" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
Certificate signing request was sent to apiserver and a response was received.
The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

查询所有node及pod状态

[root@hanyu1 ~]# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
hanyu1 Ready master 5d22h v1.16.0 10.20.0.210 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu2 Ready <none> 5d22h v1.16.0 10.20.0.212 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu3 Ready <none> 5d19h v1.16.0 10.20.0.213 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu4 Ready <none> 5d18h v1.16.0 10.20.0.214 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu5 Ready <none> 4h9m v1.16.0 10.20.0.215 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu6 Ready <none> 4h9m v1.16.0 10.20.0.211 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3

[root@hanyu1 ~]# kubectl get pod --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-58cc8c89f4-nx8p7 1/1 Running 3 5d22h 172.17.0.2 hanyu1 <none> <none>
kube-system coredns-58cc8c89f4-tpd9h 1/1 Running 3 5d22h 172.17.0.3 hanyu1 <none> <none>
kube-system etcd-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1 <none> <none>
kube-system kube-apiserver-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1 <none> <none>
kube-system kube-controller-manager-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1 <none> <none>
kube-system kube-proxy-cd6lb 1/1 Running 1 5d18h 10.20.0.214 hanyu4 <none> <none>
kube-system kube-proxy-grp6n 1/1 Running 1 5d19h 10.20.0.213 hanyu3 <none> <none>
kube-system kube-proxy-kthlt 1/1 Running 0 4h11m 10.20.0.211 hanyu6 <none> <none>
kube-system kube-proxy-vpx6w 1/1 Running 1 5d22h 10.20.0.212 hanyu2 <none> <none>
kube-system kube-proxy-wqgrc 1/1 Running 0 4h11m 10.20.0.215 hanyu5 <none> <none>
kube-system kube-proxy-xvtjc 1/1 Running 3 5d22h 10.20.0.210 hanyu1 <none> <none>
kube-system kube-scheduler-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1 <none> <none>

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

推荐阅读更多精彩内容