k8s实战一使用kubeadm部署集群
环境
https://labs.play-with-k8s.com 提供的使用4小时的centos虚拟机
官方文档
https://kubernetes.io/docs/setup/independent/install-kubeadm/
1 检查
1.1 检查MAC地址和 product_uuid是否唯一
- ip addr 检查MAC地址
- sudo cat /sys/class/dmi/id/product_uuid 检查product_uuid
1.2 检查下列端口占用情况 (推荐用裸机)
1.3 安装 kubeadm, kubelet and kubectl
- kubeadm: 部署k8s集群的命令
- kubelet: K8s Nodes节点的服务
- kubectl: 集群操作命令
# yum 源配置上(翻墙替代源自己找吧)
cat <<EOF > /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
exclude=kube*
EOF
# 关掉seliunx
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# 安装
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# 用systemctl管理启动
systemctl enable kubelet && systemctl start kubelet
一定要开开内核路由转发
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
1.4 Master配置cgroup驱动
配置文件应该有这种配置 (这里没有配置)
cat /etc/default/kubelet
KUBELET_EXTRA_ARGS=--cgroup-driver=<value>
2 使用kubeadm创建master
2.1 初始化
注意:依旧是网络镜像的问题,看看要不要vpn,使用kubeadm config images pull 来测试网络
使用kubeadm init <args>
kubeadm init 有很多参数可以配置,典型的有
- 选择特定的Pod网络
# --pod-network-cidr
--pod-network-cidr=10.244.0.0/16
- 选择特定的IP端广播
# --apiserver-advertise-address=\<ip-address>
## 这里服务器一般有多个不同网络的IP
真正初始化(我们使用–apiserver-advertise-address)
kubeadm init --apiserver-advertise-address $(hostname -i)
等待一个比较长的时间后,显示如下
[node2 ~]$ kubeadm init --apiserver-advertise-address $(hostname -i)
Initializing machine ID from random generator.
[init] using Kubernetes version: v1.11.6
[preflight] running pre-flight checks
[WARNING Service-Docker]: docker service is not active, please run 'systemctl start doc
ker.service' [WARNING FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridg
e/bridge-nf-call-iptables does not exist
I1221 08:24:27.211969 747 kernel_validator.go:81] Validating kernel versionI1221 08:24:27.212815 747 kernel_validator.go:96] Validating kernel config
[preflight] The system verification failed. Printing the output from the verification:KERNEL_VERSION: 4.4.0-127-genericDOCKER_VERSION: 18.06.1-ce
OS: Linux
CGROUPS_CPU: enabled
CGROUPS_CPUACCT: enabled
CGROUPS_CPUSET: enabled
CGROUPS_DEVICES: enabled
...
Your Kubernetes master has initialized successfully!
...
2.2 使用kubectl的前置配置
配置kubectl不使用root执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
当然了你可以直接用root执行
export KUBECONFIG=/etc/kubernetes/admin.conf
3 安装Pod网络
有很多种网络可供选择
阿里的k8s有两个一个是Flannel,一个是自己研发的
我们使用测试网站推荐的weave.
$ kubectl apply -n kube-system -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
serviceaccount/weave-net createdclusterrole.rbac.authorization.k8s.io/weave-net created
clusterrolebinding.rbac.authorization.k8s.io/weave-net createdrole.rbac.authorization.k8s.io/weave-net createdrolebinding.rbac.authorization.k8s.io/weave-net created
daemonset.extensions/weave-net created
看看网络是不是安装好了
$ kubectl get pods --all-namespaces
[node1 ~]$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-78fcdf6894-n4d5b 1/1 Running 0 3m
kube-system coredns-78fcdf6894-wkcgl 1/1 Running 0 3m
kube-system etcd-node1 1/1 Running 0 3m
kube-system kube-apiserver-node1 1/1 Running 0 3m
kube-system kube-controller-manager-node1 1/1 Running 0 3m
kube-system kube-proxy-6klvq 1/1 Running 0 2mkube-system kube-scheduler-node1 1/1 Running 0 2m
kube-system weave-net-f86xg 2/2 Running 0 36s
[node1 ~]$