kubernetes基本概念
Pod:k8s最小部署单元,一组容器的集合
Deployment:最常见的控制器,用于更高级别部署和管理Pod
Service:为一组Pod提供负载均衡,对外提供统一访问入口
Label:标签,附加到某个资源上,用于关联对象、查询和筛选
Namespaces∶命名空间。将对象逻辑上隔离,也利于权限控制
edit(编辑资源)
//运行一个pod类型的nginx,名字叫nginx,定义的标签是app=nginx
[root@master ~]# kubectl run nginx --image=nginx --labels="app=nginx"
pod/nginx created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 44s
// 使用edit命令编辑
[root@master ~]# kubectl edit pods/nginx
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2021-12-20T09:26:53Z"
labels:
app: test # 把原本app:nginx改为app:test
name: nginx
namespace: default
[root@master ~]# kubectl edit pods/nginx
pod/nginx edited
// 查看是否更改成功
[root@master ~]# kubectl describe pods/nginx
Name: nginx
Namespace: default
Priority: 0
Node: node1.example.com/192.168.47.164
Start Time: Mon, 20 Dec 2021 19:22:07 +0800
Labels: app=test
Annotations: <none>
Status: Running
IP: 10.244.1.2
scale(动态扩展)
#创建一个deployment类型的nginx2的pod
[root@master ~]# kubectl create deployment nginx2 --image=nginx
deployment.apps/nginx2 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 7m38s
nginx2-85bf7b8976-n9vcl 1/1 Running 0 13s
# 使用scale扩展
[root@master ~]# kubectl scale --replicas 4 deployment/nginx2
deployment.apps/nginx2 scaled
# 扩展后查看多了几个相同类型的pod
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 8m55s
nginx2-85bf7b8976-bmcq4 0/1 ContainerCreating 0 11s
nginx2-85bf7b8976-n9vcl 1/1 Running 0 90s
nginx2-85bf7b8976-qzfhz 0/1 ContainerCreating 0 11s
nginx2-85bf7b8976-xk594 0/1 ContainerCreating 0 11s
# 如果只需要2个deployment类型的nginx的pod
[root@master ~]# kubectl scale --replicas 2 deployment/nginx2
deployment.apps/nginx2 scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx2-85bf7b8976-4dm2h 1/1 Running 0 39s
nginx2-85bf7b8976-fwj55 1/1 Running 0 49s
autoscale(自动扩展)
自动扩展,给定一个范围,自动根据业务的访问量增加或减少
[root@master ~]# kubectl autoscale --min=2 --max=7 --cpu-percent=60 deployment/nginx2
horizontalpodautoscaler.autoscaling/nginx2 autoscaled
[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx <unknown>/80% 1 3 3 29m
nginx2 Deployment/nginx2 <unknown>/60% 2 7 0 10s
cluster-info(显示集群信息)
[root@master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.47.163:6443
KubeDNS is running at https://192.168.47.163:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
describe(显示指定pod的详细信息)
[root@master ~]# kubectl describe pod nginx
Name: nginx-6799fc88d8-5kshn
Namespace: default
Priority: 0
Node: node2.example.com/192.168.47.158
Start Time: Sat, 18 Dec 2021 15:42:42 +0800
Labels: app=nginx
pod-template-hash=6799fc88d8
Annotations: <none>
Status: Running
IP: 10.244.2.2
IPs:
IP: 10.244.2.2
Controlled By: ReplicaSet/nginx-6799fc88d8
Containers:
nginx:
Container ID: docker://9b416e8d6cc8bf26f5d16bc50b2effa9c21478684a59a937c307b59006676e55
Image: nginx
Image ID: docker-pullable://nginx@sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Port: <none>
Host Port: <none>
State: Running
Started: Sat, 18 Dec 2021 15:43:16 +0800
Ready: True
logs(查看日志)
[root@master ~]# kubectl logs deployment/nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/18 07:43:16 [notice] 1#1: using the "epoll" event method
2021/12/18 07:43:16 [notice] 1#1: nginx/1.21.4
2021/12/18 07:43:16 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/12/18 07:43:16 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/18 07:43:16 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/18 07:43:16 [notice] 1#1: start worker processes
2021/12/18 07:43:16 [notice] 1#1: start worker process 31
2021/12/18 07:43:16 [notice] 1#1: start worker process 32
attach(附加在一个容器里)
[root@master ~]# kubectl attach nginx-6799fc88d8-5kshn
Defaulting container name to nginx.
Use 'kubectl describe pod/nginx-6799fc88d8-5kshn -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
exec(执行容器命令)
进到容器内执行一个命令
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-5kshn 1/1 Running 0 2d4h
[root@master ~]# kubectl exec nginx-6799fc88d8-5kshn -- date
Mon Dec 20 12:03:22 UTC 2021
[root@master ~]# kubectl exec -it nginx-6799fc88d8-5kshn -- /bin/bash
root@nginx-6799fc88d8-5kshn:/# ls
bin docker-entrypoint.d home media proc sbin tmp
boot docker-entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
root@nginx-6799fc88d8-5kshn:/#
prot-forward(转发一个或多个端口到pod里面去)
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-5kshn 1/1 Running 0 2d4h
[root@master ~]# kubectl port-forward deployment/nginx 80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80
[root@master ~]# ss -antl
LISTEN 0 128 127.0.0.1:80 0.0.0.0:*
LISTEN 0 128 127.0.0.1:10257 0.0.0.0:*
[root@master ~]# curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master ~]# kubectl port-forward --address 0.0.0.0 deployment/nginx 80
Forwarding from 0.0.0.0:80 -> 80
#允许所有IP访问80端口
[root@master ~]# curl http://192.168.47.163
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
cp(复制 )
拷贝文件或目录到容器中,或者从容器内向外拷贝
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-5kshn 1/1 Running 0 2d4h
[root@master ~]# kubectl cp anaconda-ks.cfg nginx-6799fc88d8-5kshn:/usr
[root@master ~]# kubectl exec nginx-6799fc88d8-5kshn -- ls -l /usr
total 4
-rw------- 1 root root 1092 Dec 20 12:26 anaconda-ks.cfg
drwxr-xr-x 1 root root 261 Dec 2 10:59 bin
drwxr-xr-x 2 root root 6 Oct 3 09:15 games
drwxr-xr-x 2 root root 6 Oct 3 09:15 include
drwxr-xr-x 1 root root 79 Dec 2 10:59 lib
label(标签)
给资源设置、更新标签
#创建标签
[root@master ~]# kubectl label --overwrite=true deployment nginx app=nginx
deployment.apps/nginx labeled
#查看标签
[root@master ~]# kubectl describe pod nginx
Name: nginx-6799fc88d8-5kshn
Namespace: default
Priority: 0
Node: node2.example.com/192.168.47.158
Start Time: Sat, 18 Dec 2021 15:42:42 +0800
Labels: app=nginx
pod-template-hash=6799fc88d8
Annotations: <none>
Status: Running
IP: 10.244.2.2
#追加标签
[root@master ~]# kubectl label pods nginx-6799fc88d8-5kshn xaw=xaw
pod/nginx-6799fc88d8-5kshn labeled
##查看已经最加成功
[root@master ~]# kubectl describe pod nginx-6799fc88d8-5kshn
Name: nginx-6799fc88d8-5kshn
Namespace: default
Priority: 0
Node: node2.example.com/192.168.47.158
Start Time: Sat, 18 Dec 2021 15:42:42 +0800
Labels: app=nginx
pod-template-hash=6799fc88d8
xaw=xaw
#更改标签
[root@master ~]# kubectl label pod nginx-6799fc88d8-5kshn --overwrite xaw=my
pod/nginx-6799fc88d8-5kshn labeled
#查看是否更改成功
[root@master ~]# kubectl describe pod nginx-6799fc88d8-5kshn
Name: nginx-6799fc88d8-5kshn
Namespace: default
Priority: 0
Node: node2.example.com/192.168.47.158
Start Time: Sat, 18 Dec 2021 15:42:42 +0800
Labels: app=nginx
pod-template-hash=6799fc88d8
xaw=my
api-resources(查看所有资源)
查看所有资源
[root@master ~]# kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
endpoints ep v1 true Endpoints
events ev v1 true Event
limitranges limits v1 true LimitRange
namespaces ns v1 false Namespace
nodes no v1 false Node
persistentvolumeclaims pvc v1 true PersistentVolumeClaim
persistentvolumes pv v1 false PersistentVolume
pods po v1 true Pod
podtemplates v1 true PodTemplate
replicationcontrollers rc v1 true ReplicationController
api-versions(API版本)
打印受支持的API版本
[root@master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
extensions/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
滚动更新和回滚
写两个镜像
[root@master httpd]# cat Dockerfile
FROM busybox
RUN mkdir /data && \
echo "test page on v1" > /data/index.html
ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
//第二个镜像
[root@master httpd]# cat Dockerfile
FROM busybox
RUN mkdir /data && \
echo "test page on v2" > /data/index.html
ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
使用
[root@master ~]# kubectl create deploy httpd --image my922/httpd:v1 --replicas 3
deployment.apps/httpd created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-7f6fd56575-g5m5d 1/1 Running 0 36s
httpd-7f6fd56575-t5r2s 1/1 Running 0 36s
httpd-7f6fd56575-xxk8g 1/1 Running 0 36s
[root@master ~]# kubectl expose deploy httpd --port 80 --type NodePort
service/httpd exposed
[root@master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd NodePort 10.96.115.185 <none> 80:30296/TCP 9s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9h
[root@master ~]# curl http://10.96.115.185
test page on v1
更新
[root@master ~]# kubectl set image deploy/httpd httpd=my922/httpd:v2
deployment.apps/httpd image updated
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-599c45c854-qzgts 0/1 ContainerCreating 0 3s
httpd-7f6fd56575-g5m5d 1/1 Running 0 16m
httpd-7f6fd56575-t5r2s 1/1 Running 0 16m
httpd-7f6fd56575-xxk8g 1/1 Running 0 16m
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-599c45c854-7jpcb 1/1 Running 0 3s
httpd-599c45c854-fhpnb 1/1 Running 0 2s
httpd-599c45c854-qzgts 1/1 Running 0 12s
httpd-7f6fd56575-g5m5d 1/1 Terminating 0 16m
httpd-7f6fd56575-t5r2s 1/1 Terminating 0 16m
httpd-7f6fd56575-xxk8g 1/1 Terminating 0 16m
[root@master ~]# curl http://10.96.115.185
test page on v2
回滚
[root@master ~]# kubectl rollout undo deploy/httpd
deployment.apps/httpd rolled back
[root@master ~]# curl http://10.96.115.185
test page on v1