使用kubeadm方式安装kubernetes 1.10.0
- 环境准备
#系统环境 lsb_release -a Distributor ID: CentOS Description: CentOS Linux release 7.3.1611 (Core) Release: 7.3.1611 Codename: Core 说明:如提示:-bash: lsb_release: command not found,请yum install -y redhat-lsb #查看selinux状态 getenforce Disabled #关闭防火墙 systemctl disable firewalld systemctl stop firewalld systemctl status firewalld
- docker安装部署
所需docker安装包,请点击17.03自行下载使用。#docker安装 yum install -y yum-utils device-mapper-persistent-data lvm2 yum install -y docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm yum install -y docker-ce-17.03.1.ce-1.el7.centos.x86_64.rpm #docker启动 systemctl enable docker systemctl start docker systemctl status docker #docker 版本 docker --version Docker version 17.03.1-ce, build c6d412e
- 基础镜像准备
本次演示 kubernetes 所依赖的各个镜像列表如下:k8s.gcr.io/kube-apiserver-amd64:v1.10.0 k8s.gcr.io/kube-controller-manager-amd64:v1.10.0 k8s.gcr.io/kube-scheduler-amd64:v1.10.0 k8s.gcr.io/kube-proxy-amd64:v1.10.0 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.8 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.8 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.8 k8s.gcr.io/pause-amd64:3.1 quay.io/coreos/flannel:v0.9.1 quay.io/calico/node:v2.6.2 quay.io/calico/cni:v1.11.0 k8s.gcr.io/etcd-amd64:3.1.12 k8s.gcr.io/heapster-amd64:v1.5.3 k8s.gcr.io/heapster-influxdb-amd64:v1.3.3 k8s.gcr.io/heapster-grafana-amd64:v4.4.3 k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3 k8s.gcr.io/kubernetes-dashboard-init-amd64:v1.0.1
所需镜像,请点击images自行下载。
- 系统配置
根据官方文档中limitations小节中的内容,对各节点系统做如下设置:#创建/etc/sysctl.d/k8s.conf文件: touch /etc/sysctl.d/k8s.conf #添加如下内容: net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 #执行 sysctl -p /etc/sysctl.d/k8s.conf 使修改生效。
-
安装kubeadm和kubelet
下面在各节点安装kubeadm和kubelet#配置yum源 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 #生成缓存 yum clean all yum makecache #查看kubeadm, kubelet, kubectl的最新版本 yum list kubeadm --showduplicates |sort -r kubeadm.x86_64 1.10.0-0 kubernetes yum list kubelet --showduplicates |sort -r kubelet.x86_64 1.10.0-0 kubernetes yum list kubectl --showduplicates |sort -r kubectl.x86_64 1.10.0-0 kubernetes 直接安装即可: yum install -y kubelet kubeadm kubectl kubernetes-cni #启动kubelet.service systemctl enable kubelet.service systemctl start kubelet.service systemctl status kubelet.service
注意⚠️:阿里云的kubernetes的yum源是不断更新的,所以yum安装之前,请先确定下安装包版本是否是1.10.0的!
-
初始化master准备
#说明:
(1)kubeadm 等相关 rpm 安装后会生成 /etc/kubernetes 目录,而 kubeadm init 时候又会检测这些目录是否存在,如果存在则停止初始化,所以要先清理一下。#清理命令 kubeadm reset
参考官网tear down部分内容。
(2)初始化以前记得一定要启动 kubelet。
#启动命令如下: systemctl enable kubelet systemctl start kubelet
(3)安装ebtables包
新版本直接 init 会提示 ebtables not found in system path 错误,所以还得先安装一下这个包在初始化#安装 ebtables yum install -y ebtables
(4)修改kubelet配置文件
kubelet和docker 的cgroup driver 有2种方式:cgroupfs和systemd.注意保持 2个应用 的driver保持一致。 #查看kubelet的driver cat /etc/systemd/system/kubelet.service.d/10-kubeadm.conf其中包含如下内容: Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=systemd" #查看docker的driver docker info Server Version: 17.03.1-ce 。。。 Cgroup Driver: cgroupfs 。。。 #更改kubelet的driver Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=cgroupfs"
(5)关闭系统Swap
Kubernetes 1.8开始要求关闭系统的Swap,如果不关闭,默认配置下kubelet将无法启动。可以通过kubelet的启动参数--fail-swap-on=false更改这个限制。#关闭系统的Swap方法 swapoff -a #修改 /etc/fstab 文件,注释掉 SWAP 的自动挂载,使用free -m确认swap已经关闭。 swappiness参数调整,修改/etc/sysctl.d/k8s.conf添加下面一行: vm.swappiness=0 #执行 sysctl -p /etc/sysctl.d/k8s.conf 使修改生效。
说明:因为这里本次用于测试的主机上还运行其他服务,关闭swap可能会对其他服务产生影响,所以这里修改kubelet的启动参数--fail-swap-on=false去掉这个限制。
注意⚠️如果碰到一下错误:[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10255/healthz' failed with error: Get http://localhost:10255/healthz: dial tcp [::1]:10255: getsockopt: connection refused.
解决方法:
#修改/etc/systemd/system/kubelet.service.d/10-kubeadm.conf,加入: Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false" #使配置修改生效: systemctl daemon-reload
- 使用kubeadm init初始化集群
#使用kubeadm初始化集群,在Master Node上执行下面的命令:kubeadm init \ --kubernetes-version=v1.10.0 \ --pod-network-cidr=10.244.0.0/16 \ --apiserver-advertise-address=10.0.0.39 (master这台主机的IP)
#说明
我们选择flannel作为Pod网络插件,所以上面的命令指定–pod-network-cidr=10.244.0.0/16
#执行时的WARNING[WARNING FileExisting-crictl]: crictl not found in system path Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
#执行时如有:
[preflight] Some fatal errors occurred: [ERROR Swap]: running with swap on is not supported. Please disable swap 请添加–ignore-preflight-errors=Swap参数忽略这个错误,重新运行。 即: kubeadm init \ --kubernetes-version=v1.10.0 \ --pod-network-cidr=10.244.0.0/16 \ --apiserver-advertise-address=10.0.0.39 \ --ignore-preflight-errors=Swap
#整个初始化流程
kubeadm init --kubernetes-version=v1.10.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.0.0.39 [init] Using Kubernetes version: v1.10.0 [init] Using Authorization modes: [Node RBAC] [preflight] Running pre-flight checks. [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 [swarm2 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] an d IPs [10.96.0.1 10.0.0.39] [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 [swarm2] and IPs [10.0.0.39] [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.co nf" [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 22.002127 seconds [uploadconfig] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [markmaster] Will mark node swarm2 as master by adding a label and a taint [markmaster] Master swarm2 tainted and labelled with key/value: node-role.kubernetes.io/master="" [bootstraptoken] Using token: 4g0p8w.w5p29ukwvitim2ti [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 10.0.0.39:6443 --token 4g0p8w.w5p29ukwvitim2ti --discovery-token -ca-cert-hash sha256:21d0adbfcb409dca97e655641573b2ee51c77a212f194e20a3 07cb459e5f77c8
#关键内容说明
[certificates] 生成相关的各种证书 [kubeconfig] 接下来是生成证书和相关的kubeconfig文件, 这个目前我们在Kubernetes 1.6 高可用集群部署也是这么做的, 目前没看出有什么新东西。 [bootstraptoken] 生成token记录下来,后边使用kubeadm join往集群中添加节点时 会用到。 下面的命令是配置常规用户如何使用kubectl访问集群: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config 最后给出了将节点加入集群的命令: kubeadm join 10.0.0.39:6443 --token 4g0p8w.w5p29ukwvitim2ti --discovery-token-ca-cert-hash sha256:21d0adbfcb409dca97e655641573b2ee51c 77a212f194e20a307cb459e5f77c8 这条命令一定保存好,因为后期没法重现的!!
如果你需要重新做 kubeadm init ,那么我们最好把上次初始化和容器服务全部清理掉,具体步骤:
(1)kubeadm reset (2)systemctl stop kubelet (3)docker stop $(docker ps -qa) && docker rm $(docker ps -qa) ## 如果docker上有其他服务,请不要使用这个命令,这时你需要手动排查出kubernetes相关容器并删除 (4)systemctl start kubelet (5)kubeadm init
- master节点验证
#查看一下集群状态kubectl get cs error: the server doesn't have a resource type "cs" kubectl get nodes Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "kubernetes")
#解决方法:
mkdir -p /root/.kube/ cp -i /etc/kubernetes/admin.conf /root/.kube/config chown root:root /root/.kube/config
#再次查看集群状态
kubectl get cs NAME STATUS MESSAGE ERROR controller-manager Healthy ok scheduler Healthy ok etcd-0 Healthy {"health": "true"}
#查看集群节点
kubectl get nodes NAME STATUS ROLES AGE VERSION swarm2 Ready master 1d v1.10.0
#命令行验证
命令: curl --cacert /etc/kubernetes/pki/ca.crt --cert /etc/kubernetes/pki/apiserver-kubelet-client.crt --key /etc/kubernetes/pki/apiserver-kubelet-client.key https://10.0.0.39:6443
- 查看当前Network
kubeadm init成功之后,此时的master node上k8s的核心组件均正常启动,而且是多以container的形式启动。docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c2eb8ca152a2 k8s.gcr.io/pause-amd64:3.1 "/pause" 20 minutes ago Up 20 minutes k8s_POD_kube-flannel-ds-nhjx5_kube-system_a3016d6d-51dd-11e8-a243-0017fa00e437_0 eb98616f7458 6f7f2dc7fab5 "/sidecar --v=2 --..." 25 minutes ago Up 25 minutes k8s_sidecar_kube-dns-86f4d74b45-ztrbs_kube-system_17bb3bf9-50e1-11e8-921b-0017fa00e437_4 912942318fed c2ce1ffb51ed "/dnsmasq-nanny -v..." 25 minutes ago Up 25 minutes k8s_dnsmasq_kube-dns-86f4d74b45-ztrbs_kube-system_17bb3bf9-50e1-11e8-921b-0017fa00e437_4 d5b193a4d60e 80cc5ea4b547 "/kube-dns --domai..." 25 minutes ago Up 25 minutes k8s_kubedns_kube-dns-86f4d74b45-ztrbs_kube-system_17bb3bf9-50e1-11e8-921b-0017fa00e437_4 c120c761b764 9df3c00f55e6 "kube-apiserver --..." 25 minutes ago Up 25 minutes k8s_kube-apiserver_kube-apiserver-swarm2_kube-system_659a6e4a0a2629e2c62563857da54a7f_3 81533f3ea2a7 k8s.gcr.io/pause-amd64:3.1 "/pause" 25 minutes ago Up 25 minutes k8s_POD_kube-dns-86f4d74b45-ztrbs_kube-system_17bb3bf9-50e1-11e8-921b-0017fa00e437_4 867ce7e1d7f8 ceecd7155649 "kube-scheduler --..." 25 minutes ago Up 25 minutes k8s_kube-scheduler_kube-scheduler-swarm2_kube-system_0ede54c0e24ebcdc8ec84ec2aa830bfc_4 01474f2a8879 6e6237849607 "/usr/local/bin/ku..." 25 minutes ago Up 25 minutes k8s_kube-proxy_kube-proxy-2tghd_kube-system_17c7aa1b-50e1-11e8-921b-0017fa00e437_4 be4c6d21dc4d 52920ad46f5b "etcd --peer-key-f..." 25 minutes ago Up 25 minutes k8s_etcd_etcd-swarm2_kube-system_11d7cb74cd31e890a93f59d783573f27_3 912413c032e1 8401bb3ff261 "kube-controller-m..." 25 minutes ago Up 25 minutes k8s_kube-controller-manager_kube-controller-manager-swarm2_kube-system_c9074b2decba8a970cedd0fa9c4dd366_3 6609d479936e k8s.gcr.io/pause-amd64:3.1 "/pause" 25 minutes ago Up 25 minutes k8s_POD_kube-apiserver-swarm2_kube-system_659a6e4a0a2629e2c62563857da54a7f_3 ddfb3e0b37f5 k8s.gcr.io/pause-amd64:3.1 "/pause" 25 minutes ago Up 25 minutes k8s_POD_kube-scheduler-swarm2_kube-system_0ede54c0e24ebcdc8ec84ec2aa830bfc_4 67c0c0001635 k8s.gcr.io/pause-amd64:3.1 "/pause" 25 minutes ago Up 25 minutes k8s_POD_kube-proxy-2tghd_kube-system_17c7aa1b-50e1-11e8-921b-0017fa00e437_4 5dac6220b6dc k8s.gcr.io/pause-amd64:3.1 "/pause" 25 minutes ago Up 25 minutes k8s_POD_etcd-swarm2_kube-system_11d7cb74cd31e890a93f59d783573f27_5 7c88b16b245d k8s.gcr.io/pause-amd64:3.1 "/pause" 25 minutes ago Up 25 minutes k8s_POD_kube-controller-manager-swarm2_kube-system_c9074b2decba8a970cedd0fa9c4dd366_4
不过这些核心组件并不是跑在pod network中的(没错,此时的pod network还没有创建),而是采用了host network。以kube-apiserver的pod信息为例:
kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE kube-apiserver-swarm2 1/1 Running 3 1d
#查看kube-apiserver的容器id
docker ps |grep apiserver c120c761b764 9df3c00f55e6 "kube-apiserver --..." 33 minutes ago Up 33 minutes k8s_kube-apiserver_kube-apiserver-swarm2_kube-system_659a6e4a0a2629e2c62563857da54a7f_3
#查看对应的pause容器的network属性
docker inspect c120c761b764 "NetworkMode": "host",
- 安装flannel pod网络
#创建kube-flannel.yml mkdir /etc/kubernetes/manifests/my.conf #下载kube-flannel.yml文件 wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml #修改文件 image:quay.io/coreos/flannel:v0.10.0-amd64 更改为: image: quay.io/coreos/flannel:v0.9.1 #启动flannel cd /etc/kubernetes/manifests/my.conf kubectl create -f kube-flannel.yml
- 查看master中所有pod的状态
kubectl get pod --all-namespaces -o wide NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE kube-system etcd-swarm2 1/1 Running 3 1d 10.0.0.39 swarm2 kube-system kube-apiserver-swarm2 1/1 Running 3 1d 10.0.0.39 swarm2 kube-system kube-controller-manager-swarm2 1/1 Running 3 1d 10.0.0.39 swarm2 kube-system kube-dns-86f4d74b45-ztrbs 3/3 Running 12 1d 10.244.0.6 swarm2 kube-system kube-flannel-ds-nhjx5 1/1 Running 0 36m 10.0.0.39 swarm2 kube-system kube-proxy-2tghd 1/1 Running 4 1d 10.0.0.39 swarm2 kube-system kube-scheduler-swarm2 1/1 Running 4 1d 10.0.0.39 swarm2 或使用: kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE etcd-swarm2 1/1 Running 3 1d kube-apiserver-swarm2 1/1 Running 3 1d kube-controller-manager-swarm2 1/1 Running 3 1d kube-dns-86f4d74b45-ztrbs 3/3 Running 12 1d kube-flannel-ds-nhjx5 1/1 Running 0 37m kube-proxy-2tghd 1/1 Running 4 1d kube-scheduler-swarm2 1/1 Running 4 1d
-
向Kubernetes集群添加/删除Node
关于这部分操作,请点击参考Node操作 - 参考文档
转载于:https://blog.51cto.com/wutengfei/2110752