K8S基础学习
https://www.cnblogs.com/wdzhz/p/11096996.html
----------------------
命令
k8s基础学习
https://www.cnblogs.com/linuxws/p/10657716.html
kubectl get namespaces #显示域名空间信息
kubectl cluster-info # 显示集群信息
kubectl version # 查看版本
kubectl get nodes # 查看集群中有几个Node
kubectl run my-nginx --image=nginx --replicas=2 --port=80 # 运行一个镜像
kubectl get pods # 查看pod
kubectl describe pod my-nginx-379829228-cwlbb # 查看服务详情信息
kubectl get deployments # 查看已部署
kubectl delete pod my-nginx-379829228-cwlbb # 删除pod
kubectl get pods # 再次查看pod,发现由于replicas机制,pod又生成一个新的
kubectl delete deployment my-nginx # 删除部署的my-nginx服务。彻底删除pod
kubectl create -f dashboard.yaml ##创建
kubectl delete -f dashboard.yaml ##删除
kubectl get pods --namespace=kube-system ##查看pods
kubectl describe service/kubernetes-dashboard --namespace="kube-system" ##查看pod明细
kubectl logs kubernetes-dashboard-latest-139424868-tdc0t --namespace=kube-system ##查看pod日志
kubectl exec -it nginx-controller-527tr -- /bin/bash ##进入容器终端
##运行容器(指定命名空间,数量,镜像名称)
kubectl run con-log-svr --image=docker.prod.prg.com:5000/qiwuiot-prod-con-log-svr --replicas=1 --namespace="qiwuiot-prod"
kubeadm token create --print-join-command ##创建token
kubeadm reset ##重置kubeadm
-------------------------
Namespace #命名空间
Endpoint #服务端点
Controller Manager #管理控制中心
Replication #副本控制器
yaml #文件管理服务
-------------------------
对比docker命令
k8s的学习路线基本都是从docker[容器]到k8s的,因此两个对比理解有助于记忆
$ 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
$ kubectl get pods
$ docker exec [容器id] ls
$ kubectl exec [pod_id] ls
$ docker exec -it [容器id] /bin/sh # docker exec 交互式
$ kubectl exec -it [pod_id] -- /bin/sh
$ docker info
$ kubectl cluster-info
------------------
# vi nginx.yaml
ApiVersion: 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
---------------------------------
启动管理服务
$ kubectl create -f nginx.yaml # 根据yaml文件创建服务
deployment "my-nginx" created
$ kubectl get deployments # 查看deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-nginx 3 3 3 3 6s
$ kubectl get pod # 查看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
$ kubectl delete -f nginx.yaml # 根据yaml文件删除服务
deployment "my-nginx" deleted
$ kubectl get pod
No resources found.
$ kubectl get deployment
No resources found.
--------
Service
kubectl run my-nginx --image=nginx --port=80 #部署一个nginx服务
$ kubectl expose deployment/my-nginx --type="NodePort" --port 80 # 创建一个service 且将其暴露到集群外可供访问
service "my-nginx" exposed
$ kubectl get services # 此时service列表多个my-nginx服务
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
-----------------
宿主主机内访问该服务
同网段的机器访问该服务
deployments
$ kubectl run my-nginx --image=nginx --port=80 # 运行nginx镜像
$ kubectl run -i --tty my-nginx --image=nginx --port=80 -- sh # 交互式 shell 的方式运行 pod
$ kubectl attach my-nginx-532658988-10kxd -i # 链接到运行中的容器
$ kubectl get deployments # 查看deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-nginx 1 1 1 1 25m
$ kubectl scale deployment my-nginx --replicas=10 # 扩展10个副本
deployment "my-nginx" scaled
$ kubectl scale deployment/my-nginx --replicas=10 # 作用效果等同上一条命令
deployment "my-nginx" scaled
$ kubectl get deployments # 再次显示deployment
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
$ kubectl scale deployment/my-nginx --replicas=1 # 缩扩到1个副本
deployment "my-nginx" scaled
$ kubectl scale deployment my-nginx --replicas=1 # 作用效果等同上一条命令
--------------
deployment的更新回滚
$ 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
$ kubectl set image deployment/my-nginx nginx=nginx:1.9.1 # 更新应用的镜像从1.7.9版本——>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