1. 操作系统
操作系统 | 内存 | CPU |
---|---|---|
CentOS Linux release 7.8.2003 | 16G | 8 |
2. 软件版本
软件名称 | 版本 |
---|---|
docker | 18.06.3-ce |
kubelet | 1.13.12 |
kubectl | 1.13.12 |
kubeadm | 1.13.12 |
dashboard | 2.0.0-rc5 |
3. 关闭selinux
setenforce 0 && sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
4. 关闭防火墙
由于是单机部署,可以不关闭
systemctl stop firewalld
5. 主机名配置
hostnamectl --static set-hostname master
6. 修改内核参数和模块
/etc/sysctl.d/k8s.conf文件中添加,没有此文件的话则新增文件
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
使内核参数配置生效
sysctl --system
modprobe br_netfilter
lsmod | grep br_netfilter
7. 关闭交换内存(何为交换内存,自行百度)
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
1. 基础依赖
yum -y install yum-utils device-mapper-persistent-data lvm2
2. 修改源地址
yum-config-manager -y --add-repo https://download.docker.com/linux/centos/docker-ce.repo
3. 安装docker
yum -y install docker-ce-18.06.3.ce-3.el7 docker-ce-cli-18.06.3.ce-3.el7 containerd.io
4. 设置国内docker仓库
/etc/docker/daemon.json文件registry-mirrors值修改为:
{
"registry-mirrors": [
"https://3laho3y3.mirror.aliyuncs.com"
]
}
5. 启动docker
systemctl start docker
systemctl enable docker
1. 配置kubernetes yum源
新增 /etc/yum.repos.d/kubernetes.repo文件,此处repo_gpgcheck为0
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
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
2. 安装Kubernetes基础服务及工具
yum -y install kubelet-1.13.12 kubeadm-1.13.12 kubectl-1.13.12
systemctl start kubelet
systemctl enable kubelet.service
此处可能会报错,缺少socat的包,先安装socat依赖即可
yum install socat
3. 下载k8s相关镜像并打标签
kubeadm config images list
编写个shell脚本文件,增加下列内容,执行文件即可
for i in `kubeadm config images list`; do
imageName=${i#k8s.gcr.io/}
docker pull registry.aliyuncs.com/google_containers/$imageName
docker tag registry.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
docker rmi registry.aliyuncs.com/google_containers/$imageName
done;
4. 初始化kubectl配置
cloud.weave.works域名已经不能访问了,因此得手动安装cni网络
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#cloud.weave.works域名已经不能访问了,因此我们得手动安装cni网络
kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')
5. 手动安装cni(用于K8S网络配置)
由于cloud.weave.works地址已经不能访问,我这边手动安装cni网络
我下载得是Release 2.8.1版本的weave-daemonset-k8s-1.11.yaml,可以从git上下载,也可以从我上传的资源文件里面下载
#githup下载weave-daemonset-k8s-1.11.yaml
https://github.com/weaveworks/weave/releases
#csdn下载weave-daemonset-k8s-1.11.yaml
https://download.csdn.net/download/Small_Green_Hand/86830603
#执行命令安装cni网络
kubectl create -f weave-daemonset-k8s-1.11.yaml
6. 初始化k8s和网络
kubeadm init --kubernetes-version=v1.13.12 --pod-network-cidr=10.100.0.0/16
#安装成功标志
#Your Kubernetes master has initialized successfully!
#开机启动 && 启动服务
systemctl enable kubelet && systemctl start kubelet
如果不安装cni网络,则会报错Unable to update cni config: No networks found in /etc/cni/net.d
7. 解除k8s master限制(默认k8s的master节点是不能跑pod的业务)
kubectl taint nodes --all node-role.kubernetes.io/master-
#如果不允许调度
#kubectl taint nodes master1 node-role.kubernetes.io/master=:NoSchedule
#污点可选参数
NoSchedule: 一定不能被调度
PreferNoSchedule: 尽量不要调度
NoExecute: 不仅不会调度, 还会驱逐Node上已有的Pod
此处会报错error: taint “node-role.kubernetes.io/master:” not found。不用管这个错误
8. 查看主节点运行 Pod 的状态
kubectl get pods --all-namespaces -o wide
1. 获取资源配置文件
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc5/aio/deploy/recommended.yaml
2.vim recommended.yaml
vim recommended.yaml
#定位到39行,修改其提供的service资源
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 31000
selector:
k8s-app: kubernetes-dashboard
3.部署pod应用
kubectl apply -f recommended.yaml
4.Token 方式认证登录
新增dashboard-adminuser.yml文件
#创建admin-user账户及授权的资源配置文件
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
---
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: kube-system
5.创建资源实例
kubectl create -f dashboard-adminuser.yml
6.获取账户admin-user的Token用于登录
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
token:
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tcjU3NTIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiODgwYTBjMGQtNDA3Yy0xMWVjLWJhOGYtMDAwYzI5MjI0NTA1Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.iNaIURxCV9ZeTz0CuIjrlB57q2edr6_zJL_wIn1_2MMaXra5mEcqSPwsOBPb4JK-R4DFBpBcmcyNOjNSmzi_fXYbdPDVt1LhMFhyqYCHY2lJmyIlzHusMoOLq2yZ5lMG7Z7_ot-wl5rKSX
7.登录验证
1、访问:https://ip:31000,使用上面的登录登录即可
当在K8S部署spring boot服务之前,必须把spring boot服务打包成docker镜像,并且上传到镜像仓库中,镜像仓库可以采阿里云,此处我自己创建了个仓库。
1.创建仓库
docker run -d -p 5000:5000 --restart=always --name registry registry:2
2.指定insecure-registries为本地仓库地址
修改/etc/docker/daemon.json文件insecure-registries的内容
ip:5000
1.准备一个spring boot服务
请读者自行准备
2.Dockerfile文件内容
编写Dockerfile文件,springboot-k8s-0.0.1-SNAPSHOT.jar名称请修改为你自己的jar包名
FROM openjdk:8-alpine
COPY ./target/springboot-k8s-0.0.1-SNAPSHOT.jar /app/springboot-k8s-demo.jar
ENTRYPOINT ["java", "-jar" , "/app/springboot-k8s-demo.jar"]
3.执行构建命令
docker build -t boot-k8s-demo:v1 #构建镜像
4.把镜像 push 到本地仓库
docker build -t ip:5000/boot-k8s-demo:v1 .
docker push ip:5000/boot-k8s-demo:v1
5.查看镜像
curl -X GET http://192.168.6.128:5000/v2/_catalog
curl -X GET http://192.168.6.128:5000/v2/spring-app/tags/list
6.编写部署springboot服务的yaml文件
vim springboot.yml,文件里面的boot-demo修改自己需要的,image对应的ip地址修改本地镜像地址
apiVersion: apps/v1
kind: Deployment #部署
metadata:
name: boot-demo
spec:
replicas: 1 #1个副本
selector:
matchLabels:
app: boot-demo
template:
metadata:
labels:
app: boot-demo
spec:
containers:
- name: boot-demo
image: ip:5000/boot-demo:v1 #刚刚push到阿里云上的镜像地址
ports:
- containerPort: 18081 #默认springboot端口
---
apiVersion: v1
kind: Service
metadata:
name: boot-demo
spec:
selector:
app: boot-demo #选中上面的 Deployment
ports:
- port: 18081 #对外7003端口
targetPort: 18081
---
#Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myingress
labels:
name: myingress
spec:
# IngressClass: nginx
rules:
- host: springboot.demo.com #所有的host这个域名请求 转发到上面的 Service= springboot-app
http:
paths:
#- pathType: Prefix
# path: "/"
- backend:
serviceName: boot-demo
servicePort: 18081
7.部署服务
kubectl create -f springboot.yaml
https://blog.csdn.net/weixin_45531306/article/details/121419041
https://blog.csdn.net/qq_34285557/article/details/124460872