美文网首页
k8s学习笔记(1)

k8s学习笔记(1)

作者: PurelightMe | 来源:发表于2020-08-30 21:09 被阅读0次

    使用mac docker自带的k8s入门kubernetes:

    Deploy an App

    kubectl run k8s-redis --image=redis
    
    kubectl get deployments
    kubectl get pods
    
    kubectl port-forward pod-name 6379:6379
    

    Explore your App

    kubectl exec -it pod_name bash
    
    kubectl logs pod_name
    

    Expose your App Publicly

    kubectl get services
    kubectl expose deployment/k8s-redis --type="NodePort" --port 6379
    kubectl get services
    kubectl describe services/k8s-redis
    //找到NodePort一项,得到ip
    redis-cli -p ip_from_last_command //连接成功
    
    kubectl describe deployment //可以查看到相应的label
    kubectl get pods -l run=k8s-redis
    kubectl get services -l run=k8s-redis
    kubectl label pod k8s-redis-7f9776d9f4-jzm2m app=v1
    kubectl describe pods k8s-redis
    kubectl get pods -l app=v1
    
    kubectl delete service -l run=k8s-redis //这里用app=v1不行,会提示No resources found
    kubectl get services
    //仅仅删除service,pod还在的
    kubectl exec -it pod_name bash //里面的数据都还在
    

    Scale Your App

    kubectl get pods
    kubectl scale deployments/kubernetes-bootcamp --replicas=4
    kubectl get deployments
    kubectl get pods //将会显示4个pod
    kubectl describe deployments/kubernetes-bootcamp
    kubectl describe services/kubernetes-bootcamp //找到NodePort
    curl发现,这4个服务是负载均衡[轮询?]的
    kubectl scale deployments/kubernetes-bootcamp --replicas=2
    

    Update Your App

    kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
    kubectl get pods
    kubectl describe services/kubernetes-bootcamp //查看NodePort
    curl $(minikube ip):$NODE_PORT //内容已升级到v2
    输出类似:Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-cfc74666-qnps5 | v=2
    kubectl rollout status deployments/kubernetes-bootcamp
    kubectl describe pods
    kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
    kubectl get deployments //没有对应个数的实例
    kubectl describe pods //查看详情,发现是image未找到,现在回滚
    kubectl rollout undo deployments/kubernetes-bootcamp
    kubectl get pods //此时又看到之前的4个pods
    kubectl describe pods //查看image是v2的,回滚成功
    

    使用 https://github.com/gotok8s/k8s-docker-desktop-for-mac 安装K8S环境

    部署 Kubernetes Dashboard

    $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended.yaml
    
    # 开启本机访问代理
    $ kubectl proxy
    

    创建Dashboard管理员用户并用token登陆

    # 创建 ServiceAccount kubernetes-dashboard-admin 并绑定集群管理员权限
    $ kubectl apply -f https://raw.githubusercontent.com/gotok8s/gotok8s/master/dashboard-admin.yaml
    
    # 获取登陆 token
    $ kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep kubernetes-dashboard-admin | awk '{print $1}')
    

    通过下面的连接访问 Dashboard: http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

    输入上一步获取的token, 验证并登陆。

    实际遇到了 kubernetesui/metrics-scraper:v1.0.4 这个镜像下载不了的问题。使用阿里云镜像加速即可解决。

    相关文章

      网友评论

          本文标题:k8s学习笔记(1)

          本文链接:https://www.haomeiwen.com/subject/thvpsktx.html