1. 修改docker mirror为阿里云:

    vim /etc/docker/daemon.json
    

    内容如下:

    {
      "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
    }
    
  2. 运行rancher镜像:

    docker run --name rancher -d --restart=unless-stopped \
    -p 80:80 -p 443:443 \
    rancher/rancher:stable
    
  3. 修改主机的hosts,方便访问:

    vim /etc/hosts
    127.0.0.1    rancher.site
    
  4. 添加节点:

    sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.0.8 --server https://rancher.site --token bnlvxdc8gqzc4sqvlrd6q2tbbk2f77lk4wsw66ns77rsdtx2kdsvzv --ca-checksum fae7cc19ed6a446b8429c42403c9e7a5fb957a26c7705343e61e6e104aa28497 --etcd --controlplane --worker
    
  5. 等待kubernetes安装完成:

    https://rancher.site/c/c-9lc2x
    此集群当前为Provisioning,在API准备就绪之前,直接与其交互的功能将不可用。    
    

    安装完毕之后,访问:https://rancher.site ,设置用户名密码。

  6. 为defalut service account授权(参考:https://jimmysong.io/kubernetes-handbook/concepts/rbac.html ),在主机上执行kubectl命令:

    kubectl create clusterrolebinding serviceaccounts-cluster-admin \
    --clusterrole=cluster-admin \
    --group=system:serviceaccounts
    
  7. 修改主机的/etc/resolv.conf,添加自定义DNS,这样pod内部的container才能访问外部网络的DNS:

    vim /etc/resolv.conf
    # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    #    DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    nameserver 114.114.114.114
    nameserver 8.8.8.8
    
  8. rancher运行jenkins镜像(带kubectl):

    registry.cn-beijing.aliyuncs.com/im_wower/jenkins-in-kubernetes:lts-k8s
    
  9. 在jenkins的pod内部配置kubectl(参考:https://github.com/imwower/docker-kubectl ):

    	kubectl config set-cluster dev --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt --embed-certs=true --server="https://kubernetes.default/"
    	kubectl config set-credentials user --token="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"
    	kubectl config set-context default --cluster=dev --user=user
    	kubectl config use-context default
    
  10. 测试kubectl是否正常:

    kubectl get services
    
  11. 创建setup_kubectl.sh脚本,用于启动container时自动配置kubectl config:

    vim setup_kubectl.sh
    #!/bin/sh
    
    kubectl config set-cluster dev --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt --embed-certs=true --server="https://kubernetes.default/"
    kubectl config set-credentials user --token="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"
    kubectl config set-context default --cluster=dev --user=user
    kubectl config use-context default
    echo "finished."
    /usr/local/bin/jenkins.sh  #用来启动jenkins
    
  12. k8s部署jenkins的服务时,添加命令行(需要先将脚本以volume的形式挂载到pod上):

    bash /var/jenkins_home/kube/setup_kubectl.sh
    

  13. jenkins.yaml示例文件: https://gist.github.com/imwower/afdd8247179656b3f1cc3899740bb275

转载于:https://my.oschina.net/wower/blog/2050735

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐