CentOS7使用kubeadm安装k8s

CentOS7使用kubeadm安装k8s


0.前言

文章使用的k8s版本为1.10.0

系统为CentOS7

总共有三台机器:

Name IP Role
ceph1 172.16.32.70 Master
ceph2 172.16.32.107 Node
ceph3 172.16.32.71 Node

1.准备工作

Master和Node中都需要操作的:

  • 环境问题

    • 添加hostname

      [root@ceph1 ~]# cat /etc/hosts
      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      
      172.16.32.70 ceph1
      172.16.32.107 ceph2
      172.16.32.71 ceph3
      
    • 关闭防火墙

      systemctl stop firewalld

      systemctl disable firewalld

    • 禁用SELINUX

      sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux

      setenforce 0

      这里/etc/sysconfig/selinux中SELINUX=enforcing这一项不一定默认是这个,也可能是permissive,最好就是打开文件进行修改为disabled。

    • 关闭swap

      swapoff -a

      上述命令在系统重启后会失效,可以修改配置文件达到永久关闭swap:

      打开/etc/fstab,注释掉有关swap的那一句:

      /dev/mapper/centos-swap swap swap defaults 0 0

    • 设置系统路由参数,创建/etc/sysctl.d/k8s.conf ,添加以下内容

      net.bridge.bridge-nf-call-ip6tables = 1
      net.bridge.bridge-nf-call-iptables = 1
      net.ipv4.ip_forward = 1
      

      添加完成后,执行以下命令使修改能生效:

      modprobe br_netfilter

      sysctl -p /etc/sysctl.d/k8s.conf

  • 安装docker

    • 安装命令

      yum install -y docker
      systemctl start docker
      systemctl enable docker
      
    • 添加docker加速器和私有镜像仓库地址

      [root@ceph1 ~]# cat /etc/docker/daemon.json 
      {
        "registry-mirrors": ["https://p7t6rz3f.mirror.aliyuncs.com"],
        "insecure-registries": ["172.16.32.48:5000"]
      }
      
    • 重新加载启动docker服务

      systemctl daemon-reload
      systemctl restart docker 
      
  • 安装kubeadm,kubelet,kubectl

    • 添加阿里云源

      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=0
      repo_gpgcheck=0
      gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
              http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
      EOF
      
    • 开始安装

      yum makecache fast && yum install -y kubelet-1.10.0-0 kubeadm-1.10.0-0 kubectl-1.10.0-0

    • 配置kubelet

      1.首先查看docker的Cgroup类型,docker info |grep Cgroup ,kubelet配置文件的**KUBELET_CGROUP_ARGS=--cgroup-driver=xxx **需要与docker的一致,如果不一致就修改。

      2.添加一句Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"

      原因是:更改系统要关闭swap这个限制。

      3.重新加载服务systemctl daemon-reload


2.开始集群安装

  • 由于无法拉取k8s.gcr.io的镜像,需要本地先拉取,再修改tag

    将拉取镜像并修改tag的操作写入到一个shell脚本里:

    master:

    #!/bin/bash
    
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/etcd-amd64:3.1.12
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/flannel:v0.10.0-amd64
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-apiserver-amd64:v1.10.0
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-scheduler-amd64:v1.10.0
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-controller-manager-amd64:v1.10.0
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-proxy-amd64:v1.10.0
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/k8s-dns-kube-dns-amd64:1.14.8
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/k8s-dns-dnsmasq-nanny-amd64:1.14.8
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/k8s-dns-sidecar-amd64:1.14.8
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/pause-amd64:3.1
    
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/etcd-amd64:3.1.12 k8s.gcr.io/etcd-amd64:3.1.12
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-apiserver-amd64:v1.10.0 k8s.gcr.io/kube-apiserver-amd64:v1.10.0
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-scheduler-amd64:v1.10.0 k8s.gcr.io/kube-scheduler-amd64:v1.10.0
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-controller-manager-amd64:v1.10.0 k8s.gcr.io/kube-controller-manager-amd64:v1.10.0
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-proxy-amd64:v1.10.0 k8s.gcr.io/kube-proxy-amd64:v1.10.0
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/k8s-dns-kube-dns-amd64:1.14.8 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.8
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/k8s-dns-dnsmasq-nanny-amd64:1.14.8 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.8
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/k8s-dns-sidecar-amd64:1.14.8 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.8
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1
    

    node:

    #!/bin/bash
    
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-proxy-amd64:v1.10.0
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/flannel:v0.10.0-amd64
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/pause-amd64:3.1
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kubernetes-dashboard-amd64:v1.8.3
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/heapster-influxdb-amd64:v1.3.3
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/heapster-grafana-amd64:v4.4.3
    docker pull registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/heapster-amd64:v1.5.3
    
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kube-proxy-amd64:v1.10.0 k8s.gcr.io/kube-proxy-amd64:v1.10.0
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/kubernetes-dashboard-amd64:v1.8.3 k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/heapster-influxdb-amd64:v1.3.3 k8s.gcr.io/heapster-influxdb-amd64:v1.3.3
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/heapster-grafana-amd64:v4.4.3 k8s.gcr.io/heapster-grafana-amd64:v4.4.3
    docker tag registry.cn-hangzhou.aliyuncs.com/zhanye-zhang/heapster-amd64:v1.5.3 k8s.gcr.io/heapster-amd64:v1.5.3
    
  • 在Master执行初始化

    kubeadm init --kubernetes-version=v1.10.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=172.16.32.70

    这里的参数:

    --kubernetes-version 集群版本

    --pod-network-cidr Pod网段(后续使用flannel作为网络插件)

    --apiserver-advertise-address apiserver的通信地址(Master的IP地址)

    等待几分钟,就可以安装完成。。提示信息如下:

    [root@localhost init]# kubeadm init --kubernetes-version=v1.10.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=172.16.32.70
    [init] Using Kubernetes version: v1.10.0
    [init] Using Authorization modes: [Node RBAC]
    [preflight] Running pre-flight checks.
            [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
            [WARNING FileExisting-crictl]: crictl not found in system path
    Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
    [preflight] Starting the kubelet service
    [certificates] Generated ca certificate and key.
    [certificates] Generated apiserver certificate and key.
    [certificates] apiserver serving cert is signed for DNS names [ceph1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.16.32.70]
    [certificates] Generated apiserver-kubelet-client certificate and key.
    [certificates] Generated etcd/ca certificate and key.
    [certificates] Generated etcd/server certificate and key.
    [certificates] etcd/server serving cert is signed for DNS names [localhost] and IPs [127.0.0.1]
    [certificates] Generated etcd/peer certificate and key.
    [certificates] etcd/peer serving cert is signed for DNS names [ceph1] and IPs [172.16.32.70]
    [certificates] Generated etcd/healthcheck-client certificate and key.
    [certificates] Generated apiserver-etcd-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: "/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.003060 seconds
    [uploadconfig] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
    [markmaster] Will mark node ceph1 as master by adding a label and a taint
    [markmaster] Master ceph1 tainted and labelled with key/value: node-role.kubernetes.io/master=""
    [bootstraptoken] Using token: nz9430.5xujb6kf7pcu5qkw
    [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: kube-dns
    [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.32.70:6443 --token nz9430.5xujb6kf7pcu5qkw --discovery-token-ca-cert-hash sha256:7e6af34aa62a2fa79c8f6e56781193b7ebbabd0e883b099a378f850686bd9730
    
  • 根据提示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
    
  • 验证集群

    [root@localhost init]# kubectl get nodes
    NAME      STATUS    ROLES     AGE       VERSION
    ceph1     NotReady     master    2h        v1.10.0
    

    在还没安装Pod的网络插件前,get nodes 都会显示是NotReady

3.安装网络插件

  • 下载官方yaml文件

    wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

    由于之前已经把官方的镜像下载好,这里不需要修改镜像地址

  • 执行

    kubectl create -f kube-flannel.yaml

    过一会使用命令kubectl get pods -n kube-system 查看:

    [root@localhost init]# kubectl get pods -n kube-system
    NAME                                    READY     STATUS    RESTARTS   AGE
    etcd-ceph1                              1/1       Running   0          3h
    kube-apiserver-ceph1                    1/1       Running   0          2h
    kube-controller-manager-ceph1           1/1       Running   0          2h
    kube-dns-86f4d74b45-2cdgj               3/3       Running   0          3h
    kube-flannel-ds-amd64-n7gdx             1/1       Running   0          2h
    kube-proxy-2r6wl                        1/1       Running   0          3
    kube-scheduler-ceph1                    1/1       Running   0          3h
    

    所有Pod都处于Running状态,再次执行kubectl get nodes可看到Master已经变为Ready。

4.节点加入集群

以上步骤除了准备工作以及node下载镜像的shell脚本外,其他都是在Master上操作。在Master已经部署好的前提下,将Node节点加入集群(执行master初始化成功后输出的命令):

kubeadm join 172.16.32.70:6443 --token nz9430.5xujb6kf7pcu5qkw --discovery-token-ca-cert-hash sha256:7e6af34aa62a2fa79c8f6e56781193b7ebbabd0e883b099a378f850686bd9730

过一会,在Master上查看node:

[root@localhost init]# kubectl get nodes
NAME      STATUS    ROLES     AGE       VERSION
ceph1     Ready     master    2h        v1.10.0
ceph2     Ready     <none>    2h        v1.10.0
ceph3     Ready     <none>    2h        v1.10.0

5.验证DNS是否正常

  • 创建一个Pod验证

    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox
      namespace: default
    spec:
      containers:
      - name: busybox
        image: busybox
        command:
          - sleep
          - "3600"
        imagePullPolicy: IfNotPresent
      restartPolicy: Always
    
  • 执行

    kubectl create -f busybox.yaml

  • 验证

    等待busybox的Pod状态变为Running时执行:

    kubectl exec -ti busybox -- nslookup kubernetes.default

    如果输出:

    [root@localhost test]# kubectl exec -ti busybox -- nslookup kubernetes.default
    Server:    10.96.0.10
    Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
    
    Name:      kubernetes.default
    Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
    

    表示DNS正常工作。

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

推荐阅读更多精彩内容