感谢老哥的分享:https://www.cnblogs.com/tangy1/p/14925216.html
Kubernetes部署环境准备
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config #永久
setenforce 0 #临时
关闭swap
# k8s禁止虚拟内存以提高性能
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久
swapoff -a #临时
在master添加hosts
cat >> /etc/hosts << EOF
192.168.10.111 k8smaster
192.168.10.112 k8snode1
192.168.10.113 k8snode2
EOF
设置网桥参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
Docker安装具体步骤
添加k8s的阿里云YUM源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
安装docker
yum install docker-ce-19.03.13 -y
启动docker
systemctl start docker.service
systemctl enable docker.service
配置加速器加速下载
# 阿里云容器镜像服务地址:https://cr.console.aliyun.com/
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://gg3gwnry.mirror.aliyuncs.com"]
}
Kubernetes安装具体步骤
添加k8s的阿里云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
安装 kubeadm、kubelet、kubectl
yum install kubelet-1.19.4 kubeadm-1.19.4 kubectl-1.19.4 -y
systemctl enable kubelet.service
验证k8s相关软件/命令
kubelet --version
yum list installed | grep kubelet
yum list installed | grep kubeadm
yum list installed | grep kubectl
重启CentOS主机系统!!!!!!!!
master节点执行
kubeadm init --apiserver-advertise-address=192.168.10.111 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.19.4 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
kubeadm join 192.168.10.111:6443 --token r19ru2.bugw3kwtnd7em9an --discovery-token-ca-cert-hash sha256:ce3f8a50ee94a874414d13e8531895f1deda0bd7c656acb6607224fbe2214c70
部署网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
在master机器上执行:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看节点状态
# 看看所有节点是否均已Ready
kubectl get nodes
在k8s集群中部署Nginx
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
# 或执行 kubectl get services
# 检查应用是否可以正常访问
curl http://NodePort:Portcurl
# 或 http://NodePort:Port
示例:
<!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>
...
</body>
</html>
可以看出nginx已经部署成功
常用命令
# 部署应用
kubectl create deployment nginx --image=nginx
# 查看所有deployment
kubectl get deployments
# 查看所有节点
kubectl get nodes
# 查看节点状态(node NotReady时排查问题)
kubectl describe nodes k8snode01
# 查看所有pod
kubectl get pods
# 查看启动的应用IP及端口
kubectl get pods,svc
# 查看kube-system命名空间下所有pod
kubectl get pods -n kube-system
# 使用yaml(deployment/service/pod等)
kubectl apply -f xxx.yml
# 导出命名空间default
kubectl get namespace default -o=yaml>./default/namespace.yaml
# 导出deployments
kubectl get deployments -n default -o=yaml>./default/deployments.yaml
# 查看资源的组成
kubectl explain deployment
kubectl explain deployment.spec
进阶示例及参数说明
生成yaml文件及部署应用
# 创建yaml
kubectl create deployment nginx --image=nginx --dry-run -o yaml > nginx-deployment.yaml
# 使用yaml文件部署应用
kubectl apply -f nginx-deployment.yaml
# 暴露端口
kubectl expose deployment nginx --port=80 --tartget-port=80 --type=NodePort --name=nginx -o yaml > nginx-service.yaml
# 查看pod和service
kubectl get pods,svc
dry-run:不实际运行
应用升级、回滚与弹性伸缩
# 升级
kubectl set image deployment nginx nginx=nginx:1.15
# 查看升级状态
kubectl rollout status deployment nginx
# 查看升级的历史版本
kubectl rollout history deployment nginx
# 回滚到上个版本
kubectl rollout undo deployment nginx
# 回滚到指定版本
kubectl rollout undo deployment nginx --to-reversion=2
# 弹性伸缩
kubectl scale deployment nginx --replicas=2
Deployment示例
apiVersion: apps/v1 ##版本
kind: Deployment ##类型
metadata: ##Deployment的元数据
name: nginx ##Deployment的名字
labels: ##标签
app: nginx ##标签app=nginx
spec: ##Pod的信息
replicas: 1 ##Pod的副本数
selector: ##标签选择器
matchLabels: ##查找匹配的标签
app: nginx ##标签app=nginx
strategy: {} ##滚动更新yaml文件示例(推荐使用滚动更新)
template: ##Pod模板信息
metadata: ##Pod元数据
creationTimestamp: null ##创建时的时间戳
labels: ##Pod的元数据
app: nginx ##标签app=nginx
spec: ##容器的信息
containers: ##容器
- name: nginx ##容器名称
image: nginx:1.4 ##镜像及版本
ports: ##端口信息
- containerPort: 80 ##容器暴露的端口
strategy参数说明:
# 滚动更新yaml文件示例(推荐使用滚动更新)
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
# 重建更新yaml文件示例(不推荐):
strategy:
type: Recreate
# 重建更新不支持maxSurge、maxUnavailable参数,因为此策略是将原有pod删除后重建新的pod,这期间应用不可用,这也是不推荐此策略的原因。
Pod示例
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.4
imagePullPolicy: Always
restartPolicy: Always
resources:
request:
memory: "256Mi"
cpu: "128m"
limits:
memory: "512Mi"
cpu: "256m"
imagePullPolicy参数说明:
IfNotPresent:镜像在宿主机不存在时才拉取(默认策略)
Always:每次创建Pod都会拉取镜像
Never:永远不会主动拉取镜像
restartPolicy参数说明:
Always:容器终止退出后,总是重启容器(默认策略)
OnFailure:容器异常退出后,才重启容器
Never:容器终止退出后,不重启容器
resources参数说明:
request:资源最低要求
limits:资源最高限制