美文网首页
使用kind快速搭建k8s学习环境

使用kind快速搭建k8s学习环境

作者: 怀旧的艾克 | 来源:发表于2019-10-16 18:21 被阅读0次

    首先准备机器

    我这边有个Ubuntu16.04 1core,2G的阿里云机器,用这个来学习

    配置终端zsh

    参考一键安装zsh和自动补全高亮:https://github.com/onesafe/easy_install_zsh

    安装go

    # 从官网https://golang.org/dl/ 下载
    wget https://dl.google.com/go/go1.13.1.linux-amd64.tar.gz
    
    # 解压到指定目录/usr/local
    tar zxvf go1.13.1.linux-amd64.tar.gz -C /usr/local
    
    # 配置环境变量
    cat ~/.bash_profile
    export PATH=$PATH:/usr/local/go/bin:$(go env GOPATH)/bin
    export GOPROXY=https://goproxy.cn
    export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
    

    安装docker

    apt-get update
    apt-get -y install apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    apt-get -y update
    apt-get -y install docker-ce
    

    安装kind

    # 执行下面的命令,k8s就创建好了
    GO111MODULE="on" go get sigs.k8s.io/kind@v0.5.1 && kind create cluster
    
    # 配置环境变量
    cat ~/.bash_profile
    export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
    
    # 下载kubelet,这里和kind的image版本对应一下,kind用的kindest/node:v1.15.3
    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kubectl
    mv kubectl /usr/local/bin/k
    

    查看k8s环境是否已经装好

    创建deployment和service,看看k8s是否真的ready

    创建一个文件k8s-test-nginx.yml,内容如下

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      replicas: 2
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.7.9
            ports:
            - containerPort: 80
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: my-nginx
      labels:
        app: nginx
    spec:
      ports:
      - name: my-nginx-port
        port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: nginx
      type: NodePort
    

    执行命令创建

    k create -f k8s-test-nginx.yml
    

    查看部署结果

    访问nginx服务

    # 查看k8s的node(用docker模拟的)的IP
    ➜  /mnt docker inspect kind-control-plane --format '{{.NetworkSettings.Networks.bridge.IPAddress}}'
    172.17.0.3
    

    可以看到我们的服务成功部署了

    试用curl访问k8s的api

    # 启动一个反向代理
    k proxy --port=8080 &
    
    # 访问k8s的watch API
    curl -i http://localhost:8080/api/v1/watch/pods\?watch\=yes
    HTTP/1.1 200 OK
    Content-Type: application/json
    Date: Wed, 16 Oct 2019 10:18:44 GMT
    Transfer-Encoding: chunked
    
    {"type":"ADDED", "object":{"kind":"Pod","apiVersion":"v1",...}}
    {"type":"ADDED", "object":{"kind":"Pod","apiVersion":"v1",...}}
    {"type":"MODIFIED", "object":{"kind":"Pod","apiVersion":"v1",...}}
    

    相关文章

      网友评论

          本文标题:使用kind快速搭建k8s学习环境

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