kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具,比起复杂繁琐的源码安装要方便快捷很多。这一章我们就通过kubeadm搭建起一个Kubernetes集群。为后面的学习做准备。
我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。
为了方便重复安装,我将本节中一直到flannel部分的步骤制作成了一键安装脚本,分为master部分和node部分。
master: https://github.com/Victor2Code/centos-k8s-init/blob/master/centos_7-k8s_v1_15-init-master.sh
node: https://github.com/Victor2Code/centos-k8s-init/blob/master/centos_7-k8s_v1_15-init-node.sh
有兴趣的朋友可以对照着本文理解和使用一键安装脚本。
在正式用kubeadm安装之前,有一些准备条件先要做好
准备了两台全新的centos7机器如下,按照本文章中方法请先确保所有机器都可以科学上网
172.29.56.175 k8s-master
172.29.56.176 k8s-node1
其中每台机器的规格为
确保所有机器的hosts文件都同步
cat >> /etc/hosts << EOF
172.29.56.175 k8s-master
172.29.56.176 k8s-node1
EOF
后续如果有新的机器加入集群可以考虑用saltstack之类的工具批量修改文件
关掉系统自带的firewalld
防火墙服务而改为iptables
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service
sudo yum -y install iptables-services
sudo systemctl start iptables.service
sudo systemctl enable iptables.service
开放官方文档中列出的组件所需要的端口
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 6443 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 2379:2380 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 10250:10252 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 30000:32767 -j ACCEPT
根据机器局域网环境开放局域网ip段,并将上述规则保存至配置文件
iptables -I INPUT -d 172.29.0.0/16 -j ACCEPT
iptables -I INPUT -s 172.29.0.0/16 -j ACCEPT
service iptables save
之后跑iptables -nvL
确认一下iptables filter表中FOWARD链的默认策略(pllicy)为ACCEPT。
这里补充下一个比较坑的点,后来在安装完了flannel以后,coreDNS一直报错,根据网上的建议发现是防火墙的原因
将默认的拒绝FORWARD包的规则注释掉,重启生效
sed -i '/-A FORWARD -j REJECT/s/^/#/g' /etc/sysconfig/iptables
sudo systemctl restart iptables
禁用Selinux
setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
修改内核参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
kube-proxy可以开启ipvs模式或者iptables模式,因为效率更高所以采用ipvs模式,如果下面这些模块没加载成功即使配置开启了ipvs模式也会回退到iptables模式。
加载5个所需模块并设置为机器重启也能自动加载
cat > /etc/sysconfig/modules/ipvs.modules <
安装ipvs管理工具ipvsadm以及ipset
sudo yum -y install ipvsadm
sudo yum -y install ipset
Docker目前仍然是Kubernetes默认的容器运行接口,也就是CRI(Container Runtime Interface),所以要安装Docker。
安装Docker的yum源
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
查看一下可安装的docker版本
yum list docker-ce.x86_64 --showduplicates |sort -r
Kubernetes 1.15当前支持的docker版本列表是1.13.1, 17.03, 17.06, 17.09, 18.06, 18.09。 这里在各节点安装docker的18.09.9版本。
yum makecache fast
yum install -y --setopt=obsoletes=0 \
docker-ce-18.09.9-3.el7
systemctl start docker
systemctl enable docker
根据文档CRI installation中的内容,对于使用systemd作为init system的Linux的发行版,使用systemd作为docker的cgroup driver可以确保服务器节点在资源紧张的情况更加稳定,因此这里修改各个节点上docker的cgroup driver为systemd
cat > /etc/docker/daemon.json <
准备工作做好,下面就要正式开始部署了。
对所有节点做如下操作。
配置yum源
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
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
因为是从Google云上下载,可以先
curl https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
看下是否可用,如果不可用需要科学上网
安装之前同样也可以查看一下所有可供安装的版本
yum list kubelet --showduplicates
最新版本是1.18,不过我们还是安装1.15版本
yum makecache fast
yum install kubeadm-1.15.11-0 kubelet-1.15.11-0 kubectl-1.15.11-0
成功安装的显示如下
Installed:
kubeadm.x86_64 0:1.15.11-0 kubectl.x86_64 0:1.15.11-0 kubelet.x86_64 0:1.15.11-0
Dependency Installed:
conntrack-tools.x86_64 0:1.4.4-5.el7_7.2 cri-tools.x86_64 0:1.13.0-0 kubernetes-cni.x86_64 0:0.7.5-0
libnetfilter_cthelper.x86_64 0:1.0.0-10.el7_7.1 libnetfilter_cttimeout.x86_64 0:1.0.0-6.el7_7.1 libnetfilter_queue.x86_64 0:1.0.2-2.el7_2
socat.x86_64 0:1.7.3.2-2.el7
运行kubelet –help可以看到原来kubelet的绝大多数命令行flag参数都被DEPRECATED了,如:
--address 0.0.0.0 The IP address for the Kubelet to serve on (set to 0.0.0.0 for all IPv4 interfaces and `::` for all IPv6 interfaces) (default 0.0.0.0) (DEPRECATED: This parameter should be set via the config file specified by the Kubelet's --config flag. See https://kubernetes.io/docs/tasks/administer-cluster/kubelet-config-file/ for more information.)
而官方推荐我们使用本地配置文件去对kubelet进行设置,具体可以参考Set Kubelet parameters via a config file,待会在下面的安装过程中我们就会看到这个配置文件。kubelet的配置文件必须是json或yaml格式。
关闭swap
swapoff -a
sed -i '/swap/s/^/#/g' /etc/fstab
cat >> /etc/sysctl.d/k8s.conf << EOF
vm.swappiness=0
EOF
sysctl -p /etc/sysctl.d/k8s.conf
运行
free -m
发现都是0表示swap已经被关闭
对所有节点做如下操作。
开机启动kubelet
systemctl enable kubelet.service
对k8s-master节点做如下操作。
创建初始化集群所需的配置文件kubeadm.yaml。不过在此之前可以先通过kubeadm config print init-defaults
来查看一下默认的初始化配置参数
[root@k8s-master yum.repos.d]# kubeadm config print init-defaults
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 1.2.3.4
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: k8s-master
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.15.0
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
scheduler: {}
effect: NoSchedule
会在master上打一个污点,不让master接受任务调度,如果想让master也接受调度可以改为effect: PreferNoSchedule
advertiseAddress: 1.2.3.4
需要修改为master的物理网卡IPpodSubnet: 10.244.0.0/16
于是自定义配置文件如下
cat > /root/k8s-install/kubeadm.yaml << EOF
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 172.29.56.175
bindPort: 6443
nodeRegistration:
taints:
- effect: PreferNoSchedule
key: node-role.kubernetes.io/master
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: v1.15.0
networking:
podSubnet: 10.244.0.0/16
EOF
下面开始在master上进行集群初始化,并保存log,因为这个log里面有加入node节点的命令
kubeadm init --config /root/k8s-install/kubeadm.yaml | tee /root/k8s-install/init-log.log
初始化的log如下
[init] Using Kubernetes version: v1.15.0
[preflight] Running pre-flight checks
[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 "etcd/ca" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [172.29.56.175 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [172.29.56.175 127.0.0.1 ::1]
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.29.56.175]
[certs] Generating "apiserver-kubelet-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 35.503997 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" 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 k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:PreferNoSchedule]
[bootstrap-token] Using token: 085q2m.0dp7oldsibne4tv4
[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 172.29.56.175:6443 --token 085q2m.0dp7oldsibne4tv4 \
--discovery-token-ca-cert-hash sha256:611c726b02601f18cec16a9aec8ef985d1236557b59029e56fd8e6d43fe04c81
可以看出初始化一个kubernetes集群大概有如下几个步骤
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 172.29.56.175:6443 --token 085q2m.0dp7oldsibne4tv4 \
--discovery-token-ca-cert-hash sha256:611c726b02601f18cec16a9aec8ef985d1236557b59029e56fd8e6d43fe04c81
在master上执行完上面给出的三条命令后,查看一下集群状态,确认个组件都处于healthy状态
[root@k8s-master ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health":"true"}
flannel必须通过yaml配置文件来安装,将该文件和之前的一些重要文件放在一起
cd /root/k8s-install/
curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
如果机器有多个网卡,还需要在yml文件中用iface
来指定内网网卡,而如果只有一个内网网卡则不必指定
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.11.0-amd64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=eth1
......
然后根据配置文件进行安装
kubectl apply -f kube-flannel.yml
结果如下
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created
使用kubectl get pod --all-namespaces -o wide确保所有的Pod都处于Running状态
[root@k8s-master k8s-install]# kubectl get pod --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-5d4dd4b4db-78x58 1/1 Running 0 31m 10.244.0.3 k8s-master
kube-system coredns-5d4dd4b4db-m88wx 1/1 Running 0 31m 10.244.0.2 k8s-master
kube-system etcd-k8s-master 1/1 Running 0 30m 172.29.56.175 k8s-master
kube-system kube-apiserver-k8s-master 1/1 Running 0 30m 172.29.56.175 k8s-master
kube-system kube-controller-manager-k8s-master 1/1 Running 0 30m 172.29.56.175 k8s-master
kube-system kube-flannel-ds-amd64-zr7jk 1/1 Running 0 2m51s 172.29.56.175 k8s-master
kube-system kube-proxy-tgn7p 1/1 Running 0 31m 172.29.56.175 k8s-master
kube-system kube-scheduler-k8s-master 1/1 Running 0 30m 172.29.56.175 k8s-master
需要注意,node上无需安装flannel,因为一旦加入集群,会自动起flannel容器。
[root@k8s-master k8s-install]# kubectl run curl --image=radial/busyboxplus:curl -it
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
If you don't see a command prompt, try pressing enter.
[ root@curl-6bf6db5c4f-mztwf:/ ]$
之后测试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解析有问题,检查下CoreDNS的log,kubectl logs coredns-xxx -n kube-system
,如果有no route to host
的报错考虑是防火墙的原因(这是我自己遇到的)
根据前面保存的添加node的脚本,在待添加的机器上跑一下
kubeadm join 172.29.56.175:6443 --token 085q2m.0dp7oldsibne4tv4 \
--discovery-token-ca-cert-hash sha256:611c726b02601f18cec16a9aec8ef985d1236557b59029e56fd8e6d43fe04c81
之后在master上就可以查看了
[root@k8s-master k8s-init]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 9m9s v1.15.11
k8s-node1 Ready 2m v1.15.11
master的安装还是有点繁琐的,因为毕竟它是以后整个集群的大脑。相对而言,node节点只需要安装好了kubelet和kubeadm,确保能正常加入集群,很多事情就不用自己操心了,例如网络。
这一节只是确保了集群是正常可用的,但是还有很多的插件以及配置需要去个性化和调优,等到后面我们再慢慢探索和学习。
本篇文章步骤参考自中文官方文档,后续有新版本Kubernetes,官方文档也会第一时间进行安装操作更新,大家可以持续关注。
也可以关注我的github仓库,有更新的测试可用的一键脚本我也会同步过去。