当前位置: 代码迷 >> 综合 >> Jenkins Config File Provider 插件 创建kubeconfig文件
  详细解决方案

Jenkins Config File Provider 插件 创建kubeconfig文件

热度:64   发布时间:2023-09-30 12:11:33.0

Jenkins Config File Provider 插件 创建kubeconfig文件

 

Config File Provider:存储kubectl用于连接k8s集群的kubeconfig配置文件,也就是下面这个文件

[root@master ~]# ls .kube/
cache  config  http-cache

所以要将这个文件保存到Jenkins的插件当中,在生成的slave pod当中,指定kubeconfig这个文件

 

 

Jenkins在Kubernetes中持续部署


自动部署应用(yaml):将kubectl工具封装到Slave镜像中,并通过Config File Provider插件存储连接K8s集群的kubeconfig认证文件,然后挂载到Slave容器中,这样就能用kubectl apply deploy.yaml --kubeconfig=config

注:为提高安全性,kubeconfig文件应分配权限
除了上述方式,还可以使用Kubernetes Continuous Deploy插件,将资源配置(YAML)部署到Kubernetes,这种不是很灵活性。

将kubeconfig存储在Jenkins,用于slave镜像里kubectl连接k8s集群

管理Jenkins-> Managed files->Add->Custom file ->Content字段内容是kubeconfig(默认路径在
master节点/root/.kube/config),然后复制ID替换上述脚本中k8s_auth变量的值。 

Jenkins Config File Provider 插件 创建kubeconfig文件 将里面的内容粘贴进去

[root@master ~]# cat .kube/config 
apiVersion: v1
clusters:
- cluster:certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeE1Ea3lOREV5TkRZek0xb1hEVE14TURreU1qRXlORFl6TTFvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBT2pVCnVERGlDcXBodjFrN1pGSVJHS0ovREh4cWUrWWZ4TU1waDY5ejVjK3doTUxrRDdueGxQbHpjK0JjQWxMa0FFRV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: cluster.local
contexts:
- context:cluster: cluster.localuser: kubernetes-adminname: kubernetes-admin@cluster.local
current-context: kubernetes-admin@cluster.local
kind: Config
preferences: {}
users:
- name: kubernetes-adminuser:client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURFekNDQWZ1Z0F3SUJBZ0lJQUovSDhvYzFxbmt3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TVRBNU1qUXhNalEyTXpOYUZ3MHlNakE1TWpReE1qUTJNelZhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXNkMVBKRTBlNHFEb2krZFcKTDhRZXpxaURacGFTQzZ2Y0dkMVhHa2IxUVVOY0Zscm9PY241RGVLZXpSSnc0R0dFVVVQVXZaY0JkZlAzVWpXVwpsTEJ6TWZocFRjaFlNVVRXQis2alhhOURqYitscW52VFRRM3UvRWhLMHZhcEJRNnZXdkFqR3A0UjZhWUZ4Rmh2Ckprb1BMSEpiYms3dk1TY0w1U3RRRk5NTVovVUZHNm1tNXViREJxS2pwTWVxQXgvWDExaHMxMVFHaXZ5QitGN3kKbkhWMTVSeDJtekdwYmNvb1o2aGpzWnJyZUozb0ZwTlZDSnpESGJacUlHTmFUZXBxY0RjZjhZUGlsZ3VDemh3NApqRzRMaVd6M1dpSnlvYUh6b1k0L0dJSVdYbDUwdFo2MGNlaXRqbWlqaUovYVdHVHJtZjQwV1poSXBZeUM1S05VCmVPWXI1d0lEQVFBQm8wZ3dSakFPQmdOVkhROEJBZjhFQkFNQ0JhQXdFd1lEVlIwbEJBd3dDZ1lJS3dZQkJRVUgKQXdJd0h3WURWUjBqQkJnd0ZvQVVGeGw5d3VkanhwN295d1d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NWRDMWpEcXl5R3A2SmhqRWJNL250ZG1RODNGY1VXTQpIYVh0M1lnMTJZd0JnU21IY25aNEg5RlpXekxPQ1ZhVXpNTjlnaStsOWlEOHdHTFdvT2FITE55QjBCNmlNclZqClpTYUpyb3FIUzNjT1o2VlNORGpCTVFLQmdFdjBCU1NhYUVGdHpDY2Nva1VwMHhpTmdQbDdNY1JsTm9xbXNyM0sKU0NzaURrUWRiL25VeHFsbzF6VWxaY0lucnFBVS9QTkpGSXZHQk9hdElqMjJpVjJycnQrMG5odVlPYXBocWFaTApZb0NlZitzTjEwSkJmZ1hOTjIxdXYwQU5neFNZR2FCVWVPd2lJN0piTVQvSUpOYWJuczdUSitHTFNMSjdXQ1Z2CjJQV2RBb0dCQUtVT2dxbkJFYzJpYnY5clUycm9nODVtc21OeXBaZUVWdE9zTkJQMUZMNHNLaHN1eUpsTkc4SFgKUml6K082eWJ4ZkpuVzcrOUN4NVplQ1hvOEJDYVhNQTdCK0JqUTNXdXNMTTlnb2UrV21HUmpaTTZhYXE1TTcyLwowdlRocjRVL1RiN2VVWkNiMFhRWHZLYUtobXhnZ3JKRW5oLzltMmNRaFRsYXE3dUFRc3plCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

Jenkins Config File Provider 插件 创建kubeconfig文件

Jenkins Config File Provider 插件 创建kubeconfig文件

configFileProvider([configFile(fileId: '22bdbd6d-14f4-49c1-a924-5c844073ea69', targetLocation: 'admin.kubeconfig')]) {// some block
}
        stage('Deploy') {steps {script{configFileProvider([configFile(fileId: '22bdbd6d-14f4-49c1-a924-5c844073ea69', targetLocation: "admin.kubeconfig")]){sh """sed -i 's#IMAGE_NAME#${image_name}#' deployment.yamlsed -i 's#REPLICAS#${ReplicaCount}#' deployment.yamlkubectl apply -f deployment.yaml -n ${Namespace} --kubeconfig=admin.kubeconfig"""}        }}
  相关解决方案