Kubernetes一个编排工具
Kubernetes 特点:
K8s总体架构
K8s集群由两节点组成:Master和Node。
在Master上运行etcd,Api Server,Controller Manager和Scheduler四个组件。后三个组件构成了K8s的总控中心,负责对集群中所有资源进行管控和调度.在每个node上运行kubectl,proxy和docker daemon三个组件,负责对节点上的Pod的生命周期进行管理,以及实现服务代理的功能。另外所有节点上都可以运行kubectl命令行工具。
API Server作为集群的核心,负责集群各功能模块之间的通信。集群内的功能模块通过Api Server将信息存入到etcd,其他模块通过Api Server读取这些信息,从而实现模块之间的信息交互。Node节点上的Kubelet每隔一个时间周期,通过Api Server报告自身状态,Api Server接收到这些信息后,将节点信息保存到etcd中。Controller Manager中 的node controller通过Api server定期读取这些节点状态信息,并做响应处理。Scheduler监听到某个Pod创建的信息后,检索所有符合该pod要求的节点列表,并将pod绑定到节点列表中最符合要求的节点上。如果scheduler监听到某个Pod被删除,则调用api server删除该Pod资源对象。kubelet监听pod信息,如果监听到pod对象被删除,则删除本节点上的相应的pod实例,如果监听到修改Pod信息,则会相应地修改本节点的Pod实例。
Kubernetes主要由以下几个核心组件组成:
1关闭防火墙、selinux
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 设置selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
2.设置路由
yum install -y bridge-utils.x86_64
# 加载br_netfilter模块,使用lsmod查看开启的模块:
modprobe br_netfilter
# 修改配置
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
3.关闭swap
# 临时关闭swap
swapoff -a && sysctl -w vm.swappiness=0
# 取消开机挂载swap
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
1.通过阿里云yum源安装
yum install -y yum-utils
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
systemctl enable docker && systemctl start docker
2.修改Docker镜像仓库
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
1.设置Kubenetes为国内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.安装最新版kubelet,kubeadm, kubectl
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet
以上操作 master和node节点都需要执行
2.修改hostname
# master节点执行:
hostnamectl set-hostname k8s-master
# node节点执行:
hostnamectl set-hostname k8s-node1
3.修改hosts文件
cat >> /etc/hosts << EOF
10.0.0.129 k8s-master
10.0.0.134 k8s-node1
10.0.0.135 k8s-node2
EOF
1.安装1.22.2版本的k8s
kubeadm init \
--apiserver-advertise-address=10.0.0.129 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.2 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
成功返回:
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 10.0.0.129:6443 --token rrrhpv.mvcor9xzxl7aq3hu \
--discovery-token-ca-cert-hash sha256:6f681f68f944667e836a3d9aa9b0a375aa5be582a73e5d0d3d9aa9375c4b225f
2.设置权限
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3.创建kube-flannel网络插件
k8s上不同pod之间的网络通过CNI,是外部的网络插件实现
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get nodes
kubectl get pod -n kube-system
1.node节点加入集群 kubeadm join
kubeadm join 10.0.0.129:6443 --token 2ecj1o.btjkjpjhj1z1f25n --discovery-token-ca-cert-hash sha256:6f681f68f944667e836a3d9aa9b0a375aa5be582a73e5d0d3d9aa9375c4b225f
获取加入集群的token
kubeadm token create --print-join-command
2.检查集群健康转态
kubectl get cs
1.启动一个nginx服务
[root@k8s-master k8s]# cat server.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: nginx
name: nginx
spec:
replicas: 4
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:1.9.1
imagePullPolicy: IfNotPresent
name: nginx
[root@k8s-master k8s]# cat service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
run: nginx
name: nginx
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30096
selector:
run: nginx