首页 > 数据库 >K3S+MySQL

K3S+MySQL

时间:2022-12-29 21:57:54浏览次数:61  
标签:K3S kubernetes created MySQL dashboard mysql docker name

K3S + MySQL

先更新apt-get

sudo apt-get update

下载curl

sudo apt install curl

安装docker

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

开启docker

systemctl start docker
docker version # 查看docker 是否开启
systemctl enable docker

容器镜像服务 (aliyun.com)开启容器镜像服务

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://t26vj3ej.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装k3s

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - --docker

看k3s状态

systemctl start k3s # 开启k3s
service k3s status

安装k3s dashboard

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml

编辑yaml文件:默认的yaml文件中 Dashboard 服务类型是ClusterIP,ClusterIP只能在集群内部访问,不能使用外部浏览器访问,我们可以选择将"ClusterIP"改为"NodePort"或"LoadBalancer",也可以使用ingress做代理,本示例选择使用LoadBalancer

# vim recommended.yaml
....
kind: Service
apiVersion: v1
metadata:
 labels:
   k8s-app: kubernetes-dashboard
 name: kubernetes-dashboard
 namespace: kubernetes-dashboard
spec:
 type: LoadBalancer                   # 新增,缺省情况下type默认是ClusterIP,我们这里指定type位LoadBalancer
 loadBalancerIP: 192.168.1.207        # 设置外部ip,此ip需要能够在宿主机使用
 ports:
   - port: 443
     targetPort: 8443
 selector:
   k8s-app: kubernetes-dashboard

部署,建议使用 apply 参数创建,而非 create 参数创建

# kubectl apply -f recommended.yaml
namespace/kubernetes-dashboard created
serviceaccount/kubernetes-dashboard created
service/kubernetes-dashboard created
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
secret/kubernetes-dashboard-key-holder created
configmap/kubernetes-dashboard-settings created
role.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/dashboard-metrics-scraper created
deployment.apps/dashboard-metrics-scraper created

查看namespace名为kubernetes-dashboard下的服务

kubectl get -n kubernetes-dashboard svc

image-20221229213045451

我这里的地址是https://192.168.1.207:30651

这里要配置用户管理

创建资源清单的yaml文件

# vim dashboard.admin-user.yaml
# 创建 ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
 name: admin-user
 namespace: kubernetes-dashboard
---
# 创建 ClusterRoleBinding
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

部署资源

kubectl apply -f dashboard.admin-user.yaml

获取token

sudo k3s kubectl -n kubernetes-dashboard create token admin-user

登录了之后就是这样的

image-20221229213311779

之后安装mysql

按照图下图操作,打开部署界面

46d84fff4a3c335b46fadab9a20e85e5

部署myql,部署用的yaml如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0.30-debian
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-data
        - name: mysql-config
          mountPath: /etc/mysql/mysql.conf.d
      volumes:
        - name: mysql-data
          hostPath:
            path: /root/data/mysql
            type: Directory
        - name: mysql-config
          hostPath:
            path: /root/data/mysql-config
            type: Directory

注意:1.其中mysql-config挂载,是为了自定义mysql启动配置。

​ mysql-data是mysql的数据文件,挂载出来可以持久化,避免数据文件丢失。

3304fa1c7031c3f6463947ca27df741a

myql的dockerhub镜像地址

image: mysql:8.0.30-debian

编辑完成后,点击‘Upload’即可部署成功。

此时,无法通过外网和服务间无法调用,需要部署service来实现,部署yaml如下

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
    nodePort: 30000

注意:通过NodePort方式,将服务开启在了30000端口。

ae8c6128ee8092ae2f703f0f49070c1f

之后,在本机上可以直接关闭防火墙

sudo ufw disable

之后,就可以用常用数据库连接工具连接了

QQ图片20221229213736

标签:K3S,kubernetes,created,MySQL,dashboard,mysql,docker,name
From: https://www.cnblogs.com/xcqandwrq/p/17013614.html

相关文章