K8S
安装
# 创建用户并在wheel组下(可sudo)
useradd -d /home/devshen -m devshen
usermod -aG wheel devshen
passwd devshen
su - devshen
# 删除 docker
[root@VM-0-9-centos ~]# sudo yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
# yum拓展
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker
[root@VM-0-9-centos ~]# sudo yum install docker-ce docker-ce-cli containerd.io -y
# docker 验证
[root@VM-0-9-centos ~]# docker version
Client: Docker Engine - Community
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:05:12 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
# 启动docker服务
[root@VM-0-9-centos ~]# sudo systemctl start docker
# 将用户添加到docker组
[shendev@VM-0-9-centos ~]$ sudo usermod -aG docker shendev && newgrp docker
[sudo] password for shendev:
# 用docker跑镜像
[root@VM-0-9-centos ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Status: Downloaded newer image for hello-world:latest
Hello from Docker!.
# 安装kubectl
# 国内
curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.13.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
minikube start --driver=docker
[devshen@VM-0-9-centos ~]$ minikube version
minikube version: v1.13.0
commit: 23aa1eb200a03ae5883dd9d453d4daf3e0f59668
## 停止运行
minikube stop
## 执行卸载命令
minikube delete --all
## 删除 ~/.minikube 目录缓存的文件
rm -rf ~/.minikube
# 安装1.13.0版本的kubectl
[devshen@VM-0-9-centos ~]$ curl -LO https://dl.k8s.io/release/v1.13.0/bin/linux/amd64/kubectl
[devshen@VM-0-9-centos ~]$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
[devshen@VM-0-9-centos ~]$ kubectl version --output=yaml
clientVersion:
buildDate: "2018-12-03T21:04:45Z"
compiler: gc
gitCommit: ddf47ac13c1a9483ea035a79cd7c10005ff21a6d
gitTreeState: clean
gitVersion: v1.13.0
goVersion: go1.11.2
major: "1"
minor: "13"
platform: linux/amd64
serverVersion:
buildDate: "2020-08-26T14:23:04Z"
compiler: gc
gitCommit: e19964183377d0ec2052d1f1fa930c4d7575bd50
gitTreeState: clean
gitVersion: v1.19.0
goVersion: go1.15
major: "1"
minor: "19"
platform: linux/amd64
# 启动仪表盘
# 可以Ctrl-C掉, 不影响使用
[devshen@VM-0-9-centos ~]$ minikube dashboard
* Enabling dashboard ...
* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...
* Opening http://127.0.0.1:34997/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
- http://127.0.0.1:34997/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
# 设置代理
[devshen@VM-0-9-centos ~]$ kubectl proxy --address='0.0.0.0' --disable-filter=true
W0627 22:30:52.746644 294076 proxy.go:140] Request filter disabled, your proxy is vulnerable to XSRF attacks, please be cautious
Starting to serve on [::]:8001
基础
- 创建集群
minikube start
minikube ip
获取集群 ip
[devshen@VM-0-9-centos ~]$ kubectl cluster-info
Kubernetes master is running at https://172.17.0.2:8443
KubeDNS is running at https://172.17.0.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
kubectl get nodes
kubectl get pods
- 部署应用
kubectl create deployment [应用名] --image=[镜像地址]
kubectl proxy
curl http://localhost:8081/version
curl http://localhost:8001/api/v1/namespaces/default/pods/[pod名]/
kubectl get deployments
- 了解应用
kubectl logs [pod名]
kubectl describle pods
curl http://localhost:8001/api/v1/namespaces/default/pods/[pod名]/proxy/
kubectl exec $POD_NAME -- env
kubectl exec -ti $POD_NAME -- bash
进入pod的容器中
- 暴露应用
kubectl get services
kubectl expose deployment/[应用名] --type="NodePort" --port=8080
kubectl describe services/kubernetes-bootcamp
查看Service具体配置
kubectl describe deployment
kubectl get pods -l app=kubernetes-bootcamp
kubectl get services -l app=kubernetes-bootcamp
kubectl label pods $POD_NAME version=v1
给pod打标
kubectl describe pods $POD_NAME
kubectl delete service -l app=kubernetes-bootcamp
删除service
kubectl exec -it kubernetes-bootcamp-fb5c67579-9zmrq -- curl http://localhost:8080/version
只能进入pod中执行请求
- 扩缩应用
kubectl get rs
获取集群信息
kubectl exec -it kubernetes-bootcamp-fb5c67579-9zmrq -- curl http://localhost:8080/version
kubectl describe deployments/kubernetes-bootcamp
kubectl scale deployments/kubernetes-bootcamp --replicas=2
kubectl scale deployments/kubernetes-bootcamp --replicas=4
扩缩容
- 更新应用
kubectl set image deployment/[应用名] [下边应用配置:Pod Template: Containers]=[新镜像]
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 0/4 0 0 12s
$ kubectl describe deployments
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Sat, 25 Jun 2022 15:43:42 +0000
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=kubernetes-bootcamp
Replicas: 4 desired | 4 updated | 4 total | 4 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: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-fb5c67579 (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 37s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 4
kubectl rollout status deployments/kubernetes-bootcamp
执行滚动跟新
$ kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-fb5c67579-2m6m7 1/1 Running 0 38s kubernetes-bootcamp-fb5c67579-667f5 1/1 Running 0 38s kubernetes-bootcamp-fb5c67579-c5nc8 1/1 Running 0 38s kubernetes-bootcamp-fb5c67579-w5s6x 1/1 Running 0 38s $ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2 deployment.apps/kubernetes-bootcamp image updated $ kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-7d44784b7c-9qzjk 1/1 Running 0 3s kubernetes-bootcamp-7d44784b7c-hvx6b 1/1 Running 0 5s kubernetes-bootcamp-7d44784b7c-v5k2n 1/1 Running 0 3s kubernetes-bootcamp-7d44784b7c-wx2s6 1/1 Running 0 5s kubernetes-bootcamp-fb5c67579-2m6m7 1/1 Terminating 0 46s kubernetes-bootcamp-fb5c67579-667f5 1/1 Terminating 0 46s kubernetes-bootcamp-fb5c67579-c5nc8 1/1 Terminating 0 46s kubernetes-bootcamp-fb5c67579-w5s6x 1/1 Terminating 0 46s $ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10 deployment.apps/kubernetes-bootcamp image updated $ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 3/4 2 3 5m48s $ kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-59b7598c77-qrspn 0/1 ErrImagePull 0 9s kubernetes-bootcamp-59b7598c77-xjtmz 0/1 ErrImagePull 0 9s kubernetes-bootcamp-7d44784b7c-9qzjk 1/1 Running 0 4m55s kubernetes-bootcamp-7d44784b7c-hvx6b 1/1 Running 0 4m57s kubernetes-bootcamp-7d44784b7c-v5k2n 1/1 Terminating 0 4m55s kubernetes-bootcamp-7d44784b7c-wx2s6 1/1 Running 0 4m57s $ $ kubectl rollout undo deployments/kubernetes-bootcamp deployment.apps/kubernetes-bootcamp rolled back $ kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-59b7598c77-qrspn 0/1 Terminating 0 2m35s kubernetes-bootcamp-59b7598c77-xjtmz 0/1 Terminating 0 2m35s kubernetes-bootcamp-7d44784b7c-4kfhp 1/1 Running 0 10s kubernetes-bootcamp-7d44784b7c-9qzjk 1/1 Running 0 7m21s kubernetes-bootcamp-7d44784b7c-hvx6b 1/1 Running 0 7m23s kubernetes-bootcamp-7d44784b7c-wx2s6 1/1 Running 0 7m23s $
kubectl rollout status deployments/kubernetes-bootcamp
- API
$ kubectl -h
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Documentation of resources
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a Deployment, ReplicaSet or Replication Controller
autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController
Cluster Management Commands:
certificate Modify certificate resources.
cluster-info Display cluster info
top Display Resource (CPU/Memory/Storage) usage.
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes
Troubleshooting and Debugging Commands:
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers.
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff live version against would-be applied version
apply Apply a configuration to a resource by filename or stdin
patch Update field(s) of a resource
replace Replace a resource by filename or stdin
wait Experimental: Wait for a specific condition on one or many resources.
kustomize Build a kustomization target from a directory or a remote url.
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins.
version Print the client and server version information
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
配置
$ cat kubernetes.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: system-deployment
labels:
app: system
spec:
selector:
matchLabels:
app: system
template:
metadata:
labels:
app: system
spec:
containers:
- name: system-container
image: system:1.0-SNAPSHOT
ports:
- containerPort: 9080
readinessProbe:
httpGet:
path: /health/ready
port: 9080
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 3
failureThreshold: 1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: inventory-deployment
labels:
app: inventory
spec:
selector:
matchLabels:
app: inventory
template:
metadata:
labels:
app: inventory
spec:
containers:
- name: inventory-container
image: inventory:1.0-SNAPSHOT
ports:
- containerPort: 9080
readinessProbe:
httpGet:
path: /health/ready
port: 9080
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 3
failureThreshold: 1
---
apiVersion: v1
kind: Service
metadata:
name: system-service
spec:
type: NodePort
selector:
app: system
ports:
- protocol: TCP
port: 9080
targetPort: 9080
nodePort: 31000
---
apiVersion: v1
kind: Service
metadata:
name: inventory-service
spec:
type: NodePort
selector:
app: inventory
ports:
- protocol: TCP
port: 9080
targetPort: 9080
nodePort: 32000
坑
[root@VM-0-9-centos ~]# kubectl get pods The connection to the server localhost:8080 was refused - did you specify the right host or port? [root@VM-0-9-centos ~]#
具体根据情况,此处记录linux设置该环境变量
方式一:编辑文件设置
vim /etc/profile
在底部增加新的环境变量 export KUBECONFIG=/etc/kubernetes/admin.conf
方式二:直接追加文件内容
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
\