环境配置

  • ubuntu:16.04
  • docker:20.10.7
  • golang:1.17.8
  • kubernetes:1.23.4

环境的搭建就不多赘述了,可以参照网上的资源自行搭建。

下载k8s源码并编译

cd ~/go#环境变量GOPATH=$HOME/go
git clone https://github.com/kubernetes/kubernetes.git

下载完成后,进入kubernetes文件夹编译kube-scheduler

cd kubernetes/
CGO_ENABLED=0 GOOS=linux GOARCH=amd64  make WHAT=cmd/kube-scheduler/

这里需要注意的是,CGO_ENABLED=0必须要有,后面两个选项可有可无,这是为了让项目编译时,把依赖库文件也链接进可执行文件中,如果不指定这个选项,后面启动容器时会失败,pod会一直处于CrashLoopBackOff的状态。
WHAT=cmd/kube-scheduler/告诉编译器只编译kube-scheduler模块,生成的文件在kubernetes/_output/local/bin/linux/amd64/路径下。

制作镜像

创建一个文件夹,并添加如下两个文件:
在这里插入图片描述
其中Dockerfile的内容为:

FROM busybox
ADD ./kube-scheduler /usr/local/bin/kube-scheduler

如果后面运行调度器时,遇到如下问题:
在这里插入图片描述
可以在文件中加一句:

FROM busybox
ADD ./kube-scheduler /usr/local/bin/kube-scheduler
RUN chmod a+x /usr/local/bin/kube-scheduler

kube-scheduler文件即为之前编译生成的可执行文件,从原本的路径复制过来。
在两个文件所在的目录下,执行镜像构建命令:

docker build -t XXX/kube-scheduler:1.0 .

XXX改成你的docker仓库名。
推送镜像:

docker push XXX/kube-scheduler:1.0

部署deployment

.yaml文件内容:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-scheduler
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: my-scheduler-as-kube-scheduler
subjects:
- kind: ServiceAccount
  name: my-scheduler
  namespace: kube-system
roleRef:
  kind: ClusterRole
  name: system:kube-scheduler
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scheduler-config
  namespace: kube-system
data:
  my-scheduler-config.yaml: |
    apiVersion: kubescheduler.config.k8s.io/v1beta2
    kind: KubeSchedulerConfiguration
    profiles:
      - schedulerName: my-scheduler
    leaderElection:
      leaderElect: false    
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: my-scheduler-as-volume-scheduler
subjects:
- kind: ServiceAccount
  name: my-scheduler
  namespace: kube-system
roleRef:
  kind: ClusterRole
  name: system:volume-scheduler
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    component: scheduler
    tier: control-plane
  name: my-scheduler
  namespace: kube-system
spec:
  selector:
    matchLabels:
      component: scheduler
      tier: control-plane
  replicas: 1
  template:
    metadata:
      labels:
        component: scheduler
        tier: control-plane
        version: second
    spec:
      serviceAccountName: my-scheduler
      containers:
      - command:
        - /usr/local/bin/kube-scheduler
        - --config=/etc/kubernetes/my-scheduler/my-scheduler-config.yaml
        image: XXX/my-kube-scheduler:1.0 #修改成你的镜像名
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10259
            scheme: HTTPS
          initialDelaySeconds: 15
        name: kube-second-scheduler
        readinessProbe:
          httpGet:
            path: /healthz
            port: 10259
            scheme: HTTPS
        resources:
          requests:
            cpu: '0.1'
        securityContext:
          privileged: false
        volumeMounts:
          - name: config-volume
            mountPath: /etc/kubernetes/my-scheduler
      hostNetwork: false
      hostPID: false
      volumes:
        - name: config-volume
          configMap:
            name: my-scheduler-config


注意修改文件中加注释的部分,改成你的镜像名。

执行如下命令:

kubectl create -f my-scheduler.yaml

查看pod状态

kubectl get pods -n kube-system

在这里插入图片描述
名为my-scheduler-xxx-xxxpod运行正常。

纠错

有两个命令比较有用:

kubectl describe pod PODNAME#输出pod的详细信息
kubectl logs -f PODNAME#输出pod的日志

对报错信息积极百度,总能找到解决办法。

结束

祝各位顺利。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐