k8s 单节点部署 + 学习
k8s 单节点部署 + 学习
部署参考
kubeasz: https://github.com/easzlab/kubeasz/blob/master/docs/setup/quickStart.md
dashboard: https://github.com/easzlab/kubeasz/blob/master/docs/guide/dashboard.md
学习参考
官网 https://kubernetes.io/zh-cn/docs/tutorials/
常用命令
创建deployment
kubectl create deployment deplyment-name --image=app image location
exp: kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
获取部署列表
kubectl get deployments
pod列表
kubectl get pods
获取pod信息
curl http://localhost:8001/api/v1/namespaces/default/pods/${pod_name}
列出资源
kubectl get
显示资源详细信息
kubectl describe
打印 pod 和其中容器的日志
kubectl logs
在 pod 中的容器上执行命令
kubectl exec
以NodePort类型暴露应用,pod外部就可以访问该服务了,通过 ip:NodePort。 go-template?
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
获取详细信息
kubectl describe services/kubernetes-bootcamp
获取端口
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
集群外部访问应用
curl ip:NODE_PORT
label
[root@localhost ~]# kubectl describe deployment
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Thu, 26 Jan 2023 23:53:35 +0800
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=kubernetes-bootcamp
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-5485cc6795 (1/1 replicas created)
Events: <none>
[root@localhost ~]# kubectl get pods -l app=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5485cc6795-rmd5b 1/1 Running 2 (65m ago) 14h
[root@localhost ~]# kubectl get svc -l app=kubernetes-bootcamp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
AGE
kubernetes-bootcamp NodePort 10.68.227.123 <none> 8080:31371/TCP 14m
# 获取pod名
[root@localhost ~]# export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
[root@localhost ~]# echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-5485cc6795-rmd5b
#给pod打标签
[root@localhost ~]# kubectl label pods $POD_NAME version=1
pod/kubernetes-bootcamp-5485cc6795-rmd5b labeled
[root@localhost ~]# kubectl describe pods $POD_NAME
Name: kubernetes-bootcamp-5485cc6795-rmd5b
Namespace: default
Priority: 0
Service Account: default
Node: 192.168.10.3/192.168.10.3
Start Time: Thu, 26 Jan 2023 23:53:36 +0800
Labels: app=kubernetes-bootcamp
pod-template-hash=5485cc6795
version=1
Annotations: <none>
Status: Running
IP: 172.20.102.150
IPs:
IP: 172.20.102.150
Controlled By: ReplicaSet/kubernetes-bootcamp-5485cc6795
Containers:
kubernetes-bootcamp:
Container ID: containerd://9425c0485612ccc633ea3f25266b16b047c00f7baf150542e4953bfe1c7a7774
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: <none>
Host Port: <none>
State: Running
Started: Fri, 27 Jan 2023 12:50:48 +0800
Last State: Terminated
Reason: Unknown
Exit Code: 255
Started: Fri, 27 Jan 2023 00:10:09 +0800
Finished: Fri, 27 Jan 2023 12:50:35 +0800
Ready: True
Restart Count: 2
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qg4jq (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-qg4jq:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
[root@localhost ~]# kubectl get pods -l version=1
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5485cc6795-rmd5b 1/1 Running 2 (73m ago) 14h
# 删除服务,但是pod内部依然可以访问。
[root@localhost ~]# kubectl delete service -l app=kubernetes-bootcamp
service "kubernetes-bootcamp" deleted
[root@localhost ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.68.0.1 <none> 443/TCP 15h
[root@localhost ~]# echo $NODE_PORT
31371
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
curl: (7) Failed to connect to 192.168.10.3 port 31371: 拒绝连接
[root@localhost ~]# kubectl exec -ti $POD_NAME -- curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-rmd5b | v=1
使用 Service 暴露你的应用
Service 匹配一组 Pod 是使用 标签(Label)和选择器(Selector), 它们是允许对 Kubernetes 中的对象进行逻辑操作的一种分组原语。
扩缩应用程序
前面的例子创建了一个 Deployment,然后通过 Service让其可以开放访问。Deployment 仅为跑这个应用程序创建了一个 Pod。 当流量增加时,我们需要扩容应用程序满足用户需求。
扩缩 是通过改变 Deployment 中的副本数量来实现的。
- NAME lists the names of the Deployments in the cluster.
- READY shows the ratio of CURRENT/DESIRED replicas
- UP-TO-DATE displays the number of replicas that have been updated to achieve the desired state.
- AVAILABLE displays how many replicas of the application are available to your users.
- AGE displays the amount of time that the application has been running.
kubectl get rs - DESIRED displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.
- CURRENT displays how many replicas are currently running.
[root@localhost ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 14h
# 获取副本 NAME: [DEPLOYMENT-NAME]-[RANDOM-STRING]
[root@localhost ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-5485cc6795 1 1 1 14h
# 改变副本数量
[root@localhost ~]# kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.apps/kubernetes-bootcamp scaled
[root@localhost ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 14h
[root@localhost ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-5485cc6795 4 4 4 14h
[root@localhost ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-5485cc6795-7n9hc 1/1 Running 0 88s 172.20.102.157 192.168.10.3 <none> <none>
kubernetes-bootcamp-5485cc6795-g6l2q 1/1 Running 0 88s 172.20.102.158 192.168.10.3 <none> <none>
kubernetes-bootcamp-5485cc6795-j8zck 1/1 Running 0 88s 172.20.102.156 192.168.10.3 <none> <none>
kubernetes-bootcamp-5485cc6795-rmd5b 1/1 Running 2 (106m ago) 14h 172.20.102.150 192.168.10.3 <none> <none>
# 暴露服务,并访问,会发现请求转到了不同的pod上
[root@localhost ~]# kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service/kubernetes-bootcamp exposed
[root@localhost ~]# kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.68.18.131
IPs: 10.68.18.131
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 31453/TCP
Endpoints: 172.20.102.150:8080,172.20.102.156:8080,172.20.102.157:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@localhost ~]# export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
[root@localhost ~]# echo NODE_PORT=$NODE_PORT
NODE_PORT=31453
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-g6l2q | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-7n9hc | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-j8zck | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-rmd5b | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-g6l2q | v=1
# 减小副本数量
[root@localhost ~]# kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
[root@localhost ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 2 2 14h
[root@localhost ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-5485cc6795-7n9hc 1/1 Terminating 0 11m 172.20.102.157 192.168.10.3 <none> <none>
kubernetes-bootcamp-5485cc6795-g6l2q 1/1 Terminating 0 11m 172.20.102.158 192.168.10.3 <none> <none>
kubernetes-bootcamp-5485cc6795-j8zck 1/1 Running 0 11m 172.20.102.156 192.168.10.3 <none> <none>
kubernetes-bootcamp-5485cc6795-rmd5b 1/1 Running 2 (117m ago) 14h 172.20.102.150 192.168.10.3 <none> <none>
# 请求后服务在2个pod间转发
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-j8zck | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-rmd5b | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-j8zck | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-rmd5b | v=1
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5485cc6795-j8zck | v=1
滚动更新
[root@localhost ~]# kubectl describe pod|grep -i image
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Normal Pulled 34m kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
# 更换镜像版本
[root@localhost ~]# kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated
[root@localhost ~]# kubectl describe pod|grep -i image
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Normal Pulled 35m kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Image: jocatalin/kubernetes-bootcamp:v2
Image ID:
Normal Pulling 5s kubelet Pulling image "jocatalin/kubernetes-bootcamp:v2"
# 查看pod状态
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5485cc6795-j8zck 1/1 Terminating 0 35m
kubernetes-bootcamp-5485cc6795-rmd5b 1/1 Running 2 (140m ago) 15h
kubernetes-bootcamp-7c6644499c-djpng 0/1 ContainerCreating 0 2s
kubernetes-bootcamp-7c6644499c-hf6nx 1/1 Running 0 22s
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7c6644499c-djpng 1/1 Running 0 39s
kubernetes-bootcamp-7c6644499c-hf6nx 1/1 Running 0 59s
# 访问服务
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7c6644499c-djpng | v=2
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7c6644499c-hf6nx | v=2
[root@localhost ~]# curl 192.168.10.3:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7c6644499c-djpng | v=2
[root@localhost ~]# kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out
[root@localhost ~]# kubectl describe pod|grep -i image
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker.io/jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Normal Pulled 82s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker.io/jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
# 升级失败,镜像下载失败,回滚
[root@localhost ~]# kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
deployment.apps/kubernetes-bootcamp image updated
[root@localhost ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 1 2 15h
[root@localhost ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 1 2 15h
[root@localhost ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 1 2 15h
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7589867599-b55wd 0/1 ImagePullBackOff 0 57s
kubernetes-bootcamp-7c6644499c-djpng 1/1 Running 0 5m55s
kubernetes-bootcamp-7c6644499c-hf6nx 1/1 Running 0 6m15s
[root@localhost ~]# kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7589867599-b55wd 0/1 Terminating 0 109s
kubernetes-bootcamp-7c6644499c-djpng 1/1 Running 0 6m47s
kubernetes-bootcamp-7c6644499c-hf6nx 1/1 Running 0 7m7s
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7c6644499c-djpng 1/1 Running 0 6m54s
kubernetes-bootcamp-7c6644499c-hf6nx 1/1 Running 0 7m14s
[root@localhost ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 2 2 15h
遇到的问题
执行docker exec -it kubeasz ezctl start-aio
报错
Failed to get information on remote file (/etc/kubeasz/clusters/default/ssl/etcd-csr.json): /bin/sh: /usr/bin/python3.6: not found
安装python2 并设置为默认
yum install python2 -y
alternatives --set python /usr/bin/python2