k8s 部署kubernetes-dashboard

参考:

K8s-k8s集群部署-8-部署Dashboard_8. 部署 dashboard-CSDN博客k8s-kubernetes常用命令,服务部署,可视化控制台安装及token的生成_kubernetes-dashboard 生成token-CSDN博客

  • 首先下载yaml文件

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

// 查看yaml里面使用的kubernetes-dashboard 和 metrics-scraper的版本

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      securityContext:
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: kubernetes-dashboard
          image: kubernetesui/dashboard:v2.7.0



kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: dashboard-metrics-scraper
  name: dashboard-metrics-scraper
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: dashboard-metrics-scraper
  template:
    metadata:
      labels:
        k8s-app: dashboard-metrics-scraper
    spec:
      securityContext:
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: dashboard-metrics-scraper
          image: kubernetesui/metrics-scraper:v1.0.8
  • 部署dashboard

  • 如果网络没问题,就直接执行下面的命令,应该就直接部署好了,可以跳过下面的步骤,直接看创建用户并用token登录那部分
$ kubectl apply -f recommended.yaml
  • 如果出现image pull error,那么就需要离线部署
//首先下载离线包
$ sudo docker pull kubernetesui/dashboard:v2.7.0
$ sudo docker save  kubernetesui/dashboard:v2.7.0 -o dashboard.tar
$ sudo docker pull kubernetesui/metrics-scraper:v1.0.8
$ sudo docker save kubernetesui/metrics-scraper:v1.0.8 -o metrics-scraper.tar

//然后加载到k8s命名空间中
$ sudo ctr -n k8s.io images import dashboard.tar
$ sudo ctr -n k8s.io images import metrics-scraper.tar

//修改yaml,在kubernetes-dashboard和metrics-scraper中加下面内容
containers:
        - name: kubernetes-dashboard
          image: kubernetesui/dashboard:v2.7.0
          imagePullPolicy: IfNotPresent

containers:
        - name: dashboard-metrics-scraper
          image: kubernetesui/metrics-scraper:v1.0.8
          imagePullPolicy: IfNotPresent


//最后部署dashboard
$ kubectl apply  -f recommended.yaml

  • 查看pod,svc

$  kubectl get pod,svc -n kubernetes-dashboard
NAME                                             READY   STATUS    RESTARTS   AGE
pod/dashboard-metrics-scraper-5657497c4c-fr2gh   1/1     Running   0          21m
pod/kubernetes-dashboard-5b749d9495-6db76        1/1     Running   0          21m

NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)         AGE
service/dashboard-metrics-scraper   ClusterIP   10.109.181.241   <none>        8000/TCP        21m
service/kubernetes-dashboard        NodePort    10.106.145.226   <none>        443:32331/TCP   21m

kubernetes-dashboard暴露的访问端口号是32331,然后可以通过网页访问UI,在登录时需要输入token

  • 创建用户及角色

需要新建一个用户用于登录dashboard,创建下面的dashboard-adminuser.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


// 执行下面命令创建用户
$ kubectl apply -f  dashboard-adminuser.yaml

创建了一个名为admin-user的用户

  • 生成访问token

用下面的命令生成token

$ kubectl -n kubernetes-dashboard create token admin-user
eyJhbGciOdasdasdaiJSUzI1NiIsImtpZCI6IlloQUZ3MzhTTVJsZWw4QjZtU0w4V2d1ZkpheU15SGd1OTFKZHlVazVQN1EifQ.eyJhdWQiOlsiaHR0cHM6Ly9rdWJlcm5ldGVzLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWwiXSwiZXhwIjoxNzIzNjMxNjc0LCJpYXQiOjE3MjM2MjgwNzQsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsInNlcnZpY2VhY2NvdW50Ijp7Im5hbWUiOiJhZG1pbi11c2VyIiwidWlkIjoiZTZiZmFmOWEtMDM3OC00MWQ0LWIwNGUtZjYyMDc1OWQ5MTdjIn19LCJuYmYiOjE3MjM2MjgwNzQsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlcm5ldGVzLWRhc2hib2FyZDphZG1pbi11c2VyIn0.45HWFcn9uFEn1M1C6yMMmf8UniUtv882N6QHjrpH3Jcas0DnHFZIZzriRk5WoY7FbTmurv6avXJx3hlf9Pm-enhfqziiSZuCxhdH0orz7njnC6z8A5AJkHZualRwvyQZkJFryQthFfetHn_5JMiadRr8XxYBdU3uspzGNmcVlm03NccAuCYd0UN-zRJqU69_OPiWVBpgJ8jQPdIKkUVhI5U5aNb78n4zQc9OMp70WWBYg5S_Z2VL66j44jadZfDfWqLVHg8ornH3RcZ-OwXZggfzho_n2HE3OLbk1Guxq_A-OZAhn5PRkPpwkLbIPDEOtx1fhxsIuTobWOPXXDWD6g

//也可以用--duration 指定token有效期
$ kubectl -n kubernetes-dashboard create token admin-user --duration=720h

 然后在网页上就可以用这个token登录dashboard了

部署完成。

Kubernetes DashboardKubernetes 官方提供的一个 Web 界面,用于管理 Kubernetes 集群。以下是部署 Kubernetes Dashboard 的步骤: 1. 下载 Kubernetes Dashboard YAML 文件: ```bash curl -LO https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml ``` 2. 修改 Kubernetes Dashboard Service 类型: 将 recommended.yaml 文件中的 Service 类型从 ClusterIP 修改为 NodePort,这样可以通过 Node 的 IP 地址加端口号访问 Kubernetes Dashboard。 ```yaml apiVersion: v1 kind: Service metadata: name: kubernetes-dashboard namespace: kubernetes-dashboard labels: k8s-app: kubernetes-dashboard spec: type: NodePort # 将类型修改为 NodePort ports: - port: 443 targetPort: 8443 selector: k8s-app: kubernetes-dashboard ``` 3. 部署 Kubernetes Dashboard: ```bash kubectl apply -f recommended.yaml ``` 4. 创建 Service Account: ```bash cat <<EOF | kubectl apply -f - apiVersion: v1 kind: ServiceAccount metadata: name: admin-user namespace: kubernetes-dashboard EOF ``` 5. 创建 Cluster Role Binding: ```bash cat <<EOF | kubectl apply -f - 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 EOF ``` 6. 获取 Token: ```bash kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}') ``` 7. 访问 Kubernetes Dashboard: 使用浏览器访问 Node 的 IP 地址加端口号,输入 Token 登录即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值