请参照以前Docker安装。先提前为所有机器安装Docker
一台兼容的 Linux 主机。Kubernetes 项目为基于 Debian 和 Red Hat 的 Linux 发行版以及一些不提供包管理器的发行版提供通用的指令
每台机器 2 GB 或更多的 RAM (如果少于这个数字将会影响你应用的运行内存)
2 CPU 核或更多
集群中的所有机器的网络彼此均能相互连接(公网和内网都可以)
节点之中不可以有重复的主机名、MAC 地址或 product_uuid。请参见这里了解更多详细信息。
开启机器上的某些端口。请参见这里 了解更多详细信息。
禁用交换分区。为了保证 kubelet 正常工作,你 必须 禁用交换分区。
所有机器执行以下操作
#各个机器设置自己的域名
hostnamectl set-hostname xxxx
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
#关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
# 指定安装版本
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
sudo systemctl enable --now kubelet
PS:kubelet 现在每隔几秒就会重启,因为它陷入了一个等待 kubeadm 指令的死循环
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
chmod +x ./images.sh && ./images.sh
#所有机器添加master域名映射,以下需要修改为自己的(让所有机器知道主节点地址)
echo "192.168.10.31 master1" >> /etc/hosts
#主节点初始化
kubeadm init \
--apiserver-advertise-address=192.168.10.31 \
--control-plane-endpoint=master1 \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.10.0.0/16
#所有网络范围不重叠
参数 | 解释 |
---|---|
--apiserver-advertise-address |
主机点的IP地址 |
--control-plane-endpoint |
主节点的主机名称 |
--image-repository |
镜像地址 |
--kubernetes-version |
kubernetes版本 |
--service-cidr |
k8s集群内部对大量服务进行负载均衡的网络范围 |
--pod-network-cidr |
k8s集群内部对大量服务进行负载均衡的网络范围 |
下面是初始化完成
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
# 初始化集群完成后先操作这3步
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
# 当作主节点加入集群
kubeadm join master1:6443 --token 1iv154.hajr21x2aa1cb085 \
--discovery-token-ca-cert-hash sha256:83d5200f847c52574ec9e2becfa8dadaf32adb721399239562756115b34c3306 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
# 当作工作节点加入集群
kubeadm join master1:6443 --token 1iv154.hajr21x2aa1cb085 \
--discovery-token-ca-cert-hash sha256:83d5200f847c52574ec9e2becfa8dadaf32adb721399239562756115b34c3306
[root@master1 ~]# mkdir -p $HOME/.kube
[root@master1 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master1 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@master1 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master1 NotReady control-plane,master 3d22h v1.20.9
calico官网
[root@master1 ~]# curl https://docs.projectcalico.org/manifests/calico.yaml -O
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 212k 100 212k 0 0 30832 0 0:00:07 0:00:07 --:--:-- 35250
[root@master1 ~]# ls
anaconda-ks.cfg calico.yaml images.sh
[root@master1 ~]# kubectl apply -f calico.yaml
下面是配置文件
calico.yaml
如果上面初始化主节点时修改过POD的IP,去掉注释,替换为修改的pod的IP
# 查看集群所有节点
kubectl get nodes
# 查看集群部署了哪些应用
docker ps
kubectl get pods -A
生成token
[root@k8s-master1 ~]# kubeadm token create --print-join-command --ttl=0
kubeadm join master1:6443 --token 2uikx9.17vow5i1h4k6bf6u --discovery-token-ca-cert-hash sha256:83d5200f847c52574ec9e2becfa8dadaf32adb721399239562756115b34c3306
说明:
–ttl=0代表时间永不过期,不加此参数默认24小时过期
node节点加入集群
[root@node1 ~]# kubeadm join --token 5qtmh7.3ux095roh6cqn3yz --discovery-token-ca-cert-hash sha256:83d5200f847c52574ec9e2becfa8dadaf32adb721399239562756115b34c3306 master1:6443
master节点加入集群需要输入以下步骤
在master上生成用于新master加入的证书
[root@master1 ~]# kubeadm init phase upload-certs --upload-certs
I1110 16:05:46.973918 238162 version.go:254] remote version is much newer: v1.22.3; falling back to: stable-1.20
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
4f3b592cb3606104d0edcd18e35ced06d35810fefe618e309605a9ec655b335e
master节点加入集群
kubeadm join master1:6443 --token 5qtmh7.3ux095roh6cqn3yz \
--discovery-token-ca-cert-hash sha256:83d5200f847c52574ec9e2becfa8dadaf32adb721399239562756115b34c3306 \
--control-plane --certificate-key 4f3b592cb3606104d0edcd18e35ced06d35810fefe618e309605a9ec655b335e
查看集群状态
[root@master1 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master1 Ready control-plane,master 5d21h v1.20.9
master2 Ready control-plane,master 20h v1.20.9
node1 Ready <none> 22h v1.20.9
node2 Ready <none> 21h v1.20.9
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml
recommended.yaml
设置访问端口
kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
type: ClusterIP 改为 type: NodePort
# 找到端口,在安全组放行
[root@master1 ~]# kubectl get svc -A |grep kubernetes-dashboard
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.96.56.238 <none> 8000/TCP 17m
kubernetes-dashboard kubernetes-dashboard NodePort 10.96.173.93 <none> 443:31436/TCP 17m
访问: https://集群任意IP:端口 https://192.168.10.31:31436
创建访问账号,准备一个yaml文件; vi dashadm.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
[root@master1 ~]# kubectl apply -f dashadm.yaml
serviceaccount/admin-user created
clusterrolebinding.rbac.authorization.k8s.io/admin-user created
获取访问令牌
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
名称空间用来隔离资源
# 创建名称空间hello
kubectl create ns hello
# 删除名称空间hello
kubectl delete ns hello
yaml创建名称空间
vim hellons.yaml
apiVersion: v1
kind: Namespace
metadata:
name: hello
apiVersion: v1 == 版本号
kind: Namespace == 资源类型(此资类型是名称空间)
metadata: == 元数据
name: hello == 名称空间名字
# 创建名称空间hello
kubectl appyl -f hellone.yaml
# 删除名称空间hello
kubectl delete -f hellone.yaml
运行中的一组容器,Pod是kubernetes中应用的最小单位.
命令行创建nginx应用
kubectl run mynginx --image=nginx
# 查看default名称空间的Pod
kubectl get pod
# 描述
kubectl describe pod 你自己的Pod名字
# 删除
kubectl delete pod Pod名字
# 查看Pod的运行日志
kubectl logs Pod名字
# 每个Pod - k8s都会分配一个ip
kubectl get pod -o wide
# 使用Pod的ip+pod里面运行容器的端口
curl 192.168.169.136
# 集群中的任意一个机器以及任意的应用都能通过Pod分配的ip来访问这个Pod
run == 启动应用
mynginx == 给应用起一个名字
–image=nginx == 使用了nginx镜像创建
默认在default名称空间
描述Pod启动状态
kubectl describe pod mynginx
描述内容为:
Scheduled 第一步,把这个应用放到node2节点中去
Pulling 第二步,在node2节点里下载nginx镜像
Pulled 第三步,nginx镜像下载完成
Created 第四步,创建mynginx容器
Started 第五步,启动mynginx容器
yaml文件创建nginx应用
apiVersion: v1
kind: Pod
metadata:
labels:
run: mynginx
name: mynginx
# namespace: default
spec:
containers:
- image: nginx
name: mynginx
apiVersion: v1
kind: Pod
metadata:
labels:
run: myapp
name: myapp
spec:
containers:
- image: nginx
name: nginx
- image: tomcat:8.5.68
name: tomcat
apiVersion: v1 == 版本号
kind: Pod == 资源类型(此资类型是Pod)
metadata: == 元数据
labels: == 固定写法
run: myapp == 启动容器
name: myapp == 容器名字
spec: == Pod的详细配置信息
containers: == 容器的配置
- image: nginx == 使用的镜像
name: nginx