企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
kubernetes:一个管理容器组的工具,具有先进的调度能力 [Docker生态系统系列之一:常用组件介绍](https://yq.aliyun.com/articles/224629?spm=a2c4e.11153959.blogcont11035.12.5883ee043iKaV6) [kubernetes1.9离线部署](https://segmentfault.com/a/1190000012755243) ### 一、准备 #### 1. 前提 > 先安装etcd环境,etcd可以是集群,可以是单点。然后安装flannel。 flannel在 node上都需要安装。master可以不安装。 >当然,可以在master的机器上作为一个node。只需要安装node的要求安装所需服务。 #### 2. 节点所需 | master | node | | ------------- |:-------------:| | kube-apiserver | kubelet | | kube-controller-manager | kube-proxy| | kube-scheduler | .... | #### 3. Kubernetes下载 [下载地址](https://github.com/kubernetes/kubernetes/releases?after=v1.7.12-beta.0) > 下载二进制文件由于访问外网,网络原因可能下载不了。可以编译源代码获取二进制部署文件。 见[编译kubernetes](build_kubernetes.md) **获取到二进制安装包之后,将相关可执行文件拷贝到/usr/bin/目录中。** ``` kube-apiserver kube-controller-manager kube-scheduler kubectl kube-proxy ``` ### 二、Kubernetes安装配置 #### 1. Kubernetes通用配置文件 - 创建 Kubernetes 配置目录 ``` sudo mkdir /etc/kubernetes ``` - 创建 Kubernetes 通用配置 ``` sudo vim /etc/kubernetes/config ``` ``` KUBE_LOGTOSTDERR="--logtostderr=true" KUBE_LOG_LEVEL="--v=0" KUBE_ALLOW_PRIV="--allow-privileged=false" KUBE_MASTER="--master=http://{master}:8080" ``` #### 2. 配置kube-apiserver服务 在Kubernetes的master主机上 ``` sudo vim /etc/kubernetes/apiserver ``` ``` ### # kubernetes system config # # The following values are used to configure the kube-apiserver # # The address on the local server to listen to. KUBE_API_ADDRESS="--address=0.0.0.0" #KUBE_API_ADDRESS="--insecure-bind-address=127.0.0.1" # The port on the local server to listen on. KUBE_API_PORT="--port=8080" # Port minions listen on KUBELET_PORT="--kubelet-port=10250" # Comma separated list of nodes in the etcd cluster KUBE_ETCD_SERVERS="--etcd-servers=http://{ectd_ip}:2379" # Address range to use for services KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range={192.168.4.0/24}" # default admission control policies KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,ResourceQuota" # Add your own! KUBE_API_ARGS="" ``` KUBE_SERVICE_ADDRESSES 配置为etcd创建的可用网段地址 - 创建systemd文件 ``` sudo vim /lib/systemd/system/kube-apiserver.service ``` ``` [Unit] Description=Kubernetes API Server Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=network.target After=etcd.service Wants=etcd.service [Service] User=root EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/apiserver ExecStart=/usr/bin/kube-apiserver \ $KUBE_LOGTOSTDERR \ $KUBE_LOG_LEVEL \ $KUBE_ETCD_SERVERS \ $KUBE_API_ADDRESS \ $KUBE_API_PORT \ $KUBELET_PORT \ $KUBE_ALLOW_PRIV \ $KUBE_SERVICE_ADDRESSES \ $KUBE_ADMISSION_CONTROL \ $KUBE_API_ARGS Restart=on-failure Type=notify LimitNOFILE=65536 [Install] WantedBy=multi-user.target ``` #### 3. 配置kube-controller-manager服务 ``` sudo vim /etc/kubernetes/controller-manager ``` ``` KUBE_CONTROLLER_MANAGER_ARGS="" ``` - 创建systemd文件 ``` sudo vim /lib/systemd/system/kube-controller-manager.service ``` ``` [Unit] Description=Kubernetes Controller Manager Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=etcd.service After=kube-apiserver.service Requires=etcd.service Requires=kube-apiserver.service [Service] User=root EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/controller-manager ExecStart=/usr/bin/kube-controller-manager \ $KUBE_LOGTOSTDERR \ $KUBE_LOG_LEVEL \ $KUBE_MASTER \ $KUBE_CONTROLLER_MANAGER_ARGS Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target ``` #### 4. 配置kube-scheduler服务 ``` sudo vim /etc/kubernetes/scheduler ``` ``` KUBE_SCHEDULER_ARGS="" ``` - 创建systemd文件 ``` sudo vim /lib/systemd/system/kube-scheduler.service ``` ``` [Unit] Description=Kubernetes Scheduler Documentation=https://github.com/kubernetes/kubernetes [Service] User=root EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/scheduler ExecStart=/usr/bin/kube-scheduler \ $KUBE_LOGTOSTDERR \ $KUBE_MASTER Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target ``` #### 5. 启动Kubernetes master节点的服务 ``` sudo systemctl daemon-reload sudo systemctl enable kube-apiserver kube-controller-manager kube-scheduler sudo systemctl start kube-apiserver kube-controller-manager kube-scheduler ``` #### 6. Kubernetes node配置 Kubernetes node节点也需要配置 **/etc/kubernetes/config** 文件,内容与Kubernetes mater节点一致。 #### 7. 配置kubelet服务 每个节点都需要配置。 在kubernetes集群中,每个Node节点都会启动kubelet进程,用来处理Master节点下发到本节点的任务,管理Pod和其中的容器。 [Kubernetes核心原理(四)之Kubelet](http://blog.csdn.net/huwh_/article/details/77922293) - 创建kubelet的数据目录 ``` sudo mkdir /var/lib/kubelet ``` - 创建kubelete配置文件 ``` sudo vim /etc/kubernetes/kubelet ``` ``` KUBELET_ADDRESS="--address=127.0.0.1" KUBELET_HOSTNAME="--hostname-override=node_out_ip" KUBELET_API_SERVER="--api-servers=http://kub_master_ip:8080" # pod infrastructure container KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.RedHat.com/rhel7/pod-infrastructure:latest" KUBELET_ARGS="--enable-server=true --enable-debugging-handlers=true" ``` - 创建systemd文件 ``` sudo vim /lib/systemd/system/kubelet.service ``` ``` [Unit] Description=Kubernetes Kubelet Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=docker.service Requires=docker.service [Service] WorkingDirectory=/var/lib/kubelet EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/kubelet ExecStart=/usr/bin/kubelet \ $KUBE_LOGTOSTDERR \ $KUBE_LOG_LEVEL \ $KUBELET_API_SERVER \ $KUBELET_ADDRESS \ $KUBELET_PORT \ $KUBELET_HOSTNAME \ $KUBE_ALLOW_PRIV \ $KUBELET_POD_INFRA_CONTAINER \ $KUBELET_ARGS Restart=on-failure KillMode=process [Install] WantedBy=multi-user.target ``` - 启动kubelet服务 ``` sudo systemctl daemon-reload sudo systemctl enable kubelet sudo systemctl start kubelet ``` #### 8. 配置kube-proxy服务 - 创建kube-proxy配置文件 ``` sudo vim /etc/kubernetes/proxy ``` ``` # kubernetes proxy config # default config should be adequate # Add your own! KUBE_PROXY_ARGS="" ``` - 创建systemd文件 ``` sudo vim /lib/systemd/system/kube-proxy.service ``` ``` [Unit] Description=Kubernetes Proxy Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=network.target [Service] EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/proxy ExecStart=/usr/bin/kube-proxy \ $KUBE_LOGTOSTDERR \ $KUBE_LOG_LEVEL \ $KUBE_MASTER \ $KUBE_PROXY_ARGS Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target ``` - 启动kube-proxy服务 ``` sudo systemctl daemon-reload sudo systemctl enable kube-proxy sudo systemctl start kube-proxy ``` - 查询node状态 执行kubectl get node命令来查看node状态。都为Ready状态时,则说明node节点已经成功连接到master,如果不是该状态,则需要到该节点上,定位下原因。可通过journalctl -u kubelet.service命令来查看kubelet服务的日志。 ``` kubectl get node ``` #### 9. Kubernetes测试 测试Kubernetes是否成功安装。 - 编写yaml文件 在Kubernetes master上创建一个nginx.yaml,用于创建一个nginx的ReplicationController。 ``` vim rc_nginx.yaml ``` ``` apiVersion: v1 kind: ReplicationController metadata: name: nginx labels: name: nginx spec: replicas: 2 selector: name: nginx template: metadata: labels: name: nginx spec: containers: - name: nginx image: nginx ``` ### 参考 [Ubuntu上手动安装部署Kubernetes详细指南](http://www.linuxidc.com/Linux/2017-04/142514.htm) [docker kubernetes dashboard安装部署详细介绍](http://www.jb51.net/article/94343.htm)