目前很多企业都将自己的服务上云了,有些使用自建云,但大部分使用公有云服务。服务上公有云除了费用高以外,其他都很方便高效。在云技术中,Kubernetes无疑是其中的佼佼者,其稳定的组件设计,丰富的插件选择和活跃的社区开发使得其成为资源池化的首选。熟悉并掌握Kubernetes使用方法已经成为企业对运维、DevOps工程师的基本要求。
文章对Kubernetes集群的部署做介绍。
环境介绍
为了可以轻松在8G内存的笔记本上运行集群,使用两台虚拟机,安装CentOS 7
Master节点
主机名:master.k8s.xyz,IP:192.168.52.131,内存:2G,CPU:2核,硬盘:30G
Node节点
主机名:node.k8s.xyz,IP:192.168.52.132,内存:2G,CPU:2核,硬盘:20G
两台服务器网络互通并可以访问Internet,修改/etc/hosts使得主机名可以在任一台主机上解析。为防止不必要的麻烦,建议禁用主机防火墙并关闭SELinux。
注意两点,服务器需禁用swap;master节点最低2核,不然使用kubeadm集群安装工具会报错。
安装
1,安装Docker(两台服务器都需要)
安装Docker源
yum install -y wget && wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
安装Docker并配置自启动
yum -y install docker-ce
systemctl enable docker && systemctl start docker
2,安装kubeadm,kubelet和kubectl(前两个组件两台服务器都需要,后一个可以仅在master节点安装,实际会依赖安装)
(1)添加阿里云YUM源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
(2)安装kubeadm、kubelet和kubectl
yum install -y kubelet kubeadm kubectl
kubernetes更新迭代速度很快,如果需要安装特定版本可人为指定包的版本号。
设置kubelet开机自启动
systemctl enable kubelet
简单介绍下三个组件。kubeadm是官方推出的快速安装Kubernetes集群的命令行工具,它只关注启动引导,而非配置机器。安装各种扩展,例如 Kubernetes Dashboard、 监控方案、以及特定云平台的扩展,kubeadm都不负责。kubelet就像docker的经理(当然你可以根据需要自由选择容器运行时,不一定非是docker),kubelet从API Server接收到指令,会操作docker下载容器镜像,启动容器等工作,当然它还有别的功能,kubelet是集群中默认唯一通过systemd管理的组件,集群组件中它需要第一个安装。kubectl是管理集群的命令行工具。
3,安装Kubernetes控制平台组件(仅在master节点操作)
输入下面的命令,
kubeadm init \
--apiserver-advertise-address=192.168.52.131 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.4 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
命令选项和参数都是自解释的,如有不熟悉的,可以输入“kubeadm help”查看每个子命令的详细解释。注意一点,指定阿里云镜像仓库用来拉取镜像,因为默认集群组件镜像从“k8s.gcr.io”拉取,不知什么原因,拉不下来。
kubeadm命令的工作过程,从上面命令的执行输出可以看出来,为了让大家有个直观认识,这里全部贴出来,
[root@master ]# kubeadm init --apiserver-advertise-address=192.168.52.131 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.4 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.23.4
[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'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master.k8s.xyz] and IPs [10.1.0.1 192.168.52.131]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master.k8s.xyz] and IPs [192.168.52.131 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master.k8s.xyz] and IPs [192.168.52.131 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-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
[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] Starting the kubelet
[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 30.522586 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kubelet-config". Kubeadm upgrade will handle this transition transparently.
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master.k8s.xyz as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node master.k8s.xyz as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 3mhq29.jrwbs2kgiytddqsb
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[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
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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 192.168.52.131:6443 --token 3mhq29.jrwbs24giytdabec \
--discovery-token-ca-cert-hash sha256:80a5e825a4c5f0fd123277505f166f7e16ddd78f8b256d627b6d834342e746e9
kubectl命令查看控制平台组件已经就绪了,
[root@master ]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-6d8c4cb4d-l6nbp 1/1 Running 0 14m
coredns-6d8c4cb4d-pnvz8 1/1 Running 0 14m
etcd-master.k8s.xyz 1/1 Running 3 14m
kube-apiserver-master.k8s.xyz 1/1 Running 3 14m
kube-controller-manager-master.k8s.xyz 1/1 Running 3 14m
kube-proxy-dwvsx 1/1 Running 0 14m
kube-scheduler-master.k8s.xyz 1/1 Running 3 14m
4,部署Pod网络插件
兼容CNI的网络插件市面上有很多,根据实际需要选择,这里安装易维护的flannel pod网络插件。输入下面命令安装,直截了当(详情可登录github搜索flannel-io了解)
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
5,将node节点加入集群
[root@node ~]# kubeadm join 192.168.52.131:6443 --token 3mhq29.jrwbs24giytdabec \
> --discovery-token-ca-cert-hash sha256:80a5e825a4c5f0fd123277505f166f7e16ddd78f8b256d627b6d834342e746e9
[preflight] Running pre-flight checks
[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@master ]# kb get nodes
NAME STATUS ROLES AGE VERSION
master.k8s.xyz Ready control-plane,master 22m v1.23.4
node.k8s.xyz Ready <none> 3m32s v1.23.4
至此,一个可供测试使用的kubernetes集群就部署完了,但这个离生产环境部署还差很远。生产环境要部署监控、日志收集、APM、Dashboard等,最重要是集群组件高可用保障。
总结
文章介绍依赖命令行工具kubeadm安装kubernetes集群的步骤和方法,从上面的过程看,并不复杂,在虚拟机就绪情况下,半个小时就可以部署完成,但维护一个kubernetes生产集群远比这个复杂,需要考虑的东西很多。
希望这篇文章能够帮助到读者!