kubebuilder实战之一:准备工作

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos

系列文章链接

  1. kubebuilder实战之一:准备工作
  2. kubebuilder实战之二:初次体验kubebuilder
  3. kubebuilder实战之三:基础知识速览
  4. kubebuilder实战之四:operator需求说明和设计
  5. kubebuilder实战之五:operator编码
  6. kubebuilder实战之六:构建部署运行
  7. kubebuilder实战之七:webhook
  8. kubebuilder实战之八:知识点小记

关于kubebuilder

  1. 在实际工作中,对kubernetes的资源执行各种个性化配置和控制是很常见的需求,例如自定义镜像的pod如何控制副本数、主从关系,以及各种自定义资源的控制等;
  2. 对于上述需求,很适合使用Operator 模式来解决,这里有官方对Operator的介绍:https://kubernetes.io/zh/docs/concepts/extend-kubernetes/operator/ ,Operator模式的执行流程如下图所示:
    在这里插入图片描述
  3. 为了简化Operator开发,我们可以选用一些已有的开源工具,kubebuilder就是其中之一,《kubebuilder实战》系列就是对此工具由浅入深的实践;

本篇概览

作为《kubebuilder实战》系列的开篇,除了前面对kubebuilder的简单说明,还会列出整个实战的通用环境信息,以及涉及到的软件版本,然后再搭建好kubebuilder开发环境,总的来说需要做好以下准备工作,才能顺利开始kubebuilder的开发工作:

  1. kubectl安装和配置,这样可以在kubebuilder电脑上操作kubernetes环境;
  2. 安装golang
  3. 安装docker
  4. 安装kustomize
  5. 安装kubebuilder

环境信息

如下图,整个实战环境一共由两台电脑组成:
在这里插入图片描述

  1. kubernetes电脑:hostname是k8s,上面运行着1.20版本的kubernetes,关于kubernetes的部署不是本文重点,请参考其他教程完成,需要确保kubernetes正常可用;
  2. kubebuilder电脑:操作系统是CentoOS-7.9.2009,hostname是kubebuilder,咱们的实战就在这台电脑上操作;
  3. kubebuilder版本:2.3.1
  4. go版本:1.15.6
  5. docker版本:19.03.13
  6. 为了省事儿,所有操作都是用root帐号执行的;

kubectl安装和配置

  1. 执行以下命令在线安装:
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

如果您无法访问上述地址,可以在此下载(0积分):https://download.csdn.net/download/boling_cavalry/14925948

  1. kubectl文件下载到linux机器后,执行命令chmod +x ./kubectl,给文件可执行权限;
  2. 执行以下命令,将kubectl移动到可以全局访问的目录下:
mv ./kubectl /usr/local/bin/kubectl
  1. 新建~/.kube/目录;
  2. 登录k8s电脑,在~/.kube/目录下有个config文件,将此文件复制到kubebuilder电脑的~/.kube/目录下;
  3. 回到kubebuilder电脑,此时执行kubectl命令可以直接操作k8s电脑上的kubernetes了:
[root@kubebuilder ~]# kubectl get nodes -o wide
NAME   STATUS   ROLES                  AGE   VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
k8s    Ready    control-plane,master   33m   v1.20.2   192.168.133.211   <none>        CentOS Linux 7 (Core)   3.10.0-1160.11.1.el7.x86_64   docker://19.3.13
[root@kubebuilder ~]# 
[root@kubebuilder ~]# kubectl get pod --all-namespaces
NAMESPACE     NAME                          READY   STATUS    RESTARTS   AGE
kube-system   coredns-7f89b7bc75-fw928      1/1     Running   0          33m
kube-system   coredns-7f89b7bc75-tv7tk      1/1     Running   0          33m
kube-system   etcd-k8s                      1/1     Running   0          33m
kube-system   kube-apiserver-k8s            1/1     Running   0          33m
kube-system   kube-controller-manager-k8s   1/1     Running   0          33m
kube-system   kube-flannel-ds-q9f64         1/1     Running   0          32m
kube-system   kube-proxy-clmbf              1/1     Running   0          33m
kube-system   kube-scheduler-k8s            1/1     Running   0          33m

安装golang

  1. 安装必要应用:
yum install unzip tree wget gcc gcc-c++ kernel-devel -y
  1. 用我准备好的脚本安装golang并设置好环境变量:
curl -o install-go.sh \
https://raw.githubusercontent.com/zq2599/blog_demos/master/files/install-go.sh \
&& chmod a+x ./install-go.sh \
&& ./install-go.sh
  1. 执行完上述命令后,控制台会输出类似下面的内容,可见hello.go文件可以被成功执行,表示go环境部署成功,并且输出的环境变量也是正常的:
...
5. create go source file
package main

import "fmt"

func main() {
fmt.Println("Hello world!")
}
6. run hello.go
Hello world!
go1.15.6 install and check finished
  1. 执行source .bashrc确保当前shell环境中的环境变量已经生效;

安装docker

  1. 执行以下命令完成docker的安装和启动:
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates | sort -r
yum -y install docker-ce-19.03.13
systemctl start docker
systemctl enable docker
  1. 验证安装是否成功:
[root@kubebuilder ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.2
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        2291f61
 Built:             Mon Dec 28 16:17:48 2020
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:02:21 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
  1. 设置镜像加速,否则下载镜像慢并且时常超时,执行以下命令完成镜像加速的设置并使其生效:
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

安装kustomize

后面的操作中需要使用kustomize去做配置管理,执行以下命令即可安装:

mkdir -p $GOPATH/bin
cd $GOPATH/bin
GOBIN=$(pwd)/ GO111MODULE=on go get sigs.k8s.io/kustomize/kustomize/v3

安装kubebuilder

  1. 以下脚本通过go命令确定当前系统和CPU架构,再去服务器下载对应的kubebuilder文件,然后设置环境变量:
os=$(go env GOOS)
arch=$(go env GOARCH)
curl -L https://go.kubebuilder.io/dl/2.3.1/${os}/${arch} | tar -xz -C /tmp/
mv /tmp/kubebuilder_2.3.1_${os}_${arch} /usr/local/kubebuilder
export PATH=$PATH:/usr/local/kubebuilder/bin
  1. 执行命令确认安装成功:
[root@kubebuilder ~]# kubebuilder version
Version: version.Version{KubeBuilderVersion:"2.3.1", KubernetesVendor:"1.16.4", GitCommit:"8b53abeb4280186e494b726edf8f54ca7aa64a49", BuildDate:"2020-03-26T16:42:00Z", GoOs:"unknown", GoArch:"unknown"}
  • 至此,kubebuilder开发环境的准备工作就完成了,接下来的章节咱们正式进入开发实战,一起去学习精彩的kubernetes operator;

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢迎关注公众号:程序员欣宸

微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界…

已标记关键词 清除标记
相关推荐
<div class="post-text" itemprop="text"> <p>My probelm is that i am trying to use the <code>unstructured.Unstructured</code> type to create a deployment as such:</p> <pre><code>// +kubebuilder:rbac:groups=stable.resource.operator.io,resources=resource,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=stable.resource.operator.io,resources=resource/status,verbs=get;update;patch // +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;list;watch;create;update;patch;delete func (r *ResourceReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { ctx := context.Background() log := r.Log.WithValues("resource", req.NamespacedName) instance := &stablev1.Resource{} // your logic here if err := r.Get(ctx, req.NamespacedName, instance); err != nil { log.Error(err, "unable to fetch Resource") // we'll ignore not-found errors, since they can't be fixed by an immediate // requeue (we'll need to wait for a new notification), and we can get them // on deleted requests. return ctrl.Result{}, ignoreNotFound(err) } // your logic here u := &unstructured.Unstructured{} u.Object = map[string]interface{}{ "name": "name", "namespace": "namespace", "spec": map[string]interface{}{ "replicas": 2, "selector": map[string]interface{}{ "matchLabels": map[string]interface{}{ "foo": "bar", }, }, "template": map[string]interface{}{ "labels": map[string]interface{}{ "foo": "bar", }, "spec": map[string]interface{}{ "containers": []map[string]interface{}{ { "name": "nginx", "image": "nginx", }, }, }, }, }, } u.SetGroupVersionKind(schema.GroupVersionKind{ Group: "apps", Kind: "Deployment", Version: "v1", }) err = r.Create(context.Background(), u) log.Error(err, "unable to get object") log.V(1).Info("reconciling") return ctrl.Result{}, nil } </code></pre> <p>My understanding is that I have specified the rbac rules, my operator should be able to create said Deployment, but I am still getting the error:</p> <pre><code>the server does not allow this method on the requested resource </code></pre> <p>All the examples I have seen are based of using the actual deployment type, I cant find anywhere where there is an example of doing this with type unstructured, am i missing something? Just to save time, I have tried:</p> <ul> <li>Applying the clusterroles manually</li> <li>Given the operator cluster-admin</li> <li>used both make run and make deploy (obviously after running make manifests etc..)</li> <li>The roles generator is working</li> <li>I have started a new project to make sure me playing around with env is not the cause</li> </ul> </div>
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页