ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## kubernets单机版安装部署 > kubernetes部署算是有一定门槛的。为了不从入门到放弃,推荐一开始安装单机版作为入门熟悉kubectl指令、了解工作原理。 * #### 安装流程,切换成root ```powershell 1.关闭centos自带的防火墙 # systemctl disable firewalld # systemctl stop firewalld 2.安装etcd和kubernetes软件(会自动安装docker) # yum install -y etcd kubernetes ``` * #### 修改两处配置 1. Docker配置文件/etc/sysconfig/docker, OPTIONS='--selinux-enabled=false --insecure-registry gcr.io' ![](https://box.kancloud.cn/02c8c1f3c46e813a699a0aedc5438be7_1024x384.png) 2. Kubernetes apiservce配置文件/etc/kubernetes/apiserver,把--admission_control参数钟的ServiceAccount删除 ![](https://box.kancloud.cn/9dec10ab63bdefa4edec02502799b78f_1326x501.png) #### 启动所有服务 ```shell # systemctl start etcd # systemctl start docker # systemctl start kube-apiserver # systemctl start kube-controller-manager # systemctl start kube-scheduler # systemctl start kubelet # systemctl start kube-proxy ``` ## 初入门小实例 * 部署nginx服务 ```powershell $ kubectl run my-nginx --image=nginx --port=80 $ kubectl get pod # 查看pod ``` > **发现pod状态无论多久都是处于pending。READY字段一直是0/1**,服务部署失败的原因是”中国墙“的问题导致无法下载pod启动时需要的谷歌镜像,所以我们得间接的创建所需的镜像。 > > **补充:** Pending状态表示API Server已经创建Pod,但Pod内还有一个或者多个容器没有创建,或者正在下载镜像的过程。详细的参考`Pod声明周期和重启策略` * 创建gcr.io/google_containers/pause-amd64:3.0镜像 ```powershell $ docker pull googlecontainer/pause-amd64:3.0 $ docker tag googlecontainer/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0 ``` ​ ## kubernets指令 * 实例流程 ```powershell # 查看版本 $ kubectl version Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"} # 显示集群信息 $ kubectl cluster-info Kubernetes master is running at http://localhost:8080 To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. # 查看集群中有几个Node $ kubectl get nodes NAME STATUS AGE 127.0.0.1 Ready 18h # 运行一个镜像 $ kubectl run my-nginx --image=nginx --replicas=2 --port=80 deployment "my-nginx" created # 查看pod $ kubectl get pods NAME READY STATUS RESTARTS AGE my-nginx-379829228-cwlbb 0/1 ContainerCreating 0 20s my-nginx-379829228-czk6w 1/1 Running 0 20s # 查看服务详情信息 $ kubectl describe pod my-nginx-379829228-cwlbb # 查看已部署 $ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE my-nginx 2 2 2 2 3m # 删除pod $ kubectl delete pod my-nginx-379829228-cwlbb pod "my-nginx-379829228-cwlbb" deleted # 再次查看pod,发现由于replicas机制,pod又生成一个新的 $ kubectl get pods NAME READY STATUS RESTARTS AGE my-nginx-379829228-czk6w 1/1 Running 0 11m my-nginx-379829228-gjd7d 0/1 ContainerCreating 0 5s # 删除部署的my-nginx服务。彻底删除pod $ kubectl delete deployment my-nginx deployment "my-nginx" deleted ``` #### 对比docker命令 > k8s的学习路线基本都是从docker[容器]到k8s的,因此两个对比理解有助于记忆 ```powershell # docker run $ docker run -d -e DOMAIN=cluster --name my-nginx -p 80:80 nginx $ kubectl run my-nginx --image=nginx --port=80 --env="DOMAIN=cluster" # docker ps $ docker ps $ kubectl get pods # docker exec $ docker exec [容器id] ls $ kubectl exec [pod_id] ls # docker exec 交互式 $ docker exec -it [容器id] /bin/sh $ kubectl exec -it [pod_id] -- /bin/sh # docker info $ docker info $ kubectl cluster-info ``` ## 重要名词 | 名词 | 翻译 | | :----------------- | :----- | | Namespace | 命名空间 | | Endpoint | 服务端点 | | Controller Manager | 管理控制中心 | | Replication | 副本控制器 | | | | ## yaml文件管理服务 * 用yaml文件来创建服务 ```yaml # vi nginx.yaml piVersion: extensions/v1beta1 kind: Deployment metadata: name: my-nginx spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80 ``` * 启动管理服务 ```powershell # 根据yaml文件创建服务 $ kubectl create -f nginx.yaml deployment "my-nginx" created # 查看deployment $ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE my-nginx 3 3 3 3 6s # 查看Pod $ kubectl get pod NAME READY STATUS RESTARTS AGE my-nginx-4087004473-dtrjp 1/1 Running 0 7s my-nginx-4087004473-jz80p 1/1 Running 0 7s my-nginx-4087004473-wh576 1/1 Running 0 7s # 根据yaml文件删除服务 $ kubectl delete -f nginx.yaml deployment "my-nginx" deleted $ kubectl get pod No resources found. $ kubectl get deployment No resources found. ``` ​ ​ ## Service * 到此,我们部署一个nginx服务 ```shell $ kubectl run my-nginx --image=nginx --port=80 # 创建一个service 且将其暴露到集群外可供访问 $ kubectl expose deployment/my-nginx --type="NodePort" --port 80 service "my-nginx" exposed # 此时service列表多个my-nginx服务 $ kubectl get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 7d my-nginx 10.254.255.103 <nodes> 80:32589/TCP 7s ``` > 宿主主机内访问该服务 > ![](https://box.kancloud.cn/55b8afe858ca7fe37984d6487adf0a7a_897x736.png) > 同网段的机器访问该服务 ![](https://box.kancloud.cn/fba283e93e584346e6040e0fce93a72c_2181x277.png) ## deployments ```powershell # 运行nginx镜像 $ kubectl run my-nginx --image=nginx --port=80 # 交互式 shell 的方式运行 pod $ kubectl run -i --tty my-nginx --image=nginx --port=80 -- sh # 链接到运行中的容器 $ kubectl attach my-nginx-532658988-10kxd -i # 查看deployment $ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE my-nginx 1 1 1 1 25m # 扩展10个副本 $ kubectl scale deployment my-nginx --replicas=10 deployment "my-nginx" scaled $ kubectl scale deployment/my-nginx --replicas=10 # 作用效果等同上一条命令 deployment "my-nginx" scaled # 再次显示deployment $ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE my-nginx 10 10 10 1 26m $ kubectl get pods NAME READY STATUS RESTARTS AGE my-nginx-379829228-38hkg 1/1 Running 0 5m my-nginx-379829228-7j15l 1/1 Running 0 31m my-nginx-379829228-c8mt3 1/1 Running 0 5m my-nginx-379829228-f6mm8 1/1 Running 0 5m my-nginx-379829228-q1rj0 1/1 Running 0 5m my-nginx-379829228-qg7lf 1/1 Running 0 5m my-nginx-379829228-rjfbq 1/1 Running 0 5m my-nginx-379829228-v581r 1/1 Running 0 5m my-nginx-379829228-wh49w 1/1 Running 0 5m my-nginx-379829228-wpn98 1/1 Running 0 5m # 缩扩到1个副本 $ kubectl scale deployment/my-nginx --replicas=1 deployment "my-nginx" scaled $ kubectl scale deployment my-nginx --replicas=1 # 作用效果等同上一条命令 ``` #### deployment的更新回滚 ![](https://box.kancloud.cn/0535d4dcc6b0313053ea02411b3f6dec_387x296.png) ```powershell $ kubectl create -f nginx.yaml $ kubectl get pod NAME READY STATUS RESTARTS AGE my-nginx-4087004473-4xj74 1/1 Running 0 3m my-nginx-4087004473-jkptq 1/1 Running 0 3m my-nginx-4087004473-m55s1 1/1 Running 0 3m $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE my-nginx 3 3 3 3 4m # 更新应用的镜像从1.7.9版本——>1.9.1 $ kubectl set image deployment/my-nginx nginx=nginx:1.9.1 deployment "my-nginx" image updated # 确认是否更新成功 $ kubectl rollout status deployment/my-nginx deployment "my-nginx" successfully rolled out # 回滚到上一代版本 $ kubectl rollout undo deployment/my-nginx deployment "my-nginx" rolled back ``` #### 删除Pod * 有时候deployment、rs、rc、services都为0,但是Pod确存在着。则重启kubelet服务即可。 ```powershell $ systemctl restart kubelet ``` #### 补充 ```powershell # 列出当前节点名 kubectl get node NAME STATUS AGE 127.0.0.1 Ready 6d # 已知当前节点名为127.0.0.1,用如下命令即可获得该节点上所有运行节点 $ curl localhost:8080/api/v1/proxy/nodes/127.0.0.1/pods {"kind":"PodList","apiVersion":"v1","metadata":{},"items":null} ``` #### 报错 ```powershell $ sudo kubectl create -f file.yaml YAML error: found character that cannot start any token # or error:yaml: line 15: found a tab character that violate indentation #file.yaml不可用tab键来空格 $ sudo kubectl create -f mysql-rc.yaml error: error validating "mysql-rc.yaml": error validating data: [found invalid field app for v1.ObjectMeta, found invalid field value for v1.Container]; if you choose to ignore these errors, turn validation off with --validate=false #3 $ Error from server (BadRequest): container "mysql" in pod "mysql-677b8" is waiting to start: ContainerCreating --------------------- # kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default my-nginx-379829228-b796w 1/1 Running 0 12m kube-system kubernetes-dashboard-2397086622-46tvx 0/1 ContainerCreating 0 8s [root@node01 ~]# kubectl logs -f kubernetes-dashboard-2397086622-46tvx --namespace=kube-system Using HTTP port: 9090 Using apiserver-host location: http://127.0.0.1:8080 Creating API server client for http://127.0.0.1:8080 Error while initializing connection to Kubernetes apiserver. This most likely means that the cluster is misconfigured (e.g., it has invalid apiserver certificates or service accounts configuration) or the --apiserver-host param points to a server that does not exist. Reason: Get http://127.0.0.1:8080/version: dial tcp 127.0.0.1:8080: getsockopt: connection refused Refer to the troubleshooting guide for more information: https://github.com/kubernetes/dashboard/blob/master/docs/user-guide/troubleshooting.md ``` ##### 我们最好不要越过RC而直接创建Pod,因为Replication Controller会通过RC管理Pod副本。实现自动创建、补足、替换、删除Pod副本,大大提高系统的容灾能力 * 重新调度(Rescheduling) * 弹性伸缩(Scaling) * 滚动更新(Rolling Updates) #### 指令 ```powershell # 查看集群中有几个Node 【swarm:docker node ls】 $ kubectl get nodes NAME STATUS AGE 127.0.0.1 Ready 1h # $ kubectl describe node 127.0.0.1 Name: 127.0.0.1 Role: Labels: beta.kubernetes.io/arch=amd64 beta.kubernetes.io/os=linux kubernetes.io/hostname=127.0.0.1 ..................... # Replication controller 复制控制器 $ kubectl get rc NAME DESIRED CURRENT READY AGE mysql 1 1 0 1h #修改RC的副本数,实现Pod的动态缩放 $ kubectl scale rc mysql --replicas=3 replicationcontroller "mysql" scaled ```