一. mysql节点

搭建nfs服务器

# 安装
apt install nfs-kernel-server
# 创建文件夹
mkdir -p /root/sharedata/mysql
# 编辑
vim /etc/exports
/root/sharedata/mysql *(rw,sync,no_root_squash)
# 使配置生效,不用重启 nfs 服务器,客户端实时更新
exportfs -rv
# 启动
systemctl start rpcbind
systemctl start nfs-server
systemctl enable --now nfs-server
# 查看
showmount -e
# 说明
/root/sharedata/mysql        # 共享的目录
*               # 指客户端所有主机都可以使用, 也可以指定某个主机
rw              # 读写权限
no_root_squash  # 登入 NFS 主机使用者如果是 root 的话,那么他就具有 root 的权限

配置文件mysql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /root/sharedata/mysql
    server: 127.0.0.1

配置文件mysql-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

配置文件mysql-server.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  serviceName: "mysql"
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:8
        name: mysql
        env:
         - name: "MYSQL_ROOT_PASSWORD"
           value: '123456'
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

如果要使得mysql能被外部访问,可以将service部分改成如下(要删掉原service,否则会报错):

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    nodePort: 30080 #对k8s外部30080端口
    targetPort: 3306
  selector:
    app: mysql

分别执行

kubectl create -f mysql-pv.yaml
kubectl create -f mysql-pvc.yaml
kubectl create -f mysql-server.yaml

消除master节点的taint

kubectl taint nodes --all node-role.kubernetes.io/master-
kubectl taint nodes --all node-role.kubernetes.io/control-plane-

进入mysql

kubectl exec -it mysql-0 /bin/bash
mysql -uroot -p123456

二. springboot节点

dockerfile如下:

#!/bin/bash
# 使用Ubuntu作为基础镜像
FROM arm64v8/ubuntu:latest
 
# 维护人信息
MAINTAINER ethereal
 
# 切换到usr/local 目录下
WORKDIR ./usr/local
 
# 创建jdk目录
RUN mkdir jdk
 
# 对jdk赋权
RUN chmod 777 /usr/local/jdk
 
# 将下载的jdk 的压缩包拷贝到镜像中,注意 ADD和COPY的区别,ADD 会解压,COPY不会解压
ADD jdk-8u341-linux-aarch64.tar /usr/local/jdk
 
# 设置JAVA_HOME 的环境变量
ENV JAVA_HOME /usr/local/jdk/jdk1.8.0_341
 
# 设置JAVA 环境
ENV CLASSPATH=$JAVA_HOME/bin:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
 
# 将java可执行文件设置到PATH中,这样就可以使用java命令了
ENV PATH=.:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

COPY ./target/backend-0.0.1-SNAPSHOT.jar /app/spring-boot-k8s-app.jar

ENTRYPOINT ["java", "-jar" , "/app/spring-boot-k8s-app.jar"]

推送与拉取

# build
docker buildx build --platform arm64 -f Dockerfile -t jdk1.8 .
# tag
docker tag <imageID> registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1
# push
docker push registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1
# pull
crictl pull --creds aliyun8516592724(username):[password] registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1
# tag
ctr -n k8s.io i tag registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1 javabackend.io/springboot:v1

配置文件springboot.yaml

apiVersion: apps/v1
kind: Deployment #部署
metadata:
  name: springboot-app
spec:
  replicas: 2 #2个副本
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
      - name: springboot-app
        # image: registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1 #刚刚push到阿里云上的镜像地址
        image: javabackend.io/springboot:v1
        ports:
        - containerPort: 8080 #默认springboot端口 

---

apiVersion: v1
kind: Service
metadata:
  name: springboot-app
spec:
  type: NodePort
  selector:
    app: springboot-app #选中上面的 Deployment
  ports:
  - port: 8080 #对service外部8080端口
    nodePort: 30090 #对k8s外部30090端口
    targetPort: 8080

部署

kubectl apply -f springboot.yaml

三. redis节点

搭建nfs服务器

# 创建文件夹
mkdir /root/sharedata/redis
# 编辑
vim /etc/exports
/root/sharedata/redis *(rw,sync,no_root_squash)
# 使配置生效,不用重启 nfs 服务器,客户端实时更新
exportfs -rv

配置文件redis-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv-volume
  labels:
    type: local
spec:
  capacity:
    storage: 200M
  accessModes:
    - ReadWriteMany
  nfs:
    path: /root/sharedata/redis
    server: 127.0.0.1

配置文件redis.conf

ignore-warnings ARM64-COW-BUG
appendonly yes
cluster-enabled yes
cluster-config-file /var/lib/redis/nodes.conf
cluster-node-timeout 5000
dir /var/lib/redis
port 6379

配置文件redis-server.yaml

apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    app: redis
spec:
  ports:
  - name: redis-port
    port: 6379
  clusterIP: None
  selector:
    app: redis
    appCluster: redis-cluster

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: "redis"
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
        appCluster: redis-cluster
    spec:
      terminationGracePeriodSeconds: 20
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - redis
              topologyKey: kubernetes.io/hostname
      containers:
      - name: redis
        image: redis:7
        command:
          - "redis-server"
        args:
          - "/etc/redis/redis.conf"
          - "--protected-mode"
          - "no"
        resources:
          requests:
            cpu: "100m"
            memory: "100Mi"
        ports:
            - name: redis
              containerPort: 6379
              protocol: "TCP"
            - name: cluster
              containerPort: 16379
              protocol: "TCP"
        volumeMounts:
          - name: "redis-conf"
            mountPath: "/etc/redis"
          - name: "redis-data"
            mountPath: "/var/lib/redis"
      volumes:
      - name: "redis-conf"
        configMap:
          name: "redis-conf"
          items:
            - key: "redis.conf"
              path: "redis.conf"
  volumeClaimTemplates:
  - metadata:
      name: redis-data
    spec:
      accessModes: [ "ReadWriteMany" ]
      resources:
        requests:
          storage: 200M

分别执行

kubectl create -f redis-pv.yaml 
kubectl create configmap redis-conf --from-file=redis.conf
kubectl create -f redis-server.yaml 

分配slots

# 进入主机
kubectl exec -it redis-0 /bin/bash
# 检查
redis-cli --cluster check 127.0.0.1:6379
# 修复
redis-cli --cluster fix 127.0.0.1:6379
# 检验
redis-cli 
set a 1
get a
del a
get a

四. 附录

kubectl常用命令

kubectl get nodes
kubectl get pods
kubectl delete pod <podname>
kubectl describe pod <podname>
kubectl logs <podname>
kubectl exec -it <podname> /bin/bash

docker常用命令

docker images
docker rmi <imagesID>
docker rm <containerID>
docker buildx build --platform arm64 -f Dockerfile -t jdk1.8 .
docker tag [imageID] registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1
docker push registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1

crictl常用命令

crictl img
crictl rmi
crictl pull --creds aliyun8516592724(username):[password] registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1

ctr常用命令

ctr -n k8s.io i rm <imageID>
ctr -n k8s.io i tag registry.cn-shanghai.aliyuncs.com/ethereal-o/docker:v1 javabackend.io/springboot:v1
ctr -n=k8s.io i ls
ctr -n=k8s.io i ls|awk -F ' ' '{print $1}'

五. 参考

https://blog.csdn.net/mshxuyi/article/details/115102838

https://blog.csdn.net/asufeiya/article/details/119595862

https://blog.csdn.net/sebeefe/article/details/124473706

https://blog.csdn.net/zxc_123_789/article/details/122924616

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐