Install Python 3.7.5 on CentOS 7 by source tarball

目前CentOS 7的 yum repo中只有Python 3.6.8, 项目中要使用3.7.5, 只能从源码安装

1) 安装依赖组件

# yum install gcc openssl-devel bzip2-devel libffi-devel

2) 下载Python 3.7.5源码包, 解压
From https://www.python.org/downloads/release/python-375/

# cd /usr/src
# curl https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz -O
# tar zxvf Python-3.7.5.tgz

3) 配置安装

# cd /usr/src/Python-3.7.5
# ./configure --enable-optimizations
# make altinstall

4) 创建python软链接

安装后的Python 3.7 执行文件位于: /usr/local/bin/python3.7

# ln -s /usr/local/bin/python3.7 /usr/bin/python3
# ln -s /usr/bin/python3 /usr/bin/python

# python -V
Python 3.7.5

5) 清理

# rm -f /usr/src/Python-3.7.5.tgz

[Kubernetes] Create deployment, service by Python client

Install Kubernetes Python Client and PyYaml:

# pip install kubernetes pyyaml

1. Get Namespaces or Pods by CoreV1Api:

# -*- coding: utf-8 -*-
from kubernetes import client, config, utils

config.kube_config.load_kube_config(config_file="../kubecfg.yaml")
coreV1Api = client.CoreV1Api()

print("\nListing all namespaces")
for ns in coreV1Api.list_namespace().items:
    print(ns.metadata.name)

print("\nListing pods with their IP, namespace, names:")
for pod in coreV1Api.list_pod_for_all_namespaces(watch=False).items:
    print("%s\t\t%s\t%s" % (pod.status.pod_ip, pod.metadata.namespace, pod.metadata.name))

2. Create Deployment and Service by AppsV1Api:

# -*- coding: utf-8 -*-
from kubernetes import client, config, utils
import yaml

config.kube_config.load_kube_config(config_file="../kubecfg.yaml")
yamlDeploy = open( r'deploy.yaml')
jsonDeploy = yaml.load(yamlDeploy, Loader = yaml.FullLoader)

yamlService = open(r'service.yaml')
jsonService = yaml.load(yamlService, Loader = yaml.FullLoader)

appsV1Api = client.AppsV1Api()

if jsonDeploy['kind'] == 'Deployment':
    appsV1Api.create_namespaced_deployment(
        namespace="default", body = jsonDeploy
    )

if jsonService['kind'] == 'Service':
    coreV1Api.create_namespaced_service(
        namespace="default",
        body=jsonService
    )

3. Create ANY type of objects from a yaml file by utils.create_from_yaml, you can put multiple resources in one yaml file:

# -*- coding: utf-8 -*-
from kubernetes import client, config, utils

config.kube_config.load_kube_config(config_file="../kubecfg.yaml")

k8sClient = client.ApiClient()
utils.create_from_yaml(k8sClient, "deploy-service.yaml")

Reference:
https://github.com/kubernetes-client/python/blob/6709b753b4ad2e09aa472b6452bbad9f96e264e3/examples/create_deployment_from_yaml.py
https://stackoverflow.com/questions/56673919/kubernetes-python-api-client-execute-full-yaml-file

yum is broken after upgrade python from 2.x to 3.x on CentOS 7

yum is broken after upgrade python from 2.x to 3.x on CentOS7:

# yum
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^

Resolution: modify /usr/bin/yum and /usr/libexec/urlgrabber-ext-down, change first line from “#!/usr/bin/python” to “#!/usr/bin/python2”:

#!/usr/bin/python2
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
...
#! /usr/bin/python2
#  A very simple external downloader
#  Copyright 2011-2012 Zdenek Pavlas

#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public

 

Install Python 3.6 on CentOS 7

1)安装IUS软件源

#安装EPEL依赖
sudo yum install epel-release

#安装IUS软件源
sudo yum install https://centos7.iuscommunity.org/ius-release.rpm

2)安装Python3.6

sudo yum install python36u

#创建符号链接(可选)
sudo ln -s /bin/python3.6 /bin/python3

3)安装pip3(可选)

sudo yum install python36u-pip

#创建一个到pip3的符号链接(可选)
sudo ln -s /bin/pip3.6 /bin/pip3