一、命名空间
- 作用:用来隔离资源
- 添加删除命名空间
[root@k8s-master01 ~]# kubectl create ns hello
namespace/hello created
[root@k8s-master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 7d4h
hello Active 2s
[root@k8s-master01 ~]# kubectl delete ns hello
namespace "hello" deleted
[root@k8s-master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 7d4h
- 使用yaml文件创建命名空间
[root@k8s-master01 ~]# cat hello.yaml
apiVersion: v1
kind: Namespace
metadata:
name: hello
[root@k8s-master01 ~]# kubectl apply -f hello.yaml
namespace/hello created
[root@k8s-master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 7d4h
hello Active 3s
二、Pod
- 定义:运行中的一组容器,Pod是kubernetes中应用的最小单位
- 说明:一个Pod中可能包含多个容器
- 通过命令行创建Pod
[root@k8s-master01 ~]# kubectl run mynginx --image=nginx
pod/mynginx created
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-m6974 1/1 Running 0 3d21h
my-dep-5b7868d854-qndjz 1/1 Running 0 3d21h
my-dep-5b7868d854-tnthn 1/1 Running 0 3d21h
mynginx 0/1 ContainerCreating 0 8s
- 查看Pod的详细信息
[root@k8s-master01 ~]# kubectl describe pods mynginx
Name: mynginx
Namespace: default
Priority: 0
Node: k8s-node01/172.16.0.14
Start Time: Tue, 03 Jan 2023 18:01:08 +0800
Labels: run=mynginx
Annotations: cni.projectcalico.org/containerID: 71ecc806be2c4d191bfd18ebc890cd90cf441f46bd9cf4b6c4a2a5d36bd385af
cni.projectcalico.org/podIP: 192.168.85.208/32
cni.projectcalico.org/podIPs: 192.168.85.208/32
Status: Running
IP: 192.168.85.208
IPs:
IP: 192.168.85.208
Containers:
mynginx:
Container ID: docker://f9689a2a90672e01136084b09ef8264d055a98e9f709b670c042a79ed79db698
Image: nginx
Image ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 03 Jan 2023 18:01:24 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gnzps (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-gnzps:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gnzps
Optional: false
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:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 117s default-scheduler Successfully assigned default/mynginx to k8s-node01
Normal Pulling 117s kubelet Pulling image "nginx"
Normal Pulled 102s kubelet Successfully pulled image "nginx" in 15.412530753s
Normal Created 102s kubelet Created container mynginx
Normal Started 102s kubelet Started container mynginx
[root@k8s-master01 ~]#
- 删除Pod
[root@k8s-master01 ~]# kubectl delete pods mynginx
pod "mynginx" deleted
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-m6974 1/1 Running 0 3d21h
my-dep-5b7868d854-qndjz 1/1 Running 0 3d21h
my-dep-5b7868d854-tnthn 1/1 Running 0 3d21h
- 查看Pod的运行日志
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-m6974 1/1 Running 0 3d22h
my-dep-5b7868d854-qndjz 1/1 Running 0 3d22h
my-dep-5b7868d854-tnthn 1/1 Running 0 3d22h
mynginx 1/1 Running 0 23s
[root@k8s-master01 ~]# kubectl logs mynginx
/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
2023/01/03 10:55:40 [notice] 1#1: using the "epoll" event method
2023/01/03 10:55:40 [notice] 1#1: nginx/1.21.5
2023/01/03 10:55:40 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/01/03 10:55:40 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
2023/01/03 10:55:40 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/03 10:55:40 [notice] 1#1: start worker processes
2023/01/03 10:55:40 [notice] 1#1: start worker process 33
2023/01/03 10:55:40 [notice] 1#1: start worker process 34
2023/01/03 10:55:40 [notice] 1#1: start worker process 35
2023/01/03 10:55:40 [notice] 1#1: start worker process 36
[root@k8s-master01 ~]#
- 查看Pode的相信信息
[root@k8s-master01 ~]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-dep-5b7868d854-m6974 1/1 Running 0 3d22h 192.168.58.209 k8s-node02 <none> <none>
my-dep-5b7868d854-qndjz 1/1 Running 0 3d22h 192.168.58.211 k8s-node02 <none> <none>
my-dep-5b7868d854-tnthn 1/1 Running 0 3d22h 192.168.58.210 k8s-node02 <none> <none>
mynginx 1/1 Running 0 81s 192.168.85.209 k8s-node01 <none> <none>
[root@k8s-master01 ~]#
- 使用Pod的ip+pod里面运行容器的端口测试Pod
注意:集群中的任意一个机器以及任意的应用都能通过Pod分配的ip来访问这个Pod
[root@k8s-master01 ~]# curl 192.168.85.209:80
<!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@k8s-master01 ~]#
- 使用yaml文件创建Pod
[root@k8s-master01 ~]# cat mynginx.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: mynginx
name: mynginx
spec:
containers:
- image: nginx
name: mynginx
[root@k8s-master01 ~]# kubectl apply -f mynginx.yaml
pod/mynginx created
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-m6974 1/1 Running 0 3d22h
my-dep-5b7868d854-qndjz 1/1 Running 0 3d22h
my-dep-5b7868d854-tnthn 1/1 Running 0 3d22h
mynginx 1/1 Running 0 25s
- 使用yaml文件同时创建包含两个容器的Pod
[root@k8s-master01 ~]# cat multicontainer-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: myapp
name: myapp
spec:
containers:
- image: nginx
name: nginx
- image: tomcat:8.5.68
name: tomcat
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-m6974 1/1 Running 0 3d22h
my-dep-5b7868d854-qndjz 1/1 Running 0 3d22h
my-dep-5b7868d854-tnthn 1/1 Running 0 3d22h
myapp 2/2 Running 0 91s
[root@k8s-master01 ~]# kubectl get pods myapp -oyaml |grep image
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"run":"myapp"},"name":"myapp","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"nginx"},{"image":"tomcat:8.5.68","name":"tomcat"}]}}
f:image: {}
f:imagePullPolicy: {}
f:image: {}
f:imagePullPolicy: {}
- image: nginx
imagePullPolicy: Always
- image: tomcat:8.5.68
imagePullPolicy: IfNotPresent
image: nginx:latest
imageID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
image: tomcat:8.5.68
imageID: docker-pullable://tomcat@sha256:a266dd222864de2fe72e0464e6d91c406a687c861bb72a07218e6d7c89fe1d3e
[root@k8s-master01 ~]#
三、Deployment
- 功能:控制Pod,使Pod拥有多副本,自愈,扩缩容等能力
- 特点:删除Pod后,会自动重启生成一个Pod
- 创建多副本
[root@k8s-master01 ~]# kubectl create deployment my-dep --image=nginx --replicas=3
deployment.apps/my-dep created
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-9ghf9 1/1 Running 0 41s
my-dep-5b7868d854-l6zqs 1/1 Running 0 41s
my-dep-5b7868d854-pflfk 1/1 Running 0 41s
myapp 2/2 Running 0 7m17s
[root@k8s-master01 ~]#
- 使用yaml文件创建Deploymnet
[root@k8s-master01 ~]# cat my-dep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: my-dep
name: my-dep
spec:
replicas: 3
selector:
matchLabels:
app: my-dep
template:
metadata:
labels:
app: my-dep
spec:
containers:
- image: nginx
name: nginx
[root@k8s-master01 ~]# kubectl apply -f my-dep.yaml
deployment.apps/my-dep created
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-7jb7r 1/1 Running 0 64s
my-dep-5b7868d854-rpcxn 1/1 Running 0 64s
my-dep-5b7868d854-vxf9h 1/1 Running 0 64s
myapp 2/2 Running 0 17m
[root@k8s-master01 ~]#
- 扩缩容
[root@k8s-master01 ~]# kubectl scale --replicas=5 deployment/my-dep
deployment.apps/my-dep scaled
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-7jb7r 1/1 Running 0 4m13s
my-dep-5b7868d854-cdxtd 1/1 Running 0 65s
my-dep-5b7868d854-rpcxn 1/1 Running 0 4m13s
my-dep-5b7868d854-vxf9h 1/1 Running 0 4m13s
my-dep-5b7868d854-zqfmq 1/1 Running 0 65s
myapp 2/2 Running 0 21m
- 滚动更新
[root@k8s-master01 ~]# kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record
deployment.apps/my-dep image updated
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-7jb7r 1/1 Running 0 5m54s
my-dep-5b7868d854-rpcxn 1/1 Running 0 5m54s
my-dep-5b7868d854-vxf9h 1/1 Running 0 5m54s
my-dep-5b7868d854-zqfmq 1/1 Running 0 2m46s
my-dep-6b48cbf4f9-qdwz6 0/1 ContainerCreating 0 4s
my-dep-6b48cbf4f9-x2xgm 0/1 ContainerCreating 0 4s
my-dep-6b48cbf4f9-zvvhk 0/1 ContainerCreating 0 4s
myapp 2/2 Running 0 22m
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-7jb7r 1/1 Running 0 6m2s
my-dep-5b7868d854-rpcxn 1/1 Running 0 6m2s
my-dep-5b7868d854-vxf9h 1/1 Running 0 6m2s
my-dep-5b7868d854-zqfmq 1/1 Running 0 2m54s
my-dep-6b48cbf4f9-qdwz6 0/1 ContainerCreating 0 12s
my-dep-6b48cbf4f9-x2xgm 0/1 ContainerCreating 0 12s
my-dep-6b48cbf4f9-zvvhk 0/1 ContainerCreating 0 12s
myapp 2/2 Running 0 22m
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-6b48cbf4f9-4k8zw 1/1 Running 0 36s
my-dep-6b48cbf4f9-fk6cw 1/1 Running 0 16s
my-dep-6b48cbf4f9-qdwz6 1/1 Running 0 53s
my-dep-6b48cbf4f9-x2xgm 1/1 Running 0 53s
my-dep-6b48cbf4f9-zvvhk 1/1 Running 0 53s
myapp 2/2 Running 0 23m
[root@k8s-master01 ~]# kubectl rollout status deployment/my-dep
deployment "my-dep" successfully rolled out
- 版本回退
[root@k8s-master01 ~]# kubectl rollout history deployment/my-dep
deployment.apps/my-dep
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record=true
[root@k8s-master01 ~]# kubectl rollout history deployment/my-dep --revision=2
deployment.apps/my-dep with revision #2
Pod Template:
Labels: app=my-dep
pod-template-hash=6b48cbf4f9
Annotations: kubernetes.io/change-cause: kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record=true
Containers:
nginx:
Image: nginx:1.16.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@k8s-master01 ~]# kubectl rollout undo deployment/my-dep
deployment.apps/my-dep rolled back
[root@k8s-master01 ~]# kubectl rollout history deployment/my-dep
deployment.apps/my-dep
REVISION CHANGE-CAUSE
2 kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record=true
3 <none>
[root@k8s-master01 ~]# kubectl rollout undo deployment/my-dep --to-revision=2
deployment.apps/my-dep rolled back
[root@k8s-master01 ~]#
四、Service
- 功能:将一组Pod公开为网络服务的抽象方法
- 暴露Deploy
[root@k8s-master01 ~]# kubectl expose deployment my-dep --port=8000 --target-port=80
service/my-dep exposed
[root@k8s-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d6h
my-dep ClusterIP 10.96.231.188 <none> 8000/TCP 5s
[root@k8s-master01 ~]# kubectl get pods -l app=my-dep
NAME READY STATUS RESTARTS AGE
my-dep-6b48cbf4f9-45k75 1/1 Running 0 3m50s
my-dep-6b48cbf4f9-bkf9j 1/1 Running 0 3m33s
my-dep-6b48cbf4f9-c88m6 1/1 Running 0 3m50s
my-dep-6b48cbf4f9-k7lpl 1/1 Running 0 3m50s
my-dep-6b48cbf4f9-qwxpf 1/1 Running 0 3m33s
[root@k8s-master01 ~]#
- 使用yaml文件创建Service
[root@k8s-master01 ~]# cat my-dep-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: my-dep
name: my-dep
spec:
selector:
app: my-dep
ports:
- port: 8000
protocol: TCP
targetPort: 80
[root@k8s-master01 ~]# kubectl apply -f my-dep-service.yaml
service/my-dep created
[root@k8s-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d6h
my-dep ClusterIP 10.96.124.231 <none> 8000/TCP 35s
[root@k8s-master01 ~]#
- ClusterIP
kubectl expose deployment my-dep --port=8000 --target-port=80 --type=ClusterIP
# 等同于没有 --type=ClusterIP
- NodePort
[root@k8s-master01 ~]# kubectl expose deployment my-dep --port=8000 --target-port=80 --type=NodePort
service/my-dep exposed
[root@k8s-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d6h
my-dep NodePort 10.96.88.7 <none> 8000:30232/TCP 3s
- 使用yaml文件创建NodePort
[root@k8s-master01 ~]# cat my-dep-NodePort.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: my-dep
name: my-dep
spec:
ports:
- port: 8000
protocol: TCP
targetPort: 80
selector:
app: my-dep
type: NodePort
[root@k8s-master01 ~]# kubectl apply -f my-dep-NodePort.yaml
service/my-dep created
[root@k8s-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d6h
my-dep NodePort 10.96.208.124 <none> 8000:30441/TCP 5s