概述
我们知道通过 run
命令启动容器非常麻烦,Docker 提供了 Compose 为我们解决了这个问题。那 Kubernetes 是如何解决这个问题的呢?其实很简单,使用 kubectl create
命令就可以做到和 Compose 一样的效果了,该命令可以通过配置文件快速创建一个集群资源对象。
创建 YAML 配置文件
以部署 Nginx 为例
部署 Deployment 创建一个名为 nginx-deployment.yml
的配置文件
v1.16.0 之前 注意: extensions/v1beta1
不再支持部署 Deployment
,并且修改了少量命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-app spec: replicas: 2 template: metadata: labels: name: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80
v1.16.0 之后 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-app spec: selector: matchLabels: app: nginx replicas: 2 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80
1 2 3 4 5 $ kubectl create -f nginx-deployment.yml $ kubectl delete -f nginx-deployment.yml
发布 Service
创建一个名为 nginx-service.yml
的配置文件
v1.16.0 之前 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 apiVersion: v1 kind: Service metadata: name: nginx-http spec: ports: - port: 80 targetPort: 80 type: LoadBalancer selector: name: nginx
v1.16.0 之后 1 2 3 4 5 6 7 8 9 10 11 12 apiVersion: v1 kind: Service metadata: name: nginx-http spec: ports: - port: 80 targetPort: 80 type: LoadBalancer selector: app: nginx
1 2 3 4 5 $ kubectl create -f nginx-service.yml $ kubectl delete -f nginx-service.yml
验证是否生效
查看 Pod 列表 1 2 3 4 5 6 $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-app-64bb598779-2pplx 1/1 Running 0 25m nginx-app-64bb598779-824lc 1/1 Running 0 25m
查看 Deployment 列表 1 2 3 4 5 $ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE nginx-app 2/2 2 2 25m
查看 Service 列表 1 2 3 4 5 6 $ kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20h nginx-http LoadBalancer 10.98.49.142 <pending> 80:31631/TCP 14m
查看 Service 详情 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ kubectl describe service nginx-app Name: nginx-http Namespace: default Labels: <none> Annotations: <none> Selector: name=nginx Type: LoadBalancer IP: 10.98.49.142 Port: <unset > 80/TCP TargetPort: 80/TCP NodePort: <unset > 31631/TCP Endpoints: 10.244.141.205:80,10.244.2.4:80 Session Affinity: None External Traffic Policy: Cluster Events: <none>
通过浏览器访问 通过浏览器访问 http://192.168.141.150:31631/ ,出现 Nginx 欢迎页即表示成功
集成环境部署
也可以不区分配置文件,一次性部署 Deployment 和 Service,创建一个名为 nginx.yml
的配置文件,配置内容如下:
v1.16.0 之前 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-app spec: replicas: 2 template: metadata: labels: name: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-http spec: ports: - port: 80 targetPort: 80 type: LoadBalancer selector: name: nginx
v1.16.0 之后 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-app spec: selector: matchLabels: app: nginx replicas: 2 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-http spec: ports: - port: 80 targetPort: 80 type: LoadBalancer selector: app: nginx
1 2 3 4 5 $ kubectl create -f nginx.yml $ kubectl delete -f nginx.yml
附:修改默认的端口范围
Kubernetes 服务的 NodePort 默认端口范围是 30000-32767,在某些场合下,这个限制不太适用,我们可以自定义它的端口范围,操作步骤如下:
编辑 vi /etc/kubernetes/manifests/kube-apiserver.yaml
配置文件,增加配置 --service-node-port-range=2-65535
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: component: kube-apiserver tier: control-plane name: kube-apiserver namespace: kube-system spec: containers: - command: - kube-apiserver - --service-node-port-range=2-65535 - --advertise-address=192.168.141.150 - --allow-privileged=true - --authorization-mode=Node,RBAC - --client-ca-file=/etc/kubernetes/pki/ca.crt - --enable-admission-plugins=NodeRestriction - --enable-bootstrap-token-auth=true - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt - --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt // 以下配置省略...
使用 docker ps
命令找到 kube-apiserver
容器,再使用 docker restart <ApiServer 容器 ID>
即可生效。
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !