Mac下kubernetes安装及搭建部署单机服务
安装
安装Docker Desktop;
更换阿里源
启用kubernetes
注意事项:
如果Docker Desktop中K8S一直卡在starting状态。
$ git clone https://github.com/AliyunContainerService/k8s-for-docker-desktop.git $ cd k8s-for-docker-desktop // 查看版本镜像;替换镜像的版本为你当前使用的版本,如:1.19.3 $ cat images.properties // 执行脚本安装K8S相关镜像 $ ./load_images.sh
安装完成后重启Docker Desktop即可。
验证K8s
// 查询有哪些集群: $ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE * docker-desktop docker-desktop docker-desktop // 切换k8s的上下文状态到docker-desktop $ kubectl config use-context docker-desktop Switched to context "docker-desktop". // 验证集群状态 $ kubectl cluster-info Kubernetes master is running at https://kubernetes.docker.internal:6443 KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy // 查看节点信息 $ kubectl get nodes NAME STATUS ROLES AGE VERSION docker-desktop Ready master 5m33s v1.19.3
K8s控制台安装
// 创建配置文件 $ kubectl create -f kubernetes-dashboard.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 // 检查kubernetes-dashboard的应用状态 $ kubectl get pod -n kubernetes-dashboard NAME READY STATUS RESTARTS AGE dashboard-metrics-scraper-7b59f7d4df-9xw7l 1/1 Running 0 18s kubernetes-dashboard-665f4c5ff-zcgbt 1/1 Running 0 18s // 开启API Server应用代理 $ kubectl proxy Starting to serve on 127.0.0.1:8001
配置访问令牌
TOKEN=$(kubectl -n kube-system describe secret default| awk '$1=="token:"{print $2}') kubectl config set-credentials docker-for-desktop --token="${TOKEN}" echo $TOKEN
将生成的token输入到仪表盘,登录
部署服务
本示例项目testk8s;Docker镜像已打包创建下,新建testk8s.yaml 配置文件
apiVersion: apps/v1 kind: Deployment metadata: name: testk8s-deployment spec: replicas: 1 selector: matchLabels: app: testk8s template: metadata: labels: app: testk8s spec: containers: - name: testk8s-controller image: testk8s:latest imagePullPolicy: IfNotPresent # Alway,IfNotPresent,Never ports: - containerPort: 8998 --- apiVersion: v1 kind: Service metadata: name: testk8s-service spec: ports: - port: 8998 protocol: TCP targetPort: 8998 nodePort: 30998 selector: app: testk8s type: NodePort
// 切换路径到项目下 $ cd /Users/jzpc/IdeaProjects/y_cloud/testK8s // 部署服务 $ kubectl apply -f testk8s.yaml deployment.apps/testk8s-deployment created service/testk8s-service created
仪表盘显示服务如下: