Kubernetes快速部署
kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。
这个工具能通过两条指令完成一个kubernetes集群的部署:
# 创建一个 Master 节点
$ kubeadm init
# 将一个 Node 节点加入到当前集群中
$ kubeadm join <Master节点的IP和端口>
1. 安装要求
在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
-至少3台机器,操作系统 CentOS7+
- 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘20GB或更多
- 集群中所有机器之间网络互通
- 可以访问外网,需要拉取镜像
- 禁止swap分区
2. 学习目标
- 在所有节点上安装Docker和kubeadm
- 部署Kubernetes Master
- 部署容器网络插件
- 部署 Kubernetes Node,将节点加入Kubernetes集群中
- 部署Dashboard Web页面,可视化查看Kubernetes资源
3. 准备环境
角色 | IP |
---|---|
master | 192.168.153.164 |
node1 | 192.168.153.166 |
node2 | 192.168.153.167 |
关闭防火墙:
[root@master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
关闭selinux:
[root@master ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
[root@node1 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@node2 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
关闭swap:
[root@master ~]# vim /etc/fstab
#/dev/mapper/cs-swap none swap defaults 0 0
[root@node1 ~]# vim /etc/fstab
#/dev/mapper/cs-swap none swap defaults 0 0
[root@node2 ~]# vim /etc/fstab
#/dev/mapper/cs-swap none swap defaults 0 0
注释掉swap分区
设置主机名:
[root@localhost ~]# hostnamectl set-hostname master.example.com
[root@localhost ~]# bash
[root@localhost ~]# hostnamectl set-hostname node1.example.com
[root@localhost ~]# bash
[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# bash
在master添加hosts:
[root@master ~]# cat >> /etc/hosts << EOF
192.168.153.164 master master.example.com
192.168.153.166 node1 node1.example.com
192.168.153.167 node2 node2.example.com
EOF
将桥接的IPv4流量传递到iptables的链:
[root@master ~]# cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 生效
[root@master ~]# sysctl --system
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-coredump.conf ...
kernel.core_pattern = |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %e
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
kernel.kptr_restrict = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.promote_secondaries = 1
net.core.default_qdisc = fq_codel
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /usr/lib/sysctl.d/50-libkcapi-optmem_max.conf ...
net.core.optmem_max = 81920
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
kernel.pid_max = 4194304
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
* Applying /etc/sysctl.conf ...
时间同步:
[root@master ~]# yum -y install chrony
[root@master ~]# systemctl enable --now chronyd
[root@master ~]# vim /etc/chrony.conf
pool time1.aliyun.com iburst
[root@master ~]# systemctl restart chronyd
[root@node1 ~]# yum -y install chrony
[root@node1 ~]# systemctl enable --now chronyd
[root@node1 ~]# vim /etc/chrony.conf
pool time1.aliyun.com iburst
[root@node1 ~]# systemctl restart chronyd
[root@node2 ~]# yum -y install chrony
[root@node2 ~]# systemctl enable --now chronyd
[root@node2 ~]# vim /etc/chrony.conf
pool time1.aliyun.com iburst
[root@node2 ~]# systemctl restart chronyd
免密认证:
[root@master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Yw/oF80oOymjNypmIKfLv5o+4gsfBVKCII4qj+5c8RA root@master.example.com
The key's randomart image is:
+---[RSA 3072]----+
|* . |
|=o |
|o..E |
|.. .. . + |
|o o. o S o |
|+o..+. = = |
|=oo.o.= . . |
|O=oooo o |
|O&O=.. |
+----[SHA256]-----+
[root@master ~]# ssh-copy-id master
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'master (192.168.153.164)' can't be established.
ECDSA key fingerprint is SHA256:BycR/AEKrPbE2/K7IZGMQOaOky9SOcep6J4h0bMYt4Y.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@master's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'master'"
and check to make sure that only the key(s) you wanted were added.
[root@master ~]# ssh-copy-id node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node1 (192.168.153.166)' can't be established.
ECDSA key fingerprint is SHA256:BycR/AEKrPbE2/K7IZGMQOaOky9SOcep6J4h0bMYt4Y.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node1's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'node1'"
and check to make sure that only the key(s) you wanted were added.
[root@master ~]# ssh-copy-id node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node2 (192.168.153.167)' can't be established.
ECDSA key fingerprint is SHA256:BycR/AEKrPbE2/K7IZGMQOaOky9SOcep6J4h0bMYt4Y.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node2's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'node2'"
and check to make sure that only the key(s) you wanted were added.
4. 所有节点安装Docker/kubeadm/kubelet
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
4.1 安装Docker
[root@master ~]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
[root@master ~]# yum clean all
[root@master ~]# yum makeache
[root@master ~]# yum -y install docker-ce
[root@master ~]# systemctl enable --now docker
[root@master ~]# docker --version
[root@master ~]# cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://in3617d8.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
4.2 添加kubernetes阿里云YUM软件源
[root@master ~]# 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
4.3 安装kubeadm,kubelet和kubectl
由于版本更新频繁,这里指定版本号部署:
[root@master ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@master ~]# systemctl enable --now kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.
5. 部署Kubernetes Master
在192.168.153.164(Master)执行。
[root@master ~]# kubeadm init \
--apiserver-advertise-address=192.168.153.164 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
.....
kubeadm join 192.168.153.164:6443 --token 0wmr2w.zlvl8ytqr6aylw9y \
--discovery-token-ca-cert-hash sha256:521e6865b00e239856949b8466746075cffb9166a4407e91209776fef818c620
由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。
使用kubectl工具:
[root@master ~]# echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' > /etc/profile.d/k8s.sh
[root@master ~]# source /etc/profile.d/k8s.sh
6. 安装Pod网络插件(CNI)
[root@master ~]# vim /etc/hosts
185.199.111.133 raw.githubusercontent.com
[root@master ~]# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[root@master ~]# 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 created
确保能够访问到quay.io这个registery。
7. 加入Kubernetes Node
在192.168.153.166、192.168.153.167上(Node)执行。
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:
[root@node1 ~]# kubeadm join 192.168.153.164:6443 --token 0wmr2w.zlvl8ytqr6aylw9y \
--discovery-token-ca-cert-hash sha256:521e6865b00e239856949b8466746075cffb9166a4407e91209776fef818c620
[root@node2 ~]# kubeadm join 192.168.153.164:6443 --token 0wmr2w.zlvl8ytqr6aylw9y \
--discovery-token-ca-cert-hash sha256:521e6865b00e239856949b8466746075cffb9166a4407e91209776fef818c620
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master.example.com Ready control-plane,master 2m36s v1.20.0
node1.example.com Ready <none> 2m13s v1.20.0
node2.example.com Ready <none> 2m2s v1.20.0
8. 测试kubernetes集群
在Kubernetes集群中创建一个pod,验证是否正常运行:
[root@master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
[root@master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
[root@master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-6799fc88d8-v8b9q 0/1 ContainerCreating 0 18s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3m21s
service/nginx NodePort 10.105.38.253 <none> 80:30672/TCP 4s
root@master ~]# curl 10.105.38.253:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
访问浏览器