文章目录
1 k8s核心组件
1.1 HPA
- Deployment还支持二级控制器
- HPA(HorizontalPodAutoscaler,水平pod自动伸缩控制器)
- 一般情况下我们可以确保一个node上有2个pod在运行,万一用户访问流量增加,2个pod不足以承载这么多访问量怎么办?此时我们就应该要增加pod资源,那么到底应该加几个?
- HPA控制器可自动监控pod、自动进行扩展。
1.2 service
-
假如有2个pod,pod有其生命周期,万一pod所在的节点宕机了,那么此pod将应该要在其他的节点上重建,而重建完的pod与原来的pod已经不是同一个pod了,只是两者都是运行的同一个服务而已。且每个容器都有其IP地址,重建的pod中的容器其IP地址与之前的pod中容器的IP地址是不一样的,如此一来就会存在一个问题,客户端如何访问这些pod中的容器呢?
-
服务发现
-
pod是有生命周期的,一个pod随时都有可能离去,随时都有可能会有其他pod加进来,假如它们提供的是同一种服务,客户端是无法通过固定的手段来访问这些pod的,因为pod本身是不固定的,它们随时可能被替换掉,无论使用主机名还是P地址,都随时会被替换掉。
-
为了尽可能的降低客户端与pod问协调的复杂度,k8s为每-组提供同类服务的pod和其客户端之间添加了一个中间层,这个中间层是固定的,这+中间层就叫service
-
service只要不被删除,其地址与名称皆是固定的,当客户端需要在其配置文件中写*问某个服务时,它不再需要自动发现,只需要在配置文件中写明service的名称即可,而这个service是个调度器,其不但能够提供一个稳定的访问入口,还可以做反向代理,当service接收到客户端的请求后,会将其代理到后端的pod之上,一旦pod宕机了会立即新建一个pod,这个新建的pod会立即被service关联上,作为service后端的可用pod之一
-
客户端程序访问服务都是通过IP+端口或者主机名+端口的方式来实现的。而service关联后端的pod不是靠它的IP和主机名,而是靠pod的标签选择器。只要创建的pod的label是统一的,无论IP地址和主机如何改变,其都能被service所识别。如此一来,只要pod属于标签选择器,只要其在service的管理范围之内,则其就会被关联到service中,当这个动态的pod关联到service中之后,再进行动态的探测此pod的IP地址、端口,再将其作为自己后端可调度的可用服务器主机对象。因此,客户端的请求发送到service,然后由service代理到后端真实的pod中的容器进行响应。
-
service不是一个程序,也不是一个组件,它只是一个iptables的dnat规则
-
service作为k8s的对象,有其自身的名称,而service的名称相当于服务的名称,而这个名称可以被解析addOns附件
-
dns pod
-
装完k8s后第一件事就需要在k8s集群上部署一个dns pod,以确保各service的名称能够被解析
-
可以动态改变,包括动态创建、动态删除、动态修改
-
比如把service的名称改了,dnspod会自动触发,将dns解析记录中的名称也给改掉;假如我们手动把service的ip地址给改了,改完以后会自动触发,将dns服务中的解析记录给改掉。
-
如此一来,客户端去访问pod资源的时候可以直接访问service的名称,然后由集群中专用的dns服务来负责解析。
-
-
这种pod是k8s自身的服务就需要用到的pod,所以我们把它称为基础性的系统架构级的pod对象,而且它们也被称为集群附件
2 Kubernetes快速部署
kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具
这个工具能通过两条指令完成一个kubernetes集群的部署
# 创建一个 Master 节点
$ kubeadm init
# 将一个 Node 节点加入到当前集群中
$ kubeadm join <Master节点的IP和端口>
2.1 Kubernetes安装要求
在开始之前,部署Kubernetes集群机器需要满足以下几个条件
至少3台机器,操作系统 CentOS7+
- 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘20GB或更多
- 集群中所有机器之间网络互通
- 可以访问外网,需要拉取镜像
- 禁止swap分区
2.2 安装步骤
- 在所有节点上安装Docker和kubeadm
- 部署Kubernetes Master
- 部署容器网络插件
- 部署 Kubernetes Node,将节点加入Kubernetes集群中
- 部署Dashboard Web页面,可视化查看Kubernetes资源
2.3 准备环境
主机 | IP |
---|---|
master | 192.168.25.146 |
node1 | 192.168.25.147 |
node2 | 192.168.25.148 |
部署
//修改主机名
[root@localhost ~]# hostnamectl set-hostname master.example.com
[root@localhost ~]# bash
[root@master ~]# hostname
master.example.com
[root@node ~]# hostnamectl set-hostname node1.example.com
[root@node ~]# bash
[root@node1 ~]# hostname
node1.example.com
[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# bash
[root@node2 ~]# hostname
node2.example.com
#需在三台主机上操作步骤
//关闭防火墙和selinux
[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@master ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
//关闭swap分区
# vim /etc/fstab
注释掉swap分区
//添加hosts
[root@master ~]# cat >> /etc/hosts << EOF
192.168.25.146 master master.example.com
192.168.25.147 node1 node1.example.com
192.168.25.148 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 # 生效
//配置时间同步
[root@master ~]# yum -y install chrony
[root@master ~]# vi /etc/chrony.conf
pool time1.aliyun.com iburst
[root@master ~]# systemctl enable --now chronyd
[root@master ~]# for i in master node1 node2 ;do ssh $i 'date' ;done
2021年 12月 18日 星期六 04:59:41 EST
2021年 12月 18日 星期六 04:59:42 EST
2021年 12月 18日 星期六 04:59:42 EST
//免密认证(只需要在master上做)
[root@master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:3DYIG2AYzaeUUWN8asgfqScnXoRyn+tYGOexy92qhD4 root@master.example.com
The key's randomart image is:
+---[RSA 3072]----+
| .=+=+ |
| ..=oo.. |
| o =o+ |
| . * *= o |
| o.*+oS + |
| +*Bo . . |
| .o*=. |
| .E=.o . |
| oo=.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"
/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"
/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"
/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.
// 重启三台主机,使上面的一些配置生效
[root@master ~]# reboot
2.4 所有节点安装Docker/kubeadm/kubelet
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
2.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 -y install docker-ce
[root@master ~]# systemctl enable --now docker
[root@master ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disab>
Active: active (running) since Sat 2021-12-18 05:10:41 EST; 9s ago
Docs: https://docs.docker.com
Main PID: 7143 (dockerd)
Tasks: 9
Memory: 36.3M
CGroup: /system.slice/docker.service
└─7143 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.840348434-0>
12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.865784971-0>
12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.865832834-0>
12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.866033860-0>
12月 18 05:10:41 master.example.com dockerd[7143]: time="2021-12-18T05:10:41.040791665-0>
12月 18 05:10:41 master.example.com dockerd[7143]: time="2021-12-18T05:10:41.093668762-0>
12月 18 05:10:41 master.example.com dockerd[7143]: time="2021-12-18T05:10:41.109693941-0>
lines 1-17
[root@master ~]# docker --version
Docker version 20.10.12, build e91ed57
[root@master ~]# cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://xj3hc284.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
2.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
2.4.3 安装kubeadm,kubelet和kubectl
由于版本更新频繁,这里指定版本号部署
[root@node2 ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@master ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.
2.5 部署Kubernetes Master
在192.168.25.146(Master)执行
[root@master ~]# kubeadm init \
--apiserver-advertise-address=192.168.25.146 \
--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.25.146:6443 --token 481ink.zzl4pouej6vq82bs \
--discovery-token-ca-cert-hash sha256:94dc5e83208b662f4a9f7f133a1da3ed6052ead72922de5aee750ab498d44031
由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址
[root@master ~]# export 'KUBECONFIG=/etc/kubernetes/admin.conf' > /etc/profile.d/k8s.sh
[root@master ~]# source /etc/profile.d/k8s.sh
2.6 安装Pod网络插件(CNI)
//使用这条命令直接安装
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
//我这里是把文件下载下来安装
[root@master ~]# ls
anaconda-ks.cfg kube-flannel.yml
[root@master ~]# kubectl apply -f /root/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
2.7 加入Kubernetes Node
在192.168.25.147、192.168.25.148上(Node)执行,执行刚刚保存的命令
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令
[root@node1 ~]# kubeadm join 192.168.25.146:6443 --token 481ink.zzl4pouej6vq82bs --discovery-token-ca-cert-hash sha256:94dc5e83208b662f4a9f7f133a1da3ed6052ead72922de5aee750ab498d44031
[root@node2 ~]# kubeadm join 192.168.25.146:6443 --token 481ink.zzl4pouej6vq82bs --discovery-token-ca-cert-hash sha256:94dc5e83208b662f4a9f7f133a1da3ed6052ead72922de5aee750ab498d44031
3 测试kubernetes集群
在Kubernetes集群中创建一个pod,验证是否正常运行
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master.example.com Ready control-plane,master 15m v1.20.0
node1.example.com Ready <none> 5m58s v1.20.0
node2.example.com Ready <none> 5m53s v1.20.0
[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-sk9zs 1/1 Running 0 4m56s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21m
service/nginx NodePort 10.110.176.8 <none> 80:32279/TCP 4m36s
//访问在pod中运行的容器
[root@master ~]# curl 10.110.176.8
<!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>
在浏览器*问