部署Kubernetes Cluster

机器规划

k8s master:  172.26.237.103
k8s node1:172.26.237.105
k8s node2: 172.26.237.103

修改hosts文件

[root@k8s001 .ssh]# vi /etc/hosts
::1     localhost       localhost.localdomain   localhost6      localhost6.localdomain6
127.0.0.1       localhost       localhost.localdomain   localhost4      localhost4.localdomain4

172.26.237.103  k8s001  k8s001
172.26.237.105  k8s002  k8s002
172.26.237.104  k8s003  k8s003

安装Docker

见[https://www.jianshu.com/writer#/notebooks/31084596/notes/36288645]

安装kubelet、kubeadm和kubectl

1、添加源

[root@k8s001 soft]# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
> [kubernetes]
> name=Kubernetes
> baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=1
> repo_gpgcheck=1
> gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> exclude=kube*
> EOF

2、安装

[root@k8s001 soft]# yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
[root@k8s001 soft]# systemctl enable kubelet && systemctl start kubelet

3、修改网络配置

[root@k8s001 soft]# cat <<EOF >  /etc/sysctl.d/k8s.conf
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@k8s001 soft]# sysctl --system

初始化Master

[root@k8s001 soft]# kubeadm config print init-defaults > kubeadm-init.yaml

修改ip和image仓库地址

advertiseAddress: 172.26.237.103
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers

下载镜像

[root@k8s001 soft]# kubeadm config images pull --config kubeadm-init.yaml

下载镜像报错

failed to pull image "registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0": output: Error response from daemon: pull access denied for registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns, repository does not exist or may require 'docker login'
, error: exit status 1
To see the stack trace of this error execute with --v=5 or higher

手动下载镜像

[root@k8s001 soft]# docker pull coredns/coredns
Using default tag: latest
latest: Pulling from coredns/coredns

查看kubeadm需要镜像,并修改名称

[root@k8s001 soft]# kubeadm config images list --config kubeadm-init.yaml
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0
[root@k8s001 soft]# docker tag coredns/coredns:latest registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0 
[root@k8s001 soft]# docker images
REPOSITORY                                                                    TAG                 IMAGE ID            CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver            v1.21.0             4d217480042e        4 weeks ago         126MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy                v1.21.0             38ddd85fe90e        4 weeks ago         122MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler            v1.21.0             62ad3129eca8        4 weeks ago         50.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager   v1.21.0             09708983cc37        4 weeks ago         120MB
coredns/coredns                                                               latest              3885a5b7f138        2 months ago        43.5MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns           v1.8.0              3885a5b7f138        2 months ago        43.5MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause                     3.4.1               0f8457a4c2ec        3 months ago        683kB
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd                      3.4.13-0            0369cf4303ff        8 months ago        253MB

然后删除多余镜像

[root@k8s001 soft]# docker rmi coredns/coredns:latest
Untagged: coredns/coredns:latest
Untagged: coredns/coredns@sha256:642ff9910da6ea9a8624b0234eef52af9ca75ecbec474c5507cb096bdfbae4e5
[root@k8s001 soft]# docker images
REPOSITORY                                                                    TAG                 IMAGE ID            CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver            v1.21.0             4d217480042e        4 weeks ago         126MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy                v1.21.0             38ddd85fe90e        4 weeks ago         122MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager   v1.21.0             09708983cc37        4 weeks ago         120MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler            v1.21.0             62ad3129eca8        4 weeks ago         50.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns           v1.8.0              3885a5b7f138        2 months ago        43.5MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause                     3.4.1               0f8457a4c2ec        3 months ago        683kB
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd                      3.4.13-0            0369cf4303ff        8 months ago        253MB

执行初始化

[root@k8s001 soft]# kubeadm init --config kubeadm-init.yaml
[init] Using Kubernetes version: v1.21.0
[preflight] Running pre-flight checks
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!
Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 172.26.237.103:6443 --token abcdef.0123456789abcdef \
        --discovery-token-ca-cert-hash sha256:efdcd7f0265b9b2669b9590767cf95828c92d630127a155e46b5d90133900d3b 

初始化环境,使当前用户可以使用kubectl

[root@k8s001 soft]# mkdir -p $HOME/.kube
[root@k8s001 soft]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s001 soft]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s001 soft]# kubectl get node
NAME   STATUS     ROLES                  AGE    VERSION
node   NotReady   control-plane,master   2m8s   v1.21.0

配置网络

[root@k8s001 soft]# wget https://docs.projectcalico.org/v3.8/manifests/calico.yaml
[root@k8s001 soft]# cat kubeadm-init.yaml | grep serviceSubnet:
  serviceSubnet: 10.96.0.0/12
#192.168.0.0/16修改为10.96.0.0/12
[root@k8s001 soft]# sed -i 's/192.168.0.0\/16/10.96.0.0\/12/g' calico.yaml
#初始化网络
[root@k8s001 soft]# kubectl apply -f calico.yaml
#再查看
[root@k8s001 soft]# kubectl get node
NAME   STATUS   ROLES                  AGE   VERSION
node   Ready    control-plane,master   30m   v1.21.0

添加node节点

[root@k8s002 ~]# kubeadm join 172.26.237.103:6443 --token abcdef.0123456789abcdef \
>         --discovery-token-ca-cert-hash sha256:efdcd7f0265b9b2669b9590767cf95828c92d630127a155e46b5d90133900d3b
[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/
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[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] Starting the kubelet
[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.

查看节点

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

推荐阅读更多精彩内容